GitHub CLI使ってみた

GitHubからCLI上でPRやIssueの操作をすることができるGitHub CLIがリリースされたので早速触ってみました。 PRの操作とIssueの操作はだいたい似ているのでPRの方だけレポートしたいと思いますー。

cli.github.com

環境は、OSが macOS Mojave 10.14.6GitHub CLIはhomebrewで入れました。

公式のドキュメントはこちら

gh pr create

現在使っているブランチを元にCLI上でPRが作れるコマンド。 タイトルやディスクリプションもCLI上で書くことが可能です。

gh pr create を実行するとPRのタイトルを求められます。少し時間がかかるんですが、これは裏でremoteにブランチをpushしているからでした。

  $ gh pr create

Creating pull request for test into master in kamillle/tutorial_app

? Title gh pr createで作ってみた

ディスクリプション部分を入力することができます。 e を入力するとnvimが開いてmarkdownを編集するモードになり、enterを入力するとディスクリプションの入力をスキップできます。 e で開けるエディタは $EDITOR の値を見ていました。

  $ gh pr create

Creating pull request for test into master in kamillle/tutorial_app

? Title gh pr create で作ってみた
? Body [(e) to launch nvim, enter to skip]

Bodyを入力 or スキップすると、Preview in browser, Submit, Cancelの3択が選べます。矢印キーで移動可能、文字入力による絞り込みもできました。

  $ gh pr create
gh pr create で
Creating pull request for test into master in kamillle/tutorial_app

? Title gh pr create で作ってみた
? Body <Received>
? What's next?  [Use arrows to move, type to filter]
> Preview in browser
  Submit
  Cancel

Preview in browser

Preview in browserをするとブラウザでいつものこの画面が開きます。

f:id:kamillle:20200213153302p:plain
gh pr statsuでPreview in browserを選んだ図

CLIで入力した項目がクエリパラメータで載っかってました。 f:id:kamillle:20200213153509p:plain

Submit

Submitを選択するとPRが作成され、そのURLが返ってきます。

  $ gh pr create

Creating pull request for test into master in kamillle/tutorial_app

? Title gh pr create で作ってみた
? Body <Received>
? What's next? Submit
https://github.com/kamillle/tutorial_app/pull/62

Cancel

Discarding. が返ってきて、処理が中断されます。入力していたタイトルとかはリストアできないので注意。

gh pr list

PRの番号、タイトル、ブランチ名が返ってきます。 一度に表示されるのは最新の30件まででした。

  $ gh pr list

Pull requests for kamillle/tutorial_app

#62  gh pr create で作ってみた                             test
#61  [Security] Bump rack from 2.0.7 to 2.2.1        dependabot/bundler/rack-2.2.1
#60  [Security] Bump nokogiri from 1.10.4 to 1.10.8  dependabot/bundler/nokogiri-1.10.8
#58  [Security] Bump excon from 0.65.0 to 0.72.0     dependabot/bundler/excon-0.72.0
#57  Bump faker from 2.2.1 to 2.10.1                 dependabot/bundler/faker-2.10.1
#56  Bump pg from 0.18.4 to 1.2.2                    dependabot/bundler/pg-1.2.2
#46  Bump listen from 3.1.5 to 3.2.1                 dependabot/bundler/listen-3.2.1
#44  [Security] Bump puma from 4.1.0 to 4.3.1        dependabot/bundler/puma-4.3.1
#42  [Security] Bump loofah from 2.2.3 to 2.4.0      dependabot/bundler/loofah-2.4.0
#39  Bump guard from 2.15.0 to 2.16.1                dependabot/bundler/guard-2.16.1
#37  Bump minitest-reporters from 1.3.6 to 1.4.2     dependabot/bundler/minitest-reporters-1.4.2
#33  Bump will_paginate from 3.1.7 to 3.2.1          dependabot/bundler/will_paginate-3.2.1
#28  Bump carrierwave from 2.0.1 to 2.0.2            dependabot/bundler/carrierwave-2.0.2
#27  Bump uglifier from 4.1.20 to 4.2.0              dependabot/bundler/uglifier-4.2.0
#25  Bump turbolinks from 5.2.0 to 5.2.1             dependabot/bundler/turbolinks-5.2.1

gh pr status

Current branch に現在のブランチ Created by you に自分が作成したPR Requesting a code review from you に自分以外の人が作成したPRで、自分がReviewerに追加されているPR が並びます。

  $ gh pr status

Relevant pull requests in kamillle/tutorial_app

Current branch
  There is no pull request associated with [master]

Created by you
  #62  gh pr create で作ってみた [test]
   - Checks passing

Requesting a code review from you
  #60  [Security] Bump nokogiri from 1.10.4 to 1.10.8 [dependabot/bundler/nokogiri-1.10.8]
   - Checks passing
  #58  [Security] Bump excon from 0.65.0 to 0.72.0 [dependabot/bundler/excon-0.72.0]
   - Checks passing
  #57  Bump faker from 2.2.1 to 2.10.1 [dependabot/bundler/faker-2.10.1]
   - Checks passing
  #56  Bump pg from 0.18.4 to 1.2.2 [dependabot/bundler/pg-1.2.2]
   - Checks passing
  #46  Bump listen from 3.1.5 to 3.2.1 [dependabot/bundler/listen-3.2.1]
   - Checks passing
  #44  [Security] Bump puma from 4.1.0 to 4.3.1 [dependabot/bundler/puma-4.3.1]
   - Checks passing
  #42  [Security] Bump loofah from 2.2.3 to 2.4.0 [dependabot/bundler/loofah-2.4.0]
   - Checks passing
  #39  Bump guard from 2.15.0 to 2.16.1 [dependabot/bundler/guard-2.16.1]
   - Checks passing
  #37  Bump minitest-reporters from 1.3.6 to 1.4.2 [dependabot/bundler/minitest-reporters-1.4.2]
   - Checks passing
  #33  Bump will_paginate from 3.1.7 to 3.2.1 [dependabot/bundler/will_paginate-3.2.1]
   - Checks passing

Created by you, Requesting a code review from youに表示されるのは最新の10件までです。 Created by youではChecksがグリーンになっているかとapproveがついていれば approved と表示されます。

Created by you
  #1  Test Pr... [test-pr]
   - Checks passing - approved

レビュー済みのものは Requesting a code review from you には表示されなくなります。

gh pr view

gh pr view 1 とすると1番が割り当てられているPRがブラウザで開かれます。番号でなくてもURLやブランチ名でも開けるみたいです。 番号を省略した時にcurrent branchのPRを開いてくれるので便利だなと思いました。(PRが存在しない場合はエラー)

-p or --preview をつけるとCLI上でPRの情報を閲覧できます。

gh pr checkout

gh pr checkout 1 とすると1番が割り当てられてるブランチにcheckoutできます。番号でなくてもURLやブランチ名でも開けるみたいです。 他の人がforkして作ったブランチにもcheckoutできるので便利です。( user_name:branch_name で開けます) 同じことをhubコマンドでできたはずですが、hubコマンドはVCSの表示が遅くなるのが理由で使用を避けていたのでこれは嬉しい!!