@aspruyt/xfg 8.6.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,9 @@ import { isTextContent, isObjectContent, isStructuredFileExtension, validValues,
2
2
  import { validateRuleset } from "./ruleset-validator.js";
3
3
  import { validateRepoSettings } from "./repo-settings-validator.js";
4
4
  import { validateActionsSettings } from "./actions-validator.js";
5
+ import { assertNoRootInherit, validateInheritFlag } from "./inherit-flag.js";
5
6
  import { isPlainObject } from "../../shared/type-guards.js";
7
+ import { arrayValues, explainNotArray } from "../array-directive.js";
6
8
  import { ValidationError } from "../../shared/errors.js";
7
9
  export function buildRootSettingsContext(config) {
8
10
  return {
@@ -75,6 +77,15 @@ export function validateFileConfigFields(fileConfig, fileName, context) {
75
77
  }
76
78
  }
77
79
  }
80
+ /** `inherit` on a ruleset, label or environment entry; root entries have nothing to inherit. */
81
+ function validateEntryInherit(entry, kind, name, context, isRoot) {
82
+ if (!isPlainObject(entry))
83
+ return;
84
+ const entryName = `${kind} '${name}'`;
85
+ if (isRoot)
86
+ assertNoRootInherit(entry, entryName, context);
87
+ validateInheritFlag(entry, `${context}: ${entryName}`);
88
+ }
78
89
  function validateLabel(label, name, context) {
79
90
  if (!isPlainObject(label)) {
80
91
  throw new ValidationError(`${context}: label '${name}' must be an object`);
@@ -118,10 +129,9 @@ function validateCodeScanningSettings(settings, context) {
118
129
  if (!isPlainObject(settings)) {
119
130
  throw new ValidationError(`${context}: must be an object`);
120
131
  }
121
- if (settings.state === undefined) {
122
- throw new ValidationError(`${context}: state is required`);
123
- }
124
- if (!VALID_CODE_SCANNING_STATES.includes(settings.state)) {
132
+ validateInheritFlag(settings, context);
133
+ if (settings.state !== undefined &&
134
+ !VALID_CODE_SCANNING_STATES.includes(settings.state)) {
125
135
  throw new ValidationError(`${context}: state must be one of: ${VALID_CODE_SCANNING_STATES.join(", ")}`);
126
136
  }
127
137
  if (settings.querySuite !== undefined &&
@@ -129,16 +139,23 @@ function validateCodeScanningSettings(settings, context) {
129
139
  throw new ValidationError(`${context}: querySuite must be one of: ${VALID_CODE_SCANNING_QUERY_SUITES.join(", ")}`);
130
140
  }
131
141
  if (settings.languages !== undefined) {
132
- if (!Array.isArray(settings.languages)) {
133
- throw new ValidationError(`${context}: languages must be an array`);
142
+ const languages = arrayValues(settings.languages);
143
+ if (languages === null) {
144
+ throw new ValidationError(explainNotArray(`${context}: languages must be an array or $arrayMerge directive`, settings.languages));
134
145
  }
135
- for (const lang of settings.languages) {
146
+ for (const lang of languages) {
136
147
  if (!VALID_CODE_SCANNING_LANGUAGES.includes(lang)) {
137
148
  throw new ValidationError(`${context}: invalid language "${lang}". Valid languages: ${VALID_CODE_SCANNING_LANGUAGES.join(", ")}`);
138
149
  }
139
150
  }
140
151
  }
141
152
  }
153
+ /** A layer may omit `state` and inherit it, so only the merged block can be checked. */
154
+ export function validateMergedCodeScanningSettings(codeScanning, context) {
155
+ if (codeScanning && codeScanning.state === undefined) {
156
+ throw new ValidationError(`${context} codeScanning: state is required, but no layer sets it (a layer with inherit: false drops the inherited state)`);
157
+ }
158
+ }
142
159
  function validateSettingsRulesets(settings, context, rootCtx) {
143
160
  if (settings.rulesets === undefined)
144
161
  return;
@@ -155,6 +172,7 @@ function validateSettingsRulesets(settings, context, rootCtx) {
155
172
  }
156
173
  continue;
157
174
  }
175
+ validateEntryInherit(ruleset, "ruleset", name, context, !rootCtx);
158
176
  validateRuleset(ruleset, name, context);
159
177
  }
160
178
  }
@@ -174,6 +192,7 @@ function validateSettingsLabels(settings, context, rootCtx) {
174
192
  }
175
193
  continue;
176
194
  }
195
+ validateEntryInherit(label, "label", name, context, !rootCtx);
177
196
  validateLabel(label, name, context);
178
197
  }
179
198
  }
@@ -196,6 +215,7 @@ function validateSettingsRepo(settings, context, rootCtx) {
196
215
  return;
197
216
  }
198
217
  validateRepoSettings(settings.repo, context);
218
+ validateInheritFlag(settings.repo, `${context} repo`);
199
219
  }
200
220
  function validateSettingsCodeScanning(settings, context, rootCtx) {
201
221
  if (settings.codeScanning === undefined)
@@ -265,11 +285,11 @@ function validateSettingsCollaborators(settings, context) {
265
285
  throw new ValidationError(`${context}: collaborators.${key} must be a boolean`);
266
286
  }
267
287
  }
268
- const users = collaborators.users;
269
- if (users === undefined)
288
+ if (collaborators.users === undefined)
270
289
  return;
271
- if (!Array.isArray(users)) {
272
- throw new ValidationError(`${context}: collaborators.users must be an array of GitHub usernames`);
290
+ const users = arrayValues(collaborators.users);
291
+ if (users === null) {
292
+ throw new ValidationError(explainNotArray(`${context}: collaborators.users must be an array of GitHub usernames or $arrayMerge directive`, collaborators.users));
273
293
  }
274
294
  for (const user of users) {
275
295
  if (typeof user !== "string" || !GITHUB_USERNAME_PATTERN.test(user)) {
@@ -277,11 +297,16 @@ function validateSettingsCollaborators(settings, context) {
277
297
  }
278
298
  }
279
299
  }
280
- const ENVIRONMENT_KEYS = new Set(["deploymentBranchPolicy", "secrets"]);
300
+ const ENVIRONMENT_KEYS = new Set([
301
+ "deploymentBranchPolicy",
302
+ "secrets",
303
+ "inherit",
304
+ ]);
281
305
  const BRANCH_POLICY_KEYS = new Set([
282
306
  "protectedBranches",
283
307
  "custom",
284
308
  "deleteOrphaned",
309
+ "inherit",
285
310
  ]);
286
311
  const BRANCH_PATTERN_KEYS = new Set(["type", "name"]);
287
312
  const ENVIRONMENT_NAME_MAX_LENGTH = 255;
@@ -293,11 +318,15 @@ function assertKnownKeys(obj, allowed, context) {
293
318
  }
294
319
  }
295
320
  function validateBranchPatterns(custom, context) {
296
- if (!Array.isArray(custom) || custom.length === 0) {
297
- throw new ValidationError(`${context}: custom must be a non-empty array`);
321
+ if (isPlainObject(custom) && custom.$arrayMerge === "merge") {
322
+ throw new ValidationError(`${context}: custom does not accept $arrayMerge: merge, because a pattern is identified by type and name together. Use append, prepend or replace.`);
323
+ }
324
+ const patterns = arrayValues(custom);
325
+ if (patterns === null || patterns.length === 0) {
326
+ throw new ValidationError(`${context}: custom must be a non-empty array or $arrayMerge directive`);
298
327
  }
299
328
  const seen = new Set();
300
- custom.forEach((pattern, i) => {
329
+ patterns.forEach((pattern, i) => {
301
330
  const ctx = `${context}: custom[${i}]`;
302
331
  if (!isPlainObject(pattern)) {
303
332
  throw new ValidationError(`${ctx} must be an object`);
@@ -316,7 +345,7 @@ function validateBranchPatterns(custom, context) {
316
345
  seen.add(key);
317
346
  });
318
347
  }
319
- function validateDeploymentBranchPolicy(policy, context, isRoot) {
348
+ function validateDeploymentBranchPolicyLayer(policy, context, isRoot) {
320
349
  const ctx = `${context} deploymentBranchPolicy`;
321
350
  if (policy === false) {
322
351
  if (isRoot) {
@@ -328,24 +357,43 @@ function validateDeploymentBranchPolicy(policy, context, isRoot) {
328
357
  throw new ValidationError(`${ctx} must be an object`);
329
358
  }
330
359
  assertKnownKeys(policy, BRANCH_POLICY_KEYS, `${context}: deploymentBranchPolicy`);
360
+ if (isRoot)
361
+ assertNoRootInherit(policy, "deploymentBranchPolicy", context);
362
+ validateInheritFlag(policy, `${context}: deploymentBranchPolicy`);
363
+ if (policy.protectedBranches !== undefined &&
364
+ policy.protectedBranches !== true) {
365
+ throw new ValidationError(`${ctx}: protectedBranches must be true. Omit deploymentBranchPolicy to let any branch deploy.`);
366
+ }
367
+ if (policy.protectedBranches !== undefined && policy.custom !== undefined) {
368
+ throw new ValidationError(`${ctx} has both 'protectedBranches: true' and 'custom'. Set only one.`);
369
+ }
370
+ if (policy.custom !== undefined) {
371
+ validateBranchPatterns(policy.custom, ctx);
372
+ }
373
+ if (policy.deleteOrphaned !== undefined &&
374
+ typeof policy.deleteOrphaned !== "boolean") {
375
+ throw new ValidationError(`${context}: deploymentBranchPolicy: deleteOrphaned must be a boolean`);
376
+ }
377
+ }
378
+ /**
379
+ * A layer sets only some fields, so the options that must go together are
380
+ * checked on the merged policy.
381
+ */
382
+ export function validateDeploymentBranchPolicy(policy, context) {
383
+ const ctx = `${context} deploymentBranchPolicy`;
331
384
  const hasProtected = policy.protectedBranches !== undefined;
332
385
  const hasCustom = policy.custom !== undefined;
333
- if (hasProtected === hasCustom) {
334
- throw new ValidationError(`${ctx} needs exactly one of 'protectedBranches: true' or 'custom'`);
386
+ if (hasProtected && hasCustom) {
387
+ throw new ValidationError(`${ctx} has both 'protectedBranches: true' and 'custom'. Set inherit: false on the layer that changes the policy kind to drop the inherited one.`);
335
388
  }
336
- if (hasProtected && policy.protectedBranches !== true) {
337
- throw new ValidationError(`${ctx}: protectedBranches must be true. Omit deploymentBranchPolicy to let any branch deploy.`);
389
+ if (!hasProtected && !hasCustom) {
390
+ throw new ValidationError(`${ctx} needs exactly one of 'protectedBranches: true' or 'custom'`);
338
391
  }
339
392
  if (hasCustom) {
340
393
  validateBranchPatterns(policy.custom, ctx);
341
394
  }
342
- if (policy.deleteOrphaned !== undefined) {
343
- if (typeof policy.deleteOrphaned !== "boolean") {
344
- throw new ValidationError(`${context}: deploymentBranchPolicy: deleteOrphaned must be a boolean`);
345
- }
346
- if (!hasCustom) {
347
- throw new ValidationError(`${context}: deploymentBranchPolicy: deleteOrphaned only applies to a 'custom' policy`);
348
- }
395
+ if (policy.deleteOrphaned !== undefined && !hasCustom) {
396
+ throw new ValidationError(`${context}: deploymentBranchPolicy: deleteOrphaned only applies to a 'custom' policy`);
349
397
  }
350
398
  }
351
399
  function validateEnvironmentSecrets(secrets, context) {
@@ -383,6 +431,7 @@ function validateSettingsEnvironments(settings, context, rootCtx) {
383
431
  }
384
432
  validateEnvironmentName(name, context, seenNames);
385
433
  if (env !== false) {
434
+ validateEntryInherit(env, "environment", name, context, !rootCtx);
386
435
  validateEnvironmentEntry(env, `${context}: environment '${name}'`, !rootCtx);
387
436
  }
388
437
  }
@@ -411,7 +460,7 @@ function validateEnvironmentEntry(env, envCtx, isRoot) {
411
460
  }
412
461
  assertKnownKeys(env, ENVIRONMENT_KEYS, envCtx);
413
462
  if (env.deploymentBranchPolicy !== undefined) {
414
- validateDeploymentBranchPolicy(env.deploymentBranchPolicy, envCtx, isRoot);
463
+ validateDeploymentBranchPolicyLayer(env.deploymentBranchPolicy, envCtx, isRoot);
415
464
  }
416
465
  if (env.secrets !== undefined) {
417
466
  validateEnvironmentSecrets(env.secrets, envCtx);
@@ -14,8 +14,9 @@ export declare function assertAiKeyEnvAllowed(provider: string, apiKeyEnv: strin
14
14
  */
15
15
  export declare function assertAiHeadersEnvAllowed(headersEnv: unknown, path?: string): void;
16
16
  /**
17
- * `baseUrlEnv` names the env var that holds the base URL. A URL is never
18
- * accepted inline, the two forms are exclusive, and the env var may not be a
19
- * credential xfg uses elsewhere.
17
+ * `baseUrlEnv` names the env var that holds the base URL, never a URL or a credential xfg uses elsewhere.
18
+ * Checks the one field, so it also holds for a layer before the merge.
20
19
  */
20
+ export declare function assertAiBaseUrlEnvName(baseUrlEnv: unknown, path?: string): void;
21
+ /** `baseUrl` and `baseUrlEnv` are exclusive, then `baseUrlEnv` must pass its own checks. */
21
22
  export declare function assertAiBaseUrlEnvAllowed(baseUrlEnv: unknown, baseUrl: unknown, path?: string): void;
@@ -92,16 +92,12 @@ export function assertAiHeadersEnvAllowed(headersEnv, path = "prOptions.ai") {
92
92
  }
93
93
  }
94
94
  /**
95
- * `baseUrlEnv` names the env var that holds the base URL. A URL is never
96
- * accepted inline, the two forms are exclusive, and the env var may not be a
97
- * credential xfg uses elsewhere.
95
+ * `baseUrlEnv` names the env var that holds the base URL, never a URL or a credential xfg uses elsewhere.
96
+ * Checks the one field, so it also holds for a layer before the merge.
98
97
  */
99
- export function assertAiBaseUrlEnvAllowed(baseUrlEnv, baseUrl, path = "prOptions.ai") {
98
+ export function assertAiBaseUrlEnvName(baseUrlEnv, path = "prOptions.ai") {
100
99
  if (baseUrlEnv === undefined)
101
100
  return;
102
- if (baseUrl !== undefined) {
103
- throw new ValidationError(`${path}: baseUrl and baseUrlEnv are mutually exclusive`);
104
- }
105
101
  if (typeof baseUrlEnv !== "string" || !ENV_NAME.test(baseUrlEnv)) {
106
102
  throw new ValidationError(`${path}.baseUrlEnv must be an env var name, not a value`);
107
103
  }
@@ -110,3 +106,12 @@ export function assertAiBaseUrlEnvAllowed(baseUrlEnv, baseUrl, path = "prOptions
110
106
  `export the URL under a dedicated name.`);
111
107
  }
112
108
  }
109
+ /** `baseUrl` and `baseUrlEnv` are exclusive, then `baseUrlEnv` must pass its own checks. */
110
+ export function assertAiBaseUrlEnvAllowed(baseUrlEnv, baseUrl, path = "prOptions.ai") {
111
+ if (baseUrlEnv === undefined)
112
+ return;
113
+ if (baseUrl !== undefined) {
114
+ throw new ValidationError(`${path}: baseUrl and baseUrlEnv are mutually exclusive`);
115
+ }
116
+ assertAiBaseUrlEnvName(baseUrlEnv, path);
117
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aspruyt/xfg",
3
- "version": "8.6.0",
3
+ "version": "9.0.0",
4
4
  "description": "Manage files, settings, and repositories across GitHub, Azure DevOps, and GitLab — declaratively, from a single YAML config",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -88,6 +88,7 @@
88
88
  },
89
89
  "devDependencies": {
90
90
  "@types/node": "^24.12.2",
91
+ "ajv": "^8.20.0",
91
92
  "bottleneck": "^2.19.5",
92
93
  "c8": "^12.0.0",
93
94
  "tsx": "^4.15.0",