Skip to content

Commit

Permalink
add format key to table
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Feb 23, 2025
1 parent 7793e76 commit 7e3cffd
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
40 changes: 37 additions & 3 deletions lua/octo/gh/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,50 @@ function M.api.graphql(opts)
}
end

local rest = function(method, opts)
local run_opts = opts.opts or {}
---Format the endpoint with the format table
---@param endpoint string the endpoint to format
---@param format table<key, value> the format table
local format_endpoint = function(endpoint, format)
for key, value in pairs(format) do
endpoint = endpoint:gsub("{" .. key .. "}", value)
end
return endpoint
end

---@param method string the rest method
---@param opts table the options for the rest command
---@return table|nil
M.create_rest_args = function(method, opts)
local format = opts.format or {}

local endpoint = opts[1]
if not endpoint then
return
end
endpoint = format_endpoint(endpoint, format)
opts[1] = endpoint

local args = { "api" }
if method ~= nil then
table.insert(args, "--method")
table.insert(args, method)
end

opts.format = nil
opts.opts = nil
args = M.insert_args(args, opts)
return M.insert_args(args, opts)
end

---Run a rest command
local rest = function(method, opts)
local run_opts = opts.opts or {}

local args = M.create_rest_args(method, opts)
if not args then
local utils = require "octo.utils"
utils.error "Endpoint is required"
return
end

run {
args = args,
Expand Down
68 changes: 68 additions & 0 deletions lua/tests/plenary/gh_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,74 @@ describe("insert_args:", function()
end)
end)

local tables_have_same_elements = function(t1, t2)
if #t1 ~= #t2 then
return false
end
for _, v in ipairs(t1) do
if not vim.tbl_contains(t2, v) then
return false
end
end
for _, v in ipairs(t2) do
if not vim.tbl_contains(t1, v) then
return false
end
end
return true
end

local assert_tables_have_same_elements = function(t1, t2)
assert(
tables_have_same_elements(t1, t2),
string.format("Expected tables to have the same elements:\n%s\n%s", vim.inspect(t1), vim.inspect(t2))
)
end

describe("REST API args", function()
it("no args", function()
local actual = gh.create_rest_args(nil, {})
eq(actual, nil)
end)
it("Endpoint is required", function()
local actual = gh.create_rest_args(nil, { format = { owner = "pwntester" }, json = "id", jq = ".id" })
eq(actual, nil)
end)
it("Returns table with untouched endpoint", function()
local actual = gh.create_rest_args("GET", {
"repos/pwntester/octo.nvim/pulls",
jq = ".[].number",
paginate = true,
})
assert_tables_have_same_elements(actual, {
"api",
"--method",
"GET",
"repos/pwntester/octo.nvim/pulls",
"--jq",
".[].number",
"--paginate",
})
end)
it("Returns table with formated endpoint", function()
local actual = gh.create_rest_args("GET", {
"repos/{owner}/{name}/pulls",
format = { owner = "pwntester", name = "octo.nvim" },
jq = ".[].number",
paginate = true,
})
assert_tables_have_same_elements(actual, {
"api",
"--method",
"GET",
"repos/pwntester/octo.nvim/pulls",
"--jq",
".[].number",
"--paginate",
})
end)
end)

describe("CLI commands", function()
it("gh.<something> returns table", function()
local commands = {
Expand Down

0 comments on commit 7e3cffd

Please sign in to comment.