Skip to content

Commit

Permalink
allow for arbitrary callback in label pickers
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Feb 17, 2025
1 parent a325314 commit 706e1d1
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 102 deletions.
63 changes: 34 additions & 29 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1632,23 +1632,24 @@ function M.create_label(label)
color = string.gsub(color, "#", "")
end

local query = graphql("create_label_mutation", repo_id, name, description, color)
gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.json.decode(output)
local label = resp.data.createLabel.label
utils.info("Created label: " .. label.name)
local refresh_details = function()
require("octo").load(buffer.repo, buffer.kind, buffer.number, function(obj)
writers.write_details(bufnr, obj, true)
end)
end

-- refresh issue/pr details
require("octo").load(buffer.repo, buffer.kind, buffer.number, function(obj)
writers.write_details(bufnr, obj, true)
end)
end
end,
local query = graphql("create_label_mutation", repo_id, name, description, color)
gh.api.graphql {
query = query,
jq = ".data.createLabel.label.name",
opts = {
cb = gh.create_callback {
success = function(label_name)
utils.info("Created label: " .. label_name)
refresh_details()
end,
},
},
}
end

Expand Down Expand Up @@ -1688,19 +1689,20 @@ local function label_action(opts)
table.insert(label_ids, lbl.id)
end

local refresh_details = function()
require("octo").load(buffer.repo, buffer.kind, buffer.number, function(obj)
writers.write_details(bufnr, obj, true)
end)
end

local query = graphql(opts.query_name, iid, create_list(label_ids, format))
gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
-- refresh issue/pr details
require("octo").load(buffer.repo, buffer.kind, buffer.number, function(obj)
writers.write_details(bufnr, obj, true)
end)
end
end,
gh.api.graphql {
query = query,
opts = {
cb = gh.create_callback {
success = refresh_details,
},
},
}
end

Expand All @@ -1712,7 +1714,10 @@ local function label_action(opts)
utils.error("Cannot find label: " .. label)
end
else
opts.labels(cb)
opts.labels {
repo = buffer.owner .. "/" .. buffer.name,
cb = cb,
}
end
end

Expand Down
162 changes: 89 additions & 73 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -771,57 +771,69 @@ local function select(opts)
cb(items)
end

function M.select_label(cb)
local opts = vim.deepcopy(dropdown_opts)
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer then
return
function M.select_label(opts)
opts = opts or {}

local cb = opts.cb
local repo = opts.repo

if not repo then
repo = utils.get_remote_name()
end
local owner, name = utils.split_repo(repo)

local query = graphql("labels_query", buffer.owner, buffer.name)
gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.json.decode(output)
local labels = resp.data.repository.labels.nodes
pickers
.new(opts, {
finder = finders.new_table {
results = labels,
entry_maker = entry_maker.gen_from_label(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
select {
bufnr = prompt_bufnr,
single_cb = cb,
multiple_cb = cb,
get_item = function(selection)
return selection.label
end,
}
end)
return true
end,
})
:find()
end
end,
opts = vim.tbl_deep_extend("force", dropdown_opts, opts)

local create_picker = function(output)
local labels = vim.json.decode(output)

pickers
.new(opts, {
finder = finders.new_table {
results = labels,
entry_maker = entry_maker.gen_from_label(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
select {
bufnr = prompt_bufnr,
single_cb = cb,
multiple_cb = cb,
get_item = function(selection)
return selection.label
end,
}
end)
return true
end,
})
:find()
end

local query = graphql("labels_query", owner, name)
gh.api.graphql {
query = query,
jq = ".data.repository.labels.nodes",
opts = {
cb = gh.create_callback {
success = create_picker,
},
},
}
end

function M.select_assigned_label(cb)
local opts = vim.deepcopy(dropdown_opts)
function M.select_assigned_label(opts)
opts = opts or {}
local cb = opts.cb
opts = vim.tbl_deep_extend("force", opts, dropdown_opts)

local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer then
return
end

local query, key
if buffer:isIssue() then
query = graphql("issue_labels_query", buffer.owner, buffer.name, buffer.number)
Expand All @@ -830,38 +842,42 @@ function M.select_assigned_label(cb)
query = graphql("pull_request_labels_query", buffer.owner, buffer.name, buffer.number)
key = "pullRequest"
end
gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.json.decode(output)
local labels = resp.data.repository[key].labels.nodes
pickers
.new(opts, {
finder = finders.new_table {
results = labels,
entry_maker = entry_maker.gen_from_label(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
select {
bufnr = prompt_bufnr,
single_cb = cb,
multiple_cb = cb,
get_item = function(selection)
return selection.label
end,
}
end)
return true
end,
})
:find()
end
end,

local create_picker = function(output)
local labels = vim.json.decode(output)

pickers
.new(opts, {
finder = finders.new_table {
results = labels,
entry_maker = entry_maker.gen_from_label(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
select {
bufnr = prompt_bufnr,
single_cb = cb,
multiple_cb = cb,
get_item = function(selection)
return selection.label
end,
}
end)
return true
end,
})
:find()
end

gh.api.graphql {
query = query,
jq = ".data.repository." .. key .. ".labels.nodes",
opts = {
cb = gh.create_callback {
success = create_picker,
},
},
}
end

Expand Down

0 comments on commit 706e1d1

Please sign in to comment.