|
| 1 | +import pathUtil from "node:path"; |
| 2 | + |
| 3 | +// https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md#limitations |
| 4 | +export const MAX_ANNOTATIONS_PER_TYPE_PER_STEP = 10; |
| 5 | +export const MAX_ANNOTATIONS_PER_JOB = 50; |
| 6 | + |
| 7 | +/** |
| 8 | + * @returns {boolean} true if running in GitHub Actions, etc. |
| 9 | + */ |
| 10 | +export const isCI = () => !!process.env.CI; |
| 11 | + |
| 12 | +/** |
| 13 | + * @returns {Promise<Set<string>>} List of paths (relative to root without leading / or ./) changed by this PR. |
| 14 | + */ |
| 15 | +export const getChangedFiles = async () => { |
| 16 | + if (!isCI()) { |
| 17 | + return []; |
| 18 | + } |
| 19 | + |
| 20 | + const prNumber = +process.env.PR_NUMBER; |
| 21 | + if (!prNumber) { |
| 22 | + throw new Error('Missing PR_NUMBER'); |
| 23 | + } |
| 24 | + |
| 25 | + const repo = process.env.GH_REPO; |
| 26 | + if (typeof repo !== 'string' || !repo.includes('/')) { |
| 27 | + throw new Error('Missing GH_REPO'); |
| 28 | + } |
| 29 | + |
| 30 | + const diffResponse = await fetch(`https://patch-diff.githubusercontent.com/raw/${repo}/pull/${prNumber}.diff`); |
| 31 | + const diffText = await diffResponse.text(); |
| 32 | + const fileMatches = [...diffText.matchAll(/^(?:---|\+\+\+) [ab]\/(.+)$/gm)] |
| 33 | + .map(match => match[1]); |
| 34 | + |
| 35 | + return new Set(fileMatches); |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * @typedef Annotation |
| 40 | + * @property {'notice'|'warning'|'error'} type |
| 41 | + * @property {string} file Absolute path to file or relative from repository root |
| 42 | + * @property {string} title |
| 43 | + * @property {string} message |
| 44 | + * @property {number} [line] 1-indexed |
| 45 | + * @property {number} [col] 1-indexed |
| 46 | + * @property {number} [endLine] 1-indexed |
| 47 | + * @property {number} [endCol] 1-indexed |
| 48 | + */ |
| 49 | + |
| 50 | +/** |
| 51 | + * @param {Annotation} annotation |
| 52 | + */ |
| 53 | +export const createAnnotation = (annotation) => { |
| 54 | + const rootDir = pathUtil.join(import.meta.dirname, ".."); |
| 55 | + const relativeFileName = pathUtil.relative(rootDir, annotation.file); |
| 56 | + |
| 57 | + let output = ""; |
| 58 | + output += `::${annotation.type} `; |
| 59 | + output += `file=${relativeFileName}`; |
| 60 | + |
| 61 | + if (typeof annotation.line === "number") output += `,line=${annotation.line}`; |
| 62 | + if (typeof annotation.col === "number") output += `,col=${annotation.col}`; |
| 63 | + if (typeof annotation.endLine === "number") |
| 64 | + output += `,endLine=${annotation.endLine}`; |
| 65 | + if (typeof annotation.endCol === "number") |
| 66 | + output += `,endCol=${annotation.endCol}`; |
| 67 | + |
| 68 | + output += `,title=${annotation.title}::`; |
| 69 | + output += annotation.message; |
| 70 | + |
| 71 | + console.log(output); |
| 72 | +}; |
0 commit comments