diff --git a/lua/octo/gh/init.lua b/lua/octo/gh/init.lua index 1356d3fa..536cb9f2 100644 --- a/lua/octo/gh/init.lua +++ b/lua/octo/gh/init.lua @@ -141,7 +141,7 @@ end ---Run a gh command ---@param opts RunOpts ---@return string[]|nil -function M.run(opts) +local function run(opts) if not Job then return end @@ -279,12 +279,15 @@ local create_graphql_args = function(query, fields, paginate, slurp, jq) return M.insert_args(args, opts) end +--- The gh.api commands +M.api = {} + ---Run a graphql query ---@param opts table the options for the graphql query ---@return table|nil -function M.graphql(opts) +function M.api.graphql(opts) local run_opts = opts.opts or {} - return M.run { + return run { args = create_graphql_args(opts.query, opts.fields, opts.paginate, opts.slurp, opts.jq), mode = run_opts.mode, cb = run_opts.cb, @@ -305,7 +308,7 @@ local rest = function(method, opts) opts.opts = nil args = M.insert_args(args, opts) - M.run { + run { args = args, mode = run_opts.mode, cb = run_opts.cb, @@ -315,24 +318,25 @@ local rest = function(method, opts) } end -M.api = { - graphql = M.graphql, - get = function(opts) - return rest("GET", opts) - end, - post = function(opts) - return rest("POST", opts) - end, - patch = function(opts) - return rest("PATCH", opts) - end, - delete = function(opts) - return rest("DELETE", opts) - end, - put = function(opts) - return rest("PUT", opts) - end, -} +M.api.get = function(opts) + return rest("GET", opts) +end + +M.api.post = function(opts) + return rest("POST", opts) +end + +M.api.patch = function(opts) + return rest("PATCH", opts) +end + +M.api.delete = function(opts) + return rest("DELETE", opts) +end + +M.api.put = function(opts) + return rest("PUT", opts) +end ---Call the api without specifying the method. GitHub CLI determines the method based on the arguments setmetatable(M.api, { @@ -346,6 +350,12 @@ local create_subcommand = function(command) subcommand.command = command setmetatable(subcommand, { + __call = function(_, opts) + --- Allow for backwards compatibility with the old API gh.run { ... } + if command == "run" then + return run(opts) + end + end, __index = function(t, key) return function(opts) opts = opts or {} @@ -360,7 +370,7 @@ local create_subcommand = function(command) opts.opts = nil args = M.insert_args(args, opts, { ["_"] = "-" }) - return M.run { + return run { args = args, mode = run_opts.mode, cb = run_opts.cb, diff --git a/lua/octo/pickers/telescope/provider.lua b/lua/octo/pickers/telescope/provider.lua index 2e725c91..68446f11 100644 --- a/lua/octo/pickers/telescope/provider.lua +++ b/lua/octo/pickers/telescope/provider.lua @@ -522,7 +522,7 @@ end local function get_search_size(prompt) local query = graphql("search_count_query", prompt) - return gh.graphql { + return gh.api.graphql { query = query, jq = ".data.search.issueCount", opts = { @@ -1391,7 +1391,7 @@ function M.discussions(opts) local query = graphql "discussions_query" utils.info "Fetching discussions (this may take a while) ..." - gh.graphql { + gh.api.graphql { query = query, fields = { owner = owner, @@ -1418,7 +1418,7 @@ function M.milestones(opts) local owner, name = utils.split_repo(repo) local query = graphql "open_milestones_query" - gh.graphql { + gh.api.graphql { query = query, fields = { owner = owner,