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
gh
to work with them.Background on
gh.run
CommandImportant
This is just background and is not the recommended way to use the GitHub CLI with the plugin.
The
gh.run
command is used to run arbitrary GitHub CLI commands. It usesplenary.job
under the hood.In addition to
args
, there are parameters to handle:mode
:async
orsync
(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_DEBUG
environment 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
mode
By 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
cb
Callbacks will take
stdout
andstderr
.There is a convenience function
gh.create_callback
which handles success and failure cases.Tip
By default, the
success
isutils.info
andfailure
isutils.error
sogh.create_callback()
is easy first start.Arbitrary GitHub CLI Commands
Instead of using
gh.run
directly, thegh
module provides wrapper which builds these commands. It is meant to be as close to the CLI syntax as possible. For instance,gp pr list
is:Note
This calls
gh.run { args = { "pr", "list" } }
Any of the other
opts
fromgh.run
can 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 parameteropts
is saved to pass togh.run
. For example, running the commandgh pr list --json id,number,title --search is:merged
is: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 1
is just
Tip
Underscores in keys will become hyphens. For example,
gh issue edit 1 --add-label bug
isgh.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 create
Give 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/issues
is: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.get
specifically forGET
requests.Same goes for
POST
,PATCH
,PUT
,DELETE
requests.Format Endpoint
Use the
format
parameter 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.login
with the query being,can be run like this:
Convenient
query
parameterInstead of passing
query
tof
, 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
fields
parameter as an alternativeBeta Was this translation helpful? Give feedback.
All reactions