|
| 1 | +--- # Fail if an AI instruction file writes a NEEDS YOU marker as a GitHub shortcode |
| 2 | +# Maintain in repo: cs-template |
| 3 | +name: "Check Instructions NEEDS YOU Markers" |
| 4 | +description: "Fails if a line under ai/ or in .ai-instructions contains NEEDS YOU together with a :stop_sign:, :warning: or :white_check_mark: shortcode" |
| 5 | + |
| 6 | +runs: |
| 7 | + using: composite |
| 8 | + steps: |
| 9 | + - name: "Check NEEDS YOU markers" |
| 10 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 11 | + with: |
| 12 | + script: | |
| 13 | + // NEEDS YOU markers must be literal emoji characters because a terminal shows a |
| 14 | + // shortcode as plain text. Only lines that mention NEEDS YOU are checked, so shortcodes |
| 15 | + // elsewhere (which GitHub renders) are left alone. |
| 16 | +
|
| 17 | + const { execFileSync } = require('node:child_process'); |
| 18 | + const { readFileSync } = require('node:fs'); |
| 19 | + const path = require('node:path'); |
| 20 | +
|
| 21 | + const NEEDS_YOU = 'NEEDS YOU'; |
| 22 | + const MARKER_SHORTCODES = [':stop_sign:', ':warning:', ':white_check_mark:']; |
| 23 | +
|
| 24 | + function trackedInstructionFiles(root) { |
| 25 | + return execFileSync('git', ['ls-files', '-z', '--', 'ai', '.ai-instructions'], { cwd: root, encoding: 'utf8' }) |
| 26 | + .split('\0') |
| 27 | + .filter(Boolean); |
| 28 | + } |
| 29 | +
|
| 30 | + function shortcodesOnLine(text) { |
| 31 | + return text.includes(NEEDS_YOU) ? MARKER_SHORTCODES.filter((shortcode) => text.includes(shortcode)) : []; |
| 32 | + } |
| 33 | +
|
| 34 | + function findProblems(root, files) { |
| 35 | + return files.flatMap((file) => |
| 36 | + readFileSync(path.join(root, file), 'utf8') |
| 37 | + .split('\n') |
| 38 | + .flatMap((text, index) => |
| 39 | + shortcodesOnLine(text).map((shortcode) => ({ |
| 40 | + file, |
| 41 | + line: index + 1, |
| 42 | + message: `NEEDS YOU marker written as the shortcode ${shortcode}; use the emoji character instead`, |
| 43 | + })), |
| 44 | + ), |
| 45 | + ); |
| 46 | + } |
| 47 | +
|
| 48 | + const root = execFileSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8' }).trim(); |
| 49 | + const problems = findProblems(root, trackedInstructionFiles(root)); |
| 50 | +
|
| 51 | + if (problems.length > 0) { |
| 52 | + for (const problem of problems) { |
| 53 | + core.error(problem.message, { file: problem.file, startLine: problem.line }); |
| 54 | + } |
| 55 | +
|
| 56 | + core.setFailed('❌ Found NEEDS YOU markers written as GitHub shortcodes.'); |
| 57 | + return; |
| 58 | + } |
| 59 | +
|
| 60 | + core.info('✅ All NEEDS YOU markers use emoji characters.'); |
0 commit comments