如何在GitHub上对PR的特定行进行评论

12

我想写一个小脚本,能够使用 eslint 输出评论 GitHub PRs。

问题是 eslint 给出了每个错误的绝对行数。但是 GitHub API 要求相对于差异的行号。

从 GitHub API 文档中可以看到: https://developer.github.com/v3/pulls/comments/#create-a-comment

要在文件的特定行上发表评论,您需要首先确定差异中的位置。GitHub 提供了 application/vnd.github.v3.diff 媒体类型,您可以在先前的请求中使用它来查看拉取请求的差异。需要解释差异以从文件中的行转换为差异中的位置。该位置值是从要评论的文件中第一个 "@@" hunk header 开始向下的行数。

"@@" 行的下一行是位置 1,下一行是位置 2,依此类推。文件的差异位置会随着空白行和额外的 hunks 的行数增加而继续增加,直到到达新的文件为止。

enter image description here

因此,如果我想在上面的图像中添加一个新的行号为 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_NUMBERNEW_STARTING_LINE_NUMBER+NEW_TOTAL_LINES
  • 从每个数字中减去newStart并将其变成另一个数组relativeLineNumbers
  • 遍历该数组,并针对每个删除行(type==='del'),增加相应的剩余relativeLineNumbers
  • 对于另一个块(具有@@的行),减少相应的剩余relativeLineNumbers

你在这个问题上找到解决方案了吗? - Wärting
@Wärting 是的,我添加了一份详细的答案。如果你需要帮助,请告诉我 :) - Harry
1个回答

7
我找到了一个解决方案。我没有在这里放出来,因为它只涉及简单的循环,没有什么特别的。但是现在还是回答一下来帮助其他人。
我已经提交了一个拉取请求,创建了与问题中显示的类似情况https://github.com/harryi3t/5134/pull/7/files Diff Image 使用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发表评论。

出于我的目的,我只需要新添加的行的相对行号,但是使用上面的表格也可以获取删除行的相对行号。

这是我在该项目中使用的代码链接:https://github.com/harryi3t/lint-github-pr


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接