Skip to content

Commit 6e4c9c4

Browse files
authored
fix(ci): harden the PR labeler's title parsing and map recurring scopes (#4666)
## What this PR does Fixes three problems in `.github/workflows/pr-labeler.yaml`. Each one either killed the job or put a PR in the wrong area, and none of them showed up anywhere. - Both maps are now looked up with `Object.hasOwn`. A title typed or scoped `constructor`, `toString`, `hasOwnProperty` or `__proto__` used to resolve to an `Object.prototype` member and crash the step on `l.startsWith`, which left the PR with no labels at all. - Every leading `[Backport ...]` prefix is now stripped, not only the first one. With two prefixes, the second was read as a bracket scope, so the real scope and the `kind/*` label were both lost. The `area/release` label hid the problem by keeping `area/uncategorized` from firing. - Scopes that recur three or more times over the full PR history now map to an area. So does each area's own name (`networking`, `storage`, `database`, `virtualization`, `testing`, `dependencies`). Scope-to-area decisions: - `area/database`: `database`, `etcd-operator`, `opensearch`, `opensearch-operator` - `area/networking`: `networking`, `kubeovn`, `multus`, `kilo`, `hetzner-robotlb`, `ouroboros` (it rewrites the CoreDNS Corefile to fix hairpin NAT, so it is networking, not platform) - `area/virtualization`: `virtualization`, `vm`, `kubevirt-operator`, `kubevirt-cdi`, `kubevirt-instancetypes` - `area/platform`: `operator`, `cozystack-operator`, `controller`, `cozystack-controller`, `cozystack-basics`, `flux-shard-operator`, `migrations`, `scheduler`, `cozystack-scheduler`, `reloader`, `bootbox`, and `lineage`. I put `lineage` here on purpose: no existing label covers it more precisely, and it is too narrow for an area of its own. - `area/storage`: `storage`, `piraeus-operator`, `backupstrategy-controller` - `area/release`: `changelog`, `changelogs` - `area/ci`: `workflows`, `labeler` - `area/dependencies`: `dependencies`, `renovate` - `area/api`: `registry` (the cozystack-api storage layer) - `area/dashboard`: `dashboard-controller` - `area/monitoring`: `monitoring-agents` - `area/keycloak`: `keycloak-configure` - `area/testing`: `testing` - New `area/dx`, added to `.github/labels.yml`: `dx`, `hack`, `cozypkg`. These are developer tooling (hack/ scripts, package Makefiles, cozypkg). `area/build` covers image builds and `area/testing` covers tests, and neither describes this work. Left unmapped on purpose: - `http-cache`: the application was removed from the tree. - `openbao`: already mapped on main. - `apps`, `system`, `docs`: catch-all scopes that don't point to one area. - `fix`, `bugfix`: malformed titles. - `security`, `rbac`, `governance`, `codeowners`, `maintenance`, `images`, `readme`, `telemetry`, `cluster-autoscaler`: no single area fits any of them. I did not add spelling normalisation. `kubeovn` got its own row, since it is the only alternate spelling that recurs. A new contract test, `hack/pr-labeler-contract.bats`, pulls the workflow's own `script:` block out of the file and runs it under node with stubbed `context`, `core` and `github`, so it tests the shipped code, not a copy. On main it is red on the prototype titles (`THROW: l.startsWith is not a function`), on the double backport (only `area/release`), on the newly mapped scopes and on the area-name scopes. It also checks that every map target exists in `labels.yml` and that no key is written twice. The suite fails rather than skips when node is missing. The older node-based suites skip, so this is the first one that needs node on the unit-test runner. Some things stay out of this PR. The scope table in `docs/agents/contributing.md` is a summary. It has no row for `area/dx` and does not mention that an area's own name works as a scope. Separately, the singular `migration` still maps to `area/virtualization` as it did before, while the new `migrations` maps to `area/platform`. A backport PR whose scope is unmapped still never gets `area/uncategorized`, even with a single prefix. The `area/release` added by the strip satisfies the fallback check. This behaviour is the same on main and is not changed here. The test that checks map targets against `labels.yml` passes by itself if the map literal cannot be extracted. The duplicate-key and area-name tests go red in that case, so the suite as a whole still fails. Closes #4125, closes #4124, closes #4122 ### Screenshots Not a UI change. ### Downstream repositories - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: ### Release note ```release-note fix(ci): the PR labeler no longer crashes on titles scoped like Object.prototype members, handles nested backport prefixes, and maps more recurring scopes, including a new area/dx. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added the `area/dx` label for issues and pull requests related to developer tooling. * **Improvements** * Pull requests can now be categorized using a broader set of scope aliases. * Repeated leading backport prefixes are recognized and route the pull request to the release area. * Label mapping now avoids treating inherited property names as valid mappings. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 02fbe76 + 30689f8 commit 6e4c9c4

