Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more examples to api docs #2951

Merged
merged 1 commit into from Feb 12, 2021
Merged

Add more examples to api docs #2951

merged 1 commit into from Feb 12, 2021

Conversation

mislav
Copy link
Contributor

@mislav mislav commented Feb 11, 2021

  • Clarify that fields need to be in "key=value" format
  • Headers need to be in "key:value" format
  • Contrast POST vs GET requests with params in examples
  • Add an example of how to add HTTP headers
  • Use backticks for denoting inline code
Makes an authenticated HTTP request to the GitHub API and prints the response.

The endpoint argument should either be a path of a GitHub API v3 endpoint, or
"graphql" to access the GitHub API v4.

Placeholder values ":owner", ":repo", and ":branch" in the endpoint argument will
get replaced with values from the repository of the current directory.

The default HTTP request method is "GET" normally and "POST" if any parameters
were added. Override the method with `--method`.

Pass one or more `--raw-field` values in "key=value" format to add
JSON-encoded string parameters to the POST body.

The `--field` flag behaves like `--raw-field` with magic type conversion based
on the format of the value:

- literal values "true", "false", "null", and integer numbers get converted to
  appropriate JSON types;
- placeholder values ":owner", ":repo", and ":branch" get populated with values
  from the repository of the current directory;
- if the value starts with "@", the rest of the value is interpreted as a
  filename to read the value from. Pass "-" to read from standard input.

For GraphQL requests, all fields other than "query" and "operationName" are
interpreted as GraphQL variables.

Raw request body may be passed from the outside via a file specified by `--input`.
Pass "-" to read from standard input. In this mode, parameters specified via
`--field` flags are serialized into URL query parameters.

In `--paginate` mode, all pages of results will sequentially be requested until
there are no more pages of results. For GraphQL requests, this requires that the
original query accepts an `$endCursor: String` variable and that it fetches the
`pageInfo{ hasNextPage, endCursor }` set of fields from a collection.


USAGE
  gh api <endpoint> [flags]

FLAGS
  -F, --field key=value       Add a typed parameter in key=value format
  -H, --header key:value      Add a HTTP request header in key:value format
      --hostname string       The GitHub hostname for the request (default "github.com")
  -i, --include               Include HTTP response headers in the output
      --input file            The file to use as body for the HTTP request
  -X, --method string         The HTTP method for the request (default "GET")
      --paginate              Make additional HTTP requests to fetch all pages of results
  -f, --raw-field key=value   Add a string parameter in key=value format
      --silent                Do not print the response body

INHERITED FLAGS
  --help   Show help for command

EXAMPLES
  # list releases in the current repository
  $ gh api repos/:owner/:repo/releases
  
  # post an issue comment
  $ gh api repos/:owner/:repo/issues/123/comments -f body='Hi from CLI'
  
  # add parameters to a GET request
  $ gh api -X GET search/issues -f q='repo:cli/cli is:open remote'
  
  # set a custom HTTP header
  $ gh api -H 'Accept: application/vnd.github.XYZ-preview+json' ...
  
  # list releases with GraphQL
  $ gh api graphql -F owner=':owner' -F name=':repo' -f query='
    query($name: String!, $owner: String!) {
      repository(owner: $owner, name: $name) {
        releases(last: 3) {
          nodes { tagName }
        }
      }
    }
  '
  
  # list all repositories for a user
  $ gh api graphql --paginate -f query='
    query($endCursor: String) {
      viewer {
        repositories(first: 100, after: $endCursor) {
          nodes { nameWithOwner }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  '

ENVIRONMENT VARIABLES
  GH_TOKEN, GITHUB_TOKEN (in order of precedence): an authentication token for
  github.com API requests.
  
  GH_ENTERPRISE_TOKEN, GITHUB_ENTERPRISE_TOKEN (in order of precedence): an
  authentication token for API requests to GitHub Enterprise.
  
  GH_HOST: make the request to a GitHub host other than github.com.

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual

Ref. #2941, Closes #2948

- Clarify that fields need to be in "key=value" format
- Headers need to be in "key:value" format
- Contrast POST vs GET requests with params in examples
- Add an example of how to add HTTP headers
- Use backticks where applicable
@mislav mislav requested a review from a team February 11, 2021 18:22
@mislav mislav added this to Backlog 🗒 in The GitHub CLI via automation Feb 11, 2021
@mislav mislav moved this from Backlog 🗒 to Needs review 🤔 in The GitHub CLI Feb 11, 2021
Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the changing of single quotes to backticks.

The GitHub CLI automation moved this from Needs review 🤔 to Needs to be merged 🎉 Feb 11, 2021
@mislav mislav merged commit 6b1e6db into trunk Feb 12, 2021
The GitHub CLI automation moved this from Needs to be merged 🎉 to Pending Release 🥚 Feb 12, 2021
@mislav mislav deleted the api-docs branch February 12, 2021 11:58
@github-actions github-actions bot moved this from Pending Release 🥚 to Done 💤 in The GitHub CLI Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
The GitHub CLI
  
Done 💤
Development

Successfully merging this pull request may close these issues.

None yet

2 participants