Using GitHub CLI from Lua #876
williambdean
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Interested in using the GitHub CLI, REST API, or GraphQL API in your Lua code? Octo.nvim provides a module
ghto work with them.Background on
gh.runCommandImportant
This is just background and is not the recommended way to use the GitHub CLI with the plugin.
The
gh.runcommand is used to run arbitrary GitHub CLI commands. It usesplenary.jobunder the hood.In addition to
args, there are parameters to handle:mode:asyncorsync(default isasync)cb: callback function for async modestream_cb: callback function for streaming output in async modeheaders: table of headers to pass to the command. There are some good defaults already.debug: set theGH_DEBUGenvironment to print requests to stderr. Read more here.This function uses timeout, hostname, the GitHub CLI binary specified in octo configuration. It also ensures that the environment variables are set correctly.
Synchronous Calls with
modeBy default, all calls are made async. If the output is desired, this is a good route.
Otherwise, pass a callback with
cb.Passing callbacks to
cbCallbacks will take
stdoutandstderr.There is a convenience function
gh.create_callbackwhich handles success and failure cases.Tip
By default, the
successisutils.infoandfailureisutils.errorsogh.create_callback()is easy first start.Arbitrary GitHub CLI Commands
Instead of using
gh.rundirectly, theghmodule provides wrapper which builds these commands. It is meant to be as close to the CLI syntax as possible. For instance,gp pr listis:Note
This calls
gh.run { args = { "pr", "list" } }Any of the other
optsfromgh.runcan be passed toopts. For instance, making a synchronous call:Note
This calls
gh.run { args = { "pr", "list" }, mode = "sync" }Arguments and Flags
Any additional arguments and options are added to
args. The parameteroptsis saved to pass togh.run. For example, running the commandgh pr list --json id,number,title --search is:mergedis:Note
This calls
gh.run { args = { "pr", "list", "--json", "id,number,title", "--search", "is:merged"} }Positional arguments
Just pass positional arguments to the table. For example,
gh issue edit 1is just
Tip
Underscores in keys will become hyphens. For example,
gh issue edit 1 --add-label bugisgh.issue.edit { 1, add_label = "bug" }.User Input Hangs
Warning
Some commands require user input which will make the program hang. For example
gh pr createGive each command a try to see if it works in the terminal. Provide the necessary input to avoid.
Using extensions
The commands are not being checked so even installed extensions can be used. For instance,
gh copilot explain "tmux has-session":Or
gh models run gpt-4o-mini "How many planets are in the solar system":REST API
Access the REST API similar to with the GitHub CLI commands:
gh api <endpoint> [flags]For example,
gh api /repos/:owner/:repo/issuesis:Arguments and options can be passed just like they were above! This includes the fields and raw fields. For example,
gh api --method GET search/issues -f q='repo:cli/cli is:open remote'is done by passing table to keyf:Wrappers for HTTP Methods
Use
gh.api.getspecifically forGETrequests.Same goes for
POST,PATCH,PUT,DELETErequests.Format Endpoint
Use the
formatparameter in the table to format a provided endpoint. Any name in brackets{}will be replaced. For example,Nested Parameters
For nested parameters, use a table.
For example, creating an issue with REST API allows for lists of labels and assignees. This is done by passing a table to the key.
This get translated to
gh api --method POST /repos/:owner/:repo/issues -f "title=Found a difficult bug" -f "body=Please help fix this bug" -f "labels[]=bug" -f "labels[]=help wanted" -f "assignees[]=octocat".See a similar example here.
GraphQL API
The GraphQL API can be used in a similar manner.
For example, the request
gh api graphql -f <query> --jq .data.viewer.loginwith the query being,can be run like this:
Convenient
queryparameterInstead of passing
querytof, pass it to a designated parameterquery. For example, the logic above becomes:Parameterized Queries
Use parameterized queries just like with the GitHub CLI. For example,
gh api graphql -f <query> -F owner=pwntester -F name=octo.nvim -F states[]=OPEN -F states[]=CLOSED --jq '[ .data.repository.issues.nodes[] | {title, state, stateReason} ]'is the following:Tip
Use the designated
fieldsparameter as an alternative toF.Beta Was this translation helpful? Give feedback.
All reactions