【问题标题】:Pull Request Review Comment wrong position from the diff?从差异中提取请求评论评论错误的位置?
【发布时间】:2021-04-13 02:28:24
【问题描述】:

我有一个监听所有代码审查的 webhook,然后我获取此 PR 审查的 cmets 以获得评论在 diff 中的位置。 我正在使用 GitHub REST API,但我遇到的问题与 GraphQL API 相同。

所以工作流程是:

  1. 从 webhook 中获取评论 ID
  2. 获取该评论的评论列表
  3. 对于每条评论,获取 diff hunk 和查找编辑行的位置

所有这些在 99% 的情况下都能正常工作。 有时我在位置上得到null,我忽略了这些cmets。

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

例如,在:

@@ -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

问题在于,对于某些 cmets,我得到的位置不能在 diff 内。 例如,请参阅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 的 bug,还是我没有理解 position 字段的意义?

注意:我发布了same question on the GitHub forum

【问题讨论】:

    标签: github graphql github-api github-graphql github-api-v4


    【解决方案1】:

    来自Github API comment doc

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

    这里diffHunk 为您提供当前的差异块,这不是文件中第一个所必需的

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

    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

    【讨论】:

    • 您好,感谢您的回答!我如何从 diffHunk(或评论)中知道大块从第 77 行开始?此外,在大多数情况下,diff hunk 中的第一行是 1。我不明白。
    • 你无法从 diffHunk 中看出,你需要完整的 diff 才能知道文件的第一个块从哪一行开始
    • 这在大多数情况下都有效,因为大多数情况下文件的第一个大块是唯一的,或者它是文件的第一个。当文件中有多个差异时它会失败
    【解决方案2】:

    我也有同样的问题。我终于知道如何确定位置了。

    让我们看看你的 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的计算方式是这样的。

    我认为这个计算有问题。但是如果你想计算,你可以用这种方法。

    【讨论】:

    • 感谢您的回答!你确定这是正确的计算吗?我很难验证这一点。
    • 我没有尝试所有情况,但几乎所有情况这种方法都是正确的。我也很难找出这一点。如果您发现特殊情况,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 2014-08-27
    • 2021-10-11
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多