Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add glab support #370

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
208 changes: 208 additions & 0 deletions lua/octo/backend/gh/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
local utils = require "octo.utils"
local graphql = require "octo.backend.gh.graphql"
local cli = require "octo.backend.gh.cli"
local window = require "octo.ui.window"
local writers = require "octo.ui.writers"

local M = {}

function M.pull(opts, cb)
local repo = opts["repo"]
local number = opts["number"]

local owner, name = utils.split_repo(repo)

local query = graphql("pull_request_query", owner, name, number)
local key = "pullRequest"

cli.run {
args = { "api", "graphql", "--paginate", "--jq", ".", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = utils.aggregate_pages(output, string.format("data.repository.%s.timelineItems.nodes", key))
local obj = resp.data.repository[key]
cb(obj)
end
end,
}
end

function M.issue(opts, cb)
local repo = opts["repo"]
local number = opts["number"]

local owner, name = utils.split_repo(repo)

local query = graphql("issue_query", owner, name, number)
local key = "issue"

cli.run {
args = { "api", "graphql", "--paginate", "--jq", ".", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = utils.aggregate_pages(output, string.format("data.repository.%s.timelineItems.nodes", key))
local obj = resp.data.repository[key]
cb(obj)
end
end,
}
end

function M.repo(opts, cb)
local repo = opts["repo"]

local owner, name = utils.split_repo(repo)

local query = graphql("repository_query", owner, name)

cli.run {
args = { "api", "graphql", "--paginate", "--jq", ".", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local obj = resp.data.repository
cb(obj)
end
end,
}
end

function M.reactions_popup(opts, _)
local id = opts["id"]

local query = graphql("reactions_for_object_query", id)

cli.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local reactions = {}
local reactionGroups = resp.data.node.reactionGroups

for _, reactionGroup in ipairs(reactionGroups) do
local users = reactionGroup.users.nodes
local logins = {}

for _, user in ipairs(users) do
table.insert(logins, user.login)
end

if #logins > 0 then
reactions[reactionGroup.content] = logins
end
end

local popup_bufnr = vim.api.nvim_create_buf(false, true)
local lines_count, max_length = writers.write_reactions_summary(popup_bufnr, reactions)

window.create_popup {
bufnr = popup_bufnr,
width = 4 + max_length,
height = 2 + lines_count,
}
end
end,
}
end

function M.user_popup(opts, _)
local login = opts["login"]

local query = graphql("user_profile_query", login)

cli.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local user = resp.data.user
local popup_bufnr = vim.api.nvim_create_buf(false, true)
local lines, max_length = writers.write_user_profile(popup_bufnr, user)

window.create_popup {
bufnr = popup_bufnr,
width = 4 + max_length,
height = 2 + lines,
}
end
end,
}
end

function M.link_popup(opts, _)
local repo = opts["repo"]
local number = opts["number"]

local owner, name = utils.split_repo(repo)
local query = graphql("issue_summary_query", owner, name, number)

cli.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local issue = resp.data.repository.issueOrPullRequest
local popup_bufnr = vim.api.nvim_create_buf(false, true)
local max_length = 80
local lines = writers.write_issue_summary(popup_bufnr, issue, { max_length = max_length })

window.create_popup {
bufnr = popup_bufnr,
width = max_length,
height = 2 + lines,
}
end
end,
}
end

function M.go_to_issue(opts, _)
local repo = opts["repo"]
local number = opts["number"]

local owner, name = utils.split_repo(repo)
local query = graphql("issue_kind_query", owner, name, number)

cli.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local kind = resp.data.repository.issueOrPullRequest.__typename

if kind == "Issue" then
utils.get_issue(repo, number)
elseif kind == "PullRequest" then
utils.get_pull_request(repo, number)
end
end
end,
}
end

M.functions = {
["pull"] = M.pull,
["issue"] = M.issue,
["repo"] = M.repo,
["reactions_popup"] = M.reactions_popup,
["user_popup"] = M.user_popup,
["link_popup"] = M.link_popup,
["go_to_issue"] = M.go_to_issue
}

return M
Empty file.
11 changes: 11 additions & 0 deletions lua/octo/backend/glab/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local M = {}

function M:pull(opts, cb)
vim.notify("calling pull")
end

M.functions = {
["pull"] = M.pull,
}

return M
34 changes: 34 additions & 0 deletions lua/octo/backend/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local Gitlab = require "octo.backend.glab"
local Github = require "octo.backend.gh"

local M = {}

local backend = {
["glab"] = Gitlab,
["gh"] = Github
}

function M.run(name, opts, cb)
local cli = "gh"

if not backend[cli].functions[name] then
vim.notify(cli .. " don't have " .. name .. " implemmented")
return
end

backend[cli].functions[name](opts, cb)
end

function M.available_executables()
local available = 0

for key, _ in pairs(backend) do
if vim.fn.executable(key) then
available = available + 1
end
end

return available
end

return M
4 changes: 2 additions & 2 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local constants = require "octo.constants"
local navigation = require "octo.navigation"
local gh = require "octo.gh"
local graphql = require "octo.gh.graphql"
local gh = require "octo.backend.gh.cli"
local graphql = require "octo.backend.gh.graphql"
local picker = require "octo.picker"
local reviews = require "octo.reviews"
local window = require "octo.ui.window"
Expand Down
Loading