-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
f6f9af6
commit 6db5e79
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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++; | ||
} | ||
} |