Pull Request Review Comment wrong position from the diff?


#1

Hi guys,

I have a webhook that listen to all code reviews, then I fetch the comments of this PR review in order to get the position of the comment in the diff.
I’m using the GitHub REST API, but the issue I have is the same with the GraphQL API.

So the workflow is:

  1. Get the review ID from the webhook
  2. Fetch the comment list of that review
  3. For each comment, get the diff hunk and the position to find the edited line

All of that works fine 99% of the time.
Sometimes I get null in the position, I ignore these comments.

But this time, I get another weird issue.
Usually, the position refers to the index of the line in the diff.

For example, in:

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

If the position is 3, the edited line is +# sedy-test.

The issue is that for some comments, I get a position that can’t be inside the diff.
As an example, see this PR.

When I try to fetch the comment position of the review with the following request:

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

The response is the following:

{
  "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)}`);"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }
}

The position is 71, but the diff doesn’t contain more than 40 lines.

So is it a bug if the GitHub API, or I didn’t understand the point of the position field ?

EDIT: I posted the same question on Stack Overflow.