我正在尝试编写一个可以使用 eslint 输出对 github PR 进行评论的小脚本。
问题是 eslint 给了我每个错误的绝对行号。
但是 github API 想要相对于 diff 的行号。
来自 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 提供的 diff 转换为 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
现在您可以使用相对行号使用 github api 发表评论。
出于我的目的,我只需要新添加的行的相对行号,但使用上面的表格也可以为已删除的行获取它。
这是我使用它的 linting 项目的链接。 https://github.com/harryi3t/lint-github-pr
关于github - 如何在 github 上评论 PR 上的特定行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41662127/