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 7import process from 'node:process'; 8import readline from 'node:readline'; 9 10const [, , expectedPrUrl] = process.argv; 11 12const fileDelimiter = /^\+\+\+ b\/(.+\.md)$/; 13const changeDelimiter = /^@@ -\d+,\d+ \+(\d+),\d+ @@/; 14const prUrlDefinition = /^\+\s+pr-url: (.+)$/; 15 16const validatePrUrl = (url) => url == null || url === expectedPrUrl; 17 18let currentFile; 19let currentLine; 20 21const diff = readline.createInterface({ input: process.stdin }); 22for 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 URL of the current PR.' 32 ); 33 } else if (line[0] !== '-') { 34 // Increment line counter if line is not being deleted. 35 currentLine++; 36 } 37} 38