LoginSignup
1
3

More than 3 years have passed since last update.

GitHub APIを使ってコミットハッシュからPR名やURLを取得する

Posted at

パブリックリポジトリの場合

curl -H "Accept: application/vnd.github.cloak-preview" https://api.github.com/search/issues?q=is:pr+repo:(オーナー名)/(リポジトリ名)+sha:(コミットハッシュ)

URIは.../search/issuesですが、クエリストリングにis:prとすることで、PRの情報を取得できるところがポイントです。

例えば、

というコミットは、コミットハッシュ6f615b425c2647ed2c19c9a4e8f0513591a743dcですが、

curl -H "Accept: application/vnd.github.cloak-preview" https://api.github.com/search/issues?q=is:pr+repo:laravel/laravel+sha:6f615b425c2647ed2c19c9a4e8f0513591a743dc

とすると、以下の結果が返ってきます。

{
  "total_count": 1,
  "incomplete_results": false,
  "items": [
    {
      "url": "https://api.github.com/repos/laravel/laravel/issues/5173",
      "repository_url": "https://api.github.com/repos/laravel/laravel",
      "labels_url": "https://api.github.com/repos/laravel/laravel/issues/5173/labels{/name}",
      "comments_url": "https://api.github.com/repos/laravel/laravel/issues/5173/comments",
      "events_url": "https://api.github.com/repos/laravel/laravel/issues/5173/events",
      "html_url": "https://github.com/laravel/laravel/pull/5173",
      "id": 535748469,
      "node_id": "MDExOlB1bGxSZXF1ZXN0MzUxNDA1Mjk1",
      "number": 5173,
      "title": "[6.x] DRY up path to /home",
      "user": {
        "login": "taylorotwell",
// 以下略

にあります。

なお、リクエスト時に指定するコミットハッシュはフル桁でなくても構いません。

プライベートリポジトリの場合

-H "Authorization: token xxxx"を追加します。

curl -H "Authorization: token xxxx" -H "Accept: application/vnd.github.cloak-preview" https://api.github.com/search/issues?q=is:pr+repo:(オーナー名)/(リポジトリ名)+sha:(コミットハッシュ)

トークンには、

  • Settings > Developer settings > Personal access tokens

で、Full control of private repositoriesの権限を付けておく必要があります。

スクリーンショット 2019-12-16 18.02.06.png

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