{"meta":{"title":"处理注释","intro":"使用 REST API，您可以访问和管理拉取请求、议题或提交中的注释。","product":"REST API","breadcrumbs":[{"href":"/zh/rest","title":"REST API"},{"href":"/zh/rest/guides","title":"指南"},{"href":"/zh/rest/guides/working-with-comments","title":"处理注释"}],"documentType":"article"},"body":"# 处理注释\n\n使用 REST API，您可以访问和管理拉取请求、议题或提交中的注释。\n\n对于任何拉取请求， GitHub 提供三种类型的注释视图：整个 [拉取请求](https://github.com/octocat/Spoon-Knife/pull/1176#issuecomment-24114792) 上的注释、拉取请求内 [特定行上的注释](https://github.com/octocat/Spoon-Knife/pull/1176#discussion_r6252889) 以及请求请求中 [特定提交的注释](https://github.com/octocat/Spoon-Knife/commit/cbc28e7c8caee26febc8c013b0adfb97a4edd96e#commitcomment-4049848) 。\n\n每种类型的注释都会通过 API 的不同GitHub部分。\n在本指南中，我们将探讨如何访问和操作每一个。 对于每个示例，我们将使用“octocat”存储库中的[此示例拉取请求](https://github.com/octocat/Spoon-Knife/pull/1176)。 和往常一样，可以在[平台示例存储库](https://github.com/github/platform-samples/tree/master/api/ruby/working-with-comments)中找到示例。\n\n## 拉取请求注释\n\n若要访问针对拉取请求的注释，请使用[用于管理问题的终结点](/zh/rest/issues/comments)。\n乍一看这似乎不符合直觉。 但是，一旦你理解了拉取请求只是包含代码的问题，使用这些终结点来针对拉取请求创建注释就合情合理了。\n\n我们将演示如何使用 [Octokit.rb](https://github.com/octokit/octokit.rb) 创建 Ruby 脚本来提取拉取请求注释。 你还需要创建一个 [personal access token](/zh/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)。\n\n以下代码应该可以帮助你开始使用 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\n在这里，我们专门调用 API 来获取注释 (`issue_comments`)，同时提供存储库的名称 (`octocat/Spoon-Knife`)，以及我们感兴趣的拉取请求 ID (`1176`)。 之后，只需遍历注释以获取有关每个注释的信息即可。\n\n## 拉取请求行注释\n\n在差异视图中，你可以针对在拉取请求中进行的某个更改的特定方面开启讨论。 这些注释出现在已更改文件中的各个行上。 此讨论的终结点 URL 来自[用于管理拉取请求评审的终结点](/zh/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您会注意到，它与上面的示例非常相似。 此视图与拉取请求注释之间的不同之处在于对话的焦点。\n对拉取请求的注释应予以保留，以供讨论或就代码的总体方向提出意见。 在拉取请求评审中所做的注释应该以在文件中实施特定更改的方式进行专门处理。\n\n## 提交注释\n\n最后一类注释专门针对单个提交。 因此，他们使用[用于管理提交评论的终结点](/zh/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 调用将检索单行注释以及对整个提交所做的注释。"}