Skip to content

Commit ef8ad1a

Browse files
ci(risk): Never label PRs touching high-risk files as low risk
Co-Authored-By: Nelson Osacky <nelson.osacky@sentry.io>
1 parent 1943d47 commit ef8ad1a

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎.github/file-filters.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# This is used by the action https://github.com/dorny/paths-filter
22

3+
# pr-risk-jev.yml also reads high_risk_code from the base branch and never labels these files `risk: low`.
4+
# It supports exact paths only, so its job fails if a glob is added here.
35
high_risk_code: &high_risk_code
46
# Transport classes
57
- "sentry/src/main/java/io/sentry/transport/AsyncHttpTransport.java"

‎.github/workflows/pr-risk-jev.yml‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,42 @@ jobs:
7272
core.warning('PR changed after risk analysis; no label was published.');
7373
return;
7474
}
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;
76111
const existing = pr.labels.map(label => label.name);
77112
if (target && !existing.includes(target)) {
78113
try {
@@ -81,8 +116,8 @@ jobs:
81116
if (error.status !== 404) throw error;
82117
try {
83118
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}`,
86121
});
87122
} catch (error) {
88123
if (error.status !== 422) throw error;

0 commit comments

Comments
 (0)