Skip to content

Commit

Permalink
Merge branch 'master' into 640-close-issue-with-not-planned-reason
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 authored Dec 16, 2024
2 parents da8950f + 82ebba2 commit 26b7c33
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 48 deletions.
75 changes: 37 additions & 38 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1516,47 +1516,25 @@ function M.create_label(label)
}
end

function M.add_label(label)
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer then
return
end
local function format(str)
return string.format('"%s"', str)
end

local iid = buffer.node.id
if not iid then
utils.error "Cannot get issue/pr id"
local function create_list(values, fmt)
if type(values) == "string" then
return fmt(values)
end

local cb = function(label_id)
local query = graphql("add_labels_mutation", iid, label_id)
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,
}
end
if label then
local label_id = utils.get_label_id(label)
if label_id then
cb(label_id)
else
utils.error("Cannot find label: " .. label)
end
else
picker.labels(cb)
local formatted_values = {}
for _, value in ipairs(values) do
table.insert(formatted_values, fmt(value))
end
return "[" .. table.concat(formatted_values, ", ") .. "]"
end

function M.remove_label(label)
local function label_action(opts)
local label = opts.label

local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer then
Expand All @@ -1568,8 +1546,13 @@ function M.remove_label(label)
utils.error "Cannot get issue/pr id"
end

local cb = function(label_id)
local query = graphql("remove_labels_mutation", iid, label_id)
local cb = function(labels)
local label_ids = {}
for _, lbl in ipairs(labels) do
table.insert(label_ids, lbl.id)
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)
Expand All @@ -1593,10 +1576,26 @@ function M.remove_label(label)
utils.error("Cannot find label: " .. label)
end
else
picker.assigned_labels(cb)
opts.labels(cb)
end
end

function M.add_label(label)
return label_action {
query_name = "add_labels_mutation",
label = label,
labels = picker.labels,
}
end

function M.remove_label(label)
return label_action {
query_name = "remove_labels_mutation",
label = label,
labels = picker.assigned_labels,
}
end

function M.add_user(subject, login)
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/gh/graphql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2635,7 +2635,7 @@ M.create_label_mutation = [[
-- https://docs.github.com/en/graphql/reference/mutations#removelabelsfromlabelable
M.add_labels_mutation = [[
mutation {
addLabelsToLabelable(input: {labelableId: "%s", labelIds: ["%s"]}) {
addLabelsToLabelable(input: {labelableId: "%s", labelIds: %s}) {
labelable {
... on Issue {
id
Expand All @@ -2651,7 +2651,7 @@ M.add_labels_mutation = [[
-- https://docs.github.com/en/graphql/reference/mutations#removelabelsfromlabelable
M.remove_labels_mutation = [[
mutation {
removeLabelsFromLabelable(input: {labelableId: "%s", labelIds: ["%s"]}) {
removeLabelsFromLabelable(input: {labelableId: "%s", labelIds: %s}) {
labelable {
... on Issue {
id
Expand Down
4 changes: 3 additions & 1 deletion lua/octo/pickers/fzf-lua/pickers/assigned_labels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ return function(cb)
},
actions = {
["default"] = function(selected)
local labels = {}
for _, row in ipairs(selected) do
local id, _ = unpack(vim.split(row, " "))
cb(id)
table.insert(labels, { id = id })
end
cb(labels)
end,
},
})
Expand Down
4 changes: 3 additions & 1 deletion lua/octo/pickers/fzf-lua/pickers/labels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ return function(cb)
},
actions = {
["default"] = function(selected)
local labels = {}
for _, row in ipairs(selected) do
local id, _ = unpack(vim.split(row, " "))
cb(id)
table.insert(labels, { id = id })
end
cb(labels)
end,
},
})
Expand Down
47 changes: 41 additions & 6 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,31 @@ end
--
-- LABELS
--

local function select(opts)
local prompt_bufnr = opts.bufnr
local single_cb = opts.single_cb
local multiple_cb = opts.multiple_cb
local get_item = opts.get_item

local picker = action_state.get_current_picker(prompt_bufnr)
local selections = picker:get_multi_selection()
local cb
local items = {}
if #selections == 0 then
local selection = action_state.get_selected_entry(prompt_bufnr)
table.insert(items, get_item(selection))
cb = single_cb
else
for _, selection in ipairs(selections) do
table.insert(items, get_item(selection))
end
cb = multiple_cb
end
actions.close(prompt_bufnr)
cb(items)
end

function M.select_label(cb)
local opts = vim.deepcopy(dropdown_opts)
local bufnr = vim.api.nvim_get_current_buf()
Expand All @@ -767,9 +792,14 @@ function M.select_label(cb)
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
local selected_label = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selected_label.label.id)
select {
bufnr = prompt_bufnr,
single_cb = cb,
multiple_cb = cb,
get_item = function(selection)
return selection.label
end,
}
end)
return true
end,
Expand Down Expand Up @@ -812,9 +842,14 @@ function M.select_assigned_label(cb)
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
local selected_label = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selected_label.label.id)
select {
bufnr = prompt_bufnr,
single_cb = cb,
multiple_cb = cb,
get_item = function(selection)
return selection.label
end,
}
end)
return true
end,
Expand Down

0 comments on commit 26b7c33

Please sign in to comment.