From 706e1d1d3626c9bd90146b7a7c293be1fc786b3b Mon Sep 17 00:00:00 2001 From: Will Dean Date: Mon, 17 Feb 2025 22:18:10 +0100 Subject: [PATCH] allow for arbitrary callback in label pickers --- lua/octo/commands.lua | 63 ++++----- lua/octo/pickers/telescope/provider.lua | 162 +++++++++++++----------- 2 files changed, 123 insertions(+), 102 deletions(-) diff --git a/lua/octo/commands.lua b/lua/octo/commands.lua index a14c0725..cefa9de2 100644 --- a/lua/octo/commands.lua +++ b/lua/octo/commands.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/octo/pickers/telescope/provider.lua b/lua/octo/pickers/telescope/provider.lua index 90ee98df..76fb2d16 100644 --- a/lua/octo/pickers/telescope/provider.lua +++ b/lua/octo/pickers/telescope/provider.lua @@ -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) @@ -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