【问题标题】:Get modified line numbers in javascript github action在 javascript github 操作中获取修改后的行号
【发布时间】:2021-06-29 07:50:27
【问题描述】:

我正在尝试编写一个 JavaScript(在 TypeScript 中)Github Action 来检查拉取请求中的代码更改。我设法通过这样做来获取修改文件的名称:

import * as github from '@actions/github';
import * as core from '@actions/core';

async function run() {
  const base = github.context.payload.pull_request.base.sha;
  const head = github.context.payload.pull_request.head.sha;

  const client = github.getOctokit(core.getInput('token', {required: true});
  const response = await client.repos.compareCommits({
    base,
    head,
    owner: context.repo.owner,
    context.repo.repo,
  });

  const files = response.data.files;
  const fileNames = files.map((file) => file.filename);
}

run ();

我也想知道修改了哪些行(只有头文件的行可以),但是从files似乎无法访问,我有点卡住了。

有关信息,files 的类型为:

{
    sha: string;
    filename: string;
    status: string;
    additions: number;
    deletions: number;
    changes: number;
    blob_url: string;
    raw_url: string;
    contents_url: string;
    patch?: string | undefined;
    previous_filename?: string | undefined;
}[]

如何获取行号?

【问题讨论】:

    标签: javascript typescript github-actions pull-request


    【解决方案1】:

    我终于成功了。 .patch包含文件的变化,通常构造如下

    "@@ -6,7 +6,9 @@ <fileCodeChanges> @@ -12,7 +12,3 <fileCodeChanges2> ..."
    

    所有更改都由

    分隔
    `@@ -${startLineDeletion},${numberOfDeletion} +${startLineAddition},${numberOfAddition} @@`
    

    所以我只需要

    1. 将.patch 与"@@" 分开
    2. 过滤代码部分以仅保留行号
    3. 提取数字

    这是我的代码:

    import * as github from '@actions/github';
    import * as core from '@actions/core';
    
    interface FileLines {
      start: number;
      end: number
    }
    
    export interface ModifiedFile {
      name: string;
      deletion?: FileLines[];
      addition?: FileLines[];
    }
    
    
    async function run() {
      const base = github.context.payload.pull_request.base.sha;
      const head = github.context.payload.pull_request.head.sha;
    
      const client = github.getOctokit(core.getInput('token', {required: true});
      const response = await client.repos.compareCommits({
        base,
        head,
        owner: context.repo.owner,
        context.repo.repo,
      });
    
      const files = response.data.files;
      const modifiedFilesWithModifiedLines = files.map(parseFile);
    }
    
    function parseFile(file: {filename: string, patch?: string|undefined}): ModifiedFile {
      const modifiedFile: ModifiedFile = {
        name: file.filename
      };
      if (file.patch) {
        // The changes are included in the file
        const patches = file.patch.split('@@').filter((_, index) => index % 2); // Only take the line information and discard the modified code
        for (const patch of patches) {
          // patch is usually like " -6,7 +6,8"
          try {
            const hasAddition = patch.includes('+');
            const hasDeletion = patch.includes('-');
            if (hasAddition) {
              const lines = patch.match(/\+.*/)![0].trim().slice(1).split(',').map(num) => parseInt(num)) as [number, number];
              modifiedFile.addition ??= [];
              modifiedFile.addition?.push({
                start: lines[0],
                end: lines[0] + lines[1],
              });
            }
            if (hasDeletion) {
    
              const lines = patch.split('+')[0].trim().slice(1).split(',').map((num) => parseInt(num)) as [number, number];
              modifiedFile.deletion ??= [];
              modifiedFile.deletion?.push({
                start: lines[0],
                end: lines[0] + lines[1],
              });
            }
    
          } catch (error) {
            console.log(`Error getting the patch of the file:\n${error}`);
          }
        }
      } else {
        // Take the all file
        modifiedFile.addition = [{
            start: 0,
            end: Infinity,
        }];
        modifiedFile.deletion = [{
            start: 0,
            end: Infinity,
        }];
      }
      return modifiedFile;
    
    run ();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-07
      • 2012-07-30
      • 2021-09-04
      • 2020-04-04
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多