3
2

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 を絞り込みで選択して手元に持ってくる

Posted at

コードレビューが非常に多いので重宝していた。

んだけど、ちょっと前に今まで使っていたどこかで拾ったツールが動かなくなった。
多分 github の api の下位互換がなくなったかなんかだろう。

github api v4 は graph-ql になっていて、触ってみたくなったので自分で作り直すことにした。
(前のツールは僕の経験のない言語で出来ていたので書き足しをする気はなかった。)

準備等

jqpercol(とか)が必要。
あと github で personal access tokens を取っておく。

pull request 一覧を取得

まずこれをどこかに置いておく。

git 管理されているディレクトリでこのスクリプトを実行すると、そのリポジトリの open な pull request が一覧で取得できる。

github_pull_requests.sh
# !/bin/bash

token_path=`dirname "${0}"`/token
token=`cat $token_path`

repo_owner=`git config --get remote.origin.url | tr -s '/' '\n' | tail -r | sed -n 2,2p`
repo_name=` git config --get remote.origin.url | tr -s '/' '\n' | tail -r | sed -n 1,1p | tr -s '.' '\n' | head -n 1`

fetched=`curl -sS -H "Authorization: bearer $token" -d @- https://api.github.com/graphql <<EOF
{
    "query": "query {
      repository(owner:\"$repo_owner\", name:\"$repo_name\") {
        pullRequests(last:20, states:OPEN) {
          edges {
            node {
              title
              number
              headRefName
              author {
                login
              }
            }
          }
        }
      }
    }"
}
EOF`

echo $fetched | jq --raw-output '.data.repository.pullRequests.edges[].node | [.number, .title, .author.login, .headRefName] | @tsv' | tr '\t' '®' | column -t -s '®' | sort -r

",が pull request の title に含まれていてもちゃんと縦揃えするってのが大変だった...)

次に同じディレクトリにtokenというファイルで personal access tokens を書いて置いておく。
こんな感じで書けば良い。

echo '665axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa0' > token

きっとこんな感じに取れるはず。
スクリーンショット 2018-03-04 13.55.52.png

選択とチェックアウト

reviewingというブランチ名でチェックアウトをする。
僕はgprpというコマンドとしてzshから呼び出して使っている。

gprp
# !/bin/zsh

git fetch --prune > /dev/null 2>&1
git branch -D reviewing > /dev/null 2>&1

script_path=`dirname "${0}"`/github-pull-requests/github_pull_requests.sh

number=`sh $script_path | percol --match-method regex | tr -s ' ' '\n' | head -n 1`

git fetch origin pull/$number/head:reviewing
git checkout reviewing

github-pull-requests.gif

融通

という名の手抜き。

  • branchauthorは絞り込み検索のためのみに取ってきている
    • 実際にはnumberがあれば実現できる
    • 好きな箇所をqueryで指定すれば良い
  • gprpにパスを通せば使えるだろう
  • 名前も好きにすれば良い
  • チェックアウト先のブランチ名とかも好みで
  • pecofzfにもすぐ変えられるだろう
  • 特にエラー制御はなし

以上。
先週これが出来なくなっていて不自由していたので、これで安心。
自分で書いたことで縦揃えを組み込むことが出来たので、満足。

3
2
1

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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?