从diff中拉出请求评论注释的位置错误?

时间:2018-10-15 16:05:03

标签: github github-api

我有一个Webhook来侦听所有代码审查,然后获取此PR审查的注释,以便获得注释在diff中的位置。 我正在使用GitHub REST API,但我遇到的问题与GraphQL API相同。

所以工作流程是:

  1. 从网络挂钩获取评论ID
  2. 获取该评论的评论列表
  3. 对于每个注释,获取差异块和查找已编辑行的位置

所有这些在99%的时间内都能正常工作。 有时候我得到null的位置,而忽略了这些评论。

但是这次,我遇到了另一个奇怪的问题。 通常,位置是指差异中线的索引。

例如,在:

@@ -1 +1,3 @@
-# sedy-test
\\ No newline at end of file
+# sedy-test
+
+This repository is used to test [sedy](https://github.com/marmelab/sedy).

如果位置为3,则编辑的行为+# sedy-test

问题是对于某些评论,我得到的职位不能在差异内。 例如,请参见this PR

当我尝试通过以下请求获取评论的评论位置时:

{
  repository(owner: "Kmaschta", name: "comfygure") {
    pullRequest(number: 1) {
      reviews(last: 1) {
        edges {
          node {
            state
            comments(first: 1) {
              edges {
                node {
                  bodyText
                  authorAssociation
                  position
                  originalPosition
                  diffHunk
                }
              }
            }
          }
        }
      }
    }
  }
}

响应如下:

{
  "data": {
    "repository": {
      "pullRequest": {
        "reviews": {
          "edges": [
            {
              "node": {
                "state": "COMMENTED",
                "comments": {
                  "edges": [
                    {
                      "node": {
                        "bodyText": "s/fot/for/",
                        "authorAssociation": "OWNER",
                        "position": 71,
                        "originalPosition": 71,
                        "diffHunk": "@@ -24,31 +34,39 @@ const ls = (ui, modules) => function* () {\n };\n \n const add = (ui, modules, options) => function* () {\n-    const { red, bold } = ui.colors;\n+    const { red, bold, green } = ui.colors;\n \n     if (!options.length) {\n         ui.error(`${red('No environment specified.')}`);\n-        help(ui, 1);\n     }\n \n     if (options.length > 1) {\n         ui.error(`${red('Invalid environment format. The environment name should be one word.')}`);\n-        help(ui, 1);\n+    }\n+\n+    if (options.length !== 1) {\n+        ui.print(`${bold('SYNOPSIS')}\n+        ${bold('comfy')} env add <environment>\n+\n+Type ${green('comfy env --help')} for details`);\n+\n+        return ui.exit(0);\n     }\n \n     const project = yield modules.project.retrieveFromConfig();\n     const environment = yield modules.environment.add(project, options[0]);\n-    const addCommand = `comfy add ${environment.name}`;\n+    const addCommand = `comfy setall ${environment.name}`;\n \n-    ui.print(`${bold('Cool!')} Your new environment \"${bold(environment.name)}\" was successfully saved.`);\n-    ui.print(`You can now add a configuration, try ${bold(addCommand)}`);\n+    ui.print(`${bold(green('Environment successfully created'))}`);\n+    ui.print(`You can now set a configuration fot this environment using ${bold(addCommand)}`);"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }
}

位置为71,但差异不超过40行。

那么,如果是GitHub API,还是我不理解位置字段的意义,这是一个错误吗?

注意:我发布了same question on the GitHub forum,为什么还没有答案。

2 个答案:

答案 0 :(得分:2)

来自Github API comment doc

  

位置值等于从要添加注释的文件中第一个“ @@”大块头开始的行数。 “ @@”行正下方的行是位置1,下一行是位置2,依此类推。差异的位置通过空白行和其他大块继续增加,直到新文件开始。

这里diffHunk为您提供了当前的diff块,而文件中的第一个不必要。

如果您获得了完整的差异文件,那就更清楚了:

curl "https://api.github.com/repos/Kmaschta/comfygure/pulls/1" \
     -H "Accept: application/vnd.github.v3.diff"

评论位于env.js中,其第一个大块始于第77行,您的评论位于第148行,而您请求中的diffHunk则位于第114行

我认为目前无法使用GraphQL请求完整的PR差异,但您可以如上所述使用Rest v3

答案 1 :(得分:1)

我有同样的问题。最后,我找到了确定位置的方法。

让我们看看您的PR。

https://github.com/Kmaschta/comfygure/pull/1/files?utf8=%E2%9C%93&diff=unified#diff-10b371776dce3b12ed817f3fb8704a7d

此文件有2个差异柄。

位置从第一个大块开始。

第一个大块下面的线是位置1。

https://github.com/Kmaschta/comfygure/pull/1/files?utf8=%E2%9C%93&diff=unified#diff-10b371776dce3b12ed817f3fb8704a7dL1

第一个大块的末尾是位置36。

https://github.com/Kmaschta/comfygure/pull/1/files?utf8=%E2%9C%93&diff=unified#diff-10b371776dce3b12ed817f3fb8704a7dL18

然后github以某种方式将第一个大块结尾的+1添加到第二个大块开始之前。(36 + 1)

所以,第二个大块头的起始线是38。

您的评论上方有34行,是第二个提示。

这就是为什么您的评论是71。

https://github.com/Kmaschta/comfygure/pull/1/files?utf8=%E2%9C%93&diff=unified#diff-10b371776dce3b12ed817f3fb8704a7dR61

Github的计算方法就是这种方式。

我认为此计算有误。但是,如果要计算,可以使用此方法。