# コメントを扱う

REST API を使用すると、プルリクエスト、Issue、およびコミットにある、コメントにアクセスして管理できます。

pull Request、 GitHub には、 [Pull Request 全体のコメント、Pull Request](https://github.com/octocat/Spoon-Knife/pull/1176#issuecomment-24114792) 内の [特定の行に対するコメント](https://github.com/octocat/Spoon-Knife/pull/1176#discussion_r6252889) 、Pull Request 内の [特定のコミットに関するコメント](https://github.com/octocat/Spoon-Knife/commit/cbc28e7c8caee26febc8c013b0adfb97a4edd96e#commitcomment-4049848) の 3 種類のコメント ビューが用意されています。

これらの種類のコメントはそれぞれ、 GitHub API の異なる部分を経由します。
このガイドでは、それぞれにアクセスして操作する方法を説明します。 すべての例で、"octocat" リポジトリ上で[作成されたこのサンプル Pull Request](https://github.com/octocat/Spoon-Knife/pull/1176) を使用します。 いつもと同様に、サンプルは [platform-samples リポジトリ](https://github.com/github/platform-samples/tree/master/api/ruby/working-with-comments)にあります。

## プルリクエストのコメント

pull request のコメントにアクセスするには、[エンドポイントを使って issue を管理します](/ja/enterprise-server@3.22/rest/issues/comments)。
最初はこれを意外に思うかもしれません。 しかし、pull request がコード付きの issue に過ぎないことを理解すれば、pull request でコメントを作成するためにこれらのエンドポイントを使うことは理にかなっています。

[Octokit.rb](https://github.com/octokit/octokit.rb) を使用して Ruby スクリプトを作成することで、Pull Request コメントをフェッチする方法を示します。 また、 [personal access token](/ja/enterprise-server@3.22/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)を作成することもできます。

Octokit.rb を使って Pull Request からコメントにアクセスを始めるには、以下のコードが役立つでしょう。

```ruby
require 'octokit'

# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
# Instead, set and test environment variables, like below
client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']

client.issue_comments("octocat/Spoon-Knife", 1176).each do |comment|
  username = comment[:user][:login]
  post_date = comment[:created_at]
  content = comment[:body]

  puts "#{username} made a comment on #{post_date}. It says:\n'#{content}'\n"
end
```

ここでは、特に API を呼び出し、リポジトリの名前 (`issue_comments`) と、関心のある pull request ID (`octocat/Spoon-Knife`) の両方を指定して、コメント (`1176`) を取得します。 その後は、コメントを反復処理して、各コメントの情報を取得しているだけです。

## 行につけるプルリクエストのコメント

diff ビュー内では、Pull Request 内の一つの変更について、特定の側面からディスカッションを開始できます。 これらのコメントは、変更されたファイル内の個々の行に対して発生します。 このディスカッションのエンドポイント URL は、[pull request レビューを管理するためのエンドポイント](/ja/enterprise-server@3.22/rest/pulls/comments)から取得されます。

指定されたプルリクエスト番号に基づき、ファイルに対するすべてのプルリクエストコメントを取得する次のコードです。

```ruby
require 'octokit'

# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
# Instead, set and test environment variables, like below
client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']

client.pull_request_comments("octocat/Spoon-Knife", 1176).each do |comment|
  username = comment[:user][:login]
  post_date = comment[:created_at]
  content = comment[:body]
  path = comment[:path]
  position = comment[:position]

  puts "#{username} made a comment on #{post_date} for the file called #{path}, on line #{position}. It says:\n'#{content}'\n"
end
```

上の例と非常に似ていることにお気づきでしょう。 このビューと Pull Request のコメントとの相違点は、会話の焦点にあります。
Pull Request に対するコメントでは、コードの全体的な方向性についてのディスカッションやアイデアを扱うべきです。 Pull Request のレビューの一環として行うコメントは、ファイルで特定の変更が実装された方法について特に扱うべきです。

## コミットコメント

最後のタイプのコメントは、特に個々のコミットで発生します。 このため、[エンドポイントを使ってコミット コメントを管理](/ja/enterprise-server@3.22/rest/commits#get-a-commit-comment)します。

コミットのコメントを取得するには、コミットの SHA1 を使用します。
言い換えれば、プルリクエストに関する識別子は全く使用しません。 次に例を示します。

```ruby
require 'octokit'

# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
# Instead, set and test environment variables, like below
client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']

client.commit_comments("octocat/Spoon-Knife", "cbc28e7c8caee26febc8c013b0adfb97a4edd96e").each do |comment|
  username = comment[:user][:login]
  post_date = comment[:created_at]
  content = comment[:body]

  puts "#{username} made a comment on #{post_date}. It says:\n'#{content}'\n"
end
```

この API 呼び出しは、単一の行コメントと、コミット全体に対するコメントを取得することに注目してください。

コミット コメントの作成は、リポジトリに対して有効または無効にすることができます。 組織の所有者は、組織内のリポジトリの既定の設定を構成できます。 詳細については、「[組織のコミットコメント管理](/ja/enterprise-server@3.22/organizations/managing-organization-settings/managing-commit-comments-for-your-organization)」を参照してください。