Har*_*rry 6 github github-api eslint
我正在尝试编写一个小脚本,可以使用eslint输出对github PR进行评论.
问题是eslint给出了每个错误的绝对行号.但是github API想要相对于diff的行号.
来自github API文档:https://developer.github.com/v3/pulls/comments/#create-a-comment
要评论文件中的特定行,您需要首先确定差异中的位置.GitHub提供了一个application/vnd.github.v3.diff媒体类型,您可以在前面的请求中使用它来查看pull请求的diff.需要将diff解释为从文件中的行转换为diff中的位置.位置值是您要评论的文件中第一个"@@"hunk标题的行数.
"@@"行下方的行是位置1,下一行是位置2,依此类推.文件差异中的位置继续通过空白行和额外的空格来增加,直到达到新文件.
因此,如果我想在上面的图像中添加对第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
}
}]
}]
Run Code Online (Sandbox Code Playgroud)
我在考虑以下算法
NEW_STARTING_LINE_NUMBERto
开始创建一个新行号的数组NEW_STARTING_LINE_NUMBER+NEW_TOTAL_LINESnewStart从每个数字中减去并使其成为另一个数组relativeLineNumberstype==='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',
];
Run Code Online (Sandbox Code Playgroud)
现在只需将此数据传递给diff-parse模块并进行计算。
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',
];
Run Code Online (Sandbox Code Playgroud)
这将打印
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
Run Code Online (Sandbox Code Playgroud)
现在您可以使用相对行号使用 github api 发表评论。
出于我的目的,我只需要新添加的行的相对行号,但是使用上面的表格也可以为已删除的行获取它。
这是我使用它的 linting 项目的链接。https://github.com/harryi3t/lint-github-pr
| 归档时间: |
|
| 查看次数: |
804 次 |
| 最近记录: |