3 files changed

Lines changed: 257 additions & 6 deletions

File tree

‎.github/labels.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@
190190
color: 'bfd4f2'
191191
description: Dependency bumps (Renovate, go.mod); scope `deps`
192192

193+
- name: area/dx
194+
color: 'bfd4f2'
195+
description: Issues or PRs related to developer tooling (hack/ scripts, package Makefiles, cozypkg)
196+
193197
- name: area/dashboard
194198
color: 'bfd4f2'
195199
description: Issues or PRs related to the dashboard / UI

‎.github/workflows/pr-labeler.yaml‎

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
// area/api
4242
'api': 'area/api',
4343
'cozystack-api': 'area/api',
44+
'registry': 'area/api',
4445
4546
// area/ai
4647
'agents': 'area/ai',
@@ -51,14 +52,24 @@ jobs:
5152
5253
// area/ci
5354
'ci': 'area/ci',
55+
'workflows': 'area/ci',
56+
'labeler': 'area/ci',
5457
5558
// area/dependencies — Renovate uses scope `deps` (see .github/renovate.json)
5659
'deps': 'area/dependencies',
60+
'dependencies': 'area/dependencies',
61+
'renovate': 'area/dependencies',
62+
63+
// area/dx — developer tooling: hack/, package Makefiles, cozypkg
64+
'dx': 'area/dx',
65+
'hack': 'area/dx',
66+
'cozypkg': 'area/dx',
5767
5868
// area/dashboard
5969
'dashboard': 'area/dashboard',
6070
'cozystack-ui': 'area/dashboard',
6171
'console': 'area/dashboard',
72+
'dashboard-controller': 'area/dashboard',
6273
6374
// area/database
6475
'postgres': 'area/database',
@@ -75,6 +86,10 @@ jobs:
7586
'nats': 'area/database',
7687
'qdrant': 'area/database',
7788
'foundationdb': 'area/database',
89+
'database': 'area/database',
90+
'etcd-operator': 'area/database',
91+
'opensearch': 'area/database',
92+
'opensearch-operator': 'area/database',
7893
7994
// area/extra
8095
'extra': 'area/extra',
@@ -84,6 +99,7 @@ jobs:
8499
85100
// area/keycloak
86101
'keycloak': 'area/keycloak',
102+
'keycloak-configure': 'area/keycloak',
87103
88104
// area/kubernetes
89105
'kubernetes': 'area/kubernetes',
@@ -97,6 +113,7 @@ jobs:
97113
'vmstack': 'area/monitoring',
98114
'grafana': 'area/monitoring',
99115
'workloadmonitor': 'area/monitoring',
116+
'monitoring-agents': 'area/monitoring',
100117
101118
// area/networking
102119
'ingress': 'area/networking',
@@ -110,6 +127,12 @@ jobs:
110127
'tcp-balancer': 'area/networking',
111128
'securitygroups': 'area/networking',
112129
'cozy-proxy': 'area/networking',
130+
'networking': 'area/networking',
131+
'kubeovn': 'area/networking',
132+
'multus': 'area/networking',
133+
'kilo': 'area/networking',
134+
'hetzner-robotlb': 'area/networking',
135+
'ouroboros': 'area/networking',
113136
114137
// area/cilium
115138
'cilium': 'area/cilium',
@@ -131,10 +154,24 @@ jobs:
131154
'cozyctl': 'area/platform',
132155
'cozystack-engine': 'area/platform',
133156
'cozy-lib': 'area/platform',
157+
'operator': 'area/platform',
158+
'cozystack-operator': 'area/platform',
159+
'controller': 'area/platform',
160+
'cozystack-controller': 'area/platform',
161+
'cozystack-basics': 'area/platform',
162+
'flux-shard-operator': 'area/platform',
163+
'lineage': 'area/platform',
164+
'migrations': 'area/platform',
165+
'scheduler': 'area/platform',
166+
'cozystack-scheduler': 'area/platform',
167+
'reloader': 'area/platform',
168+
'bootbox': 'area/platform',
134169
135170
// area/release
136171
'backport': 'area/release',
137172
'release': 'area/release',
173+
'changelog': 'area/release',
174+
'changelogs': 'area/release',
138175
139176
// area/storage
140177
'seaweedfs': 'area/storage',
@@ -146,13 +183,17 @@ jobs:
146183
'harbor': 'area/storage',
147184
'backups': 'area/storage',
148185
'csi': 'area/storage',
186+
'storage': 'area/storage',
187+
'piraeus-operator': 'area/storage',
188+
'backupstrategy-controller': 'area/storage',
149189
150190
// area/tenant
151191
'tenant': 'area/tenant',
152192
153193
// area/testing
154194
'tests': 'area/testing',
155195
'e2e': 'area/testing',
196+
'testing': 'area/testing',
156197
157198
// area/virtualization
158199
'kubevirt': 'area/virtualization',
@@ -165,6 +206,11 @@ jobs:
165206
'virtual-machine': 'area/virtualization',
166207
'hami': 'area/virtualization',
167208
'gpu-operator': 'area/virtualization',
209+
'virtualization': 'area/virtualization',
210+
'vm': 'area/virtualization',
211+
'kubevirt-operator': 'area/virtualization',
212+
'kubevirt-cdi': 'area/virtualization',
213+
'kubevirt-instancetypes': 'area/virtualization',
168214
};
169215
170216
const pr = context.payload.pull_request;
@@ -173,10 +219,10 @@ jobs:
173219
const existing = new Set((pr.labels || []).map(l => l.name));
174220
const toAdd = new Set();
175221
176-
// 1. Strip "[Backport release-1.x]" prefix if present.
177-
const backportMatch = title.match(/^\[Backport ([^\]]+)\]\s+(.+)$/);
178-
const cleanTitle = backportMatch ? backportMatch[2] : title;
179-
if (backportMatch) {
222+
// 1. Strip every leading "[Backport release-1.x]" prefix: a backport of
223+
// a backport carries two, and a leftover one would be parsed as the scope.
224+
const cleanTitle = title.replace(/^(?:\[Backport [^\]]+\]\s+)+/, '');
225+
if (cleanTitle !== title) {
180226
toAdd.add('area/release');
181227
}
182228
@@ -202,7 +248,9 @@ jobs:
202248
203249
// 5. Apply kind/* from type.
204250
if (type) {
205-
if (typeToKind[type]) {
251+
// Object.hasOwn: a title typed or scoped `constructor` would otherwise
252+
// resolve to an Object.prototype member instead of to nothing.
253+
if (Object.hasOwn(typeToKind, type)) {
206254
toAdd.add(typeToKind[type]);
207255
} else {
208256
core.warning(`type "${type}" has no kind/* mapping — typo or new type? See .github/workflows/pr-labeler.yaml typeToKind`);
@@ -215,7 +263,7 @@ jobs:
215263
.map(s => s.trim())
216264
.filter(Boolean);
217265
for (const s of scopes) {
218-
if (scopeToArea[s]) {
266+
if (Object.hasOwn(scopeToArea, s)) {
219267
toAdd.add(scopeToArea[s]);
220268
} else {
221269
core.warning(`scope "${s}" has no area/* mapping — consider extending scopeToArea in .github/workflows/pr-labeler.yaml if it recurs`);

‎hack/pr-labeler-contract.bats‎

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#!/usr/bin/env bats
2+
# Contract for the PR title labeler in .github/workflows/pr-labeler.yaml.
3+
#
4+
# Every failure this guards is silent: the job either dies on a title it cannot
5+
# handle, applies a label that does not exist, or lands a PR in the wrong area
6+
# with no warning, and nobody reads the log of a labeler that did not label.
7+
#
8+
# The behavioural tests run the workflow's own github-script block under node,
9+
# extracted from the file rather than mirrored, with `context`, `core` and
10+
# `github` stubbed, so an edit to the script is what gets tested. node is
11+
# required, not optional: a suite that skips when the runtime is missing goes
12+
# green having run nothing.
13+
14+
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
15+
WF="$REPO_ROOT/.github/workflows/pr-labeler.yaml"
16+
LABELS="$REPO_ROOT/.github/labels.yml"
17+
18+
need_node() {
19+
command -v node >/dev/null 2>&1 && return 0
20+
echo "node is required to run the pr-labeler script" >&2
21+
return 1
22+
}
23+
24+
# The `script: |` block, de-indented to plain JavaScript.
25+
extract_script() {
26+
awk 'f { print substr($0, 13) } /^ +script: \|$/ { f = 1 }' "$WF" > "$1"
27+
[ -s "$1" ]
28+
}
29+
30+
# Runs the extracted script once per title read from stdin, all in one node
31+
# process, and prints one line per title: the title, a tab, then the applied
32+
# labels sorted and comma-joined, or THROW: and the error message.
33+
#
34+
# JavaScript closing braces below are indented on purpose: hack/cozytest.sh
35+
# rewrites any line that is exactly `}` into a shell `return 0`.
36+
run_labeler() {
37+
tmp="$(mktemp -d)"
38+
extract_script "$tmp/script.js"
39+
cat > "$tmp/run.js" <<'JS'
40+
const fs = require('fs');
41+
const src = fs.readFileSync(process.argv[2], 'utf8');
42+
const AsyncFunction = (async () => {}).constructor;
43+
const fn = new AsyncFunction('context', 'core', 'github', src);
44+
(async () => {
45+
const out = [];
46+
const titles = fs.readFileSync(0, 'utf8').split('\n').filter(Boolean);
47+
for (const title of titles) {
48+
let applied = [];
49+
const context = {
50+
repo: { owner: 'o', repo: 'r' },
51+
payload: { pull_request: { number: 1, title, body: '', labels: [] } },
52+
};
53+
const core = { warning() {}, info() {} };
54+
const github = { rest: { issues: { addLabels: async (a) => { applied = a.labels; } } } };
55+
try {
56+
await fn(context, core, github);
57+
out.push(title + '\t' + [...applied].sort().join(','));
58+
} catch (e) {
59+
out.push(title + '\tTHROW: ' + e.message);
60+
}
61+
}
62+
fs.writeFileSync(1, out.join('\n') + '\n');
63+
})();
64+
JS
65+
node "$tmp/run.js" "$tmp/script.js"
66+
rm -rf "$tmp"
67+
}
68+
69+
# Asserts the labeler's output for each `title<TAB>labels` line on stdin.
70+
expect_labels() {
71+
want="$(cat)"
72+
got="$(printf '%s\n' "$want" | cut -f1 | run_labeler)"
73+
[ "$got" = "$want" ] || {
74+
echo "labeler output mismatch." >&2
75+
echo "want:" >&2; printf '%s\n' "$want" | sed 's/^/ /' >&2
76+
echo "got:" >&2; printf '%s\n' "$got" | sed 's/^/ /' >&2
77+
return 1
78+
}
79+
}
80+
81+
# The scopeToArea literal as JSON pairs, plus a count of the key lines in the
82+
# source, quoted either way or bare. A duplicate key collapses when the literal
83+
# is evaluated, so the two disagree when a key is written twice, as long as each
84+
# key sits on its own line.
85+
scope_map() { # json|keylines
86+
tmp="$(mktemp -d)"
87+
extract_script "$tmp/script.js"
88+
cat > "$tmp/map.js" <<'JS'
89+
const fs = require('fs');
90+
const src = fs.readFileSync(process.argv[2], 'utf8');
91+
const m = src.match(/const scopeToArea = (\{[\s\S]*?\n\};)/);
92+
if (!m) { throw new Error('scopeToArea literal not found'); }
93+
const lines = m[1].split('\n').filter((l) => /^\s*(['"]?)[^'"\s:]+\1\s*:/.test(l));
94+
const map = new Function('return ' + m[1].replace(/;$/, ''))();
95+
if (process.argv[3] === 'keylines') {
96+
fs.writeFileSync(1, lines.length + ' ' + Object.keys(map).length + '\n');
97+
} else {
98+
fs.writeFileSync(1, Object.entries(map).map(([k, v]) => k + '\t' + v).join('\n') + '\n');
99+
}
100+
JS
101+
node "$tmp/map.js" "$tmp/script.js" "$1"
102+
rm -rf "$tmp"
103+
}
104+
105+
area_labels() {
106+
sed -n 's/^- name: \(area\/[^[:space:]]*\)[[:space:]]*$/\1/p' "$LABELS"
107+
}
108+
109+
@test "the workflow under contract exists" {
110+
[ -f "$WF" ]
111+
[ -f "$LABELS" ]
112+
}
113+
114+
# A key inherited from Object.prototype resolves to a function, which is truthy,
115+
# lands in the label set, and then kills the job on `l.startsWith`.
116+
@test "a scope or type named after an Object.prototype member does not kill the job" {
117+
need_node
118+
expect_labels <<'EOF'
119+
fix(constructor): x area/uncategorized,kind/bug
120+
fix(toString): x area/uncategorized,kind/bug
121+
fix(hasOwnProperty): x area/uncategorized,kind/bug
122+
fix(__proto__): x area/uncategorized,kind/bug
123+
[constructor] x area/uncategorized
124+
constructor: x area/uncategorized
125+
EOF
126+
}
127+
128+
# One stripped prefix left the second one to be parsed as a bracket scope, which
129+
# dropped the real scope and the kind, and the area/release from the first strip
130+
# suppressed the uncategorized signal.
131+
@test "every leading backport prefix is stripped before the title is parsed" {
132+
need_node
133+
expect_labels <<'EOF'
134+
[Backport release-1.3] fix(linstor): x area/release,area/storage,kind/bug
135+
[Backport release-1.2] [Backport release-1.3] fix(linstor): x area/release,area/storage,kind/bug
136+
[Backport release-1.2] [Backport release-1.3] [Backport release-1.4] feat(linstor): x area/release,area/storage,kind/feature
137+
EOF
138+
}
139+
140+
@test "an unmapped scope still falls back to area/uncategorized" {
141+
need_node
142+
expect_labels <<'EOF'
143+
fix(no-such-scope): x area/uncategorized,kind/bug
144+
fix: x area/uncategorized,kind/bug
145+
just a sentence area/uncategorized
146+
EOF
147+
}
148+
149+
@test "recurring scopes resolve to their area" {
150+
need_node
151+
expect_labels <<'EOF'
152+
fix(etcd-operator): x area/database,kind/bug
153+
fix(opensearch): x area/database,kind/bug
154+
fix(multus): x area/networking,kind/bug
155+
fix(kubeovn): x area/networking,kind/bug
156+
fix(kubevirt-operator): x area/virtualization,kind/bug
157+
fix(keycloak-configure): x area/keycloak,kind/bug
158+
docs(changelog): x area/release,kind/documentation
159+
fix(lineage): x area/platform,kind/bug
160+
fix(piraeus-operator): x area/storage,kind/bug
161+
fix(monitoring-agents): x area/monitoring,kind/bug
162+
ci(workflows): x area/ci
163+
fix(hack): x area/dx,kind/bug
164+
EOF
165+
}
166+
167+
# Contributors reach for the subsystem name as often as for a package name, so
168+
# every area is reachable by its own name.
169+
@test "every area label is accepted as a scope that maps to itself" {
170+
need_node
171+
map="$(scope_map json)"
172+
missing=""
173+
for a in $(area_labels); do
174+
[ "$a" = "area/uncategorized" ] && continue
175+
printf '%s\n' "$map" | grep -qxF "${a#area/} $a" || missing="$missing ${a#area/}"
176+
done
177+
[ -z "$missing" ] || { echo "scopes missing for their own area:$missing" >&2; return 1; }
178+
}
179+
180+
# A target missing from labels.yml is a label the sync workflow never creates,
181+
# and nothing in a run of the labeler reports it.
182+
@test "every scopeToArea target is an area label defined in labels.yml" {
183+
need_node
184+
defined="$(area_labels)"
185+
[ -n "$defined" ]
186+
bad="$(scope_map json | cut -f2 | sort -u | grep -vxF "$defined" || true)"
187+
[ -z "$bad" ] || { echo "targets not in labels.yml: $bad" >&2; return 1; }
188+
}
189+
190+
# A repeated key silently overrides the earlier value and reads as an ordinary
191+
# added line in review.
192+
@test "no scope key appears twice in scopeToArea" {
193+
need_node
194+
read -r lines keys <<EOF
195+
$(scope_map keylines)
196+
EOF
197+
[ "$lines" -gt 0 ]
198+
[ "$lines" = "$keys" ] || { echo "$lines key lines but $keys distinct keys" >&2; return 1; }
199+
}

0 commit comments

Comments
 (0)