From 6db5e7958ab56ede3bb88522a1c6fb0b47ea77ec Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 4 Feb 2021 15:37:47 +0100 Subject: [PATCH] tools: add GitHub Action linter for pr-url PR-URL: https://github.com/nodejs/node/pull/37221 Reviewed-By: Richard Lau Reviewed-By: Darshan Sen Reviewed-By: Rich Trott --- .github/workflows/linters.yml | 9 +++++++++ tools/lint-pr-url.mjs | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 tools/lint-pr-url.mjs diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml index 26be5a7cab2f98..9bb9cd14bc1d0f 100644 --- a/.github/workflows/linters.yml +++ b/.github/workflows/linters.yml @@ -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 }} diff --git a/tools/lint-pr-url.mjs b/tools/lint-pr-url.mjs new file mode 100755 index 00000000000000..3b491d26e389cb --- /dev/null +++ b/tools/lint-pr-url.mjs @@ -0,0 +1,37 @@ +#!/usr/bin/env node + +// Usage: +// git diff upstream/master...HEAD -G"pr-url:" -- "*.md" | \ +// ./tools/lint-pr-url.mjs + +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++; + } +}