Skip to content

Commit 137bf67

Browse files
committed
fix: harden labeler generator against silent data loss
- readDir now fails loudly instead of returning [] on error, so a missing/renamed primary input dir aborts instead of silently dropping product rules and overwriting labeler.yml - refuse to write labeler.yml when no rules were generated - merge special rule globs with any existing rule instead of overwriting, so a future docs dir named changelog/email-routing wouldn't silently replace the special rule Regenerating also picks up src/assets/images/sandbox/** for product:sandbox (previously missed due to a stale working tree).
1 parent 62f66bf commit 137bf67

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

.github/labeler.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ product:sandbox:
649649
- changed-files:
650650
- any-glob-to-any-file:
651651
- src/content/docs/sandbox/**
652+
- src/assets/images/sandbox/**
652653
- src/content/changelog/sandbox/**
653654
- src/assets/images/changelog/sandbox/**
654655

bin/generate-labeler.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,9 @@ function firstUrlSegment(url: string): string {
4949
}
5050

5151
async function readDir(pathname: string): Promise<string[]> {
52-
try {
53-
return (
54-
await readdir(path.join(rootDir, pathname), { withFileTypes: true })
55-
)
56-
.filter((entry) => entry.isDirectory())
57-
.map((entry) => entry.name);
58-
} catch {
59-
return [];
60-
}
52+
return (await readdir(path.join(rootDir, pathname), { withFileTypes: true }))
53+
.filter((entry) => entry.isDirectory())
54+
.map((entry) => entry.name);
6155
}
6256

6357
async function loadYaml(pathname: string): Promise<Record<string, unknown>> {
@@ -147,7 +141,7 @@ async function main() {
147141
}
148142

149143
for (const [label, globs] of Object.entries(specialRules)) {
150-
rules[label] = [globs];
144+
rules[label] = [globs, ...(rules[label] ?? [])];
151145
}
152146

153147
const sortedRules: Record<string, string[][]> = {};
@@ -186,6 +180,13 @@ async function main() {
186180
.split("\n")
187181
.map((line) => (line && /^[^\s#-]/.test(line) ? `\n${line}` : line))
188182
.join("\n");
183+
184+
if (Object.keys(sortedRules).length === 0) {
185+
throw new Error(
186+
"No label rules generated; refusing to overwrite .github/labeler.yml",
187+
);
188+
}
189+
189190
await writeFile(path.join(rootDir, ".github/labeler.yml"), header + labeler, {
190191
encoding: "utf-8",
191192
});

0 commit comments

Comments
 (0)