Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Commit cd25edf

Browse files
authored
fix(ci): tolerate null PR fields and honour existing area/* in pr-labeler (#17)
Two correctness bugs surfaced after the initial labeler landed: 1. Null inputs. computeLabels destructured title/body/existingLabels with defaults, but destructuring defaults only fire on undefined. GitHub PR payloads carry an explicit null for an empty description, which would crash body.match() at runtime. Switched to explicit || fallbacks on rawTitle/rawBody/rawExisting so null on any of the three is treated as the empty value. 2. hasArea ignored existing area/* labels. A maintainer-added area/* followed by a scopeless retitle would re-add area/uncategorized on top of the real area, leaving the PR with both labels until the next edit cycle cleared the fallback. Now hasArea checks the new derivation AND existing labels (excluding area/uncategorized itself so a scopeless retitle of an only-uncategorized PR still reaches the fallback path). Five new tests pin the behaviour: null body, null title, null existingLabels, scopeless retitle with maintainer-added area/* (no fallback added, stale fallback removed), and the edge case where the only existing area is area/uncategorized (fallback is not frozen in place). Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la>
1 parent 92ff040 commit cd25edf

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

‎.github/scripts/pr-labeler.js‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,13 @@ export const scopeToArea = {
127127
//
128128
// Anything else (maintainer-added `area/*`, manual `kind/*`, anything
129129
// outside these two namespaces) is preserved on every run.
130-
export function computeLabels({ title = '', body = '', existingLabels = [] } = {}) {
131-
const existing = new Set(existingLabels)
130+
export function computeLabels({ title: rawTitle, body: rawBody, existingLabels: rawExisting } = {}) {
131+
// Destructuring defaults only fire on `undefined`, not `null`. GitHub PR
132+
// payloads can carry an explicit `null` for an empty body and (less often)
133+
// labels — fall back via `||` so `.match()` and Set iteration are safe.
134+
const title = rawTitle || ''
135+
const body = rawBody || ''
136+
const existing = new Set(rawExisting || [])
132137
const toAdd = new Set()
133138

134139
// 1. Try Conventional Commits form: type(scope)?(!)?: description
@@ -186,7 +191,12 @@ export function computeLabels({ title = '', body = '', existingLabels = [] } = {
186191
}
187192

188193
// 7. Fallback: no area/* applied -> area/uncategorized.
189-
const hasArea = [...toAdd].some((l) => l.startsWith('area/'))
194+
// Honour maintainer-added area/* already on the PR — if the title has no
195+
// scope but a real area/* is already attached, the PR is categorised; do
196+
// not pile area/uncategorized on top.
197+
const hasArea =
198+
[...toAdd].some((l) => l.startsWith('area/')) ||
199+
[...existing].some((l) => l.startsWith('area/') && l !== 'area/uncategorized')
190200
if (!hasArea) {
191201
toAdd.add('area/uncategorized')
192202
}

‎.github/scripts/pr-labeler.test.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,51 @@ test('non-labeler labels are never removed', () => {
160160
})
161161
assert.deepEqual(remove, [])
162162
})
163+
164+
// ── Null-safety: destructuring defaults only fire on undefined ──
165+
166+
test('null body does not throw', () => {
167+
// GitHub PR payloads carry an explicit `null` for an empty description.
168+
const { add } = computeLabels({ title: 'feat(ui): x', body: null })
169+
assert.ok(add.includes('kind/feature'))
170+
assert.ok(add.includes('area/ui'))
171+
})
172+
173+
test('null title is treated as empty', () => {
174+
const { add } = computeLabels({ title: null })
175+
assert.ok(add.includes('area/uncategorized'))
176+
})
177+
178+
test('null existingLabels does not throw', () => {
179+
const { add } = computeLabels({ title: 'feat(ui): x', existingLabels: null })
180+
assert.ok(add.includes('area/ui'))
181+
})
182+
183+
// ── hasArea honours existing maintainer-added area/* ──
184+
185+
test('retitle to scopeless when maintainer-added area/* exists: no area/uncategorized', () => {
186+
// Maintainer manually attached area/forms. PR title is later retitled to a
187+
// scopeless form. The labeler must not pile area/uncategorized on top of
188+
// the already-categorised state, and must strip it if it was there from
189+
// an earlier run.
190+
const { add, remove } = computeLabels({
191+
title: 'chore: housekeeping',
192+
existingLabels: ['area/forms', 'area/uncategorized'],
193+
})
194+
assert.ok(!add.includes('area/uncategorized'))
195+
assert.ok(remove.includes('area/uncategorized'))
196+
})
197+
198+
test('existing only has area/uncategorized: scopeless title keeps area/uncategorized (no churn)', () => {
199+
// The "honour existing area/*" rule must not count area/uncategorized as
200+
// a real categorisation — otherwise a scopeless retitle would silently
201+
// freeze the fallback in place. Expected net effect: fallback path is
202+
// taken, area/uncategorized stays put (already-present so not re-added,
203+
// and hasArea is false so not removed), kind/cleanup derives from chore.
204+
const { add, remove } = computeLabels({
205+
title: 'chore: housekeeping',
206+
existingLabels: ['area/uncategorized'],
207+
})
208+
assert.deepEqual(add, ['kind/cleanup'])
209+
assert.deepEqual(remove, [])
210+
})

0 commit comments

Comments
 (0)