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

New hub api command for making GitHub API requests #2016

Merged
merged 9 commits into from
Jan 26, 2019
Merged

New hub api command for making GitHub API requests #2016

merged 9 commits into from
Jan 26, 2019

Conversation

mislav
Copy link
Owner

@mislav mislav commented Jan 24, 2019

Usage: hub api [-t] [-X <METHOD>] [--cache <TTL>] <ENDPOINT> [-F <KEY-VALUE>]

Goals:

  • provide a low-level interface to the GitHub API that empowers GitHub users to automate their workflows via shell scripts
  • close many outstanding feature requests in this repository that are unlikely to be implemented in hub core in the near future, but which can be easily implemented as a simple shell script using hub api

Features:

  • make arbitrary HTTP requests to the GitHub API
  • serialize GET data into query string
  • serialize POST/PUT data as JSON
  • read data to serialize from a file or standard input
  • parse response JSON into line-based output
  • cache sucessful responses
  • replace {owner} and {repo} placeholders with values from current repository

The above abilities also make it possible to make GraphQL queries, since that simply involves encoding/decoding JSON from the /graphql endpoint.

Examples:

# fetch information about the currently authenticated user as JSON
$ hub api user

# list user repositories as line-based output
$ hub api --flat users/octocat/repos

# post a comment to issue #23 of the current repository
$ hub api repos/{owner}/{repo}/issues/23/comments --raw-field "body=Nice job!"

# perform a GraphQL query read from a file
$ hub api graphql -F query=@path/to/myquery.graphql

This ensures that calling `hub api http://example.com/foo` doesn't
accidentally send the OAuth token for `github.com` to `example.com`.
- sort query string in cache key
- include "Accept", "Authorization" headers in cache key
- allow caching of `/graphql` responses
@mislav mislav merged commit bb32307 into master Jan 26, 2019
@mislav mislav deleted the api-cmd branch January 26, 2019 15:06
@mislav
Copy link
Owner Author

mislav commented Jan 26, 2019

Real-world usage example in bash:

# Usage: search [-<LIMIT>] <SEARCH-TERM> [<PAGINATION-CURSOR>]
search() {
  local args=()
  if [ "${1#-}" -gt 0 ] 2>/dev/null; then
    args+=(-F n="${1#-}")
    shift 1
  fi
  args+=(-f q="${1?}")
  [ $# -ge 2 ] && args+=(-f after="${2?}")

  hub api -t --cache 3600 graphql "${args[@]}" -f query='
    query($q: String!, $n: Int = 10, $after: String) {
      search(query: $q, type: REPOSITORY, first: $n, after: $after) {
        edges {
          node {
            ...on Repository {
              nameWithOwner
              stargazers {
                totalCount
              }
            }
          }
        }
        pageInfo {
          hasNextPage
          endCursor
        }
      }
    }
  '
}

$ search -3 "rollup plugin"
.data.search.edges.[0].node.nameWithOwner       rollup/rollup-plugin-commonjs
.data.search.edges.[0].node.stargazers.totalCount       360
.data.search.edges.[1].node.nameWithOwner       vuejs/rollup-plugin-vue
.data.search.edges.[1].node.stargazers.totalCount       453
.data.search.edges.[2].node.nameWithOwner       rollup/rollup-plugin-node-resolve
.data.search.edges.[2].node.stargazers.totalCount       319
.data.search.pageInfo.hasNextPage       true
.data.search.pageInfo.endCursor Y3Vyc29yOjM=

$ search -3 "rollup plugin" Y3Vyc29yOjM=
.data.search.edges.[0].node.nameWithOwner       rollup/rollup-plugin-babel
.data.search.edges.[0].node.stargazers.totalCount       527
.data.search.edges.[1].node.nameWithOwner       egoist/rollup-plugin-postcss
.data.search.edges.[1].node.stargazers.totalCount       222
.data.search.edges.[2].node.nameWithOwner       rollup/rollup-plugin-typescript
.data.search.edges.[2].node.stargazers.totalCount       203
.data.search.pageInfo.hasNextPage       true
.data.search.pageInfo.endCursor Y3Vyc29yOjY=

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Jan 27, 2019
pkgsrc changes:
* Add logic to generate man page via pre-install target instead of
  generating it manually

Changes:
### Changes since v2.7
* New `hub api` command for scripting with GitHub API
  <mislav/hub#2016>
* Re-implement CLI flag parsing so that `--message <MSG>` is equivalent to
  `--message=<MSG>` <mislav/hub#2008>
* Re-implement `make man-pages` in Go instead of Ruby
  <mislav/hub#1990>
* `issue create --label` is now `issue create --labels` to align with
  existing documentation
* Output crash debugging information on stderr instead of stdout
* Build improvements:
   - respect environment LDFLAGS
   - strip the build path from resulting executable
   - enable reproducible builds with SOURCE_DATE_EPOCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant