{"meta":{"title":"コメントを扱う","intro":"REST API を使用すると、プルリクエスト、Issue、およびコミットにある、コメントにアクセスして管理できます。","product":"REST API","breadcrumbs":[{"href":"/ja/rest","title":"REST API"},{"href":"/ja/rest/guides","title":"ガイド"},{"href":"/ja/rest/guides/working-with-comments","title":"コメントを扱う"}],"documentType":"article"},"body":"# コメントを扱う\n\nREST API を使用すると、プルリクエスト、Issue、およびコミットにある、コメントにアクセスして管理できます。\n\npull 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 種類のコメント ビューが用意されています。\n\nこれらの種類のコメントはそれぞれ、 GitHub API の異なる部分を経由します。\nこのガイドでは、それぞれにアクセスして操作する方法を説明します。 すべての例で、\"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)にあります。\n\n## プルリクエストのコメント\n\npull request のコメントにアクセスするには、[エンドポイントを使って issue を管理します](/ja/rest/issues/comments)。\n最初はこれを意外に思うかもしれません。 しかし、pull request がコード付きの issue に過ぎないことを理解すれば、pull request でコメントを作成するためにこれらのエンドポイントを使うことは理にかなっています。\n\n[Octokit.rb](https://github.com/octokit/octokit.rb) を使用して Ruby スクリプトを作成することで、Pull Request コメントをフェッチする方法を示します。 また、 [personal access token](/ja/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)を作成することもできます。\n\nOctokit.rb を使って Pull Request からコメントにアクセスを始めるには、以下のコードが役立つでしょう。\n\n```ruby\nrequire 'octokit'\n\n# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!\n# Instead, set and test environment variables, like below\nclient = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']\n\nclient.issue_comments(\"octocat/Spoon-Knife\", 1176).each do |comment|\n  username = comment[:user][:login]\n  post_date = comment[:created_at]\n  content = comment[:body]\n\n  puts \"#{username} made a comment on #{post_date}. It says:\\n'#{content}'\\n\"\nend\n```\n\nここでは、特に API を呼び出し、リポジトリの名前 (`issue_comments`) と、関心のある pull request ID (`octocat/Spoon-Knife`) の両方を指定して、コメント (`1176`) を取得します。 その後は、コメントを反復処理して、各コメントの情報を取得しているだけです。\n\n## 行につけるプルリクエストのコメント\n\ndiff ビュー内では、Pull Request 内の一つの変更について、特定の側面からディスカッションを開始できます。 これらのコメントは、変更されたファイル内の個々の行に対して発生します。 このディスカッションのエンドポイント URL は、[pull request レビューを管理するためのエンドポイント](/ja/rest/pulls/comments)から取得されます。\n\n指定されたプルリクエスト番号に基づき、ファイルに対するすべてのプルリクエストコメントを取得する次のコードです。\n\n```ruby\nrequire 'octokit'\n\n# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!\n# Instead, set and test environment variables, like below\nclient = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']\n\nclient.pull_request_comments(\"octocat/Spoon-Knife\", 1176).each do |comment|\n  username = comment[:user][:login]\n  post_date = comment[:created_at]\n  content = comment[:body]\n  path = comment[:path]\n  position = comment[:position]\n\n  puts \"#{username} made a comment on #{post_date} for the file called #{path}, on line #{position}. It says:\\n'#{content}'\\n\"\nend\n```\n\n上の例と非常に似ていることにお気づきでしょう。 このビューと Pull Request のコメントとの相違点は、会話の焦点にあります。\nPull Request に対するコメントでは、コードの全体的な方向性についてのディスカッションやアイデアを扱うべきです。 Pull Request のレビューの一環として行うコメントは、ファイルで特定の変更が実装された方法について特に扱うべきです。\n\n## コミットコメント\n\n最後のタイプのコメントは、特に個々のコミットで発生します。 このため、[エンドポイントを使ってコミット コメントを管理](/ja/rest/commits#get-a-commit-comment)します。\n\nコミットのコメントを取得するには、コミットの SHA1 を使用します。\n言い換えれば、プルリクエストに関する識別子は全く使用しません。 次に例を示します。\n\n```ruby\nrequire 'octokit'\n\n# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!\n# Instead, set and test environment variables, like below\nclient = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']\n\nclient.commit_comments(\"octocat/Spoon-Knife\", \"cbc28e7c8caee26febc8c013b0adfb97a4edd96e\").each do |comment|\n  username = comment[:user][:login]\n  post_date = comment[:created_at]\n  content = comment[:body]\n\n  puts \"#{username} made a comment on #{post_date}. It says:\\n'#{content}'\\n\"\nend\n```\n\nこの API 呼び出しは、単一の行コメントと、コミット全体に対するコメントを取得することに注目してください。"}