You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In addition to args, there are parameters to handle:
mode: async or sync (default is async)
cb: callback function for async mode
stream_cb: callback function for streaming output in async mode
headers: table of headers to pass to the command. There are some good defaults already.
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.
By default, the success is utils.info and failure is utils.error so gh.create_callback() is easy first start.
Arbitrary GitHub CLI Commands
Instead of using gh.run directly, the gh 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:
gh.pr.list {}
Note
This calls gh.run { args = { "pr", "list" } }
Any of the other opts from gh.run can be passed to opts. For instance, making a synchronous call:
Any additional arguments and options are added to args. The parameter opts is saved to pass to gh.run. For example, running the command gh pr list --json id,number,title --search is:merged is:
Or gh models run gpt-4o-mini "How many planets are in the solar system":
localgh=require"octo.gh"localopts= { cb=gh.create_callback() }
localmodel="gpt-4o-mini"localprompt="How many planets are in the solar system"gh.models.run {
model,
prompt,
opts=opts,
}
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 key f:
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:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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
CommandThe
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.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" }
.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,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