[GitHub] Pull Request でのファイルへのコメント機能が一般提供となりました

2023.04.12

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

このたびの GitHub のアップデートにより、Pull Request でのファイルへのコメント機能が一般提供(Generally Available)となりました。

以前までは公開ベータ版の提供でしたが、一般提供となったことにより、ファイルへのコメント機能が安定して利用できるようになりました。

このアップデートにより、従来の行ベースのコメントに加えて、ファイル自体にコメントができるようになったため、特に次のようなファイル差分に対するコメント追加が便利になります。

  • 名前が変更されたファイル
  • バイナリファイル(画像など)
  • 削除されたファイル

試してみた

Web または API からファイルへのコメントが可能となっています。それぞれ試してみます。

Web

プルリクエストの[Files changed]タブで、ファイルごとに[Comment on this file]ボタンが表示されています。

コメントをしたいファイルのボタンをクリックします。(画像はクラスメソッドの二次元社員「めそ子」)

するとコメント欄記入が表示されるので、追加します。

ファイルにコメントを追加できました。

[Conversation]タブではこのような見え方になります。

API

API によるファイルへのコメントの追加は、 REST API とGraph API のいずれからも可能です。今回はこちらの REST API を試してみます。

API を使う場合は通常のコメントとは異なり、subject_type='file'を指定する必要があります。

gh

gh api \
  --method POST \
  -H "Accept: application/vnd.github+json" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  /repos/OWNER/REPO/pulls/PULL_NUMBER/comments \
  -f body='Great stuff!' \
 -f commit_id='6dcb09b5b57875f334f61aebed695e2e4193db5e' \
 -f path='file1.txt' \
 -f subject_type='file'

実際に実行してみます。

$ gh api \
  --method POST \
  -H "Accept: application/vnd.github+json" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  /repos/cm-rwakatsuki/devio/pulls/2150/comments \
  -f body='Great stuff!' \
 -f commit_id='8b6ea4c74b32f44e79feb42213cd6ceecf164f67' \
 -f path='posts/2149/DuQaPxcVYAANQxr.jpeg' \
 -f subject_type='file'
{
  "url": "https://api.github.com/repos/cm-rwakatsuki/devio/pulls/comments/1164279785",
  "pull_request_review_id": 1381544324,
  "id": 1164279785,
  "node_id": "PRRC_kwDOEbgpus5FZX_p",
  "diff_hunk": "",
  "path": "posts/2149/DuQaPxcVYAANQxr.jpeg",
  "commit_id": "8b6ea4c74b32f44e79feb42213cd6ceecf164f67",
  "original_commit_id": "8b6ea4c74b32f44e79feb42213cd6ceecf164f67",
  "user": {
    "login": "cm-rwakatsuki",
    "id": 57384023,
    "node_id": "MDQ6VXNlcjU3Mzg0MDIz",
    "avatar_url": "https://avatars.githubusercontent.com/u/57384023?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/cm-rwakatsuki",
    "html_url": "https://github.com/cm-rwakatsuki",
    "followers_url": "https://api.github.com/users/cm-rwakatsuki/followers",
    "following_url": "https://api.github.com/users/cm-rwakatsuki/following{/other_user}",
    "gists_url": "https://api.github.com/users/cm-rwakatsuki/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/cm-rwakatsuki/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/cm-rwakatsuki/subscriptions",
    "organizations_url": "https://api.github.com/users/cm-rwakatsuki/orgs",
    "repos_url": "https://api.github.com/users/cm-rwakatsuki/repos",
    "events_url": "https://api.github.com/users/cm-rwakatsuki/events{/privacy}",
    "received_events_url": "https://api.github.com/users/cm-rwakatsuki/received_events",
    "type": "User",
    "site_admin": false
  },
  "body": "Great stuff!",
  "created_at": "2023-04-12T15:11:02Z",
  "updated_at": "2023-04-12T15:11:02Z",
  "html_url": "https://github.com/cm-rwakatsuki/devio/pull/2150#discussion_r1164279785",
  "pull_request_url": "https://api.github.com/repos/cm-rwakatsuki/devio/pulls/2150",
  "author_association": "OWNER",
  "_links": {
    "self": {
      "href": "https://api.github.com/repos/cm-rwakatsuki/devio/pulls/comments/1164279785"
    },
    "html": {
      "href": "https://github.com/cm-rwakatsuki/devio/pull/2150#discussion_r1164279785"
    },
    "pull_request": {
      "href": "https://api.github.com/repos/cm-rwakatsuki/devio/pulls/2150"
    }
  },
  "reactions": {
    "url": "https://api.github.com/repos/cm-rwakatsuki/devio/pulls/comments/1164279785/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
  },
  "start_line": null,
  "original_start_line": null,
  "start_side": null,
  "line": 1,
  "original_line": 1,
  "side": "RIGHT",
  "original_position": 1,
  "position": 1,
  "subject_type": "file"
}

するとファイルにコメントを追加できました。

参考

以上