Pull Request Review Comment 差异位置错误?
Pull Request Review Comment wrong position from the diff?
我有一个监听所有代码审查的 webhook,然后我获取此 PR 审查的评论,以便获得评论在 diff 中的位置。
我正在使用 GitHub REST API,但我遇到的问题与 GraphQL API.
相同
所以工作流程是:
- 从 webhook 获取评论 ID
- 获取该评论的评论列表
- 对于每个评论,获取 diff hunk 和找到编辑行的位置
所有这些在 99% 的时间里都工作正常。
有时我得到 null
的位置,我忽略这些评论。
但这一次,我遇到了另一个奇怪的问题。
通常,位置是指 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
。
问题是对于某些评论,我得到的位置不能在 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行。
那么GitHubAPI是bug还是我没看懂位置字段的要点?
The position value equals the number of lines down from the first "@@" hunk header in the file you want to add a comment. The line just below the "@@" line is position 1, the next line is position 2, and so on. The position in the diff continues to increase through lines of whitespace and additional hunks until the beginning of a new file.
此处 diffHunk
为您提供了当前的 diff hunk,这在文件中的第一个不是必需的
如果你得到完整的差异文件,那就更清楚了:
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 diff,但您可以像上面那样使用 Rest v3
我有同样的问题。我终于知道如何确定位置了。
让我们看看你的 PR。
此文件有 2 个 diff-hunks。
位置是从第一个hunk开始。
第一个大块头下面的线是位置 1。
第一个 hunk 的结尾是位置 36。
并且不知何故 github 在第二个大块开始之前为第一个大块的结尾添加 +1。(36+1)
所以,二哥的起跑线是38
第二大块你的评论上面有 34 行。
这就是为什么你的评论是 71。
Github的计算方法是这样的
我觉得这个计算有问题。但是如果你想计算,你可以用这个方法。
我有一个监听所有代码审查的 webhook,然后我获取此 PR 审查的评论,以便获得评论在 diff 中的位置。 我正在使用 GitHub REST API,但我遇到的问题与 GraphQL API.
相同所以工作流程是:
- 从 webhook 获取评论 ID
- 获取该评论的评论列表
- 对于每个评论,获取 diff hunk 和找到编辑行的位置
所有这些在 99% 的时间里都工作正常。
有时我得到 null
的位置,我忽略这些评论。
但这一次,我遇到了另一个奇怪的问题。 通常,位置是指 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
。
问题是对于某些评论,我得到的位置不能在 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行。
那么GitHubAPI是bug还是我没看懂位置字段的要点?
The position value equals the number of lines down from the first "@@" hunk header in the file you want to add a comment. The line just below the "@@" line is position 1, the next line is position 2, and so on. The position in the diff continues to increase through lines of whitespace and additional hunks until the beginning of a new file.
此处 diffHunk
为您提供了当前的 diff hunk,这在文件中的第一个不是必需的
如果你得到完整的差异文件,那就更清楚了:
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 diff,但您可以像上面那样使用 Rest v3
我有同样的问题。我终于知道如何确定位置了。
让我们看看你的 PR。
此文件有 2 个 diff-hunks。
位置是从第一个hunk开始。
第一个大块头下面的线是位置 1。
第一个 hunk 的结尾是位置 36。
并且不知何故 github 在第二个大块开始之前为第一个大块的结尾添加 +1。(36+1)
所以,二哥的起跑线是38
第二大块你的评论上面有 34 行。
这就是为什么你的评论是 71。
Github的计算方法是这样的
我觉得这个计算有问题。但是如果你想计算,你可以用这个方法。