Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize GraphQL args #782

Merged
merged 13 commits into from
Jan 15, 2025
78 changes: 48 additions & 30 deletions lua/octo/gh/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,45 @@ function M.run(opts)
end
end

---Format fields for the graphql query
---@param fields table key value pairs for graphql query
---@return table
local format_fields = function(fields)
local formatted_fields = {}
for key, value in pairs(fields) do
table.insert(formatted_fields, "-F")
table.insert(formatted_fields, key .. "=" .. value)
local create_flag = function(key)
if #key == 1 then
return "-" .. key
else
return "--" .. key
end
return formatted_fields
end

---Insert the options into the args table
---@param args table the arguments table
---@param options table the options to insert
---@return table the updated args table
M.insert_args = function(args, options)
for key, value in pairs(options) do
local flag = create_flag(key)

if type(value) == "table" then
for k, v in pairs(value) do
if type(v) == "table" then
for _, vv in ipairs(v) do
table.insert(args, flag)
table.insert(args, k .. "[]=" .. vv)
end
else
table.insert(args, flag)
table.insert(args, k .. "=" .. v)
end
end
elseif type(value) == "boolean" then
if value then
table.insert(args, flag)
end
else
table.insert(args, flag)
table.insert(args, value)
end
end

return args
end

---Create the arguments for the graphql query
Expand All @@ -195,28 +224,17 @@ end
local create_graphql_args = function(query, fields, paginate, slurp, jq)
local args = { "api", "graphql" }

fields = fields or {}
local formatted_fields = format_fields(fields)
for _, field in ipairs(formatted_fields) do
table.insert(args, field)
end
table.insert(args, "-f")
table.insert(args, string.format("query=%s", query))

if paginate then
table.insert(args, "--paginate")
end

if slurp then
table.insert(args, "--slurp")
end

if jq then
table.insert(args, "--jq")
table.insert(args, jq)
end
local opts = {
f = {
query = query,
},
F = fields,
paginate = paginate,
slurp = slurp,
jq = jq,
}

return args
return M.insert_args(args, opts)
end

---Run a graphql query
Expand Down
75 changes: 75 additions & 0 deletions lua/tests/plenary/gh_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local gh = require "octo.gh"
local eq = assert.are.same

describe("insert_args:", function()
it("true booleans show up as flags", function()
local args = {}
local opts = {
slurp = false,
paginate = true,
}
gh.insert_args(args, opts)
local expected = {
"--paginate",
}
eq(args, expected)
end)
it("single characters have single hyphen", function()
local args = {}
local opts = {
F = {
query = "query",
},
}
gh.insert_args(args, opts)
local expected = {
"-F",
"query=query",
}
eq(args, expected)
end)
it("non-single changes have two hyphens", function()
local args = {}
local opts = {
jq = ".",
}
gh.insert_args(args, opts)
local expected = {
"--jq",
".",
}
eq(args, expected)
end)
it("list of fields get brackets", function()
local args = {}
local opts = {
f = {
items = { "a", "b", "c" },
},
}
gh.insert_args(args, opts)
local expected = {
"-f",
"items[]=a",
"-f",
"items[]=b",
"-f",
"items[]=c",
}
eq(args, expected)
end)
it("integer values", function()
local args = {}
local opts = {
f = {
num_issues = 15,
},
}
gh.insert_args(args, opts)
local expected = {
"-f",
"num_issues=15",
}
eq(args, expected)
end)
end)