Repository navigation
Expand file tree
/
Copy pathvalidate-issue-fields.cjs
More file actions
373 lines (337 loc) · 10.6 KB
/
Copy pathvalidate-issue-fields.cjs
File metadata and controls
373 lines (337 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env node
/* eslint-disable no-console */
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const ROOT = process.cwd();
const CONFIG_PATH = path.join(ROOT, ".github/issue-fields.yml");
const LABELS_PATH = path.join(ROOT, ".github/labels.yml");
const ISSUE_TYPES_PATH = path.join(ROOT, ".github/issue-types.yml");
const DOC_PATH = path.join(ROOT, "docs/ISSUE_FIELDS.md");
function fail(msg) {
console.error(`❌ ${msg}`);
process.exitCode = 1;
}
function ok(msg) {
console.log(`✅ ${msg}`);
}
function readFileSafe(p) {
if (!fs.existsSync(p)) {
fail(`Missing required file: ${path.relative(ROOT, p)}`);
return null;
}
return fs.readFileSync(p, "utf8");
}
function parseYaml(raw, sourceLabel) {
try {
return yaml.load(raw);
} catch (err) {
fail(`Invalid YAML in ${sourceLabel}: ${err.message}`);
return null;
}
}
function collectKeys(obj) {
return Object.keys(obj || {});
}
function collectLabelNames(labelsCfg) {
if (!Array.isArray(labelsCfg)) {
fail(
".github/labels.yml must contain a top-level array of label definitions",
);
return new Set();
}
return new Set(labelsCfg.map((label) => label?.name).filter(Boolean));
}
function collectIssueTypeLabels(issueTypesCfg) {
const issueTypes = issueTypesCfg?.issue_types;
if (!Array.isArray(issueTypes)) {
fail(".github/issue-types.yml must contain an issue_types array");
return new Set();
}
return new Set(
issueTypes.map((issueType) => issueType?.label).filter(Boolean),
);
}
function reportMissingFromSet(label, values, expectedSet) {
const missing = values.filter((value) => !expectedSet.has(value));
if (missing.length > 0) {
fail(
`${label} missing canonical entries (${missing.length}): ${missing.join(", ")}`,
);
}
}
function main() {
const cfgRaw = readFileSafe(CONFIG_PATH);
const labelsRaw = readFileSafe(LABELS_PATH);
const issueTypesRaw = readFileSafe(ISSUE_TYPES_PATH);
const docRaw = readFileSafe(DOC_PATH);
if (!cfgRaw || !labelsRaw || !issueTypesRaw || !docRaw) return;
const cfg = parseYaml(cfgRaw, ".github/issue-fields.yml");
const labelsCfg = parseYaml(labelsRaw, ".github/labels.yml");
const issueTypesCfg = parseYaml(issueTypesRaw, ".github/issue-types.yml");
if (!cfg || !labelsCfg || !issueTypesCfg) return;
const labelNames = collectLabelNames(labelsCfg);
const issueTypeLabels = collectIssueTypeLabels(issueTypesCfg);
const requiredTopLevel = [
"version",
"description",
"organization",
"defaults",
"project_field_mappings",
"organization_issue_fields",
"project_fields",
"profiles",
];
for (const key of requiredTopLevel) {
if (!(key in cfg))
fail(`Missing top-level key in issue-fields config: ${key}`);
}
if (cfg.version !== 2)
fail(`Expected issue-fields version 2, found: ${cfg.version}`);
const issueDefaults = cfg.defaults?.issue || {};
const prDefaults = cfg.defaults?.pull_request || {};
const requiredIssueDefaults = [
"assignee",
"status_label_open",
"status_label_closed",
"priority_label",
"type_label",
];
const requiredPrDefaults = [
"status_label_open",
"status_label_merged",
"priority_label",
"type_label",
];
for (const key of requiredIssueDefaults) {
if (!issueDefaults[key]) fail(`Missing defaults.issue.${key}`);
}
for (const key of requiredPrDefaults) {
if (!prDefaults[key]) fail(`Missing defaults.pull_request.${key}`);
}
const fieldMappings = cfg.project_field_mappings || {};
for (const field of ["Status", "Priority", "Type"]) {
if (!fieldMappings[field] || typeof fieldMappings[field] !== "object") {
fail(`Missing or invalid project_field_mappings.${field}`);
}
}
reportMissingFromSet(
"project_field_mappings.Status",
collectKeys(fieldMappings.Status),
labelNames,
);
reportMissingFromSet(
"project_field_mappings.Priority",
collectKeys(fieldMappings.Priority),
labelNames,
);
const mappedTypeLabels = collectKeys(fieldMappings.Type);
reportMissingFromSet(
"project_field_mappings.Type vs labels",
mappedTypeLabels,
labelNames,
);
reportMissingFromSet(
"project_field_mappings.Type vs issue-types",
mappedTypeLabels,
issueTypeLabels,
);
const missingTypeMappings = [...issueTypeLabels].filter(
(label) => !fieldMappings.Type?.[label],
);
if (missingTypeMappings.length > 0) {
fail(
`Canonical issue types missing Type projection mappings (${missingTypeMappings.length}): ${missingTypeMappings.join(", ")}`,
);
}
const orgFields = cfg.organization_issue_fields || {};
if (
!Array.isArray(orgFields.enabled_issue_types) ||
orgFields.enabled_issue_types.length === 0
) {
fail(
"organization_issue_fields.enabled_issue_types must be a non-empty array",
);
}
if (
!Array.isArray(orgFields.pinned_per_issue_type) ||
orgFields.pinned_per_issue_type.length === 0
) {
fail(
"organization_issue_fields.pinned_per_issue_type must be a non-empty array",
);
}
if (orgFields.pinned_per_issue_type.length > 10) {
fail(
"organization_issue_fields.pinned_per_issue_type exceeds GitHub limit (10)",
);
}
const policy = orgFields.policy || {};
if (policy.max_pinned_fields_per_issue_type !== 10) {
fail(
"organization_issue_fields.policy.max_pinned_fields_per_issue_type must be 10",
);
}
if (policy.max_issue_fields_per_org !== 25) {
fail(
"organization_issue_fields.policy.max_issue_fields_per_org must be 25",
);
}
if (policy.single_select_max_options !== 50) {
fail(
"organization_issue_fields.policy.single_select_max_options must be 50",
);
}
if (policy.project_total_field_limit !== 50) {
fail(
"organization_issue_fields.policy.project_total_field_limit must be 50",
);
}
const customFields = orgFields.custom_fields;
if (!Array.isArray(customFields) || customFields.length === 0) {
fail("organization_issue_fields.custom_fields must be a non-empty array");
} else {
const requiredCustomKeys = [
"Domain",
"Delivery Track",
"Team",
"Effort",
"Start date",
"Target date",
"Risk",
"Customer Impact",
"Technical Impact",
"Spec Link",
];
const keySet = new Set(customFields.map((f) => f.key));
for (const key of requiredCustomKeys) {
if (!keySet.has(key))
fail(`Missing required custom field definition: ${key}`);
}
const validTypes = new Set(["single_select", "date", "text"]);
for (const field of customFields) {
if (!field.key || typeof field.key !== "string")
fail("Each custom field requires a string key");
if (!field.type || !validTypes.has(field.type)) {
fail(
`Custom field "${field.key || "unknown"}" has invalid type "${field.type}"`,
);
}
if (field.type === "single_select") {
if (!Array.isArray(field.options) || field.options.length === 0) {
fail(
`Single select custom field "${field.key}" must include options`,
);
}
if (field.options.length > 50) {
fail(`Single select custom field "${field.key}" exceeds 50 options`);
}
}
}
}
const projectFields = cfg.project_fields || {};
const hiddenIssues = projectFields.hidden_fields_to_enable?.issues || [];
for (const requiredHidden of [
"Parent issue",
"Sub-issue progress",
"Linked pull requests",
"Reviewers",
"Type",
]) {
if (!hiddenIssues.includes(requiredHidden)) {
fail(
`project_fields.hidden_fields_to_enable.issues missing "${requiredHidden}"`,
);
}
}
const iteration = projectFields.iteration_field || {};
if (iteration.key !== "Sprint")
fail('project_fields.iteration_field.key must be "Sprint"');
if (iteration.type !== "iteration")
fail('project_fields.iteration_field.type must be "iteration"');
if (iteration.duration_weeks !== 2)
fail("project_fields.iteration_field.duration_weeks must be 2");
if (iteration.auto_create_new_iterations !== true) {
fail(
"project_fields.iteration_field.auto_create_new_iterations must be true",
);
}
const docMustContain = [
".github/issue-fields.yml",
".github/workflows/project-meta-sync.yml",
"dotgithub",
"wordpress_block_theme",
"wordpress_block_plugin",
"Parent issue",
"Sub-issue progress",
"Linked pull requests",
"Reviewers",
"Type",
"Sprint",
"single_select",
"date",
"text",
];
const effortField = customFields.find((field) => field.key === "Effort");
if (!effortField) {
fail("Missing required custom field definition: Effort");
} else {
if (effortField.type !== "single_select") {
fail('Effort custom field must use type "single_select"');
}
const expectedEffort = ["XS", "S", "M", "L", "XL", "XXL", "XXXL"];
const gotEffort = Array.isArray(effortField.options)
? effortField.options
: [];
for (const option of expectedEffort) {
if (!gotEffort.includes(option)) {
fail(`Effort custom field missing expected option: ${option}`);
}
}
}
const fieldUsage = orgFields.field_usage || {};
for (const key of ["Priority", "Start date", "Target date", "Effort"]) {
if (!fieldUsage[key]) {
fail(`organization_issue_fields.field_usage missing key: ${key}`);
}
}
for (const needle of docMustContain) {
if (!docRaw.includes(needle)) {
fail(`docs/ISSUE_FIELDS.md missing required reference: ${needle}`);
}
}
const canonicalDocAnchors = [
issueDefaults.assignee,
issueDefaults.status_label_open,
issueDefaults.status_label_closed,
issueDefaults.priority_label,
issueDefaults.type_label,
prDefaults.status_label_open,
prDefaults.status_label_merged,
prDefaults.priority_label,
prDefaults.type_label,
...collectKeys(fieldMappings.Status),
...collectKeys(fieldMappings.Priority),
...collectKeys(fieldMappings.Type).slice(0, 5),
"Status",
"Priority",
"Type",
"25",
"50",
"10",
];
const missingLabelMentions = canonicalDocAnchors.filter(
(label) => !docRaw.includes(label),
);
if (missingLabelMentions.length > 0) {
fail(
`docs/ISSUE_FIELDS.md is missing canonical label references (${missingLabelMentions.length}): ${missingLabelMentions.slice(0, 12).join(", ")}${missingLabelMentions.length > 12 ? ", ..." : ""}`,
);
}
if (!process.exitCode) {
ok("Issue-fields config YAML parsed and required structure is valid");
ok("Cross-file label and issue-type parity checks passed");
ok("docs/ISSUE_FIELDS.md references canonical mappings and profile keys");
}
}
main();