Skip to content

Commit

Permalink
tools: add GitHub Action linter for pr-url
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Feb 4, 2021
1 parent bc89048 commit db0d74c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,11 @@ jobs:
- uses: mszostok/[email protected]
with:
checks: "files,duppatterns"
lint-pr-url:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
# GH Actions squash all commits into one, 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 }}
36 changes: 36 additions & 0 deletions tools/lint-pr-url.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/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: (.+)$/;

let currentFile;
let currentLine;

const validatePrUrl = (url) => url == null || url === expectedPrUrl;

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]) - 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 {
currentLine++;
}
}

0 comments on commit db0d74c

Please sign in to comment.