Skip to content

Commit 6db5e79

Browse files
aduh95danielleadams
authored andcommittedFeb 16, 2021
tools: add GitHub Action linter for pr-url
PR-URL: #37221 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent f6f9af6 commit 6db5e79

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
 

‎.github/workflows/linters.yml

+9
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,12 @@ jobs:
9898
- uses: mszostok/codeowners-validator@v0.4.0
9999
with:
100100
checks: "files,duppatterns"
101+
lint-pr-url:
102+
if: ${{ github.event.pull_request }}
103+
runs-on: ubuntu-latest
104+
steps:
105+
- uses: actions/checkout@v2
106+
with:
107+
fetch-depth: 2
108+
# GH Actions squashes all PR commits, HEAD^ refers to the base branch.
109+
- run: git diff HEAD^ HEAD -G"pr-url:" -- "*.md" | ./tools/lint-pr-url.mjs ${{ github.event.pull_request.html_url }}

‎tools/lint-pr-url.mjs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
3+
// Usage:
4+
// git diff upstream/master...HEAD -G"pr-url:" -- "*.md" | \
5+
// ./tools/lint-pr-url.mjs <expected-pr-url>
6+
7+
import process from 'node:process';
8+
import readline from 'node:readline';
9+
10+
const [, , expectedPrUrl] = process.argv;
11+
12+
const fileDelimiter = /^\+\+\+ b\/(.+\.md)$/;
13+
const changeDelimiter = /^@@ -\d+,\d+ \+(\d+),\d+ @@/;
14+
const prUrlDefinition = /^\+\s+pr-url: (.+)$/;
15+
16+
const validatePrUrl = (url) => url == null || url === expectedPrUrl;
17+
18+
let currentFile;
19+
let currentLine;
20+
21+
const diff = readline.createInterface({ input: process.stdin });
22+
for await (const line of diff) {
23+
if (fileDelimiter.test(line)) {
24+
currentFile = line.match(fileDelimiter)[1];
25+
console.log(`Parsing changes in ${currentFile}.`);
26+
} else if (changeDelimiter.test(line)) {
27+
currentLine = Number(line.match(changeDelimiter)[1]);
28+
} else if (!validatePrUrl(line.match(prUrlDefinition)?.[1])) {
29+
console.warn(
30+
`::warning file=${currentFile},line=${currentLine++},col=${line.length}` +
31+
'::pr-url doesn\'t match the actual PR URL.'
32+
);
33+
} else if (line[0] !== '-') {
34+
// Increment line counter if line is not being deleted.
35+
currentLine++;
36+
}
37+
}

0 commit comments

Comments
 (0)