Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1838,15 +1838,31 @@ function M.pr_checks()
}
end

--- Merges a PR by number or from the current buffer
function M.merge_pr(...)
local buffer = utils.get_current_buffer()
if not buffer or not buffer:isPullRequest() then
return
end

local args = { "pr", "merge", tostring(buffer.number) }
local args = { "pr", "merge" }
local params = table.pack(...)
local conf = config.values
local pr_number = nil

-- Get PR number from argument if valid
if utils.is_number_like(params[1]) then
pr_number = tostring(params[1])
table.remove(params, 1)
end
Comment on lines +1849 to +1852
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be handled with the run wrapper now

local gh = require "octo.gh"
gh.pr.merge { number } 


if not pr_number then
local buffer = utils.get_current_buffer()

if not buffer or not buffer:isPullRequest() then
utils.error("No PR number provided and buffer is not a PR")
return
end

pr_number = tostring(buffer.number)
end

table.insert(args, pr_number)

local merge_method = conf.default_merge_method
for _, param in ipairs(params) do
Expand Down Expand Up @@ -1876,8 +1892,17 @@ function M.merge_pr(...)
gh.run {
args = args,
cb = function(output, stderr)
utils.info(output .. " " .. stderr)
writers.write_state(buffer.bufnr)
if not utils.is_blank(strerr) then
utils.error(stderr)
else
utils.info(output)
end

local buffer = utils.get_current_buffer()

if buffer and buffer:isPullRequest() then
writers.write_state(buffer.bufnr)
end
end,
}
end
Expand Down
3 changes: 2 additions & 1 deletion lua/octo/pickers/snacks/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local gh = require "octo.gh"
local graphql = require "octo.gh.graphql"
local queries = require "octo.gh.queries"
local utils = require "octo.utils"
local commands = require "octo.commands"
local octo_config = require "octo.config"
local navigation = require "octo.navigation"
local Snacks = require "snacks"
Expand Down Expand Up @@ -257,7 +258,7 @@ function M.pull_requests(opts)

if not custom_actions_defined["merge_pr"] then
final_actions["merge_pr"] = function(_picker, item)
utils.merge_pr(item.number)
commands.merge_pr(item.number)
end
end
if not final_keys[cfg.picker_config.mappings.merge_pr.lhs] then
Expand Down
3 changes: 2 additions & 1 deletion lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local previewers = require "octo.pickers.telescope.previewers"
local entry_maker = require "octo.pickers.telescope.entry_maker"
local reviews = require "octo.reviews"
local utils = require "octo.utils"
local commands = require "octo.command"
local octo_config = require "octo.config"
local notifications = require "octo.notifications"

Expand Down Expand Up @@ -305,7 +306,7 @@ local function merge_pull_request()
return function(prompt_bufnr)
local sel = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
utils.merge_pr(sel.obj.number)
commands.merge_pr(sel.obj.number)
end
end

Expand Down
43 changes: 13 additions & 30 deletions lua/octo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -625,36 +625,6 @@ function M.insert_delete_flag(args, delete)
end
end

---Merges a PR by number
function M.merge_pr(pr_number)
if not Job then
M.error "Aborting PR merge"
return
end

local conf = config.values
local args = { "pr", "merge", pr_number }

M.insert_merge_flag(args, conf.default_merge_method)
M.insert_delete_flag(args, conf.default_delete_branch)

---@diagnostic disable-next-line: missing-fields
Job:new({
command = "gh",
args = args,
on_exit = vim.schedule_wrap(function(job, code)
if code == 0 then
M.info("Merged PR " .. pr_number .. "!")
else
local stderr = table.concat(job:stderr_result(), "\n")
if not M.is_blank(stderr) then
M.error(stderr)
end
end
end),
}):start()
end

--- Formats a integer a large integer by taking the most significant digits with a suffix.
--- e.g. 123456789 -> 12.3m
---@param n integer
Expand Down Expand Up @@ -2020,4 +1990,17 @@ function M.print_err(msg)
vim.api.nvim_echo({ { msg } }, true, { err = true })
end

--- @param val string
--- Check if a value is a number or numeric string (e.g. 123 or "456")
function M.is_number_like(val)
if type(val) == "number" then
return true
end
if type(val) == "string" then
local n = tonumber(val)
return n ~= nil and tostring(n) == val
end
return false
end

return M