Skip to content

Commit

Permalink
Merge branch 'refactor/gh-runs-command' of https://github.com/GustavE…
Browse files Browse the repository at this point in the history
…ikaas/octo.nvim into refactor/gh-runs-command
  • Loading branch information
GustavEikaas committed Feb 23, 2025
2 parents 93f65ed + 247b780 commit 7bea5a6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 74 deletions.
20 changes: 10 additions & 10 deletions lua/octo/gh/graphql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,8 @@ query {

-- https://docs.github.com/en/free-pro-team@latest/graphql/reference/objects#repository
M.repository_id_query = [[
query {
repository(owner: "%s", name: "%s") {
query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
id
}
}
Expand All @@ -954,8 +954,8 @@ query {
-- https://docs.github.com/en/graphql/reference/objects#issuetemplate
-- https://docs.github.com/en/graphql/reference/objects#pullrequesttemplate
M.repository_templates_query = [[
query {
repository(owner: "%s", name: "%s") {
query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
issueTemplates { body about name title }
pullRequestTemplates { body filename }
}
Expand Down Expand Up @@ -1667,8 +1667,8 @@ query($endCursor: String) {
]]

M.repository_query = [[
query {
repository(owner: "%s", name: "%s") {
query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
id
nameWithOwner
description
Expand Down Expand Up @@ -1854,17 +1854,17 @@ M.create_pr_mutation = [[

-- https://docs.github.com/en/graphql/reference/queries#user
M.user_query = [[
query {
user(login:"%s") {
query($login: String!) {
user(login: $login) {
id
}
}
]]

-- https://docs.github.com/en/graphql/reference/objects#pullrequestreviewthread
M.repo_labels_query = [[
query {
repository(owner:"%s", name:"%s") {
query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
labels(first: 100) {
...LabelConnectionFragment
}
Expand Down
135 changes: 71 additions & 64 deletions lua/octo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -698,18 +698,18 @@ end
function M.get_repo_id(repo)
if repo_id_cache[repo] then
return repo_id_cache[repo]
else
local owner, name = M.split_repo(repo)
local query = graphql("repository_id_query", owner, name)
local output = gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
mode = "sync",
}
local resp = vim.json.decode(output)
local id = resp.data.repository.id
repo_id_cache[repo] = id
return id
end

local owner, name = M.split_repo(repo)
local query = graphql "repository_id_query"
local id = gh.api.graphql {
query = query,
fields = { owner = owner, name = name },
jq = ".data.repository.id",
opts = { mode = "sync" },
}
repo_id_cache[repo] = id
return id
end

-- Checks if the current cwd is in a git repo
Expand All @@ -724,45 +724,47 @@ end
function M.get_repo_info(repo)
if repo_info_cache[repo] then
return repo_info_cache[repo]
else
local owner, name = M.split_repo(repo)
local query = graphql("repository_query", owner, name)
local output = gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
mode = "sync",
}
local resp = vim.json.decode(output)
local info = resp.data.repository
repo_info_cache[repo] = info
return info
end

local owner, name = M.split_repo(repo)
local query = graphql "repository_query"
local output = gh.api.graphql {
query = query,
fields = { owner = owner, name = name },
jq = ".data.repository",
opts = { mode = "sync" },
}
local info = vim.json.decode(output)
repo_info_cache[repo] = info
return info
end

---Gets repo's templates
function M.get_repo_templates(repo)
if repo_templates_cache[repo] then
return repo_templates_cache[repo]
else
local owner, name = M.split_repo(repo)
local query = graphql("repository_templates_query", owner, name)
local output = gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
mode = "sync",
}
local resp = vim.json.decode(output)
local templates = resp.data.repository

-- add an option to not use a template
table.insert(templates.issueTemplates, {
name = "DO NOT USE A TEMPLATE",
about = "Create issue with no template",
title = "",
body = "",
})

repo_templates_cache[repo] = templates
return templates
end

local owner, name = M.split_repo(repo)
local query = graphql "repository_templates_query"
local output = gh.api.graphql {
query = query,
fields = { owner = owner, name = name },
jq = ".data.repository",
opts = { mode = "sync" },
}
local templates = vim.json.decode(output)

-- add an option to not use a template
table.insert(templates.issueTemplates, {
name = "DO NOT USE A TEMPLATE",
about = "Create issue with no template",
title = "",
body = "",
})

repo_templates_cache[repo] = templates
return templates
end

---Helper method to aggregate an API paginated response
Expand Down Expand Up @@ -1525,17 +1527,20 @@ function M.close_preview_autocmd(events, winnr, bufnrs)
end

function M.get_user_id(login)
local query = graphql("user_query", login)
local output = gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
mode = "sync",
local query = graphql "user_query"

local id = gh.api.graphql {
query = query,
fields = { login = login },
jq = ".data.user.id",
opts = { mode = "sync" },
}
if output then
local resp = vim.json.decode(output)
if resp.data.user and resp.data.user ~= vim.NIL then
return resp.data.user.id
end

if id == "" then
return
end

return id
end

function M.get_label_id(label)
Expand All @@ -1547,21 +1552,23 @@ function M.get_label_id(label)
end

local owner, name = M.split_repo(buffer.repo)
local query = graphql("repo_labels_query", owner, name)
local output = gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
mode = "sync",
local query = graphql "repo_labels_query"
local jq = ([[
.data.repository.labels.nodes
| map(select(.name == "{label}"))
| .[0].id
]]):gsub("{label}", label)
local id = gh.api.graphql {
query = query,
fields = { owner = owner, name = name },
jq = jq,
opts = { mode = "sync" },
}
if output then
local resp = vim.json.decode(output)
if resp.data.repository.labels.nodes and resp.data.repository.labels.nodes ~= vim.NIL then
for _, l in ipairs(resp.data.repository.labels.nodes) do
if l.name == label then
return l.id
end
end
end
if id == "" then
return
end

return id
end

--- Generate maps from diffhunk line to code line:
Expand Down

0 comments on commit 7bea5a6

Please sign in to comment.