tools: add GitHub Action linter for pr-url

PR-URL: https://github.com/nodejs/node/pull/37221
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
pull/37221/head
Antoine du Hamel 2021-02-04 15:37:47 +01:00
parent beee53884d
commit 33d3a2e6cb
2 changed files with 46 additions and 0 deletions

View File

@ -98,3 +98,12 @@ jobs:
- uses: mszostok/codeowners-validator@v0.4.0
with:
checks: "files,duppatterns"
lint-pr-url:
if: ${{ github.event.pull_request }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
# GH Actions squashes all PR commits, HEAD^ refers to the base branch.
- run: git diff HEAD^ HEAD -G"pr-url:" -- "*.md" | ./tools/lint-pr-url.mjs ${{ github.event.pull_request.html_url }}

View File

@ -0,0 +1,37 @@
#!/usr/bin/env node
// Usage:
// git diff upstream/master...HEAD -G"pr-url:" -- "*.md" | \
// ./tools/lint-pr-url.mjs <expected-pr-url>
import process from 'node:process';
import readline from 'node:readline';
const [, , expectedPrUrl] = process.argv;
const fileDelimiter = /^\+\+\+ b\/(.+\.md)$/;
const changeDelimiter = /^@@ -\d+,\d+ \+(\d+),\d+ @@/;
const prUrlDefinition = /^\+\s+pr-url: (.+)$/;
const validatePrUrl = (url) => url == null || url === expectedPrUrl;
let currentFile;
let currentLine;
const diff = readline.createInterface({ input: process.stdin });
for await (const line of diff) {
if (fileDelimiter.test(line)) {
currentFile = line.match(fileDelimiter)[1];
console.log(`Parsing changes in ${currentFile}.`);
} else if (changeDelimiter.test(line)) {
currentLine = Number(line.match(changeDelimiter)[1]);
} else if (!validatePrUrl(line.match(prUrlDefinition)?.[1])) {
console.warn(
`::warning file=${currentFile},line=${currentLine++},col=${line.length}` +
'::pr-url doesn\'t match the actual PR URL.'
);
} else if (line[0] !== '-') {
// Increment line counter if line is not being deleted.
currentLine++;
}
}