Skip to content

Commit

Permalink
Refactor milestone logic (#871)
Browse files Browse the repository at this point in the history
* sub _ for - in commands

* refactor to use command syntax

* add some tests
  • Loading branch information
wd60622 authored Feb 20, 2025
1 parent 2c1647e commit dc67d3f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
11 changes: 9 additions & 2 deletions lua/octo/gh/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,19 @@ end
---Insert the options into the args table
---@param args table the arguments table
---@param options table the options to insert
---@param replace table key value pairs to replace in the key of the options
---@return table the updated args table
M.insert_args = function(args, options)
M.insert_args = function(args, options, replace)
replace = replace or {}

for key, value in pairs(options) do
if type(key) == "number" then
table.insert(args, value)
else
for k, v in pairs(replace) do
key = string.gsub(key, k, v)
end

local flag = create_flag(key)

if type(value) == "table" then
Expand Down Expand Up @@ -297,7 +304,7 @@ local create_subcommand = function(command)
}

opts.opts = nil
args = M.insert_args(args, opts)
args = M.insert_args(args, opts, { ["_"] = "-" })

return M.run {
args = args,
Expand Down
40 changes: 20 additions & 20 deletions lua/octo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,17 @@ end
---@param milestone_name string milestone name
function M.add_milestone(issue, number, milestone_name)
local command = issue and "issue" or "pr"
local args = { command, "edit", number, "--milestone", milestone_name }

gh.run {
args = args,
cb = function(output, stderr)
if stderr and not M.is_blank(stderr) then
M.error(stderr)
elseif output then
M.info("Added milestone " .. milestone_name)
end
end,
gh[command].edit {
number,
milestone = milestone_name,
opts = {
cb = gh.create_callback {
success = function(_)
M.info("Added milestone " .. milestone_name)
end,
},
},
}
end

Expand All @@ -348,17 +348,17 @@ end
---@param number number issue or PR number
function M.remove_milestone(issue, number)
local command = issue and "issue" or "pr"
local args = { command, "edit", number, "--remove-milestone" }

gh.run {
args = args,
cb = function(output, stderr)
if stderr and not M.is_blank(stderr) then
M.error(stderr)
elseif output then
M.info "Removed milestone"
end
end,
gh[command].edit {
number,
remove_milestone = true,
opts = {
cb = gh.create_callback {
success = function(_)
M.info "Removed milestone"
end,
},
},
}
end

Expand Down
35 changes: 35 additions & 0 deletions lua/tests/plenary/gh_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,41 @@ describe("insert_args:", function()
}
eq(args, expected)
end)
it("Replace mapping default", function()
local args = {}
local opts = {
remove_label = "Some label",
}
gh.insert_args(args, opts)
local expected = {
"--remove_label",
"Some label",
}
eq(args, expected)
end)
it("Replace mapping underscores", function()
local args = {}
local opts = {
remove_label = "Some label",
}
gh.insert_args(args, opts, { ["_"] = "-" })
local expected = {
"--remove-label",
"Some label",
}
eq(args, expected)
end)
it("Replace mapping underscores boolean", function()
local args = {}
local opts = {
remove_milestone = true,
}
gh.insert_args(args, opts, { ["_"] = "-" })
local expected = {
"--remove-milestone",
}
eq(args, expected)
end)
it("single characters have single hyphen", function()
local args = {}
local opts = {
Expand Down

0 comments on commit dc67d3f

Please sign in to comment.