|
72 | 72 | core.warning('PR changed after risk analysis; no label was published.'); |
73 | 73 | return; |
74 | 74 | } |
75 | | - const target = result.status === 'ok' ? `risk: ${result.risk_label}` : null; |
| 75 | + let riskLabel = result.risk_label; |
| 76 | + if (result.status === 'ok' && riskLabel === 'low') { |
| 77 | + // Files in the high_risk_code filter, which also drives the "Changes In High Risk Code" |
| 78 | + // comment, are never low. Read the filter from the base revision so a PR cannot edit it. |
| 79 | + const { data: filterFile } = await github.rest.repos.getContent({ |
| 80 | + ...repo, path: '.github/file-filters.yml', ref: pr.base.sha, |
| 81 | + }); |
| 82 | + const filterLines = Buffer.from(filterFile.content, 'base64').toString('utf8').split('\n'); |
| 83 | + const start = filterLines.findIndex(line => /^high_risk_code:/.test(line)); |
| 84 | + if (start < 0) throw new Error('.github/file-filters.yml has no high_risk_code filter.'); |
| 85 | + const highRiskFiles = new Set(); |
| 86 | + for (const line of filterLines.slice(start + 1)) { |
| 87 | + if (/^\S/.test(line)) break; |
| 88 | + const entry = line.match(/^\s+-\s+"([^"]+)"\s*$/); |
| 89 | + if (!entry) { |
| 90 | + if (line.trim() && !line.trim().startsWith('#')) { |
| 91 | + throw new Error(`Unsupported high_risk_code entry: ${line.trim()}`); |
| 92 | + } |
| 93 | + continue; |
| 94 | + } |
| 95 | + // Exact paths only: fail loudly rather than silently miss a glob added later. |
| 96 | + if (/[*?[\]{}!]/.test(entry[1])) throw new Error(`Globs are not supported here: ${entry[1]}`); |
| 97 | + highRiskFiles.add(entry[1]); |
| 98 | + } |
| 99 | + const changedFiles = await github.paginate(github.rest.pulls.listFiles, { |
| 100 | + ...repo, pull_number: prNumber, per_page: 100, |
| 101 | + }); |
| 102 | + const touched = changedFiles |
| 103 | + .flatMap(file => [file.filename, file.previous_filename]) |
| 104 | + .filter(name => name && highRiskFiles.has(name)); |
| 105 | + if (touched.length > 0) { |
| 106 | + core.notice(`Raised risk: low to risk: medium for high-risk files: ${touched.join(', ')}`); |
| 107 | + riskLabel = 'medium'; |
| 108 | + } |
| 109 | + } |
| 110 | + const target = result.status === 'ok' ? `risk: ${riskLabel}` : null; |
76 | 111 | const existing = pr.labels.map(label => label.name); |
77 | 112 | if (target && !existing.includes(target)) { |
78 | 113 | try { |
|
81 | 116 | if (error.status !== 404) throw error; |
82 | 117 | try { |
83 | 118 | await github.rest.issues.createLabel({ |
84 | | - ...repo, name: target, color: colors[result.risk_label], |
85 | | - description: `PR risk score: ${result.risk_label}`, |
| 119 | + ...repo, name: target, color: colors[riskLabel], |
| 120 | + description: `PR risk score: ${riskLabel}`, |
86 | 121 | }); |
87 | 122 | } catch (error) { |
88 | 123 | if (error.status !== 422) throw error; |
|
0 commit comments