notebook

都内でWEB系エンジニアやってます。

GitHub CLIで自身のリポジトリのPullRequest一覧を取得する

プライベートなリポジトリも含め自身が管理しているリポジトリにおいて、RenovateやDependabotなどを用いて自動的にPullRequestを出してもらうリポジトリが増えてきた

今までだと定期的にリポジトリのページに行って確認してたりしていたが、リポジトリの量が増えてくるとさすがに面倒になってきた

そこで、簡単にPullRequest一覧をGitHub CLIとjqで出して状況を確認しやすいようにしたので記事に残しておく

自身のリポジトリでOPENされているPullRequest一覧

GraphQLでクエリを書く

  • pull_requests.graphql
query($endCursor: String) {
  viewer {
    repositories(first: 100, isFork: false, ownerAffiliations: [OWNER], after: $endCursor) {
      pageInfo {
        hasNextPage
        endCursor
      }
      nodes {
        name
        url
        pullRequests(first: 100, states: OPEN) {
          totalCount
          nodes {
            title
            url
            author {
              login
            }
          }
        }
      }
    }
  }
}

単一リポジトリでPullRequestが100件以上あった場合、すべてのPullRequestを取得することはできないが…現状そういう状態になっていないのでこのクエリでも問題ない

また、ownerAffiliations: [OWNER]の箇所で自信が管理しているリポジトリに絞っている

仕事などで別のOrganizationのリポジトリやPRも必要であれば条件を外せばよい

  • 整形
gh api graphql --paginate -f query="$(cat graphql/all_pull_requests.graphql)" | jq '.data.viewer.repositories.nodes|.[]|select(.pullRequests.totalCount > 0)' | jq -s 'sort_by(.pullRequests.totalCount)|reverse|map({name: .name, url: .url, pullRequests: (.pullRequests.nodes|map({title: .title, url: .url, author: .author.login}))})'

--paginate$endCursorで100件以上のリポジトリでもページネーションして取得してくれる(GitHub CLIが)

  • 結果(一部抜粋)
[
  {
    "name": "ngx-sample",
    "url": "https://github.com/swfz/ngx-sample",
    "pullRequests": [
      {
        "title": "Update dependency prettier to v2",
        "url": "https://github.com/swfz/ngx-sample/pull/448",
        "author": "renovate"
      },
      {
        "title": "CI実行環境の更新",
        "url": "https://github.com/swfz/ngx-sample/pull/457",
        "author": "swfz"
      }
    ]
  },
  {
    "name": "tools",
    "url": "https://github.com/swfz/tools",
    "pullRequests": [
      {
        "title": "chore(deps): update dependency @types/node to v16.11.66",
        "url": "https://github.com/swfz/tools/pull/296",
        "author": "renovate"
      }
    ]
  },
.....
.....
]

これで自身が管理しているのリポジトリ一覧とPullRequestの一覧を取得できた

GitHubの検索画面でも確認できた

そもそもこういうことをやろうとした時点でまずはGitHubの検索できないか確認した

Pull Requests

https://github.com/pullsgithub.com

Created,Assigned... の並びを見てできなそうと判断してCLIを使う方法でやってみたのだが、本当にできないのか?と触ってみてたらできてしまった…

ということでこの画面を使うほうがchecksの結果ものっているので楽

今回はそれを知れたということで良しってことにしようと思います