NodeJS 从Danger.js添加和删除中排除文件

1u4esq0p  于 2022-12-26  发布在  Node.js
关注(0)|答案(3)|浏览(123)

我们试图在prs中设置最大行更改,但注意到有些 meta文件很容易超过此限制,如yarn.lock。
有人知道如何从添加和删除中排除文件吗?

// ...

const linesAdded = danger.github.pr.additions || 0;
const linesRemoved = danger.github.pr.deletions || 0;

// ...

if (linesAdded + linesRemoved > bigPRThreshold) {
  fail(
    `This PR size is too large (Over ${bigPRThreshold} lines. Please split into separate PRs to enable faster & easier review.`
  );
}

// ...
14ifxucb

14ifxucb1#

我找到了以下gitDSL函数来访问单个文件中的信息

//This should make it really easy to do work when specific keypaths have changed inside a JSON file.
JSONDiffForFile(filename: string) => Promise

// Provides a JSON patch (rfc6902) between the two versions of a JSON file, returns null if you don't have any changes for the file in the diff.

// Note that if you are looking to just see changes like: before, after, added or removed - you should use `JSONDiffForFile` instead, as this can be a bit unwieldy for a Dangerfile.
JSONPatchForFile(filename: string) => Promise

// Offers the diff for a specific file
diffForFile(filename: string) => Promise

// Offers the overall lines of code added/removed in the diff
linesOfCode() => Promise

// Offers the structured diff for a specific file
structuredDiffForFile(filename: string) => Promise

(有关这些功能的文档:https://danger.systems/js/reference.html#GitDSL)
使用danger.git.structuredDiffForFile,我可以计算出要排除的行,因此

const file = 'yarn.lock';
  const diff1 = await danger.git.structuredDiffForFile(file);
  const excludedLines = diff1.chunks[0].changes.length
9rygscc1

9rygscc12#

这样做效果很好:

import { markdown, message, danger, warn } from "danger";
import minimatch from "minimatch";

const pr = danger.github.pr;
const modifiedFiles = danger.git.modified_files;

(async function () {
    // Encourage smaller PRs
    await checkPRSize();
    
})();

async function checkPRSize() {
  const MAX_ADDITIONS_COUNT = 500;

  const ignoredLineCount = await getIgnoredLineCount();

  if (pr.additions - ignoredLineCount > MAX_ADDITIONS_COUNT) {
    warn("Its too big!");
  }
}

// Get the number of additions in files that we want to ignore
// so that we can subtract them from the total additions
// Use a glob to match multiple files in different location
async function getIgnoredLineCount(): Promise<number> {
  let ignoredLineCount = 0;
  const IGNORE_FILE_GLOB = "**/*+(.schema.json|package.lock)";
  const ignoredFiles = modifiedFiles.filter((file) =>
    minimatch(file, IGNORE_FILE_GLOB)
  );

  await Promise.all(
    ignoredFiles.map(async (file) => {
      const diff = await danger.git.structuredDiffForFile(file);
      diff.chunks.map((chunk) => {
        // Here we filter to only get the additions
        const additions = chunk.changes.filter(({ type }) => type === "add");
        ignoredLineCount = ignoredLineCount + additions.length;
      });
    })
  );

  return ignoredLineCount;
}
0lvr5msh

0lvr5msh3#

我们使用glob模式来指定要排除的文件:

const linesCount = await danger.git.linesOfCode('**/*');
  // exclude fixtures and auto generated files
  const excludeLinesCount = await danger.git.linesOfCode(
    '{**/schemaTypes.ts,fixtures/**,package*.json,**/*.spec.ts,**/*.md}',
  );
  const totalLinesCount = linesCount - excludeLinesCount;

相关问题