LoginSignup
17

More than 5 years have passed since last update.

GitHubのPR内容をcurlで取得する

Last updated at Posted at 2015-09-26

取得方法

curl -H "Authorization: token [API Token]" https://api.github.com/repos/[owner]/[repo]/pulls/[pr_number]/files
  • API Token:認証のために必要。後述。
  • owner:Owner名かOrganization名
  • repo:リポジトリ名
  • pr_number:プルリクエストの#に続く番号

取得できる情報

参考:https://developer.github.com/v3/pulls/

ファイル名一覧だとこんな感じのものがとれる

API Tokenの取得方法

$ curl -u '[UserName]' -d '{"scopes":["repo"],"note":"Help example"}' https://api.github.com/authorizations

パスワード手入力

すると以下のようなレスポンスを得ることが出来る。tokenに入っているのがAPI Token。

{
  "scopes": [
    "repo"
  ],
  "token": "your_token",
  "app": {
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api",
    "name": "Help example (API)"
  },
  "url": "https://api.github.com/authorizations/123456",
  "note": "Help example",
  "note_url": null,
  "id": 123456,
}

具体的な用途

マージされたPRの差分ファイル名一覧を取得する。

  1. GitHubのWebHookでPRのマージ時にPR情報をJenkinsに送る。
  2. PR情報のjsonからPRのnumberを取得する
  3. PRの差分ファイル一覧を取得する
    • file_list=`curl -H "Authorization: token [API Token]" https://api.github.com/repos/hoge/[repo]/pulls/${number}/files` file_list=`echo $file_list | jq -r '.[].filename'`
echo $file_list

hoge/piyo.jpg
hogehoge/foo.png

あとはチャットワークに通知するなり、ハッシュ化して別リポジトリに取り込むなり自動化しましょう。

所感

PRの差分ファイルをハッシュ化して別リポジトリに取り込む作業が面倒だったのでこういうことしてました。GitHub API使うとだいたいなんでも取れるっぽいのでいろいろできそうです。

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
17