如何评论 github 上 PR 的特定行号
How to comment on a specific line number on a PR on github
我正在尝试编写一个小脚本,可以使用 eslint 输出对 github PR 发表评论。
问题是 eslint 为我提供了每个错误的绝对行号。
但是 github API 想要相对于差异的行号。
来自 github API 文档:https://developer.github.com/v3/pulls/comments/#create-a-comment
To comment on a specific line in a file, you will need to first
determine the position in the diff. GitHub offers a
application/vnd.github.v3.diff media type which you can use in a
preceding request to view the pull request's diff. The diff needs to
be interpreted to translate from the line in the file to a position in
the diff. The position value is the number of lines down from the
first "@@" hunk header in the file you would like to comment on.
The line just below the "@@" line is position 1, the next line is
position 2, and so on. The position in the file's diff continues to
increase through lines of whitespace and additional hunks until a new
file is reached.
所以如果我想在上图中的第 5 行添加评论,那么我需要将 12 传递给 API
我的问题是如何轻松地将 eslint 在其错误消息中给出的新行号映射到 github API
所需的相对行号
到目前为止我尝试了什么
我正在使用 parse-diff 将 github API 提供的差异转换为 json 对象
[{
"chunks": [{
"content": "@@ -,OLD_TOTAL_LINES +NEW_STARTING_LINE_NUMBER,NEW_TOTAL_LINES @@",
"changes": [
{
"type": STRING("normal"|"add"|"del"),
"normal": BOOLEAN,
"add": BOOLEAN,
"del": BOOLEAN,
"ln1": OLD_LINE_NUMBER,
"ln2": NEW_LINE_NUMBER,
"content": STRING,
"oldStart": NUMBER,
"oldLines": NUMBER,
"newStart": NUMBER,
"newLines": NUMBER
}
}]
}]
我在想下面的算法
- 创建一个从
NEW_STARTING_LINE_NUMBER
到
NEW_STARTING_LINE_NUMBER+NEW_TOTAL_LINES
每个文件
- 从每个数字中减去
newStart
并使其成为另一个数组relativeLineNumbers
- 遍历数组并为每个删除的行(
type==='del'
)增加相应的剩余relativeLineNumbers
- 对于另一个大块头(具有
@@
的行)减少相应的剩余relativeLineNumbers
我找到了解决办法。我没有把它放在这里,因为它涉及简单的循环,没有什么特别的。但无论如何现在回答以帮助他人。
我已经打开了一个拉取请求来创建问题中所示的类似情况
https://github.com/harryi3t/5134/pull/7/files
使用 Github API 可以得到差异数据。
diff --git a/test.js b/test.js
index 2aa9a08..066fc99 100644
--- a/test.js
+++ b/test.js
@@ -2,14 +2,7 @@
var hello = require('./hello.js');
-var names = [
- 'harry',
- 'barry',
- 'garry',
- 'harry',
- 'barry',
- 'marry',
-];
+var names = ['harry', 'barry', 'garry', 'harry', 'barry', 'marry'];
var names2 = [
'harry',
@@ -23,9 +16,7 @@ var names2 = [
// after this line new chunk will be created
var names3 = [
'harry',
- 'barry',
- 'garry',
'harry',
'barry',
- 'marry',
+ 'marry', 'garry',
];
现在只需将此数据传递给 diff-parse
模块并进行计算。
var parsedFiles = parseDiff(data); // diff output
parsedFiles.forEach(
function (file) {
var relativeLine = 0;
file.chunks.forEach(
function (chunk, index) {
if (index !== 0) // relative line number should increment for each chunk
relativeLine++; // except the first one (see rel-line 16 in the image)
chunk.changes.forEach(
function (change) {
relativeLine++;
console.log(
change.type,
change.ln1 ? change.ln1 : '-',
change.ln2 ? change.ln2 : '-',
change.ln ? change.ln : '-',
relativeLine
);
}
);
}
);
}
);
这将打印
type (ln1) old line (ln2) new line (ln) added/deleted line relative line
normal 2 2 - 1
normal 3 3 - 2
normal 4 4 - 3
del - - 5 4
del - - 6 5
del - - 7 6
del - - 8 7
del - - 9 8
del - - 10 9
del - - 11 10
del - - 12 11
add - - 5 12
normal 13 6 - 13
normal 14 7 - 14
normal 15 8 - 15
normal 23 16 - 17
normal 24 17 - 18
normal 25 18 - 19
del - - 26 20
del - - 27 21
normal 28 19 - 22
normal 29 20 - 23
del - - 30 24
add - - 21 25
normal 31 22 - 26
现在您可以使用相对行号来 post 使用 github api 的评论。
为了我的目的,我只需要新添加行的相对行号,但是使用上面的 table 也可以为删除的行获取它。
这是我在其中使用它的 linting 项目的 link。 https://github.com/harryi3t/lint-github-pr
我正在尝试编写一个小脚本,可以使用 eslint 输出对 github PR 发表评论。
问题是 eslint 为我提供了每个错误的绝对行号。 但是 github API 想要相对于差异的行号。
来自 github API 文档:https://developer.github.com/v3/pulls/comments/#create-a-comment
To comment on a specific line in a file, you will need to first determine the position in the diff. GitHub offers a application/vnd.github.v3.diff media type which you can use in a preceding request to view the pull request's diff. The diff needs to be interpreted to translate from the line in the file to a position in the diff. The position value is the number of lines down from the first "@@" hunk header in the file you would like to comment on.
The line just below the "@@" line is position 1, the next line is position 2, and so on. The position in the file's diff continues to increase through lines of whitespace and additional hunks until a new file is reached.
所以如果我想在上图中的第 5 行添加评论,那么我需要将 12 传递给 API
我的问题是如何轻松地将 eslint 在其错误消息中给出的新行号映射到 github API
所需的相对行号到目前为止我尝试了什么
我正在使用 parse-diff 将 github API 提供的差异转换为 json 对象
[{
"chunks": [{
"content": "@@ -,OLD_TOTAL_LINES +NEW_STARTING_LINE_NUMBER,NEW_TOTAL_LINES @@",
"changes": [
{
"type": STRING("normal"|"add"|"del"),
"normal": BOOLEAN,
"add": BOOLEAN,
"del": BOOLEAN,
"ln1": OLD_LINE_NUMBER,
"ln2": NEW_LINE_NUMBER,
"content": STRING,
"oldStart": NUMBER,
"oldLines": NUMBER,
"newStart": NUMBER,
"newLines": NUMBER
}
}]
}]
我在想下面的算法
- 创建一个从
NEW_STARTING_LINE_NUMBER
到NEW_STARTING_LINE_NUMBER+NEW_TOTAL_LINES
每个文件 - 从每个数字中减去
newStart
并使其成为另一个数组relativeLineNumbers
- 遍历数组并为每个删除的行(
type==='del'
)增加相应的剩余relativeLineNumbers
- 对于另一个大块头(具有
@@
的行)减少相应的剩余relativeLineNumbers
我找到了解决办法。我没有把它放在这里,因为它涉及简单的循环,没有什么特别的。但无论如何现在回答以帮助他人。
我已经打开了一个拉取请求来创建问题中所示的类似情况 https://github.com/harryi3t/5134/pull/7/files
使用 Github API 可以得到差异数据。
diff --git a/test.js b/test.js
index 2aa9a08..066fc99 100644
--- a/test.js
+++ b/test.js
@@ -2,14 +2,7 @@
var hello = require('./hello.js');
-var names = [
- 'harry',
- 'barry',
- 'garry',
- 'harry',
- 'barry',
- 'marry',
-];
+var names = ['harry', 'barry', 'garry', 'harry', 'barry', 'marry'];
var names2 = [
'harry',
@@ -23,9 +16,7 @@ var names2 = [
// after this line new chunk will be created
var names3 = [
'harry',
- 'barry',
- 'garry',
'harry',
'barry',
- 'marry',
+ 'marry', 'garry',
];
现在只需将此数据传递给 diff-parse
模块并进行计算。
var parsedFiles = parseDiff(data); // diff output
parsedFiles.forEach(
function (file) {
var relativeLine = 0;
file.chunks.forEach(
function (chunk, index) {
if (index !== 0) // relative line number should increment for each chunk
relativeLine++; // except the first one (see rel-line 16 in the image)
chunk.changes.forEach(
function (change) {
relativeLine++;
console.log(
change.type,
change.ln1 ? change.ln1 : '-',
change.ln2 ? change.ln2 : '-',
change.ln ? change.ln : '-',
relativeLine
);
}
);
}
);
}
);
这将打印
type (ln1) old line (ln2) new line (ln) added/deleted line relative line
normal 2 2 - 1
normal 3 3 - 2
normal 4 4 - 3
del - - 5 4
del - - 6 5
del - - 7 6
del - - 8 7
del - - 9 8
del - - 10 9
del - - 11 10
del - - 12 11
add - - 5 12
normal 13 6 - 13
normal 14 7 - 14
normal 15 8 - 15
normal 23 16 - 17
normal 24 17 - 18
normal 25 18 - 19
del - - 26 20
del - - 27 21
normal 28 19 - 22
normal 29 20 - 23
del - - 30 24
add - - 21 25
normal 31 22 - 26
现在您可以使用相对行号来 post 使用 github api 的评论。
为了我的目的,我只需要新添加行的相对行号,但是使用上面的 table 也可以为删除的行获取它。
这是我在其中使用它的 linting 项目的 link。 https://github.com/harryi3t/lint-github-pr