2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Github の Pull Request に紐づく base / head branch を curl と jq で取得する

Last updated at Posted at 2018-10-06

自分が開発中の Pull Request が複数あるリポジトリで、該当するブランチどれだっけなーと確認するとき、毎回 Github の Pull Request 一覧を GUI から確認していました。

最近それだと中々面倒だなと思い、CLI から確認できるように、簡単なワンライナーを作ってみたのでそのメモです。

前提環境

tl;dr

下記のようなコマンドを作りました。

curl -sH "Authorization: token ${GITHUB_API_TOKEN}" "https://api.github.com/repos/:owner/:repo/pulls" \
| jq '.[] | { number: .number, title: .title, base: .base.ref, head: .head.ref }'
  • GITHUB_API_TOKEN は Github の Personal API Token、:owner/:repo はリポジトリオーナーとリポジトリ名で読み替えてください。
  • GITHUB_API_TOKEN は予め取得しておき、環境変数などに保持しておいて使う想定です。

実行結果サンプル

最近学習中の styled-components で試します。

curl -sH "Authorization: token ${GITHUB_API_TOKEN}" "https://api.github.com/repos/styled-components/styled-components/pulls" \
| jq '.[] | { number: .number, title: .title, base: .base.ref, head: .head.ref }'

上記コマンドを実行すると、結果はこんな感じになります。Pull Request ごとに base branch と head branch が簡易的に確認できるようになっています。

{
  "number": 2043,
  "title": "Create directory for core package WIP",
  "base": "develop",
  "head": "add-styled-components-core"
}
{
  "number": 2032,
  "title": "Add a babel-macro",
  "base": "develop",
  "head": "add-macro"
}
...

上記コマンドについて補足

curl と Github API について

curl から実行している Github APIは、GET /repos/:owner/:repo/pulls になります。こちらで対象リポジトリの Pull Request 情報の一覧を取得してきています。
Pull Requests | GitHub Developer Guide

curl の-H オプションで Personal Access Token を指定しています。プライベートリポジトリについて確認する場合は必要になります。もしパブリックリポジトリであれば設定不要です。

また curl の -s オプションでダウンロード状況など、curl の不要な情報は非表示にしています。

リクエストパラメータに何も指定しないと、Pull Request の stateopen のものが返却されます。

表示したい Pull Reuest の stateclosedall にしたい場合は、リクエストパラメータをリクエスト URL の末尾にリクエストパラメータを追加します。
例えば以下のような形です。

"https://api.github.com/repos/styled-components/styled-components/pulls?state=closed"

参考:https://developer.github.com/v3/pulls/#list-pull-requests

jq について

curl で取得した Github API の Response JSON をパースするために、今回は jq を使いました。

最初の .[] で配列内のオブジェクトを取得し、次の { number: .number, title: .title, base: .base.ref, head: .head.ref } で、必要となるオブジェクトのキーを抽出しつつ、新しくオブジェクトを作成して表示しています。

参考: https://stedolan.github.io/jq/manual/#Basicfilters

終わりに

一旦 Pull Request ごとに対応する base/head ブランチがわかるようになりました。まだ荒いですが、こちらをもとにより便利に使えるよう考えてみようと思います。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?