{"meta":{"title":"Working with comments","intro":"Using the REST API, you can access and manage comments in your pull requests, issues, or commits.","product":"REST API","breadcrumbs":[{"href":"/en/enterprise-cloud@latest/rest","title":"REST API"},{"href":"/en/enterprise-cloud@latest/rest/guides","title":"Guides"},{"href":"/en/enterprise-cloud@latest/rest/guides/working-with-comments","title":"Working with comments"}],"documentType":"article"},"body":"# Working with comments\n\nUsing the REST API, you can access and manage comments in your pull requests, issues, or commits.\n\nFor any Pull Request, GitHub provides three kinds of comment views:\n[comments on the Pull Request](https://github.com/octocat/Spoon-Knife/pull/1176#issuecomment-24114792) as a whole, [comments on a specific line](https://github.com/octocat/Spoon-Knife/pull/1176#discussion_r6252889) within the Pull Request,\nand [comments on a specific commit](https://github.com/octocat/Spoon-Knife/commit/cbc28e7c8caee26febc8c013b0adfb97a4edd96e#commitcomment-4049848) within the Pull Request.\n\nEach of these types of comments goes through a different portion of the GitHub API.\nIn this guide, we'll explore how you can access and manipulate each one. For every\nexample, we'll be using [this sample Pull Request made](https://github.com/octocat/Spoon-Knife/pull/1176) on the \"octocat\"\nrepository. As always, samples can be found in [our platform-samples repository](https://github.com/github/platform-samples/tree/master/api/ruby/working-with-comments).\n\n## Pull Request Comments\n\nTo access comments on a Pull Request, you'll use [the endpoints to manage issues](/en/enterprise-cloud@latest/rest/issues/comments).\nThis may seem counterintuitive at first. But once you understand that a Pull\nRequest is just an Issue with code, it makes sense to use these endpoints to\ncreate comments on a Pull Request.\n\nWe'll demonstrate fetching Pull Request comments by creating a Ruby script using\n[Octokit.rb](https://github.com/octokit/octokit.rb). You'll also want to create a [personal access token](/en/enterprise-cloud@latest/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token).\n\nThe following code should help you get started accessing comments from a Pull Request\nusing Octokit.rb:\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\nHere, we're specifically calling out to the API to get the comments (`issue_comments`),\nproviding both the repository's name (`octocat/Spoon-Knife`), and the Pull Request ID\nwe're interested in (`1176`). After that, it's simply a matter of iterating through\nthe comments to fetch information about each one.\n\n## Pull Request Comments on a Line\n\nWithin the diff view, you can start a discussion on a particular aspect of a singular\nchange made within the Pull Request. These comments occur on the individual lines\nwithin a changed file. The endpoint URL for this discussion comes from [the endpoint to manage pull request reviews](/en/enterprise-cloud@latest/rest/pulls/comments).\n\nThe following code fetches all the Pull Request comments made on files, given a single Pull Request number:\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\nYou'll notice that it's incredibly similar to the example above. The difference\nbetween this view and the Pull Request comment is the focus of the conversation.\nA comment made on a Pull Request should be reserved for discussion or ideas on\nthe overall direction of the code. A comment made as part of a Pull Request review should\ndeal specifically with the way a particular change was implemented within a file.\n\n## Commit Comments\n\nThe last type of comments occur specifically on individual commits. For this reason,\nthey make use of [the endpoint to manage commit comments](/en/enterprise-cloud@latest/rest/commits#get-a-commit-comment).\n\nTo retrieve the comments on a commit, you'll want to use the SHA1 of the commit.\nIn other words, you won't use any identifier related to the Pull Request. Here's an example:\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\nNote that this API call will retrieve single line comments, as well as comments made\non the entire commit."}