|
| 1 | +import { createRequire } from 'node:module'; |
| 2 | + |
| 3 | +// minimatch is resolved at runtime so this module has no build step: locally |
| 4 | +// (and in tests) it comes from node_modules; in the Auto-label workflow it is |
| 5 | +// installed in isolation and found via NODE_PATH. createRequire honors |
| 6 | +// NODE_PATH, unlike a bare ESM `import`. |
| 7 | +const { minimatch } = createRequire(import.meta.url)('minimatch'); |
| 8 | + |
| 9 | +/** |
| 10 | + * Compute the path-based labels for a pull request. |
| 11 | + * |
| 12 | + * WHY this exists (instead of just using actions/labeler): |
| 13 | + * actions/labeler reads a PR's changed files from GitHub's `pulls.listFiles` |
| 14 | + * API, which makes GitHub generate the full PR diff. That request times out |
| 15 | + * ("Sorry, this diff is taking too long to generate") on PRs that change a |
| 16 | + * lot of binary data — e.g. recompiled PHP.wasm builds — and the action then |
| 17 | + * fails the `paths` job, which is a required check, and blocks the merge. |
| 18 | + * The caller lists changed files with `git diff` (which compares tree hashes |
| 19 | + * and is instant regardless of PR size) and passes them here, so labeling |
| 20 | + * no longer depends on GitHub's diff generation. |
| 21 | + * |
| 22 | + * WHAT is supported: |
| 23 | + * Only the one labeler feature .github/labeler.yml actually uses — |
| 24 | + * |
| 25 | + * <label>: |
| 26 | + * - changed-files: |
| 27 | + * - any-glob-to-any-file: [ ...globs ] |
| 28 | + * |
| 29 | + * i.e. "apply <label> if any glob matches any changed file". Glob matching |
| 30 | + * uses minimatch with `{ dot: true }`, identical to actions/labeler v5, so |
| 31 | + * results match for this config. |
| 32 | + * |
| 33 | + * Every other labeler feature (any-glob-to-all-files, all-globs-to-any-file, |
| 34 | + * all-globs-to-all-files, base-branch, head-branch, or more than one entry |
| 35 | + * under a label) is intentionally NOT implemented. Rather than silently |
| 36 | + * ignore such a rule and mislabel PRs, this throws — so a future labeler.yml |
| 37 | + * change fails loudly and whoever makes it extends this module (and its |
| 38 | + * tests) instead. |
| 39 | + * |
| 40 | + * @param {string[]} changedFiles - Paths changed in the PR. |
| 41 | + * @param {Record<string, unknown>} config - Parsed .github/labeler.yml. |
| 42 | + * @returns {string[]} Labels whose globs match at least one changed file. |
| 43 | + */ |
| 44 | +export function matchPathLabels(changedFiles, config) { |
| 45 | + const labels = []; |
| 46 | + for (const [label, rules] of Object.entries(config ?? {})) { |
| 47 | + const globs = globsForLabel(label, rules); |
| 48 | + const matched = globs.some((glob) => |
| 49 | + changedFiles.some((file) => minimatch(file, glob, { dot: true })) |
| 50 | + ); |
| 51 | + if (matched) { |
| 52 | + labels.push(label); |
| 53 | + } |
| 54 | + } |
| 55 | + return labels; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Extract the `any-glob-to-any-file` globs for one label, rejecting any config |
| 60 | + * shape this module does not implement (see WHAT is supported, above). |
| 61 | + */ |
| 62 | +function globsForLabel(label, rules) { |
| 63 | + if (!Array.isArray(rules) || rules.length !== 1) { |
| 64 | + throw unsupported(label, 'expected exactly one `changed-files` entry'); |
| 65 | + } |
| 66 | + const [rule] = rules; |
| 67 | + const ruleKeys = Object.keys(rule ?? {}); |
| 68 | + if (ruleKeys.length !== 1 || ruleKeys[0] !== 'changed-files') { |
| 69 | + throw unsupported( |
| 70 | + label, |
| 71 | + `only \`changed-files\` is supported, saw ${JSON.stringify(ruleKeys)}` |
| 72 | + ); |
| 73 | + } |
| 74 | + const clauses = rule['changed-files']; |
| 75 | + if (!Array.isArray(clauses)) { |
| 76 | + throw unsupported(label, '`changed-files` must be a list'); |
| 77 | + } |
| 78 | + |
| 79 | + const globs = []; |
| 80 | + for (const clause of clauses) { |
| 81 | + const clauseKeys = Object.keys(clause ?? {}); |
| 82 | + if ( |
| 83 | + clauseKeys.length !== 1 || |
| 84 | + clauseKeys[0] !== 'any-glob-to-any-file' |
| 85 | + ) { |
| 86 | + throw unsupported( |
| 87 | + label, |
| 88 | + `only \`any-glob-to-any-file\` is supported, saw ${JSON.stringify( |
| 89 | + clauseKeys |
| 90 | + )}` |
| 91 | + ); |
| 92 | + } |
| 93 | + const value = clause['any-glob-to-any-file']; |
| 94 | + if (Array.isArray(value)) { |
| 95 | + globs.push(...value); |
| 96 | + } else if (typeof value === 'string') { |
| 97 | + globs.push(value); |
| 98 | + } else { |
| 99 | + throw unsupported( |
| 100 | + label, |
| 101 | + '`any-glob-to-any-file` must be a string or list of globs' |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + return globs; |
| 106 | +} |
| 107 | + |
| 108 | +function unsupported(label, detail) { |
| 109 | + return new Error( |
| 110 | + `Unsupported .github/labeler.yml rule for "${label}": ${detail}. ` + |
| 111 | + 'packages/meta/src/pr-labels/match-path-labels.mjs implements only ' + |
| 112 | + '`changed-files: [{ any-glob-to-any-file: [...] }]`. Extend it and its ' + |
| 113 | + 'tests to support the new rule.' |
| 114 | + ); |
| 115 | +} |
0 commit comments