From 6e7c5004c74aa74845e609de932ee131c18a1532 Mon Sep 17 00:00:00 2001 From: Peter Gundel Date: Wed, 1 Mar 2023 00:37:49 +0100 Subject: [PATCH 1/2] Add gh issues develop #ID --checkout This adds the only recently available Github CLI command to create a branch for an issue and checking it locally out. See https://github.com/cli/cli/issues/5469 --- README.md | 1 + lua/telescope/_extensions/gh_actions.lua | 22 ++++++++++++++++++++++ lua/telescope/_extensions/gh_builtin.lua | 1 + 3 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 64fc1ff..77b5a28 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ Telescope gh issues author=windwp label=bug | `` | insert a reference to the issue | | `` | open web | | `` | insert a markdown-link to the issue | +| `` | create a branch for the issue and check it out locally | ### Gist #### Options Filter diff --git a/lua/telescope/_extensions/gh_actions.lua b/lua/telescope/_extensions/gh_actions.lua index 964f72c..52c64e7 100644 --- a/lua/telescope/_extensions/gh_actions.lua +++ b/lua/telescope/_extensions/gh_actions.lua @@ -92,6 +92,28 @@ A.gh_issue_insert_markdown_link = function(prompt_bufnr) end end +A.gh_issue_develop = function(prompt_bufnr) + local selection = action_state.get_selected_entry() + actions.close(prompt_bufnr) + local tmp_table = vim.split(selection.value, "\t") + if vim.tbl_isempty(tmp_table) then + return + end + local id = tmp_table[1] + -- We can't use os.execute here because it takes a bit + vim.fn.jobstart( + "echo \"Creating a new branch ...\" && gh issue develop " .. id .. " --checkout && echo \"Done!\"", + { + on_stdout = function(_channel_id, data, _name) + if data[1] ~= "" then + print(data[1]) + end + end + } + ) +end + + A.gh_pr_checkout = function(prompt_bufnr) local pr_number = close_telescope_prompt(prompt_bufnr) gh_qf_action(pr_number, "checkout", "Checking out pull request #") diff --git a/lua/telescope/_extensions/gh_builtin.lua b/lua/telescope/_extensions/gh_builtin.lua index f29fd8d..599b6cf 100644 --- a/lua/telescope/_extensions/gh_builtin.lua +++ b/lua/telescope/_extensions/gh_builtin.lua @@ -111,6 +111,7 @@ B.gh_issues = function(opts) actions.select_default:replace(gh_a.gh_issue_insert) map("i", "", gh_a.gh_web_view "issue") map("i", "", gh_a.gh_issue_insert_markdown_link) + map("i", "", gh_a.gh_issue_develop) return true end, }):find() From 3ba5722e3d59da06225a98fac051dc18c9cd0aae Mon Sep 17 00:00:00 2001 From: Peter Gundel Date: Wed, 1 Mar 2023 06:58:32 +0100 Subject: [PATCH 2/2] Reuse gh_qf_action for issue develop --- lua/telescope/_extensions/gh_actions.lua | 30 ++++++++---------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/lua/telescope/_extensions/gh_actions.lua b/lua/telescope/_extensions/gh_actions.lua index 52c64e7..0080d89 100644 --- a/lua/telescope/_extensions/gh_actions.lua +++ b/lua/telescope/_extensions/gh_actions.lua @@ -16,13 +16,13 @@ local function close_telescope_prompt(prompt_bufnr) end return tmp_table[1] end -local function gh_qf_action(pr_number, action, msg) - if pr_number == nil then +local function gh_qf_action(type, pr_or_issue_number, action, msg) + if pr_or_issue_number == nil then return end local qf_entry = { { - text = msg .. pr_number .. ", please wait ...", + text = msg .. pr_or_issue_number .. ", please wait ...", } } local on_output = function(_, line) @@ -36,7 +36,7 @@ local function gh_qf_action(pr_number, action, msg) local job = Job:new { enable_recording = true, command = "gh", - args = flatten { "pr", action, pr_number }, + args = flatten { type, action, pr_or_issue_number }, on_stdout = on_output, on_stderr = on_output, @@ -45,7 +45,7 @@ local function gh_qf_action(pr_number, action, msg) pcall(vim.schedule_wrap(function() vim.cmd [[cclose]] end)) - print "Pull request completed" + print "Done!" end end, } @@ -99,24 +99,14 @@ A.gh_issue_develop = function(prompt_bufnr) if vim.tbl_isempty(tmp_table) then return end - local id = tmp_table[1] - -- We can't use os.execute here because it takes a bit - vim.fn.jobstart( - "echo \"Creating a new branch ...\" && gh issue develop " .. id .. " --checkout && echo \"Done!\"", - { - on_stdout = function(_channel_id, data, _name) - if data[1] ~= "" then - print(data[1]) - end - end - } - ) + local issue_number = tmp_table[1] + gh_qf_action("issue", issue_number, { "develop", "--checkout" }, "Create and checkout branch for issue #") end A.gh_pr_checkout = function(prompt_bufnr) local pr_number = close_telescope_prompt(prompt_bufnr) - gh_qf_action(pr_number, "checkout", "Checking out pull request #") + gh_qf_action("pr", pr_number, "checkout", "Checking out pull request #") end A.gh_web_view = function(type) @@ -249,13 +239,13 @@ A.gh_pr_merge = function(prompt_bufnr) action = "-s" end if action ~= nil then - gh_qf_action(pr_number, { "merge", action }, "Merge pull request #") + gh_qf_action("pr", pr_number, { "merge", action }, "Merge pull request #") end end A.gh_pr_approve = function(prompt_bufnr) local pr_number = close_telescope_prompt(prompt_bufnr) - gh_qf_action(pr_number, { "review", "--approve" }, "Approve pull request #") + gh_qf_action("pr", pr_number, { "review", "--approve" }, "Approve pull request #") end A.gh_run_web_view = function(prompt_bufnr)