GitHub API v3を使ってPull Requestのコメントの取得・投稿をする

2020.10.09

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部の若槻です。

GitHubでは現在REST APIとしてGitHub API v3を使用することが可能です。

今回は、GitHub API v3を使ってPull Requestのコメントの取得・投稿をShell(curl)で行ってみました。

やってみた

アクセストークンの発行

今回GitHub APIへのアクセス時の認証に使用するアクセストークンは前回記事の以下箇所で作成したもの(スコープrepo)と同じトークンを使用します。未作成の場合は作成してください。

GitHub APIアクセス時のお決まり

各種APIアクセス時にアカウント固有で共通で必要となる情報として、次の値を予め変数で指定しておきます。

% TOKEN=<アクセストークン値>
% REPOSITORY=<取得するPull RequestがあるRepository名>
% OWNER=<RepositoryのOwnerアカウント名>

上記変数やその他のお決まりについての詳細についても前回記事の以下箇所で説明をしているので本記事では省略します。

Pull Requestの取得

今回は直近で更新されたopen状態のPull Requestを取得し、そのPull Requestに対してコメントの取得・投稿を行ってみます。前回記事の以下箇所でも利用したAPI「List pull requests」を今回も使用します。

curlでAPIにアクセスしてPull Request一覧を取得してみます。取得結果は変数pullsに格納します。

% pulls=$(curl \
  -H "Accept: application/vnd.github.v3+json" \
  -H "Authorization: token ${TOKEN}" \
  "https://api.github.com/repos/${OWNER}/${REPOSITORY}/pulls?state=open&sort=updated&direction=desc")

ここで取得したい情報は直近に更新されたPull Requestの番号です。変数pullsからjqで取得して変数pull_numberに格納します。今回は16となります。

% pull_number=$(echo $pulls | jq '.[0].number')
% echo $pull_number
16

コメントの取得

次に先ほど取得したPull Requestの番号を元に、Pull Requestに投稿されたコメント一覧を取得します。その場合は「List issue comments」を使用します。

なぜ操作対象はPull RequestなのにissueのAPIを使うのかというと、GitHubにおいてPull Requestはissueの一種なのでAPIは同じとなるからです。(なので同じRepository内でのPull Requestとissueの採番は共通の通し番号が使われます)

「List issue comments」のメソッドおよびエンドポイントは以下となります。

GET /repos/{owner}/{repo}/issues/{issue_number}/comments

curlでAPIにアクセスしてPull Requestからコメント一覧を取得してみます。

% commens=$(curl \
  -H "Accept: application/vnd.github.v3+json" \
  -H "Authorization: token ${TOKEN}" \
  "https://api.github.com/repos/${OWNER}/${REPOSITORY}/issues/${pull_number}/comments")

取得できました。

% echo $comments
[
  {
    "url": "https://api.github.com/repos/<アカウント名>/<Repository名>/issues/comments/703435519",
    "html_url": "https://github.com/<アカウント名>/<Repository名>/pull/16#issuecomment-703435519",
    "issue_url": "https://api.github.com/repos/<アカウント名>/<Repository名>/issues/16",
    "id": 703435519,
    "node_id": "MDEyOklzc3VlQ29...",
    "user": {
      //user attributes
    },
    "created_at": "2020-10-05T06:49:58Z",
    "updated_at": "2020-10-06T10:44:20Z",
    "author_association": "OWNER",
    "body": "aaaaa",
    "performed_via_github_app": null
  },
  {
    "url": "https://api.github.com/repos/<アカウント名>/<Repository名>/issues/comments/703514989",
    "html_url": "https://github.com/<アカウント名>/<Repository名>/pull/16#issuecomment-703514989",
    "issue_url": "https://api.github.com/repos/<アカウント名>/<Repository名>/issues/16",
    "id": 703514989,
    "node_id": "MDEyOklzc3VlQ29...",
    "user": {
      //user attributes},
    "created_at": "2020-10-05T09:27:30Z",
    "updated_at": "2020-10-06T10:44:08Z",
    "author_association": "OWNER",
    "body": "コメントてすと",
    "performed_via_github_app": null
  },
  {
    "url": "https://api.github.com/repos/<アカウント名>/<Repository名>/issues/comments/703527858",
    "html_url": "https://github.com/<アカウント名>/<Repository名>/pull/16#issuecomment-703527858",
    "issue_url": "https://api.github.com/repos/<アカウント名>/<Repository名>/issues/16",
    "id": 703527858,
    "node_id": "MDEyOklzc3VlQ...",
    "user": {
      //user attributes},
    "created_at": "2020-10-05T09:54:03Z",
    "updated_at": "2020-10-05T09:54:03Z",
    "author_association": "OWNER",
    "body": "published",
    "performed_via_github_app": null
  },
  //...
]

コメントの投稿

次に同じPull Requestに対してコメントを投稿してみます。その場合は「Create an issue comment」を使用します。

「Create an issue comment」のメソッドおよびエンドポイントは以下となります。

POST /repos/{owner}/{repo}/issues/{issue_number}/comments

curlでAPIにアクセスしてPull RequestにコメントDevIOコメント投稿てすとを投稿してみます。

% curl -X POST \
  -H "Accept: application/vnd.github.v3+json" \
  -H "Authorization: token ${TOKEN}" \
  "https://api.github.com/repos/${OWNER}/${REPOSITORY}/issues/${pull_number}/comments" \
  -d '{"body":"DevIOコメント投稿てすと"}'

コメントを投稿できました。 image

おわりに

GitHub API v3を使ってPull Requestのコメントの取得・投稿をShell(curl)でしてみました。

Pull Requestのコメントの操作にはissueのAPIを使う必要があるというのを最初は知らず、ドキュメントをかなり探し回りましたが見つかって良かったです。

参考

以上