Repository navigation
Add an example to gh api --help demonstrating usage of --input #9104
Copy link
Copy link
Closed
Labels
Description
Activity
- addedhelp wantedContributions welcomeContributions welcomegh-apirelating to the gh api commandrelating to the gh api command
on May 21, 2024 Hey @williammartin
I recently came across this issue and was bummed that it was not on the docs, so I'm adding it now, but I have a few questions;
First I've only added two lines, do you think i should also write some tests or this is already tested?
diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index c8eac911..f6e3c5d1 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -156,6 +156,9 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command -F 'properties[][allowed_values][]=staging' \ -F 'properties[][allowed_values][]=production' + # pass an external JSON file as the request body + $ gh api repos/{owner}/{repo}/rulesets --input my-request-body.json + # list releases with GraphQL $ gh api graphql -F owner='{owner}' -F name='{repo}' -f query=' query($name: String!, $owner: String!) {
Also this comment gives a much better example but i did not want to bloat the examples like that. If thats not a problem I can propose a change like this;
index c8eac911..10cd5d7f 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -156,6 +156,39 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command -F 'properties[][allowed_values][]=staging' \ -F 'properties[][allowed_values][]=production' + # pass a JSON string as the request body + echo '{ + "name": "example ruleset", + "target": "branch", + "enforcement": "active", + "conditions": { + "ref_name": { + "exclude": [], + "include": [ + "~DEFAULT_BRANCH" + ] + } + }, + "rules": [ + { + "type": "deletion" + }, + { + "type": "pull_request", + "parameters": { + "required_approving_review_count": 1, + "dismiss_stale_reviews_on_push": true, + "require_code_owner_review": false, + "require_last_push_approval": true, + "required_review_thread_resolution": true + } + } + ] + }' | gh api repos/{owner}/{repo}/rulesets --input - + + # pass an external JSON file as the request body + $ gh api repos/{owner}/{repo}/rulesets --input my-request-body.json + # list releases with GraphQL $ gh api graphql -F owner='{owner}' -F name='{repo}' -f query=' query($name: String!, $owner: String!) {
Reacted by Babak K. Shandiz
Description
As mentioned in #8983, when it becomes challenging to use
-fand-Fto construct the exact JSON request body forgh api, the best option is to fallback to constructing JSON externally and to pass that via the--inputflag.#8983 (comment) suggests that we add to the example section of
gh api --helpto make this more obvious, and that seems like a great idea. The code to change is here:cli/pkg/cmd/api/api.go
Line 126 in ff3a598