Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

ci: label PRs by risk and size, and protect the decision axes (#694)

Tássio Noronhabba558167712
diff --git a/.github/workflows/label-guard.yml b/.github/workflows/label-guard.yml
new file mode 100644
index 00000000..5bd69192
--- /dev/null
+++ b/.github/workflows/label-guard.yml
@@ -0,0 +1,58 @@
+name: label guard
+
+# GitHub has no per-label ACL: anyone with triage can apply any label. This
+# restores the previous state whenever someone outside the list changes one of
+# the decision axes. It is after the fact, not prevention: the label stays
+# applied for a few seconds until the job runs.
+
+on:
+ issues:
+ types: [labeled, unlabeled]
+ # pull_request_target so the token can write on fork PRs too. Safe here
+ # because this job never checks out PR code.
+ pull_request_target:
+ types: [labeled, unlabeled]
+
+permissions:
+ issues: write
+ pull-requests: write
+
+jobs:
+ revert:
+ if: >-
+ startsWith(github.event.label.name, 'prio:') ||
+ startsWith(github.event.label.name, 'risk:') ||
+ startsWith(github.event.label.name, 'roadmap:')
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/github-script@v7
+ with:
+ script: |
+ const OWNERS = ['tassionoronha']; // who decides the axes
+
+ const actor = context.payload.sender.login;
+ // Without this it is an infinite loop: the revert itself fires
+ // `unlabeled`, which would trigger another revert.
+ if (actor === 'github-actions[bot]' || OWNERS.includes(actor)) return;
+
+ const target = context.payload.issue || context.payload.pull_request;
+ const label = context.payload.label.name;
+ const repo = context.repo;
+ const n = target.number;
+
+ // Tolerate the label already being gone: two people editing at
+ // once should not turn this into a red X on the issue.
+ if (context.payload.action === 'labeled') {
+ await github.rest.issues.removeLabel(
+ { ...repo, issue_number: n, name: label }).catch(() => {});
+ } else {
+ await github.rest.issues.addLabels(
+ { ...repo, issue_number: n, labels: [label] }).catch(() => {});
+ }
+
+ await github.rest.issues.createComment({
+ ...repo, issue_number: n,
+ body: `Reverted \`${label}\`. The **prio**, **risk** and **roadmap** ` +
+ `axes are set during triage only, so that the priority shown here ` +
+ `always matches the published criteria. See PRIORITIZATION.md.`
+ });
diff --git a/.github/workflows/pr-labels.yml b/.github/workflows/pr-labels.yml
new file mode 100644
index 00000000..48a9d051
--- /dev/null
+++ b/.github/workflows/pr-labels.yml
@@ -0,0 +1,145 @@
+name: pr labels
+
+# Two axes:
+# risk: what the change reaches -> decides approvers and release window
+# size: what the review costs -> decides whether it fits the time you have
+#
+# Risk has two sources and takes the higher of the two. Triage read the code
+# and reasoned about the whole change; the path rules only see file names.
+# Neither dominates: the paths catch an implementation that turned out to need
+# a migration, and triage catches a risk no file name can show. Size has one
+# source, the diff, because it is arithmetic.
+#
+# They are deliberately independent: a 20-line migration is size S with
+# risk high, and 600 lines of translations is size M with risk low.
+#
+# pull_request_target is used to get a write token on fork PRs. It is safe
+# here because this job NEVER checks out PR code: it only reads the file list.
+
+on:
+ pull_request_target:
+ types: [opened, synchronize, reopened, ready_for_review]
+
+permissions:
+ contents: read
+ # Both: labels on a PR go through the issues endpoint, and which permission
+ # GitHub checks depends on the target type. Granting one is a coin flip.
+ issues: write
+ pull-requests: write
+
+jobs:
+ label:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/github-script@v7
+ with:
+ script: |
+ // --- risk: a direct translation of the label descriptions ------
+ const RISK_RULES = [
+ [/^backend\/alembic\/versions\//, 'high'],
+ [/^backend\/app\/models\//, 'high'],
+ [/^backend\/app\/core\/(auth|auth_policy|webauthn)/, 'high'],
+ [/^backend\/app\/api\/[^/]*auth/, 'high'],
+ [/^backend\/app\/fiscal\//, 'high'],
+ [/^backend\/app\/(api|services|providers|schemas)\//, 'medium'],
+ [/^charts\//, 'medium'],
+ [/^(install\.sh|docker-compose.*\.yml)$/, 'medium'],
+ [/^frontend\/(Dockerfile|default\.conf\.template)$/, 'medium'],
+ ];
+
+ // --- size: thresholds taken from this repo's own distribution ---
+ // The median PR here is ~280 lines, so the common 9/29/99/499
+ // scale would put 80% of open PRs into two buckets and decide
+ // nothing. These cuts split the queue 9 / 20 / 10.
+ const SMALL_MAX = 150, MEDIUM_MAX = 700;
+
+ // A generated file is not review work: a dependency bump is two
+ // thousand lines of lockfile and nothing to read. Counting raw
+ // would mark the most trivial PR in the repo as the largest.
+ const GENERATED = [
+ /(^|\/)package-lock\.json$/, /(^|\/)pnpm-lock\.yaml$/,
+ /(^|\/)yarn\.lock$/, /(^|\/)poetry\.lock$/, /(^|\/)uv\.lock$/,
+ /(^|\/)Cargo\.lock$/, /(^|\/)composer\.lock$/, /\.lock$/,
+ /(^|\/)go\.sum$/, /\.min\.(js|css)$/, /(^|\/)node_modules\//,
+ /(^|\/)dist\//, /\.snap$/,
+ ];
+ const isGenerated = (p) => GENERATED.some((re) => re.test(p));
+
+ const ORDER = ['low', 'medium', 'high'];
+ const repo = context.repo;
+ const pr = context.payload.pull_request;
+ const files = await github.paginate(github.rest.pulls.listFiles,
+ { ...repo, pull_number: pr.number, per_page: 100 });
+
+ let pathLevel = 'low', pathFile = null, lines = 0, skipped = 0;
+ for (const f of files) {
+ const n = (f.additions || 0) + (f.deletions || 0);
+ if (isGenerated(f.filename)) { skipped += n; continue; }
+ lines += n;
+ for (const [re, lvl] of RISK_RULES) {
+ if (re.test(f.filename) && ORDER.indexOf(lvl) > ORDER.indexOf(pathLevel)) {
+ pathLevel = lvl; pathFile = f.filename;
+ }
+ }
+ }
+ const size = lines <= SMALL_MAX ? 'S' : (lines <= MEDIUM_MAX ? 'M' : 'L');
+
+ // Risk already decided on the linked issue, if there is one.
+ let issueLevel = null, issueRef = null;
+ const LINKED = `query($o:String!,$n:String!,$p:Int!){
+ repository(owner:$o,name:$n){ pullRequest(number:$p){
+ closingIssuesReferences(first:5){
+ nodes{ number labels(first:30){ nodes{ name } } } } } } }`;
+ try {
+ const d = await github.graphql(LINKED,
+ { o: repo.owner, n: repo.repo, p: pr.number });
+ for (const iss of d.repository.pullRequest.closingIssuesReferences.nodes) {
+ const found = iss.labels.nodes.map((l) => l.name)
+ .find((n) => n.startsWith('risk: '));
+ if (!found) continue;
+ const lvl = found.slice(6);
+ if (ORDER.indexOf(lvl) > ORDER.indexOf(issueLevel || 'low')) {
+ issueLevel = lvl; issueRef = iss.number;
+ }
+ }
+ } catch (e) {
+ // No linked issue, or the query failed: the paths decide alone.
+ }
+
+ const fromIssue = issueLevel
+ && ORDER.indexOf(issueLevel) > ORDER.indexOf(pathLevel);
+ const level = fromIssue ? issueLevel : pathLevel;
+
+ const current = pr.labels.map((l) => l.name);
+ const swap = async (prefix, target, escalateOnly) => {
+ const existing = current.filter((n) => n.startsWith(prefix));
+ if (escalateOnly && existing.length === 1) {
+ // Automation may RAISE risk, never lower it: if a reviewer set
+ // high by hand, they saw something a file path cannot show.
+ const now = existing[0].slice(prefix.length);
+ if (ORDER.indexOf(level) <= ORDER.indexOf(now)) return false;
+ }
+ if (existing.length === 1 && existing[0] === target) return false;
+ for (const old of existing) {
+ await github.rest.issues.removeLabel(
+ { ...repo, issue_number: pr.number, name: old }).catch(() => {});
+ }
+ await github.rest.issues.addLabels(
+ { ...repo, issue_number: pr.number, labels: [target] });
+ return true;
+ };
+
+ const riskChanged = await swap('risk: ', `risk: ${level}`, true);
+ await swap('size: ', `size: ${size}`, false);
+
+ if (!riskChanged || level === 'low') return;
+
+ const requires = level === 'high'
+ ? 'It needs two approvals, an agreed design on the issue, and a rollback plan.'
+ : 'It needs a review from the area owner and tests on the changed path.';
+ const note = skipped
+ ? ` (${skipped} lines in generated files were not counted for size.)` : '';
+ await github.rest.issues.createComment({ ...repo, issue_number: pr.number,
+ body: `Labelled \`risk: ${level}\` and \`size: ${size}\` automatically: ` +
+ `this PR touches \`${trigger}\` and changes ${lines} reviewable lines. ` +
+ `${requires}${note}` });