@aspruyt/xfg 6.4.6 → 7.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.
- package/dist/cli/secrets-command.d.ts +4 -8
- package/dist/cli/secrets-command.js +20 -19
- package/dist/cli/sync-command.js +2 -1
- package/dist/config/config-merger.js +8 -0
- package/dist/config/index.d.ts +2 -2
- package/dist/config/index.js +1 -1
- package/dist/config/normalizer.js +55 -62
- package/dist/config/types.d.ts +12 -6
- package/dist/config/validator.d.ts +7 -2
- package/dist/config/validator.js +72 -79
- package/dist/config/validators/shared.js +21 -0
- package/dist/settings/index.d.ts +1 -0
- package/dist/settings/index.js +2 -0
- package/dist/settings/secrets/diff.d.ts +13 -0
- package/dist/settings/secrets/diff.js +31 -0
- package/dist/{secrets → settings/secrets}/github-secrets-strategy.d.ts +3 -3
- package/dist/{secrets → settings/secrets}/github-secrets-strategy.js +3 -3
- package/dist/{secrets → settings/secrets}/index.d.ts +2 -1
- package/dist/{secrets → settings/secrets}/index.js +1 -0
- package/dist/settings/secrets/processor.d.ts +23 -0
- package/dist/settings/secrets/processor.js +75 -0
- package/dist/{secrets → settings/secrets}/types.d.ts +2 -2
- package/dist/vcs/file-mode-fixup-commit-strategy.js +1 -3
- package/package.json +3 -1
- package/dist/secrets/processor.d.ts +0 -31
- package/dist/secrets/processor.js +0 -115
- /package/dist/{secrets → settings/secrets}/encryption.d.ts +0 -0
- /package/dist/{secrets → settings/secrets}/encryption.js +0 -0
- /package/dist/{secrets → settings/secrets}/types.js +0 -0
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import type { SecretsProcessorResult } from "../secrets/
|
|
2
|
-
import type {
|
|
1
|
+
import type { SecretsProcessorResult } from "../settings/secrets/index.js";
|
|
2
|
+
import type { RepoConfig } from "../config/index.js";
|
|
3
3
|
import type { RepoInfo } from "../repo/index.js";
|
|
4
|
-
type SecretsConfig = Record<string, SecretConfig | boolean> & {
|
|
5
|
-
deleteOrphaned?: boolean;
|
|
6
|
-
};
|
|
7
4
|
export interface ISecretsProcessorAdapter {
|
|
8
|
-
process(
|
|
5
|
+
process(repoConfig: RepoConfig, repoInfo: RepoInfo, options: {
|
|
9
6
|
dryRun?: boolean;
|
|
10
7
|
token?: string;
|
|
11
8
|
noDelete?: boolean;
|
|
12
9
|
}): Promise<SecretsProcessorResult>;
|
|
13
10
|
}
|
|
14
11
|
export interface SecretsSyncDependencies {
|
|
15
|
-
processorFactory?: (
|
|
12
|
+
processorFactory?: (cwd: string, retries: number) => ISecretsProcessorAdapter;
|
|
16
13
|
}
|
|
17
14
|
export interface SecretsSyncOptions {
|
|
18
15
|
config: string;
|
|
@@ -22,4 +19,3 @@ export interface SecretsSyncOptions {
|
|
|
22
19
|
retries?: number;
|
|
23
20
|
}
|
|
24
21
|
export declare function runSecretsSync(options: SecretsSyncOptions, deps?: SecretsSyncDependencies): Promise<void>;
|
|
25
|
-
export {};
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { normalizeConfig } from "../config/
|
|
3
|
-
import {
|
|
4
|
-
import { SecretsProcessor, GitHubSecretsStrategy, SodiumEncryptor, } from "../secrets/index.js";
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
import { loadRawConfig, normalizeConfig, validateSecretsConfig, validateNormalizedConfig, } from "../config/index.js";
|
|
3
|
+
import { SecretsProcessor, GitHubSecretsStrategy, SodiumEncryptor, } from "../settings/secrets/index.js";
|
|
5
4
|
import { EnvResolver } from "../shared/env-resolver.js";
|
|
6
5
|
import { ProcessExecutor } from "../shared/command-executor.js";
|
|
7
6
|
import { parseGitUrl } from "../repo/index.js";
|
|
8
7
|
import { Logger } from "../shared/logger.js";
|
|
9
8
|
import { toErrorMessage } from "../shared/type-guards.js";
|
|
10
|
-
function createDefaultProcessor(
|
|
9
|
+
function createDefaultProcessor(cwd, retries) {
|
|
11
10
|
const executor = new ProcessExecutor(process.env);
|
|
12
11
|
const encryptor = new SodiumEncryptor();
|
|
13
12
|
const envResolver = new EnvResolver(process.env);
|
|
@@ -21,25 +20,17 @@ export async function runSecretsSync(options, deps = {}) {
|
|
|
21
20
|
const logger = new Logger(!!(process.env.DEBUG || process.env.XFG_DEBUG));
|
|
22
21
|
const { config: configPath, dryRun, workDir, retries, noDelete } = options;
|
|
23
22
|
const cwd = workDir ?? "./tmp";
|
|
23
|
+
// gh is spawned with this as its cwd; a missing dir surfaces as "spawnSync gh ENOENT".
|
|
24
|
+
mkdirSync(cwd, { recursive: true });
|
|
24
25
|
const rawConfig = loadRawConfig(configPath);
|
|
25
|
-
validateRawConfig(rawConfig);
|
|
26
26
|
validateSecretsConfig(rawConfig);
|
|
27
|
-
validateVariableSecretOverlaps(rawConfig);
|
|
28
27
|
const config = normalizeConfig(rawConfig, process.env);
|
|
29
|
-
|
|
30
|
-
logger.info("No secrets configured. Nothing to do.");
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const { deleteOrphaned, ...secretEntries } = config.secrets;
|
|
34
|
-
const secretNames = Object.keys(secretEntries).filter((k) => typeof secretEntries[k] !== "boolean");
|
|
35
|
-
if (secretNames.length === 0 && !deleteOrphaned) {
|
|
36
|
-
logger.info("No secrets configured. Nothing to do.");
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
28
|
+
validateNormalizedConfig(config);
|
|
39
29
|
const processorFactory = deps.processorFactory ?? createDefaultProcessor;
|
|
40
|
-
const processor = processorFactory(
|
|
30
|
+
const processor = processorFactory(cwd, retries ?? 3);
|
|
41
31
|
const token = process.env.GH_TOKEN || process.env.GITHUB_TOKEN;
|
|
42
32
|
let hasErrors = false;
|
|
33
|
+
let anySecretsConfigured = false;
|
|
43
34
|
logger.setTotal(config.repos.length);
|
|
44
35
|
for (let i = 0; i < config.repos.length; i++) {
|
|
45
36
|
const repoConfig = config.repos[i];
|
|
@@ -48,11 +39,17 @@ export async function runSecretsSync(options, deps = {}) {
|
|
|
48
39
|
const repoInfo = parseGitUrl(repoConfig.git, {
|
|
49
40
|
githubHosts: config.githubHosts,
|
|
50
41
|
});
|
|
51
|
-
const result = await processor.process(
|
|
42
|
+
const result = await processor.process(repoConfig, repoInfo, {
|
|
52
43
|
dryRun,
|
|
53
44
|
token,
|
|
54
45
|
noDelete,
|
|
55
46
|
});
|
|
47
|
+
if (result.noSecretsConfigured) {
|
|
48
|
+
// Silent: with per-repo scoping most repos have no secrets, and a line
|
|
49
|
+
// each would bury the repos that do.
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
anySecretsConfigured = true;
|
|
56
53
|
if (result.skipped) {
|
|
57
54
|
logger.skip(i + 1, repoName, result.message);
|
|
58
55
|
}
|
|
@@ -65,10 +62,14 @@ export async function runSecretsSync(options, deps = {}) {
|
|
|
65
62
|
}
|
|
66
63
|
}
|
|
67
64
|
catch (error) {
|
|
65
|
+
anySecretsConfigured = true;
|
|
68
66
|
logger.error(i + 1, repoName, `Secrets: ${toErrorMessage(error)}`);
|
|
69
67
|
hasErrors = true;
|
|
70
68
|
}
|
|
71
69
|
}
|
|
70
|
+
if (!anySecretsConfigured) {
|
|
71
|
+
logger.info("No secrets configured. Nothing to do.");
|
|
72
|
+
}
|
|
72
73
|
if (hasErrors) {
|
|
73
74
|
throw new Error("One or more repositories failed secrets sync.");
|
|
74
75
|
}
|
package/dist/cli/sync-command.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
|
-
import { loadRawConfig, normalizeConfig, validateForSync, } from "../config/index.js";
|
|
3
|
+
import { loadRawConfig, normalizeConfig, validateForSync, validateNormalizedConfig, } from "../config/index.js";
|
|
4
4
|
import { ValidationError, SyncError } from "../shared/errors.js";
|
|
5
5
|
import { validateBranchName } from "./branch-utils.js";
|
|
6
6
|
import { createTokenManager } from "../vcs/index.js";
|
|
@@ -66,6 +66,7 @@ export async function runSync(options, deps = {}) {
|
|
|
66
66
|
const rawConfig = loadRawConfig(configPath);
|
|
67
67
|
validateForSync(rawConfig);
|
|
68
68
|
const config = normalizeConfig(rawConfig, process.env);
|
|
69
|
+
validateNormalizedConfig(config);
|
|
69
70
|
const fileNames = getUniqueFileNames(config);
|
|
70
71
|
let branchName;
|
|
71
72
|
if (options.branch) {
|
|
@@ -13,6 +13,14 @@ export function mergeConfigFragments(fragments) {
|
|
|
13
13
|
if (fragments.length === 0) {
|
|
14
14
|
throw new ValidationError("No config fragments to merge");
|
|
15
15
|
}
|
|
16
|
+
// Root-level `secrets` is not a SINGLE_FILE_KEY and would be silently dropped
|
|
17
|
+
// before validateRawConfig ever sees it, so the migration error must fire here too.
|
|
18
|
+
for (const { fileName, config } of fragments) {
|
|
19
|
+
if (config.secrets !== undefined) {
|
|
20
|
+
throw new ValidationError(`${fileName}: root-level 'secrets' is no longer supported — move it under 'settings.secrets'. ` +
|
|
21
|
+
"See https://anthony-spruyt.github.io/xfg/migration-v7/");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
16
24
|
const merged = {};
|
|
17
25
|
const singleKeySource = {};
|
|
18
26
|
const allRepos = [];
|
package/dist/config/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export type { PRMergeOptions, MergeMode, MergeStrategy, BypassActor, StatusCheckConfig, CodeScanningTool, PullRequestRuleParameters, RulesetRule, Ruleset, GitHubRepoSettings, RepoVisibility, SquashMergeCommitTitle, SquashMergeCommitMessage, MergeCommitTitle, MergeCommitMessage, Label, CodeScanningSettings, CodeScanningState, CodeScanningQuerySuite, CodeScanningLanguage, RepoSettings, RawFileConfig, RawRepoFileOverride, RawGroupConfig, SecretConfig, RawRootSettings, RawRepoSettings, RawRepoConfig, RawConfig, RawConditionalGroupWhen, RawConditionalGroupConfig, RepoConfig, Config, FileContent, ContentValue, } from "./types.js";
|
|
1
|
+
export type { PRMergeOptions, MergeMode, MergeStrategy, BypassActor, StatusCheckConfig, CodeScanningTool, PullRequestRuleParameters, RulesetRule, Ruleset, GitHubRepoSettings, RepoVisibility, SquashMergeCommitTitle, SquashMergeCommitMessage, MergeCommitTitle, MergeCommitMessage, Label, CodeScanningSettings, CodeScanningState, CodeScanningQuerySuite, CodeScanningLanguage, RepoSettings, RawFileConfig, RawRepoFileOverride, RawGroupConfig, SecretConfig, SecretsConfig, RawRootSettings, RawRepoSettings, RawRepoConfig, RawConfig, RawConditionalGroupWhen, RawConditionalGroupConfig, RepoConfig, Config, FileContent, ContentValue, } from "./types.js";
|
|
2
2
|
export { RULESET_COMPARABLE_FIELDS } from "./types.js";
|
|
3
3
|
export { loadRawConfig, loadConfig, normalizeConfig } from "./loader.js";
|
|
4
4
|
export { convertContentToString } from "./formatter.js";
|
|
5
|
-
export { validateForSync, validateSecretsConfig } from "./validator.js";
|
|
5
|
+
export { validateForSync, validateSecretsConfig, validateNormalizedConfig, } from "./validator.js";
|
package/dist/config/index.js
CHANGED
|
@@ -5,4 +5,4 @@ export { loadRawConfig, loadConfig, normalizeConfig } from "./loader.js";
|
|
|
5
5
|
// Config formatting
|
|
6
6
|
export { convertContentToString } from "./formatter.js";
|
|
7
7
|
// Config validation
|
|
8
|
-
export { validateForSync, validateSecretsConfig } from "./validator.js";
|
|
8
|
+
export { validateForSync, validateSecretsConfig, validateNormalizedConfig, } from "./validator.js";
|
|
@@ -127,8 +127,8 @@ function mergeLabels(rootLabels, repoLabels) {
|
|
|
127
127
|
}
|
|
128
128
|
return Object.keys(result).length > 0 ? result : undefined;
|
|
129
129
|
}
|
|
130
|
-
// GitHub treats variable names case-insensitively; overlay keys win over base keys with same name but different casing.
|
|
131
|
-
function
|
|
130
|
+
// GitHub treats variable and secret names case-insensitively; overlay keys win over base keys with same name but different casing.
|
|
131
|
+
function mergeCaseInsensitiveEntries(base, overlay) {
|
|
132
132
|
const overlayUpper = new Map();
|
|
133
133
|
for (const key of Object.keys(overlay)) {
|
|
134
134
|
overlayUpper.set(key.toUpperCase(), key);
|
|
@@ -144,6 +144,47 @@ function mergeVariablesCaseInsensitive(base, overlay) {
|
|
|
144
144
|
}
|
|
145
145
|
return result;
|
|
146
146
|
}
|
|
147
|
+
const ENTRY_MAP_META_KEYS = new Set(["inherit", "deleteOrphaned"]);
|
|
148
|
+
function stripEntryMapMetaKeys(entries) {
|
|
149
|
+
const result = {};
|
|
150
|
+
for (const [name, value] of Object.entries(entries)) {
|
|
151
|
+
if (ENTRY_MAP_META_KEYS.has(name))
|
|
152
|
+
continue;
|
|
153
|
+
result[name] = value;
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Merges one name-keyed settings entry map (variables or secrets) over another.
|
|
159
|
+
* `deleteOrphaned` is a policy peer key resolved innermost-wins; `inherit: false`
|
|
160
|
+
* discards inherited entries only and never clears it.
|
|
161
|
+
*/
|
|
162
|
+
function mergeEntryMapLayer(base, overlay) {
|
|
163
|
+
const baseEntries = base ?? {};
|
|
164
|
+
const overlayEntries = overlay ?? {};
|
|
165
|
+
const effectiveDeleteOrphaned = overlayEntries.deleteOrphaned !== undefined
|
|
166
|
+
? overlayEntries.deleteOrphaned
|
|
167
|
+
: baseEntries.deleteOrphaned;
|
|
168
|
+
const inherited = overlayEntries.inherit === false ? {} : stripEntryMapMetaKeys(baseEntries);
|
|
169
|
+
const merged = mergeCaseInsensitiveEntries(inherited, stripEntryMapMetaKeys(overlayEntries));
|
|
170
|
+
const result = {};
|
|
171
|
+
for (const [name, value] of Object.entries(merged)) {
|
|
172
|
+
if (value !== false) {
|
|
173
|
+
result[name] = value;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (effectiveDeleteOrphaned !== undefined) {
|
|
177
|
+
result.deleteOrphaned = effectiveDeleteOrphaned;
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
/** deleteOrphaned alone is not actionable, so an entry-less map collapses to undefined. */
|
|
182
|
+
function dropEntryMapIfEmpty(merged) {
|
|
183
|
+
const { deleteOrphaned, ...entries } = merged;
|
|
184
|
+
if (Object.keys(entries).length === 0 && !deleteOrphaned)
|
|
185
|
+
return undefined;
|
|
186
|
+
return merged;
|
|
187
|
+
}
|
|
147
188
|
/**
|
|
148
189
|
* Merges settings: per-repo settings deep merge with root settings.
|
|
149
190
|
* Returns undefined if no settings are defined.
|
|
@@ -228,35 +269,16 @@ export function mergeSettings(root, perRepo) {
|
|
|
228
269
|
}
|
|
229
270
|
}
|
|
230
271
|
}
|
|
231
|
-
// Variables merging — deleteOrphaned is a peer key (like secrets' deleteOrphaned)
|
|
232
272
|
if (root?.variables || perRepo?.variables) {
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
.deleteOrphaned;
|
|
237
|
-
const repoDeleteOrphaned = repoVars
|
|
238
|
-
.deleteOrphaned;
|
|
239
|
-
const effectiveDeleteOrphaned = repoDeleteOrphaned !== undefined
|
|
240
|
-
? repoDeleteOrphaned
|
|
241
|
-
: rootDeleteOrphaned;
|
|
242
|
-
const inherit = repoVars.inherit;
|
|
243
|
-
if (inherit === false) {
|
|
244
|
-
const { inherit: _, deleteOrphaned: _d, ...rest } = repoVars;
|
|
245
|
-
result.variables = Object.fromEntries(Object.entries(rest).filter(([, v]) => v !== false));
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
const combined = mergeVariablesCaseInsensitive(rootVars, repoVars);
|
|
249
|
-
const { inherit: _, deleteOrphaned: _d, ...rest } = combined;
|
|
250
|
-
result.variables = Object.fromEntries(Object.entries(rest).filter(([, v]) => v !== false));
|
|
273
|
+
const merged = dropEntryMapIfEmpty(mergeEntryMapLayer(root?.variables, perRepo?.variables));
|
|
274
|
+
if (merged) {
|
|
275
|
+
result.variables = merged;
|
|
251
276
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const { deleteOrphaned: _check, ...varEntries } = result.variables;
|
|
258
|
-
if (Object.keys(varEntries).length === 0 && !effectiveDeleteOrphaned) {
|
|
259
|
-
delete result.variables;
|
|
277
|
+
}
|
|
278
|
+
if (root?.secrets || perRepo?.secrets) {
|
|
279
|
+
const merged = dropEntryMapIfEmpty(mergeEntryMapLayer(root?.secrets, perRepo?.secrets));
|
|
280
|
+
if (merged) {
|
|
281
|
+
result.secrets = merged;
|
|
260
282
|
}
|
|
261
283
|
}
|
|
262
284
|
return Object.keys(result).length > 0 ? result : undefined;
|
|
@@ -398,39 +420,11 @@ function mergeRawSettings(base, overlay) {
|
|
|
398
420
|
result.codeScanning = structuredClone(overlay.codeScanning);
|
|
399
421
|
}
|
|
400
422
|
}
|
|
401
|
-
// Variables: simple string values with false opt-outs (mergeNamedEntries won't work for strings)
|
|
402
423
|
if (overlay.variables) {
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
: {};
|
|
408
|
-
// Build overlay entries (excluding meta keys)
|
|
409
|
-
const overlayEntries = {};
|
|
410
|
-
for (const [name, entry] of Object.entries(overlay.variables)) {
|
|
411
|
-
if (name === "inherit" || name === "deleteOrphaned")
|
|
412
|
-
continue;
|
|
413
|
-
overlayEntries[name] = entry;
|
|
414
|
-
}
|
|
415
|
-
// Case-insensitive merge: overlay keys replace base keys with same name
|
|
416
|
-
const merged = mergeVariablesCaseInsensitive(baseVars, overlayEntries);
|
|
417
|
-
// Apply false opt-outs
|
|
418
|
-
const cleaned = {};
|
|
419
|
-
for (const [name, value] of Object.entries(merged)) {
|
|
420
|
-
if (name === "inherit" || name === "deleteOrphaned")
|
|
421
|
-
continue;
|
|
422
|
-
if (value !== false) {
|
|
423
|
-
cleaned[name] = value;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
const baseDelete = baseVars.deleteOrphaned;
|
|
427
|
-
const effectiveDelete = overlayVars.deleteOrphaned !== undefined
|
|
428
|
-
? overlayVars.deleteOrphaned
|
|
429
|
-
: baseDelete;
|
|
430
|
-
if (effectiveDelete !== undefined) {
|
|
431
|
-
cleaned.deleteOrphaned = effectiveDelete;
|
|
432
|
-
}
|
|
433
|
-
result.variables = cleaned;
|
|
424
|
+
result.variables = mergeEntryMapLayer(result.variables, overlay.variables);
|
|
425
|
+
}
|
|
426
|
+
if (overlay.secrets) {
|
|
427
|
+
result.secrets = mergeEntryMapLayer(result.secrets, overlay.secrets);
|
|
434
428
|
}
|
|
435
429
|
// deleteOrphaned: overlay wins
|
|
436
430
|
if (overlay.deleteOrphaned !== undefined) {
|
|
@@ -621,6 +615,5 @@ export function normalizeConfig(raw, env) {
|
|
|
621
615
|
githubHosts: raw.githubHosts,
|
|
622
616
|
deleteOrphaned: raw.deleteOrphaned,
|
|
623
617
|
settings: normalizedRootSettings,
|
|
624
|
-
secrets: raw.secrets,
|
|
625
618
|
};
|
|
626
619
|
}
|
package/dist/config/types.d.ts
CHANGED
|
@@ -271,6 +271,10 @@ export interface CodeScanningSettings {
|
|
|
271
271
|
export interface SecretConfig {
|
|
272
272
|
env: string;
|
|
273
273
|
}
|
|
274
|
+
/** Raw secrets map: entries keyed by name, plus the `deleteOrphaned` peer key. */
|
|
275
|
+
export type SecretsConfig = Record<string, SecretConfig | boolean> & {
|
|
276
|
+
deleteOrphaned?: boolean;
|
|
277
|
+
};
|
|
274
278
|
export interface RepoSettings {
|
|
275
279
|
/** GitHub rulesets keyed by name */
|
|
276
280
|
rulesets?: Record<string, Ruleset>;
|
|
@@ -284,6 +288,10 @@ export interface RepoSettings {
|
|
|
284
288
|
variables?: Record<string, string> & {
|
|
285
289
|
deleteOrphaned?: boolean;
|
|
286
290
|
};
|
|
291
|
+
/** GitHub Actions secrets keyed by name */
|
|
292
|
+
secrets?: Record<string, SecretConfig> & {
|
|
293
|
+
deleteOrphaned?: boolean;
|
|
294
|
+
};
|
|
287
295
|
deleteOrphaned?: boolean;
|
|
288
296
|
}
|
|
289
297
|
export type ContentValue = Record<string, unknown> | string | string[];
|
|
@@ -347,6 +355,7 @@ export interface RawRootSettings {
|
|
|
347
355
|
variables?: Record<string, string | false> & {
|
|
348
356
|
deleteOrphaned?: boolean;
|
|
349
357
|
};
|
|
358
|
+
secrets?: SecretsConfig;
|
|
350
359
|
deleteOrphaned?: boolean;
|
|
351
360
|
}
|
|
352
361
|
export interface RawRepoSettings {
|
|
@@ -362,6 +371,9 @@ export interface RawRepoSettings {
|
|
|
362
371
|
inherit?: boolean;
|
|
363
372
|
deleteOrphaned?: boolean;
|
|
364
373
|
};
|
|
374
|
+
secrets?: SecretsConfig & {
|
|
375
|
+
inherit?: boolean;
|
|
376
|
+
};
|
|
365
377
|
deleteOrphaned?: boolean;
|
|
366
378
|
}
|
|
367
379
|
export interface RawRepoConfig {
|
|
@@ -388,9 +400,6 @@ export interface RawConfig {
|
|
|
388
400
|
githubHosts?: string[];
|
|
389
401
|
deleteOrphaned?: boolean;
|
|
390
402
|
settings?: RawRootSettings;
|
|
391
|
-
secrets?: Record<string, SecretConfig | boolean> & {
|
|
392
|
-
deleteOrphaned?: boolean;
|
|
393
|
-
};
|
|
394
403
|
}
|
|
395
404
|
export interface FileContent {
|
|
396
405
|
fileName: string;
|
|
@@ -420,8 +429,5 @@ export interface Config {
|
|
|
420
429
|
githubHosts?: string[];
|
|
421
430
|
deleteOrphaned?: boolean;
|
|
422
431
|
settings?: RepoSettings;
|
|
423
|
-
secrets?: Record<string, SecretConfig | boolean> & {
|
|
424
|
-
deleteOrphaned?: boolean;
|
|
425
|
-
};
|
|
426
432
|
}
|
|
427
433
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RawConfig, RawRootSettings, RawRepoSettings } from "./types.js";
|
|
1
|
+
import type { Config, RawConfig, RawRootSettings, RawRepoSettings } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Validates raw config structure before normalization.
|
|
4
4
|
* @throws ValidationError if validation fails
|
|
@@ -9,7 +9,12 @@ export declare function validateRawConfig(config: RawConfig): void;
|
|
|
9
9
|
* @throws ValidationError if neither files nor settings are present
|
|
10
10
|
*/
|
|
11
11
|
export declare function validateForSync(config: RawConfig): void;
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Cross-validates the MERGED settings of each repo: a variable and a secret with
|
|
14
|
+
* the same name collide at GitHub. Must run post-normalize — a per-layer check
|
|
15
|
+
* misses a root secret colliding with a repo variable.
|
|
16
|
+
*/
|
|
17
|
+
export declare function validateNormalizedConfig(config: Config): void;
|
|
13
18
|
export declare function hasActionableSettings(settings: RawRootSettings | RawRepoSettings | undefined): boolean;
|
|
14
19
|
export declare function validateVariableName(name: string): void;
|
|
15
20
|
export declare function validateSecretName(name: string): void;
|
package/dist/config/validator.js
CHANGED
|
@@ -8,7 +8,24 @@ import { validateRepoEntry } from "./validators/repo-entry-validator.js";
|
|
|
8
8
|
const CONFIG_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
|
|
9
9
|
const VARIABLE_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
10
10
|
const VARIABLE_RESERVED_KEYS = new Set(["deleteOrphaned", "inherit"]);
|
|
11
|
+
const SECRET_RESERVED_KEYS = new Set(["deleteOrphaned", "inherit"]);
|
|
11
12
|
const CONFIG_ID_MAX_LENGTH = 64;
|
|
13
|
+
function validateNoRootLevelSecrets(config) {
|
|
14
|
+
if (config.secrets === undefined) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
throw new ValidationError("Root-level 'secrets' is no longer supported — move it under 'settings.secrets'. " +
|
|
18
|
+
"Secrets can now also be scoped per group and per repo. " +
|
|
19
|
+
"See https://anthony-spruyt.github.io/xfg/migration-v7/");
|
|
20
|
+
}
|
|
21
|
+
function collectAllSettings(config) {
|
|
22
|
+
return [
|
|
23
|
+
config.settings,
|
|
24
|
+
...(Array.isArray(config.repos) ? config.repos.map((r) => r.settings) : []),
|
|
25
|
+
...Object.values(config.groups ?? {}).map((g) => g.settings),
|
|
26
|
+
...(config.conditionalGroups ?? []).map((cg) => cg.settings),
|
|
27
|
+
];
|
|
28
|
+
}
|
|
12
29
|
function validateConfigId(config) {
|
|
13
30
|
if (!config.id || typeof config.id !== "string") {
|
|
14
31
|
throw new ValidationError("Config requires an 'id' field. This unique identifier is used to namespace managed files in .xfg.json");
|
|
@@ -48,6 +65,9 @@ function validateRootSettings(config) {
|
|
|
48
65
|
if (config.settings.variables && "inherit" in config.settings.variables) {
|
|
49
66
|
throw new ValidationError("'inherit' is not allowed in root-level variables (nothing to inherit from)");
|
|
50
67
|
}
|
|
68
|
+
if (config.settings.secrets && "inherit" in config.settings.secrets) {
|
|
69
|
+
throw new ValidationError("'inherit' is not allowed in root-level secrets (nothing to inherit from)");
|
|
70
|
+
}
|
|
51
71
|
}
|
|
52
72
|
function validateGithubHosts(config) {
|
|
53
73
|
if (config.githubHosts === undefined)
|
|
@@ -110,6 +130,9 @@ function hasConditionalGroupPR(config) {
|
|
|
110
130
|
* @throws ValidationError if validation fails
|
|
111
131
|
*/
|
|
112
132
|
export function validateRawConfig(config) {
|
|
133
|
+
// Must run before the "nothing to do" check below, or an old secrets-only
|
|
134
|
+
// config trips the generic error and never sees the migration message.
|
|
135
|
+
validateNoRootLevelSecrets(config);
|
|
113
136
|
validateConfigId(config);
|
|
114
137
|
const hasFiles = isPlainObject(config.files) && Object.keys(config.files).length > 0;
|
|
115
138
|
const hasSettings = isPlainObject(config.settings);
|
|
@@ -119,7 +142,9 @@ export function validateRawConfig(config) {
|
|
|
119
142
|
const hasCondGrpFiles = hasConditionalGroupFiles(config);
|
|
120
143
|
const hasCondGrpSettings = hasConditionalGroupSettingsPresent(config);
|
|
121
144
|
const hasCondGrpPR = hasConditionalGroupPR(config);
|
|
122
|
-
|
|
145
|
+
// config.repos is not confirmed to be an array until further down.
|
|
146
|
+
const hasRepoSettings = Array.isArray(config.repos) &&
|
|
147
|
+
config.repos.some((r) => isPlainObject(r.settings));
|
|
123
148
|
if (!hasFiles &&
|
|
124
149
|
!hasSettings &&
|
|
125
150
|
!hasGrpFiles &&
|
|
@@ -127,10 +152,10 @@ export function validateRawConfig(config) {
|
|
|
127
152
|
!hasCondGrpFiles &&
|
|
128
153
|
!hasCondGrpSettings &&
|
|
129
154
|
!hasCondGrpPR &&
|
|
130
|
-
!
|
|
131
|
-
throw new ValidationError("Config requires at least one of: 'files'
|
|
132
|
-
"Use 'files' to sync configuration files, 'settings' to manage repository
|
|
133
|
-
"
|
|
155
|
+
!hasRepoSettings) {
|
|
156
|
+
throw new ValidationError("Config requires at least one of: 'files' or 'settings'. " +
|
|
157
|
+
"Use 'files' to sync configuration files, or 'settings' to manage repository " +
|
|
158
|
+
"settings, variables, and secrets.");
|
|
134
159
|
}
|
|
135
160
|
validateRootFiles(config);
|
|
136
161
|
if (config.deleteOrphaned !== undefined &&
|
|
@@ -176,13 +201,7 @@ export function validateForSync(config) {
|
|
|
176
201
|
"For secrets, use 'xfg secrets sync'.");
|
|
177
202
|
}
|
|
178
203
|
// Validate variable names across all settings
|
|
179
|
-
const
|
|
180
|
-
config.settings,
|
|
181
|
-
...config.repos.map((r) => r.settings),
|
|
182
|
-
...Object.values(config.groups ?? {}).map((g) => g.settings),
|
|
183
|
-
...(config.conditionalGroups ?? []).map((cg) => cg.settings),
|
|
184
|
-
];
|
|
185
|
-
for (const settings of allSettings) {
|
|
204
|
+
for (const settings of collectAllSettings(config)) {
|
|
186
205
|
if (!settings?.variables)
|
|
187
206
|
continue;
|
|
188
207
|
const vars = settings.variables;
|
|
@@ -216,66 +235,33 @@ export function validateForSync(config) {
|
|
|
216
235
|
}
|
|
217
236
|
// Validate secret names and configs
|
|
218
237
|
validateSecretsConfig(config);
|
|
219
|
-
// Cross-validate: no overlap between global secret names and variable names
|
|
220
|
-
validateVariableSecretOverlaps(config);
|
|
221
238
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
239
|
+
const ENTRY_MAP_META_KEYS = new Set(["deleteOrphaned", "inherit"]);
|
|
240
|
+
function entryNames(map) {
|
|
241
|
+
if (!map)
|
|
242
|
+
return [];
|
|
243
|
+
return Object.keys(map).filter((k) => !ENTRY_MAP_META_KEYS.has(k) && typeof map[k] !== "boolean");
|
|
244
|
+
}
|
|
245
|
+
function assertNoOverlap(settings, context) {
|
|
246
|
+
const secretNames = new Set(entryNames(settings?.secrets).map((n) => n.toUpperCase()));
|
|
230
247
|
if (secretNames.size === 0)
|
|
231
248
|
return;
|
|
232
|
-
|
|
233
|
-
if (
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const overlapping = rootVariableNames.filter((n) => secretNames.has(n.toUpperCase()));
|
|
237
|
-
if (overlapping.length > 0) {
|
|
238
|
-
throw new ValidationError(`${overlapping.join(", ")} overlap between root variables and secrets. ` +
|
|
239
|
-
"GitHub does not allow variables and secrets with the same name.");
|
|
240
|
-
}
|
|
249
|
+
const overlapping = entryNames(settings?.variables).filter((n) => secretNames.has(n.toUpperCase()));
|
|
250
|
+
if (overlapping.length > 0) {
|
|
251
|
+
throw new ValidationError(`${context}: ${overlapping.join(", ")} overlap between variables and secrets. ` +
|
|
252
|
+
"GitHub does not allow variables and secrets with the same name.");
|
|
241
253
|
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Cross-validates the MERGED settings of each repo: a variable and a secret with
|
|
257
|
+
* the same name collide at GitHub. Must run post-normalize — a per-layer check
|
|
258
|
+
* misses a root secret colliding with a repo variable.
|
|
259
|
+
*/
|
|
260
|
+
export function validateNormalizedConfig(config) {
|
|
261
|
+
// config.repos never covers a root-only collision when repos is empty.
|
|
262
|
+
assertNoOverlap(config.settings, "Root settings");
|
|
242
263
|
for (const repo of config.repos) {
|
|
243
|
-
|
|
244
|
-
const variableNames = Object.keys(varEntries).filter((k) => typeof varEntries[k] !== "boolean");
|
|
245
|
-
const overlapping = variableNames.filter((n) => secretNames.has(n.toUpperCase()));
|
|
246
|
-
if (overlapping.length > 0) {
|
|
247
|
-
throw new ValidationError(`Repo '${repo.git}': ${overlapping.join(", ")} overlap between variables and secrets. ` +
|
|
248
|
-
"GitHub does not allow variables and secrets with the same name.");
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
// Check group-level variables
|
|
252
|
-
if (isPlainObject(config.groups)) {
|
|
253
|
-
for (const [groupName, group] of Object.entries(config.groups)) {
|
|
254
|
-
if (!group.settings?.variables)
|
|
255
|
-
continue;
|
|
256
|
-
const { deleteOrphaned: _gd, inherit: _gi, ...groupVarEntries } = group.settings.variables;
|
|
257
|
-
const groupVariableNames = Object.keys(groupVarEntries).filter((k) => typeof groupVarEntries[k] !== "boolean");
|
|
258
|
-
const overlapping = groupVariableNames.filter((n) => secretNames.has(n.toUpperCase()));
|
|
259
|
-
if (overlapping.length > 0) {
|
|
260
|
-
throw new ValidationError(`Group '${groupName}': ${overlapping.join(", ")} overlap between variables and secrets. ` +
|
|
261
|
-
"GitHub does not allow variables and secrets with the same name.");
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
// Check conditional group-level variables
|
|
266
|
-
if (Array.isArray(config.conditionalGroups)) {
|
|
267
|
-
for (let i = 0; i < config.conditionalGroups.length; i++) {
|
|
268
|
-
const cg = config.conditionalGroups[i];
|
|
269
|
-
if (!cg.settings?.variables)
|
|
270
|
-
continue;
|
|
271
|
-
const { deleteOrphaned: _cd, inherit: _ci, ...cgVarEntries } = cg.settings.variables;
|
|
272
|
-
const cgVariableNames = Object.keys(cgVarEntries).filter((k) => typeof cgVarEntries[k] !== "boolean");
|
|
273
|
-
const overlapping = cgVariableNames.filter((n) => secretNames.has(n.toUpperCase()));
|
|
274
|
-
if (overlapping.length > 0) {
|
|
275
|
-
throw new ValidationError(`Conditional group ${i}: ${overlapping.join(", ")} overlap between variables and secrets. ` +
|
|
276
|
-
"GitHub does not allow variables and secrets with the same name.");
|
|
277
|
-
}
|
|
278
|
-
}
|
|
264
|
+
assertNoOverlap(repo.settings, `Repo '${repo.git}'`);
|
|
279
265
|
}
|
|
280
266
|
}
|
|
281
267
|
export function hasActionableSettings(settings) {
|
|
@@ -301,6 +287,8 @@ export function hasActionableSettings(settings) {
|
|
|
301
287
|
return true;
|
|
302
288
|
}
|
|
303
289
|
}
|
|
290
|
+
// Secrets are deliberately absent: `xfg sync` must never process them.
|
|
291
|
+
// A secrets-only config leaves `xfg sync` with nothing to do.
|
|
304
292
|
return false;
|
|
305
293
|
}
|
|
306
294
|
export function validateVariableName(name) {
|
|
@@ -325,23 +313,19 @@ function validateSecretEntry(name, config) {
|
|
|
325
313
|
throw new ValidationError(`Secret '${name}' requires an 'env' field (string) specifying the environment variable source.`);
|
|
326
314
|
}
|
|
327
315
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
return;
|
|
331
|
-
const { deleteOrphaned, ...entries } = config.secrets;
|
|
332
|
-
// Reject 'deleteOrphaned' used as a secret name (it's a reserved peer key)
|
|
316
|
+
function validateSecretsLayer(secrets) {
|
|
317
|
+
const { deleteOrphaned, inherit } = secrets;
|
|
333
318
|
if (deleteOrphaned !== undefined && typeof deleteOrphaned !== "boolean") {
|
|
334
319
|
throw new ValidationError("'deleteOrphaned' is a reserved key in secrets config and cannot be used as a secret name.");
|
|
335
320
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
if (value === true) {
|
|
339
|
-
throw new ValidationError(`Secret '${name}' is set to true, which is not valid. Use false to opt out, or provide a SecretConfig object.`);
|
|
340
|
-
}
|
|
321
|
+
if (inherit !== undefined && typeof inherit !== "boolean") {
|
|
322
|
+
throw new ValidationError("'inherit' is a reserved key in secrets config and cannot be used as a secret name.");
|
|
341
323
|
}
|
|
342
324
|
// Reject duplicate case-insensitive secret names
|
|
343
325
|
const seen = new Map();
|
|
344
|
-
for (const name of Object.keys(
|
|
326
|
+
for (const name of Object.keys(secrets)) {
|
|
327
|
+
if (SECRET_RESERVED_KEYS.has(name))
|
|
328
|
+
continue;
|
|
345
329
|
const upper = name.toUpperCase();
|
|
346
330
|
const existing = seen.get(upper);
|
|
347
331
|
if (existing) {
|
|
@@ -349,9 +333,18 @@ export function validateSecretsConfig(config) {
|
|
|
349
333
|
}
|
|
350
334
|
seen.set(upper, name);
|
|
351
335
|
}
|
|
352
|
-
for (const [name, value] of Object.entries(
|
|
336
|
+
for (const [name, value] of Object.entries(secrets)) {
|
|
337
|
+
if (SECRET_RESERVED_KEYS.has(name))
|
|
338
|
+
continue;
|
|
353
339
|
if (typeof value === "boolean")
|
|
354
340
|
continue;
|
|
355
341
|
validateSecretEntry(name, value);
|
|
356
342
|
}
|
|
357
343
|
}
|
|
344
|
+
export function validateSecretsConfig(config) {
|
|
345
|
+
for (const settings of collectAllSettings(config)) {
|
|
346
|
+
if (!settings?.secrets)
|
|
347
|
+
continue;
|
|
348
|
+
validateSecretsLayer(settings.secrets);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
@@ -208,6 +208,26 @@ function validateSettingsCodeScanning(settings, context, rootCtx) {
|
|
|
208
208
|
}
|
|
209
209
|
validateCodeScanningSettings(settings.codeScanning, `${context} codeScanning`);
|
|
210
210
|
}
|
|
211
|
+
function validateSettingsSecrets(settings, context) {
|
|
212
|
+
if (settings.secrets === undefined)
|
|
213
|
+
return;
|
|
214
|
+
if (!isPlainObject(settings.secrets)) {
|
|
215
|
+
throw new ValidationError(`${context}: settings.secrets must be an object`);
|
|
216
|
+
}
|
|
217
|
+
for (const [name, value] of Object.entries(settings.secrets)) {
|
|
218
|
+
if (name === "deleteOrphaned" || name === "inherit") {
|
|
219
|
+
if (value !== undefined && typeof value !== "boolean") {
|
|
220
|
+
throw new ValidationError(`${context}: settings.secrets.${name} must be a boolean`);
|
|
221
|
+
}
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (value === false)
|
|
225
|
+
continue;
|
|
226
|
+
if (!isPlainObject(value)) {
|
|
227
|
+
throw new ValidationError(`${context}: secret '${name}' must be an object with an 'env' field, or false to opt out`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
211
231
|
export function validateSettings(settings, context, rootCtx) {
|
|
212
232
|
if (!isPlainObject(settings)) {
|
|
213
233
|
throw new ValidationError(`${context}: settings must be an object`);
|
|
@@ -217,6 +237,7 @@ export function validateSettings(settings, context, rootCtx) {
|
|
|
217
237
|
validateSettingsDeleteOrphaned(settings, context);
|
|
218
238
|
validateSettingsRepo(settings, context, rootCtx);
|
|
219
239
|
validateSettingsCodeScanning(settings, context, rootCtx);
|
|
240
|
+
validateSettingsSecrets(settings, context);
|
|
220
241
|
}
|
|
221
242
|
export function enrichSettingsContext(rootCtx, settings) {
|
|
222
243
|
if (!settings)
|
package/dist/settings/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { RepoSettingsProcessor, type IRepoSettingsProcessor, type RepoSettingsPl
|
|
|
4
4
|
export { type LabelsPlanEntry, LabelsProcessor, type ILabelsProcessor, GitHubLabelsStrategy, } from "./labels/index.js";
|
|
5
5
|
export { type CodeScanningPlanEntry, CodeScanningProcessor, type ICodeScanningProcessor, GitHubCodeScanningStrategy, } from "./code-scanning/index.js";
|
|
6
6
|
export { type VariablesPlanEntry, VariablesProcessor, type IVariablesProcessor, GitHubVariablesStrategy, } from "./variables/index.js";
|
|
7
|
+
export { SecretsProcessor, type ISecretsProcessor, type SecretsProcessorOptions, type SecretsProcessorResult, GitHubSecretsStrategy, SodiumEncryptor, type ISecretEncryptor, } from "./secrets/index.js";
|
package/dist/settings/index.js
CHANGED
|
@@ -10,3 +10,5 @@ export { LabelsProcessor, GitHubLabelsStrategy, } from "./labels/index.js";
|
|
|
10
10
|
export { CodeScanningProcessor, GitHubCodeScanningStrategy, } from "./code-scanning/index.js";
|
|
11
11
|
// Variables
|
|
12
12
|
export { VariablesProcessor, GitHubVariablesStrategy, } from "./variables/index.js";
|
|
13
|
+
// Secrets — not wired into `xfg sync`; driven by `xfg secrets sync` only.
|
|
14
|
+
export { SecretsProcessor, GitHubSecretsStrategy, SodiumEncryptor, } from "./secrets/index.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { GitHubSecret } from "./types.js";
|
|
2
|
+
import type { SettingsAction } from "../base-processor.js";
|
|
3
|
+
export type SecretAction = SettingsAction;
|
|
4
|
+
export interface SecretChange {
|
|
5
|
+
action: SecretAction;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Diffs remote secrets against the desired name set.
|
|
10
|
+
* Existing secrets always report `update`, never `unchanged` — GitHub secret
|
|
11
|
+
* values are write-only, so we cannot tell whether the value actually changed.
|
|
12
|
+
*/
|
|
13
|
+
export declare function diffSecrets(current: GitHubSecret[], desiredNames: string[], deleteOrphaned: boolean): SecretChange[];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diffs remote secrets against the desired name set.
|
|
3
|
+
* Existing secrets always report `update`, never `unchanged` — GitHub secret
|
|
4
|
+
* values are write-only, so we cannot tell whether the value actually changed.
|
|
5
|
+
*/
|
|
6
|
+
export function diffSecrets(current, desiredNames, deleteOrphaned) {
|
|
7
|
+
const changes = [];
|
|
8
|
+
const currentByName = new Map();
|
|
9
|
+
for (const s of current) {
|
|
10
|
+
currentByName.set(s.name.toUpperCase(), s);
|
|
11
|
+
}
|
|
12
|
+
const desiredUpper = new Set(desiredNames.map((n) => n.toUpperCase()));
|
|
13
|
+
for (const name of desiredNames) {
|
|
14
|
+
const existing = currentByName.get(name.toUpperCase());
|
|
15
|
+
changes.push({ action: existing ? "update" : "create", name });
|
|
16
|
+
}
|
|
17
|
+
if (deleteOrphaned) {
|
|
18
|
+
for (const [nameUpper, currentSecret] of currentByName) {
|
|
19
|
+
if (!desiredUpper.has(nameUpper)) {
|
|
20
|
+
changes.push({ action: "delete", name: currentSecret.name });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const actionOrder = {
|
|
25
|
+
delete: 0,
|
|
26
|
+
update: 1,
|
|
27
|
+
create: 2,
|
|
28
|
+
unchanged: 3,
|
|
29
|
+
};
|
|
30
|
+
return changes.sort((a, b) => actionOrder[a.action] - actionOrder[b.action]);
|
|
31
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ICommandExecutor } from "
|
|
2
|
-
import { type RepoInfo } from "
|
|
3
|
-
import { type GhApiOptions } from "
|
|
1
|
+
import type { ICommandExecutor } from "../../shared/command-executor.js";
|
|
2
|
+
import { type RepoInfo } from "../../repo/index.js";
|
|
3
|
+
import { type GhApiOptions } from "../../shared/gh-api-utils.js";
|
|
4
4
|
import type { ISecretsStrategy, GitHubSecret, GitHubPublicKey } from "./types.js";
|
|
5
5
|
interface GitHubSecretsStrategyOptions {
|
|
6
6
|
retries?: number;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { assertGitHubRepo } from "
|
|
2
|
-
import { GhApiClient } from "
|
|
3
|
-
import { parseApiJson } from "
|
|
1
|
+
import { assertGitHubRepo } from "../../repo/index.js";
|
|
2
|
+
import { GhApiClient } from "../../shared/gh-api-utils.js";
|
|
3
|
+
import { parseApiJson } from "../../shared/json-utils.js";
|
|
4
4
|
export class GitHubSecretsStrategy {
|
|
5
5
|
api;
|
|
6
6
|
constructor(executor, options) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { SecretsProcessor } from "./processor.js";
|
|
2
|
-
export type { SecretsProcessorOptions, SecretsProcessorResult, } from "./processor.js";
|
|
2
|
+
export type { ISecretsProcessor, SecretsProcessorOptions, SecretsProcessorResult, } from "./processor.js";
|
|
3
|
+
export { diffSecrets, type SecretAction, type SecretChange } from "./diff.js";
|
|
3
4
|
export { GitHubSecretsStrategy } from "./github-secrets-strategy.js";
|
|
4
5
|
export { SodiumEncryptor, type ISecretEncryptor } from "./encryption.js";
|
|
5
6
|
export type { ISecretsStrategy, GitHubSecret, GitHubPublicKey, } from "./types.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RepoInfo } from "../../repo/index.js";
|
|
2
|
+
import type { RepoConfig } from "../../config/index.js";
|
|
3
|
+
import type { ISecretsStrategy } from "./types.js";
|
|
4
|
+
import type { ISecretEncryptor } from "./encryption.js";
|
|
5
|
+
import type { IEnvResolver } from "../../shared/env-resolver.js";
|
|
6
|
+
import { type BaseProcessorOptions, type BaseProcessorResult, type ISettingsProcessor, type ChangeCounts } from "../base-processor.js";
|
|
7
|
+
export type ISecretsProcessor = ISettingsProcessor<SecretsProcessorOptions, SecretsProcessorResult>;
|
|
8
|
+
export interface SecretsProcessorOptions extends BaseProcessorOptions {
|
|
9
|
+
noDelete?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface SecretsProcessorResult extends BaseProcessorResult {
|
|
12
|
+
changes?: ChangeCounts;
|
|
13
|
+
/** Set when the skip is "nothing configured here" rather than "not a GitHub repo". */
|
|
14
|
+
noSecretsConfigured?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare class SecretsProcessor implements ISecretsProcessor {
|
|
17
|
+
private readonly strategy;
|
|
18
|
+
private readonly encryptor;
|
|
19
|
+
private readonly envResolver;
|
|
20
|
+
constructor(strategy: ISecretsStrategy, encryptor: ISecretEncryptor, envResolver: IEnvResolver);
|
|
21
|
+
process(repoConfig: RepoConfig, repoInfo: RepoInfo, options: SecretsProcessorOptions): Promise<SecretsProcessorResult>;
|
|
22
|
+
private applySettings;
|
|
23
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { diffSecrets } from "./diff.js";
|
|
2
|
+
import { withGitHubGuards, countActions, buildDryRunResult, buildApplyResult, } from "../base-processor.js";
|
|
3
|
+
function secretEntriesOf(repoConfig) {
|
|
4
|
+
const { deleteOrphaned: _d, ...entries } = (repoConfig.settings?.secrets ??
|
|
5
|
+
{});
|
|
6
|
+
return Object.entries(entries);
|
|
7
|
+
}
|
|
8
|
+
function hasDesiredSecrets(repoConfig) {
|
|
9
|
+
const s = repoConfig.settings?.secrets ?? {};
|
|
10
|
+
const { deleteOrphaned, ...entries } = s;
|
|
11
|
+
return Object.keys(entries).length > 0 || deleteOrphaned === true;
|
|
12
|
+
}
|
|
13
|
+
export class SecretsProcessor {
|
|
14
|
+
strategy;
|
|
15
|
+
encryptor;
|
|
16
|
+
envResolver;
|
|
17
|
+
constructor(strategy, encryptor, envResolver) {
|
|
18
|
+
this.strategy = strategy;
|
|
19
|
+
this.encryptor = encryptor;
|
|
20
|
+
this.envResolver = envResolver;
|
|
21
|
+
}
|
|
22
|
+
async process(repoConfig, repoInfo, options) {
|
|
23
|
+
const result = await withGitHubGuards(repoConfig, repoInfo, options, {
|
|
24
|
+
hasDesiredSettings: hasDesiredSecrets,
|
|
25
|
+
emptySettingsMessage: "No secrets configured",
|
|
26
|
+
applySettings: (githubRepo, rc, opts, token, repoName) => this.applySettings(githubRepo, rc, opts, token, repoName),
|
|
27
|
+
});
|
|
28
|
+
if (result.skipped && !hasDesiredSecrets(repoConfig)) {
|
|
29
|
+
return { ...result, noSecretsConfigured: true };
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
async applySettings(githubRepo, repoConfig, options, effectiveToken, repoName) {
|
|
34
|
+
const { dryRun, noDelete } = options;
|
|
35
|
+
const secrets = (repoConfig.settings?.secrets ?? {});
|
|
36
|
+
const configDeleteOrphaned = secrets.deleteOrphaned === true;
|
|
37
|
+
const deleteOrphaned = configDeleteOrphaned && !(noDelete ?? false);
|
|
38
|
+
const secretEntries = secretEntriesOf(repoConfig).filter((entry) => typeof entry[1] !== "boolean");
|
|
39
|
+
const strategyOptions = { token: effectiveToken, host: githubRepo.host };
|
|
40
|
+
const currentSecrets = await this.strategy.list(githubRepo, strategyOptions);
|
|
41
|
+
const changes = diffSecrets(currentSecrets, secretEntries.map(([name]) => name), deleteOrphaned);
|
|
42
|
+
const changeCounts = countActions(changes);
|
|
43
|
+
if (dryRun) {
|
|
44
|
+
return buildDryRunResult(repoName, changeCounts);
|
|
45
|
+
}
|
|
46
|
+
const resolvedValues = secretEntries.length > 0
|
|
47
|
+
? this.envResolver.resolveAll(secretEntries.map(([name, config]) => ({
|
|
48
|
+
name,
|
|
49
|
+
envVar: config.env,
|
|
50
|
+
})))
|
|
51
|
+
: new Map();
|
|
52
|
+
let appliedCount = 0;
|
|
53
|
+
const publicKey = secretEntries.length > 0
|
|
54
|
+
? await this.strategy.getPublicKey(githubRepo, strategyOptions)
|
|
55
|
+
: undefined;
|
|
56
|
+
for (const change of changes) {
|
|
57
|
+
switch (change.action) {
|
|
58
|
+
case "create":
|
|
59
|
+
case "update": {
|
|
60
|
+
const encrypted = await this.encryptor.encrypt(resolvedValues.get(change.name), publicKey.key);
|
|
61
|
+
await this.strategy.upsert(githubRepo, change.name, encrypted, publicKey.key_id, strategyOptions);
|
|
62
|
+
appliedCount++;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case "delete":
|
|
66
|
+
await this.strategy.delete(githubRepo, change.name, strategyOptions);
|
|
67
|
+
appliedCount++;
|
|
68
|
+
break;
|
|
69
|
+
case "unchanged":
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return buildApplyResult(repoName, changeCounts, appliedCount);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { RepoInfo } from "
|
|
2
|
-
import type { GhApiOptions } from "
|
|
1
|
+
import type { RepoInfo } from "../../repo/index.js";
|
|
2
|
+
import type { GhApiOptions } from "../../shared/gh-api-utils.js";
|
|
3
3
|
export interface GitHubSecret {
|
|
4
4
|
name: string;
|
|
5
5
|
created_at: string;
|
|
@@ -35,14 +35,12 @@ export class FileModeFixupCommitStrategy {
|
|
|
35
35
|
if (!isGitHubRepo(options.repoInfo)) {
|
|
36
36
|
return this.inner.commit(options);
|
|
37
37
|
}
|
|
38
|
-
let parentSha;
|
|
39
38
|
let baseResult;
|
|
40
39
|
if (hasContentChanges) {
|
|
41
40
|
baseResult = await this.inner.commit(options);
|
|
42
|
-
parentSha = baseResult.sha;
|
|
43
41
|
}
|
|
44
42
|
else {
|
|
45
|
-
parentSha = await this.resolveBranchHeadSha(options.repoInfo, options.branchName, options.baseBranch, options.workDir, options.retries ?? 3, options.token);
|
|
43
|
+
const parentSha = await this.resolveBranchHeadSha(options.repoInfo, options.branchName, options.baseBranch, options.workDir, options.retries ?? 3, options.token);
|
|
46
44
|
baseResult = { sha: parentSha, verified: true, pushed: true };
|
|
47
45
|
}
|
|
48
46
|
return await this.createFixupCommit(options.repoInfo, options.branchName, baseResult, executableFiles, options.workDir, options.retries ?? 3, options.token);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aspruyt/xfg",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.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",
|
|
@@ -42,6 +42,8 @@
|
|
|
42
42
|
"test:integration:gitlab": "npm run build && node --import tsx --test test/integration/gitlab.test.ts",
|
|
43
43
|
"test:integration:github-app": "npm run build && node --import tsx --test test/integration/github-app.test.ts",
|
|
44
44
|
"test:integration:github-rulesets": "npm run build && node --import tsx --test test/integration/github-rulesets.test.ts",
|
|
45
|
+
"test:integration:github-secrets": "npm run build && node --import tsx --test test/integration/github-secrets.test.ts",
|
|
46
|
+
"test:integration:github-variables": "npm run build && node --import tsx --test test/integration/github-variables.test.ts",
|
|
45
47
|
"test:integration:github-repo-settings": "npm run build && node --import tsx --test test/integration/github-repo-settings.test.ts",
|
|
46
48
|
"test:integration:github-labels": "npm run build && node --import tsx --test test/integration/github-labels.test.ts",
|
|
47
49
|
"test:integration:github-code-scanning": "npm run build && node --import tsx --test test/integration/github-code-scanning.test.ts",
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { type RepoInfo } from "../repo/index.js";
|
|
2
|
-
import type { ISecretsStrategy } from "./types.js";
|
|
3
|
-
import type { ISecretEncryptor } from "./encryption.js";
|
|
4
|
-
import type { IEnvResolver } from "../shared/env-resolver.js";
|
|
5
|
-
import type { SecretConfig } from "../config/index.js";
|
|
6
|
-
type SecretsConfig = Record<string, SecretConfig | boolean> & {
|
|
7
|
-
deleteOrphaned?: boolean;
|
|
8
|
-
};
|
|
9
|
-
export interface SecretsProcessorOptions {
|
|
10
|
-
dryRun?: boolean;
|
|
11
|
-
token?: string;
|
|
12
|
-
noDelete?: boolean;
|
|
13
|
-
}
|
|
14
|
-
export interface SecretsProcessorResult {
|
|
15
|
-
success: boolean;
|
|
16
|
-
repoName: string;
|
|
17
|
-
message: string;
|
|
18
|
-
skipped?: boolean;
|
|
19
|
-
dryRun?: boolean;
|
|
20
|
-
created: number;
|
|
21
|
-
updated: number;
|
|
22
|
-
deleted: number;
|
|
23
|
-
}
|
|
24
|
-
export declare class SecretsProcessor {
|
|
25
|
-
private readonly strategy;
|
|
26
|
-
private readonly encryptor;
|
|
27
|
-
private readonly envResolver;
|
|
28
|
-
constructor(strategy: ISecretsStrategy, encryptor: ISecretEncryptor, envResolver: IEnvResolver);
|
|
29
|
-
process(secretsConfig: SecretsConfig, repoInfo: RepoInfo, options: SecretsProcessorOptions): Promise<SecretsProcessorResult>;
|
|
30
|
-
}
|
|
31
|
-
export {};
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { isGitHubRepo, getRepoDisplayName, } from "../repo/index.js";
|
|
2
|
-
export class SecretsProcessor {
|
|
3
|
-
strategy;
|
|
4
|
-
encryptor;
|
|
5
|
-
envResolver;
|
|
6
|
-
constructor(strategy, encryptor, envResolver) {
|
|
7
|
-
this.strategy = strategy;
|
|
8
|
-
this.encryptor = encryptor;
|
|
9
|
-
this.envResolver = envResolver;
|
|
10
|
-
}
|
|
11
|
-
async process(secretsConfig, repoInfo, options) {
|
|
12
|
-
const repoName = getRepoDisplayName(repoInfo);
|
|
13
|
-
if (!isGitHubRepo(repoInfo)) {
|
|
14
|
-
return {
|
|
15
|
-
success: true,
|
|
16
|
-
repoName,
|
|
17
|
-
message: "Skipped: not a GitHub repository",
|
|
18
|
-
skipped: true,
|
|
19
|
-
created: 0,
|
|
20
|
-
updated: 0,
|
|
21
|
-
deleted: 0,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
const githubRepo = repoInfo;
|
|
25
|
-
const { deleteOrphaned: configDeleteOrphaned = false, ...rawEntries } = secretsConfig;
|
|
26
|
-
const { dryRun, token, noDelete } = options;
|
|
27
|
-
const deleteOrphaned = configDeleteOrphaned && !(noDelete ?? false);
|
|
28
|
-
const strategyOptions = { token, host: githubRepo.host };
|
|
29
|
-
const secretEntries = Object.entries(rawEntries).filter((entry) => typeof entry[1] !== "boolean");
|
|
30
|
-
let resolvedValues;
|
|
31
|
-
if (!dryRun && secretEntries.length > 0) {
|
|
32
|
-
resolvedValues = this.envResolver.resolveAll(secretEntries.map(([name, config]) => ({
|
|
33
|
-
name,
|
|
34
|
-
envVar: config.env,
|
|
35
|
-
})));
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
resolvedValues = new Map();
|
|
39
|
-
}
|
|
40
|
-
const currentSecrets = await this.strategy.list(githubRepo, strategyOptions);
|
|
41
|
-
const currentByName = new Set(currentSecrets.map((s) => s.name.toUpperCase()));
|
|
42
|
-
const desiredNames = new Set(secretEntries.map(([name]) => name.toUpperCase()));
|
|
43
|
-
let created = 0;
|
|
44
|
-
let updated = 0;
|
|
45
|
-
let deleted = 0;
|
|
46
|
-
if (!dryRun) {
|
|
47
|
-
if (secretEntries.length > 0) {
|
|
48
|
-
const publicKey = await this.strategy.getPublicKey(githubRepo, strategyOptions);
|
|
49
|
-
for (const [name] of secretEntries) {
|
|
50
|
-
const value = resolvedValues.get(name);
|
|
51
|
-
const encrypted = await this.encryptor.encrypt(value, publicKey.key);
|
|
52
|
-
await this.strategy.upsert(githubRepo, name, encrypted, publicKey.key_id, strategyOptions);
|
|
53
|
-
if (currentByName.has(name.toUpperCase())) {
|
|
54
|
-
updated++;
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
created++;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
if (deleteOrphaned) {
|
|
62
|
-
for (const current of currentSecrets) {
|
|
63
|
-
if (!desiredNames.has(current.name.toUpperCase())) {
|
|
64
|
-
await this.strategy.delete(githubRepo, current.name, strategyOptions);
|
|
65
|
-
deleted++;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
for (const [name] of secretEntries) {
|
|
72
|
-
if (currentByName.has(name.toUpperCase())) {
|
|
73
|
-
updated++;
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
created++;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (deleteOrphaned) {
|
|
80
|
-
for (const current of currentSecrets) {
|
|
81
|
-
if (!desiredNames.has(current.name.toUpperCase())) {
|
|
82
|
-
deleted++;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const parts = [];
|
|
88
|
-
if (created > 0)
|
|
89
|
-
parts.push(`${created} created`);
|
|
90
|
-
if (updated > 0)
|
|
91
|
-
parts.push(`${updated} updated`);
|
|
92
|
-
if (deleted > 0)
|
|
93
|
-
parts.push(`${deleted} deleted`);
|
|
94
|
-
const summary = parts.length > 0 ? parts.join(", ") : "no changes";
|
|
95
|
-
if (dryRun) {
|
|
96
|
-
return {
|
|
97
|
-
success: true,
|
|
98
|
-
repoName,
|
|
99
|
-
message: `[DRY RUN] ${summary}`,
|
|
100
|
-
dryRun: true,
|
|
101
|
-
created,
|
|
102
|
-
updated,
|
|
103
|
-
deleted,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
return {
|
|
107
|
-
success: true,
|
|
108
|
-
repoName,
|
|
109
|
-
message: summary,
|
|
110
|
-
created,
|
|
111
|
-
updated,
|
|
112
|
-
deleted,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|