Skip to content

Commit

Permalink
Generalize GraphQL args (#782)
Browse files Browse the repository at this point in the history
* generalize the args

* make public

* create tests

* point to the tests

* move the tests

* break out the tests

* change the order

* simplify

* change the names

* use the global

* add additional case

* switch f and F
  • Loading branch information
wd60622 authored Jan 15, 2025
1 parent 105dd4a commit 345ac95
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 30 deletions.
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)

0 comments on commit 345ac95

Please sign in to comment.