|
| 1 | +import pathUtil from "node:path"; |
| 2 | + |
| 3 | +/** |
| 4 | + * @fileoverview Various utilities related to integrating with CI. |
| 5 | + */ |
| 6 | + |
| 7 | +// https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md#limitations |
| 8 | +export const MAX_ANNOTATIONS_PER_TYPE_PER_STEP = 10; |
| 9 | +export const MAX_ANNOTATIONS_PER_JOB = 50; |
| 10 | + |
| 11 | +/** |
| 12 | + * @returns {boolean} true if running in CI (GitHub Actions, etc.) |
| 13 | + */ |
| 14 | +export const isCI = () => !!process.env.CI; |
| 15 | + |
| 16 | +/** |
| 17 | + * Note: PR may have changed between when this CI job was queued and when this job runs. |
| 18 | + * So, don't use this for security-critical things where accuracy is a must. |
| 19 | + * @returns {Promise<Set<string>>} List of paths (relative to root without leading / or ./) changed by this PR. |
| 20 | + */ |
| 21 | +export const getChangedFiles = async () => { |
| 22 | + if (!isCI()) { |
| 23 | + return []; |
| 24 | + } |
| 25 | + |
| 26 | + const prNumber = +process.env.PR_NUMBER; |
| 27 | + if (!prNumber) { |
| 28 | + throw new Error('Missing PR_NUMBER'); |
| 29 | + } |
| 30 | + |
| 31 | + const repo = process.env.GH_REPO; |
| 32 | + if (typeof repo !== 'string' || !repo.includes('/')) { |
| 33 | + throw new Error('Missing GH_REPO'); |
| 34 | + } |
| 35 | + |
| 36 | + const diffResponse = await fetch(`https://patch-diff.githubusercontent.com/raw/${repo}/pull/${prNumber}.diff`); |
| 37 | + const diffText = await diffResponse.text(); |
| 38 | + const fileMatches = [...diffText.matchAll(/^(?:---|\+\+\+) [ab]\/(.+)$/gm)] |
| 39 | + .map(match => match[1]); |
| 40 | + |
| 41 | + return new Set(fileMatches); |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * @typedef Annotation |
| 46 | + * @property {'notice'|'warning'|'error'} type |
| 47 | + * @property {string} file Absolute path to file or relative from repository root |
| 48 | + * @property {string} title |
| 49 | + * @property {string} message |
| 50 | + * @property {number} [line] 1-indexed |
| 51 | + * @property {number} [col] 1-indexed |
| 52 | + * @property {number} [endLine] 1-indexed |
| 53 | + * @property {number} [endCol] 1-indexed |
| 54 | + */ |
| 55 | + |
| 56 | +/** |
| 57 | + * @param {Annotation} annotation |
| 58 | + */ |
| 59 | +export const createAnnotation = (annotation) => { |
| 60 | + const rootDir = pathUtil.join(import.meta.dirname, ".."); |
| 61 | + const relativeFileName = pathUtil.relative(rootDir, annotation.file); |
| 62 | + |
| 63 | + // https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands |
| 64 | + |
| 65 | + let output = ""; |
| 66 | + output += `::${annotation.type} `; |
| 67 | + output += `file=${relativeFileName}`; |
| 68 | + |
| 69 | + // Documentation says line number is not required, but in practice that gets interpreted as |
| 70 | + // line 0 and ends up not showing up. So, we'll default to line 1. |
| 71 | + if (typeof annotation.line === "number") { |
| 72 | + output += `,line=${annotation.line}`; |
| 73 | + } else { |
| 74 | + output += ',line=1'; |
| 75 | + } |
| 76 | + |
| 77 | + // These are all actually optional. |
| 78 | + if (typeof annotation.col === "number") { |
| 79 | + output += `,col=${annotation.col}`; |
| 80 | + } |
| 81 | + if (typeof annotation.endLine === "number") { |
| 82 | + output += `,endLine=${annotation.endLine}`; |
| 83 | + } |
| 84 | + if (typeof annotation.endCol === "number") { |
| 85 | + output += `,endCol=${annotation.endCol}`; |
| 86 | + } |
| 87 | + |
| 88 | + output += `,title=${annotation.title}::`; |
| 89 | + output += annotation.message; |
| 90 | + |
| 91 | + console.log(output); |
| 92 | +}; |
0 commit comments