Jenkins Github pull request builder plugin 使う時にハマった事

いちいちコードレビューする前にビルドしてテスト実行するのがいい加減面倒になってきたので入れようとしてみました。以下のサイトを参考にさせてもらって設定したら、自前の環境だとサクッとキマったのに、どうも会社のアカウントだとビルドは機嫌よくいくのだけれど、終わった後のステータス更新が上手くいかない。。。

最初は private repository だからかなーとか、organization の関係なのかなーとか思ってたのですが、Jenkins のログ見てると、どうも権限なくて失敗してるようでした。

Commit Status API

Github 上のステータス更新には Commit Status API が使われています。

Commit Status API

で、この API を使うためには以下の権限が必要だと書かれています。

Repo Statuses API - GitHub Developer

Note that the repo:status OAuth scope grants targeted access to Statuses without also granting access to repo code, while the repo scope grants permission to code as well as statuses.

plugin が作成する key

Github pull request builder plugin は Jenkins の管理画面から、それ用のアクセスキーを発行してくれる機能があります。そのアクセスキーの内容を見てみます。

OAuth - GitHub Developer

curl -u user:pass https://api.github.com/authorizations -X GET

  {
    "id": xxxx,
    "url": "https://api.github.com/authorizations/xxxx",
    "app": {
      "name": "Jenkins Git Hub Pull Request Builder (API)",
      "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api",
      "client_id": "xxxx"
    },
    "token": "xxxx",
    "note": "Jenkins Git Hub Pull Request Builder",
    "note_url": null,
    "created_at": "2013-07-18T08:22:32Z",
    "updated_at": "2013-07-18T08:22:32Z",
    "scopes": [
      "repo:status",
      "repo"
    ]
  },

うーーーん、ちゃんと repo:statusreposcopes に入っています。

plugin の issue を見てみる

同じように悩んでいる人はいないかと、plugin の issue ページをあさって見ました。

organizations don't work - janinko/ghprb - GitHub

すると

@hradtke i've replicated this locally. The POST to create a status (http://developer.github.com/v3/repos/statuses/) doesn't work. I've contacted github for some support, to find out what I'm doing wrong.

Update from Github. The team that the bot is in must have pull and push status, as someone said above. the OAuth scope 'repo:status' is not sufficient.

を結論とする一連の書き込みが。。なるほど、確かに bot ユーザとして作ったアカウントは対象のリポジトリに対して pull の権限しか与えていませんでした。

で、確かに上記コメントでもリンク張られてますが、Statuses の説明ページには

Users with push access can create commit statuses for a given ref:

とハッキリと書いてある。。節穴過ぎる。

結果

push & pull の権限に変えたら、あっさりとステータスの変更もしてくれるようになりました。ちゃんとドキュメントは読みましょうという事でした、はい。

でも Github pull request builder はホント便利ですね!