@simplysf/simply-gitlab-core 0.2.0 → 0.3.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/lib/baseline-apply.d.ts +35 -0
- package/lib/baseline-apply.js +177 -0
- package/lib/baseline-apply.js.map +1 -0
- package/lib/baseline-config.d.ts +100 -0
- package/lib/baseline-config.js +342 -0
- package/lib/baseline-config.js.map +1 -0
- package/lib/baseline-export.d.ts +21 -0
- package/lib/baseline-export.js +245 -0
- package/lib/baseline-export.js.map +1 -0
- package/lib/baseline-plan.d.ts +96 -0
- package/lib/baseline-plan.js +412 -0
- package/lib/baseline-plan.js.map +1 -0
- package/lib/baseline-render.d.ts +13 -0
- package/lib/baseline-render.js +139 -0
- package/lib/baseline-render.js.map +1 -0
- package/lib/baseline-run.d.ts +49 -0
- package/lib/baseline-run.js +102 -0
- package/lib/baseline-run.js.map +1 -0
- package/lib/baseline-targets.d.ts +31 -0
- package/lib/baseline-targets.js +100 -0
- package/lib/baseline-targets.js.map +1 -0
- package/lib/gitlab-client.d.ts +32 -0
- package/lib/gitlab-client.js +96 -0
- package/lib/gitlab-client.js.map +1 -1
- package/lib/index.d.ts +7 -0
- package/lib/index.js +8 -0
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { BaselinePlan, TargetPlan } from './baseline-plan.js';
|
|
2
|
+
import { type RunOptions } from './baseline-run.js';
|
|
3
|
+
import type { GitLabClient } from './gitlab-client.js';
|
|
4
|
+
/** What executing one target's plan amounted to. */
|
|
5
|
+
export interface TargetResult {
|
|
6
|
+
readonly path: string;
|
|
7
|
+
readonly kind: 'project' | 'group';
|
|
8
|
+
readonly applied: number;
|
|
9
|
+
/** Set when execution stopped part-way; the actions before it are already applied. */
|
|
10
|
+
readonly error?: string;
|
|
11
|
+
/** The section that failed, so a partial application can be described precisely. */
|
|
12
|
+
readonly failedSection?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ApplyResult {
|
|
15
|
+
readonly targets: readonly TargetResult[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Executes one target's plan, in the order the sections were planned.
|
|
19
|
+
*
|
|
20
|
+
* There is no transaction across GitLab endpoints and this does not pretend otherwise: a failure
|
|
21
|
+
* part-way leaves the earlier sections applied, and the result says which section stopped it so a
|
|
22
|
+
* following `plan` can be read against that.
|
|
23
|
+
*/
|
|
24
|
+
export declare function applyTarget(client: GitLabClient, target: TargetPlan): Promise<TargetResult>;
|
|
25
|
+
/**
|
|
26
|
+
* Executes a whole plan.
|
|
27
|
+
*
|
|
28
|
+
* One target failing does not abandon the other forty-nine — that is the default, because a run
|
|
29
|
+
* that stopped at the twelfth project would leave the operator worse off than one that reported
|
|
30
|
+
* twelve successes and one failure. `--fail-fast` is there for the systematic problem where the
|
|
31
|
+
* rest are pointless.
|
|
32
|
+
*/
|
|
33
|
+
export declare function applyPlan(client: GitLabClient, plan: BaselinePlan, options?: RunOptions): Promise<ApplyResult>;
|
|
34
|
+
/** How many targets an apply failed on, for the exit code. */
|
|
35
|
+
export declare function applyFailures(result: ApplyResult): readonly TargetResult[];
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026, SimplySF.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { mapPool, DEFAULT_CONCURRENCY } from './baseline-run.js';
|
|
17
|
+
import { ConfigError } from './errors.js';
|
|
18
|
+
function asRecord(value) {
|
|
19
|
+
return (typeof value === 'object' && value !== null ? value : {});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolves the name-based references an approval rule carries into the ids the API demands.
|
|
23
|
+
*
|
|
24
|
+
* This has to happen here rather than at plan time. A rule scoped to `main` needs that branch's
|
|
25
|
+
* protection id, and the protected-branches section that creates it runs moments earlier in this
|
|
26
|
+
* same apply — so at plan time the id does not exist yet.
|
|
27
|
+
*/
|
|
28
|
+
async function resolveRuleReferences(client, ref, body, branches, groups) {
|
|
29
|
+
const resolved = { ...body };
|
|
30
|
+
if (branches !== undefined && branches.length > 0) {
|
|
31
|
+
const protectedBranches = (await client.listProtectedBranches(ref)).items.map(asRecord);
|
|
32
|
+
const byName = new Map(protectedBranches.map((branch) => [String(branch.name), branch.id]));
|
|
33
|
+
const ids = [];
|
|
34
|
+
for (const name of branches) {
|
|
35
|
+
const id = byName.get(name);
|
|
36
|
+
if (typeof id !== 'number') {
|
|
37
|
+
throw new ConfigError(`Approval rule references protected branch "${name}", which is not protected on this project. ` +
|
|
38
|
+
'Declare it under protectedBranches so it exists before the rule is written.');
|
|
39
|
+
}
|
|
40
|
+
ids.push(id);
|
|
41
|
+
}
|
|
42
|
+
resolved.protected_branch_ids = ids;
|
|
43
|
+
}
|
|
44
|
+
if (groups !== undefined && groups.length > 0) {
|
|
45
|
+
const ids = [];
|
|
46
|
+
/* Each group is one lookup; there are rarely more than a couple on a rule. */
|
|
47
|
+
/* eslint-disable no-await-in-loop */
|
|
48
|
+
for (const path of groups) {
|
|
49
|
+
const group = asRecord(await client.getGroup(path));
|
|
50
|
+
if (typeof group.id !== 'number') {
|
|
51
|
+
throw new ConfigError(`Approval rule references group "${path}", which could not be resolved to an id.`);
|
|
52
|
+
}
|
|
53
|
+
ids.push(group.id);
|
|
54
|
+
}
|
|
55
|
+
/* eslint-enable no-await-in-loop */
|
|
56
|
+
resolved.group_ids = ids;
|
|
57
|
+
}
|
|
58
|
+
return resolved;
|
|
59
|
+
}
|
|
60
|
+
/** Executes one action. Split out so the switch stays exhaustive as actions are added. */
|
|
61
|
+
async function execute(client, target, action) {
|
|
62
|
+
const ref = target.path;
|
|
63
|
+
switch (action.kind) {
|
|
64
|
+
case 'updateProject':
|
|
65
|
+
await client.updateProject(ref, action.body);
|
|
66
|
+
return;
|
|
67
|
+
case 'updateGroup':
|
|
68
|
+
await client.updateGroup(ref, action.body);
|
|
69
|
+
return;
|
|
70
|
+
case 'protectBranch':
|
|
71
|
+
await client.protectBranch(ref, action.body);
|
|
72
|
+
return;
|
|
73
|
+
case 'patchProtectedBranch':
|
|
74
|
+
await client.patchProtectedBranch(ref, action.name, action.body);
|
|
75
|
+
return;
|
|
76
|
+
case 'unprotectBranch':
|
|
77
|
+
await client.unprotectBranch(ref, action.name);
|
|
78
|
+
return;
|
|
79
|
+
case 'protectTag':
|
|
80
|
+
await client.protectTag(ref, action.body);
|
|
81
|
+
return;
|
|
82
|
+
case 'unprotectTag':
|
|
83
|
+
await client.unprotectTag(ref, action.name);
|
|
84
|
+
return;
|
|
85
|
+
case 'updateApprovalSettings':
|
|
86
|
+
await client.updateApprovalSettings(ref, action.body);
|
|
87
|
+
return;
|
|
88
|
+
case 'createApprovalRule': {
|
|
89
|
+
const groups = Array.isArray(action.body.groups) ? action.body.groups : undefined;
|
|
90
|
+
const body = await resolveRuleReferences(client, ref, action.body, action.branches, groups);
|
|
91
|
+
await client.createApprovalRule(ref, body);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
case 'updateApprovalRule': {
|
|
95
|
+
const groups = Array.isArray(action.body.groups) ? action.body.groups : undefined;
|
|
96
|
+
const body = await resolveRuleReferences(client, ref, action.body, action.branches, groups);
|
|
97
|
+
await client.updateApprovalRule(ref, action.ruleId, body);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
case 'deleteApprovalRule':
|
|
101
|
+
await client.deleteApprovalRule(ref, action.ruleId);
|
|
102
|
+
return;
|
|
103
|
+
case 'setPushRules':
|
|
104
|
+
// POST creates the rule set, PUT edits it. Which one applies was decided at plan time from
|
|
105
|
+
// whether the project already had push rules.
|
|
106
|
+
if (action.exists)
|
|
107
|
+
await client.updatePushRules(ref, action.body);
|
|
108
|
+
else
|
|
109
|
+
await client.createPushRules(ref, action.body);
|
|
110
|
+
return;
|
|
111
|
+
default: {
|
|
112
|
+
const exhaustive = action;
|
|
113
|
+
throw new ConfigError(`Unknown action ${JSON.stringify(exhaustive)}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Executes one target's plan, in the order the sections were planned.
|
|
119
|
+
*
|
|
120
|
+
* There is no transaction across GitLab endpoints and this does not pretend otherwise: a failure
|
|
121
|
+
* part-way leaves the earlier sections applied, and the result says which section stopped it so a
|
|
122
|
+
* following `plan` can be read against that.
|
|
123
|
+
*/
|
|
124
|
+
export async function applyTarget(client, target) {
|
|
125
|
+
let applied = 0;
|
|
126
|
+
for (const section of target.sections) {
|
|
127
|
+
for (const action of section.actions) {
|
|
128
|
+
try {
|
|
129
|
+
// eslint-disable-next-line no-await-in-loop -- order across sections is the contract
|
|
130
|
+
await execute(client, target, action);
|
|
131
|
+
applied += 1;
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
return {
|
|
135
|
+
path: target.path,
|
|
136
|
+
kind: target.kind,
|
|
137
|
+
applied,
|
|
138
|
+
failedSection: section.section,
|
|
139
|
+
error: error instanceof Error ? error.message : String(error),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { path: target.path, kind: target.kind, applied };
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Executes a whole plan.
|
|
148
|
+
*
|
|
149
|
+
* One target failing does not abandon the other forty-nine — that is the default, because a run
|
|
150
|
+
* that stopped at the twelfth project would leave the operator worse off than one that reported
|
|
151
|
+
* twelve successes and one failure. `--fail-fast` is there for the systematic problem where the
|
|
152
|
+
* rest are pointless.
|
|
153
|
+
*/
|
|
154
|
+
export async function applyPlan(client, plan, options = {}) {
|
|
155
|
+
const concurrency = options.concurrency ?? DEFAULT_CONCURRENCY;
|
|
156
|
+
// A target that failed to plan is not applied: acting on a half-read project is how a baseline
|
|
157
|
+
// makes things worse than it found them.
|
|
158
|
+
const runnable = plan.targets.filter((target) => target.error === undefined);
|
|
159
|
+
const unplanned = plan.targets
|
|
160
|
+
.filter((target) => target.error !== undefined)
|
|
161
|
+
.map((target) => ({ path: target.path, kind: target.kind, applied: 0, error: target.error }));
|
|
162
|
+
const groups = runnable.filter((target) => target.kind === 'group');
|
|
163
|
+
const projects = runnable.filter((target) => target.kind === 'project');
|
|
164
|
+
const stopAfter = options.failFast === true ? (result) => result.error !== undefined : undefined;
|
|
165
|
+
// Groups first: a group setting that projects inherit should be in place before the projects are
|
|
166
|
+
// reconciled against it.
|
|
167
|
+
const groupResults = await mapPool(groups, concurrency, (target) => applyTarget(client, target), stopAfter);
|
|
168
|
+
const projectResults = options.failFast === true && groupResults.some((result) => result.error !== undefined)
|
|
169
|
+
? []
|
|
170
|
+
: await mapPool(projects, concurrency, (target) => applyTarget(client, target), stopAfter);
|
|
171
|
+
return { targets: [...unplanned, ...groupResults, ...projectResults] };
|
|
172
|
+
}
|
|
173
|
+
/** How many targets an apply failed on, for the exit code. */
|
|
174
|
+
export function applyFailures(result) {
|
|
175
|
+
return result.targets.filter((target) => target.error !== undefined);
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=baseline-apply.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"baseline-apply.js","sourceRoot":"","sources":["../src/baseline-apply.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,OAAO,EAAmB,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AA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,CAAC,MAAM,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;IAExG,iGAAiG;IACjG,yBAAyB;IACzB,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;IAE5G,MAAM,cAAc,GAClB,OAAO,CAAC,QAAQ,KAAK,IAAI,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,KAAK,SAAS,CAAC;QACpF,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,MAAM,OAAO,CAAC,QAAQ,EAAE,WAAW,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;IAE/F,OAAO,EAAE,OAAO,EAAE,CAAC,GAAG,SAAS,EAAE,GAAG,YAAY,EAAE,GAAG,cAAc,CAAC,EAAE,CAAC;AACzE,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,aAAa,CAAC,MAAmB;IAC/C,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;AACvE,CAAC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The baseline configuration file: what a set of projects should have in common.
|
|
3
|
+
*
|
|
4
|
+
* Everything here is data. Reading it, diffing against an instance, and converging are three other
|
|
5
|
+
* modules; this one only turns a file into a validated, merged description of intent — which is
|
|
6
|
+
* what makes the merge semantics below testable without a server.
|
|
7
|
+
*
|
|
8
|
+
* See docs/design/0007-configuration-baselines.md.
|
|
9
|
+
*/
|
|
10
|
+
export interface BaselineConfig {
|
|
11
|
+
readonly version: 1;
|
|
12
|
+
readonly targets?: BaselineTargets;
|
|
13
|
+
readonly group?: GroupDesired;
|
|
14
|
+
readonly project?: ProjectDesired;
|
|
15
|
+
readonly overrides?: Readonly<Record<string, TargetOverride>>;
|
|
16
|
+
}
|
|
17
|
+
export interface BaselineTargets {
|
|
18
|
+
/** Group paths or ids. Expands to every project in the group and its subgroups. */
|
|
19
|
+
readonly groups?: readonly string[];
|
|
20
|
+
/** Individual project paths or ids, added to whatever the groups expanded to. */
|
|
21
|
+
readonly projects?: readonly string[];
|
|
22
|
+
/** Removed from the union. A trailing `*` drops a whole subtree. */
|
|
23
|
+
readonly exclude?: readonly string[];
|
|
24
|
+
}
|
|
25
|
+
export interface TargetOverride {
|
|
26
|
+
readonly project?: ProjectDesired;
|
|
27
|
+
readonly group?: GroupDesired;
|
|
28
|
+
}
|
|
29
|
+
export interface GroupDesired {
|
|
30
|
+
readonly settings?: Readonly<Record<string, unknown>>;
|
|
31
|
+
}
|
|
32
|
+
export interface ProjectDesired {
|
|
33
|
+
readonly settings?: Readonly<Record<string, unknown>>;
|
|
34
|
+
readonly protectedBranches?: Section;
|
|
35
|
+
readonly protectedTags?: Section;
|
|
36
|
+
readonly approvals?: Readonly<Record<string, unknown>>;
|
|
37
|
+
readonly approvalRules?: Section;
|
|
38
|
+
readonly pushRules?: Readonly<Record<string, unknown>>;
|
|
39
|
+
readonly variables?: Section;
|
|
40
|
+
}
|
|
41
|
+
/** An entry in a keyed section. Fields are GitLab's own, so this stays deliberately open. */
|
|
42
|
+
export type Entry = Readonly<Record<string, unknown>>;
|
|
43
|
+
/**
|
|
44
|
+
* A list section, in either of the two shapes the file accepts: a bare array, or an object that
|
|
45
|
+
* can also turn on pruning or wholesale replacement.
|
|
46
|
+
*/
|
|
47
|
+
export type Section = readonly Entry[] | SectionObject;
|
|
48
|
+
export interface SectionObject {
|
|
49
|
+
/** Delete entries that exist on the instance but are not declared here. Off by default. */
|
|
50
|
+
readonly prune?: boolean;
|
|
51
|
+
/** Replace the baseline's entries entirely instead of merging into them, in an override. */
|
|
52
|
+
readonly $replace?: boolean;
|
|
53
|
+
readonly entries: readonly Entry[];
|
|
54
|
+
}
|
|
55
|
+
/** A section reduced to the three things every consumer needs. */
|
|
56
|
+
export interface NormalizedSection {
|
|
57
|
+
readonly prune: boolean;
|
|
58
|
+
readonly replace: boolean;
|
|
59
|
+
readonly entries: readonly Entry[];
|
|
60
|
+
}
|
|
61
|
+
/** The list sections, in the order `apply` reconciles them. */
|
|
62
|
+
export declare const LIST_SECTIONS: readonly ["protectedBranches", "protectedTags", "approvalRules", "variables"];
|
|
63
|
+
export type ListSection = (typeof LIST_SECTIONS)[number];
|
|
64
|
+
/** The object sections, which merge key by key rather than by entry. */
|
|
65
|
+
export declare const OBJECT_SECTIONS: readonly ["settings", "approvals", "pushRules"];
|
|
66
|
+
/** The identity of one entry, as a string, for keying a map. */
|
|
67
|
+
export declare function entryKey(section: ListSection, entry: Entry): string;
|
|
68
|
+
/** The human-readable identity of one entry, for plan output. */
|
|
69
|
+
export declare function entryLabel(section: ListSection, entry: Entry): string;
|
|
70
|
+
export declare function normalizeSection(section: Section | undefined): NormalizedSection | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Validates the parsed file, reporting the JSON path of whatever is wrong.
|
|
73
|
+
*
|
|
74
|
+
* Hand-written rather than schema-driven because the error is the product: a caller who wrote
|
|
75
|
+
* `"targets": {"group": "x"}` needs to be told that the key is `groups` and that it takes an array,
|
|
76
|
+
* at that path, not that the document failed to match a schema. The published JSON Schema gives an
|
|
77
|
+
* editor completion and hover text; this is what runs.
|
|
78
|
+
*/
|
|
79
|
+
export declare function validateBaselineConfig(value: unknown, label?: string): BaselineConfig;
|
|
80
|
+
/**
|
|
81
|
+
* The effective configuration for one project: the baseline with its override merged over it.
|
|
82
|
+
*
|
|
83
|
+
* Objects merge key by key. Lists merge by the section's natural key, so an override adjusts one
|
|
84
|
+
* entry rather than restating the array — unless it sets `$replace`, which swaps the list wholesale
|
|
85
|
+
* for the project that genuinely needs different rules rather than adjusted ones.
|
|
86
|
+
*/
|
|
87
|
+
export declare function mergeProjectDesired(base?: ProjectDesired, override?: ProjectDesired): ProjectDesired;
|
|
88
|
+
export declare function mergeGroupDesired(base?: GroupDesired, override?: GroupDesired): GroupDesired;
|
|
89
|
+
/** The effective project configuration for one path, baseline merged with any override for it. */
|
|
90
|
+
export declare function effectiveProject(config: BaselineConfig, path: string): ProjectDesired;
|
|
91
|
+
/** The effective group configuration for one path. */
|
|
92
|
+
export declare function effectiveGroup(config: BaselineConfig, path: string): GroupDesired;
|
|
93
|
+
/**
|
|
94
|
+
* Reads and validates a baseline config file.
|
|
95
|
+
*
|
|
96
|
+
* A parse failure reports only the position, never the parser's snippet of the content — the same
|
|
97
|
+
* rule the rest of this package follows, because a caller can name any path and the snippet would
|
|
98
|
+
* echo the first bytes of whatever file that was.
|
|
99
|
+
*/
|
|
100
|
+
export declare function loadBaselineConfig(path: string): BaselineConfig;
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026, SimplySF.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { ConfigError } from './errors.js';
|
|
17
|
+
import { readTextFile } from './json-input.js';
|
|
18
|
+
/** The list sections, in the order `apply` reconciles them. */
|
|
19
|
+
export const LIST_SECTIONS = ['protectedBranches', 'protectedTags', 'approvalRules', 'variables'];
|
|
20
|
+
/** The object sections, which merge key by key rather than by entry. */
|
|
21
|
+
export const OBJECT_SECTIONS = ['settings', 'approvals', 'pushRules'];
|
|
22
|
+
/**
|
|
23
|
+
* What identifies an entry within its section, for merging an override onto a baseline and for
|
|
24
|
+
* matching a declared entry against one that already exists on the instance.
|
|
25
|
+
*
|
|
26
|
+
* Every section has a natural key, which is what makes a two-line override possible: restating a
|
|
27
|
+
* whole array to change one number is how these files rot.
|
|
28
|
+
*/
|
|
29
|
+
const SECTION_KEYS = {
|
|
30
|
+
protectedBranches: ['name'],
|
|
31
|
+
protectedTags: ['name'],
|
|
32
|
+
approvalRules: ['name'],
|
|
33
|
+
variables: ['key', 'environment_scope'],
|
|
34
|
+
};
|
|
35
|
+
/** GitLab treats an unset environment scope as `*`, so the merge key has to agree with it. */
|
|
36
|
+
const SCOPE_DEFAULTS = { environment_scope: '*' };
|
|
37
|
+
/**
|
|
38
|
+
* Renders a key field as text.
|
|
39
|
+
*
|
|
40
|
+
* Key fields come out of parsed JSON, so they are `unknown` until proven otherwise. Coercing an
|
|
41
|
+
* object with `String()` would key every such entry as `[object Object]` and silently collide
|
|
42
|
+
* entries that are not the same thing at all.
|
|
43
|
+
*/
|
|
44
|
+
function keyText(value) {
|
|
45
|
+
if (value === undefined || value === null)
|
|
46
|
+
return '';
|
|
47
|
+
if (typeof value === 'string')
|
|
48
|
+
return value;
|
|
49
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
50
|
+
return String(value);
|
|
51
|
+
return JSON.stringify(value) ?? '';
|
|
52
|
+
}
|
|
53
|
+
/** The identity of one entry, as a string, for keying a map. */
|
|
54
|
+
export function entryKey(section, entry) {
|
|
55
|
+
return SECTION_KEYS[section].map((field) => keyText(entry[field] ?? SCOPE_DEFAULTS[field])).join(' ');
|
|
56
|
+
}
|
|
57
|
+
/** The human-readable identity of one entry, for plan output. */
|
|
58
|
+
export function entryLabel(section, entry) {
|
|
59
|
+
return SECTION_KEYS[section]
|
|
60
|
+
.map((field) => keyText(entry[field] ?? SCOPE_DEFAULTS[field]))
|
|
61
|
+
.filter((part) => part !== '')
|
|
62
|
+
.join(' @ ');
|
|
63
|
+
}
|
|
64
|
+
function isPlainObject(value) {
|
|
65
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
66
|
+
}
|
|
67
|
+
export function normalizeSection(section) {
|
|
68
|
+
if (section === undefined)
|
|
69
|
+
return undefined;
|
|
70
|
+
if (Array.isArray(section))
|
|
71
|
+
return { prune: false, replace: false, entries: section };
|
|
72
|
+
const object = section;
|
|
73
|
+
return { prune: object.prune === true, replace: object.$replace === true, entries: object.entries };
|
|
74
|
+
}
|
|
75
|
+
// --- Validation ---------------------------------------------------------------------------------
|
|
76
|
+
/**
|
|
77
|
+
* Validates the parsed file, reporting the JSON path of whatever is wrong.
|
|
78
|
+
*
|
|
79
|
+
* Hand-written rather than schema-driven because the error is the product: a caller who wrote
|
|
80
|
+
* `"targets": {"group": "x"}` needs to be told that the key is `groups` and that it takes an array,
|
|
81
|
+
* at that path, not that the document failed to match a schema. The published JSON Schema gives an
|
|
82
|
+
* editor completion and hover text; this is what runs.
|
|
83
|
+
*/
|
|
84
|
+
export function validateBaselineConfig(value, label = 'config') {
|
|
85
|
+
const problems = [];
|
|
86
|
+
const at = (path, message) => {
|
|
87
|
+
problems.push(`${path}: ${message}`);
|
|
88
|
+
};
|
|
89
|
+
if (!isPlainObject(value)) {
|
|
90
|
+
throw new ConfigError(`${label} must be a JSON object.`);
|
|
91
|
+
}
|
|
92
|
+
if (value.version !== 1) {
|
|
93
|
+
at('version', `must be 1, got ${JSON.stringify(value.version)}`);
|
|
94
|
+
}
|
|
95
|
+
const known = new Set(['$schema', 'version', 'targets', 'group', 'project', 'overrides']);
|
|
96
|
+
for (const key of Object.keys(value)) {
|
|
97
|
+
if (!known.has(key))
|
|
98
|
+
at(key, `is not a known top-level key (expected one of ${[...known].join(', ')})`);
|
|
99
|
+
}
|
|
100
|
+
if (value.targets !== undefined)
|
|
101
|
+
validateTargets(value.targets, 'targets', at);
|
|
102
|
+
if (value.group !== undefined)
|
|
103
|
+
validateGroupDesired(value.group, 'group', at);
|
|
104
|
+
if (value.project !== undefined)
|
|
105
|
+
validateProjectDesired(value.project, 'project', at);
|
|
106
|
+
if (value.overrides !== undefined) {
|
|
107
|
+
if (!isPlainObject(value.overrides)) {
|
|
108
|
+
at('overrides', 'must be an object keyed by project or group path');
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
for (const [path, override] of Object.entries(value.overrides)) {
|
|
112
|
+
const where = `overrides["${path}"]`;
|
|
113
|
+
if (!isPlainObject(override)) {
|
|
114
|
+
at(where, 'must be an object');
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
for (const key of Object.keys(override)) {
|
|
118
|
+
if (key !== 'project' && key !== 'group')
|
|
119
|
+
at(`${where}.${key}`, 'must be "project" or "group"');
|
|
120
|
+
}
|
|
121
|
+
if (override.project !== undefined)
|
|
122
|
+
validateProjectDesired(override.project, `${where}.project`, at);
|
|
123
|
+
if (override.group !== undefined)
|
|
124
|
+
validateGroupDesired(override.group, `${where}.group`, at);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (problems.length > 0) {
|
|
129
|
+
// Every problem at once. Fixing a config one error per run, fifty projects into a change, is
|
|
130
|
+
// the kind of loop that makes people stop using the tool.
|
|
131
|
+
throw new ConfigError(`${label} is not valid:\n ${problems.join('\n ')}`);
|
|
132
|
+
}
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
function validateStringArray(value, path, at) {
|
|
136
|
+
if (!Array.isArray(value)) {
|
|
137
|
+
at(path, 'must be an array of strings');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
for (const [index, item] of value.entries()) {
|
|
141
|
+
if (typeof item !== 'string' || item.trim() === '') {
|
|
142
|
+
at(`${path}[${index}]`, `must be a non-empty string, got ${JSON.stringify(item)}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function validateTargets(value, path, at) {
|
|
147
|
+
if (!isPlainObject(value)) {
|
|
148
|
+
at(path, 'must be an object');
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const known = new Set(['groups', 'projects', 'exclude']);
|
|
152
|
+
for (const key of Object.keys(value)) {
|
|
153
|
+
if (!known.has(key))
|
|
154
|
+
at(`${path}.${key}`, `is not a known key (expected ${[...known].join(', ')})`);
|
|
155
|
+
}
|
|
156
|
+
for (const key of known) {
|
|
157
|
+
if (value[key] !== undefined)
|
|
158
|
+
validateStringArray(value[key], `${path}.${key}`, at);
|
|
159
|
+
}
|
|
160
|
+
if (value.groups === undefined && value.projects === undefined) {
|
|
161
|
+
at(path, 'must name at least one of "groups" or "projects"');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function validateGroupDesired(value, path, at) {
|
|
165
|
+
if (!isPlainObject(value)) {
|
|
166
|
+
at(path, 'must be an object');
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
for (const key of Object.keys(value)) {
|
|
170
|
+
if (key !== 'settings')
|
|
171
|
+
at(`${path}.${key}`, 'is not a known key (expected "settings")');
|
|
172
|
+
}
|
|
173
|
+
if (value.settings !== undefined && !isPlainObject(value.settings)) {
|
|
174
|
+
at(`${path}.settings`, 'must be an object of GitLab group attributes');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function validateProjectDesired(value, path, at) {
|
|
178
|
+
if (!isPlainObject(value)) {
|
|
179
|
+
at(path, 'must be an object');
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const known = new Set([...OBJECT_SECTIONS, ...LIST_SECTIONS]);
|
|
183
|
+
for (const key of Object.keys(value)) {
|
|
184
|
+
if (!known.has(key))
|
|
185
|
+
at(`${path}.${key}`, `is not a known section (expected ${[...known].join(', ')})`);
|
|
186
|
+
}
|
|
187
|
+
for (const section of OBJECT_SECTIONS) {
|
|
188
|
+
const entry = value[section];
|
|
189
|
+
if (entry !== undefined && !isPlainObject(entry)) {
|
|
190
|
+
at(`${path}.${section}`, 'must be an object of GitLab attributes');
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
for (const section of LIST_SECTIONS) {
|
|
194
|
+
if (value[section] !== undefined)
|
|
195
|
+
validateListSection(value[section], `${path}.${section}`, section, at);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function validateListSection(value, path, section, at) {
|
|
199
|
+
let entries;
|
|
200
|
+
if (Array.isArray(value)) {
|
|
201
|
+
entries = value;
|
|
202
|
+
}
|
|
203
|
+
else if (isPlainObject(value)) {
|
|
204
|
+
const known = new Set(['prune', '$replace', 'entries']);
|
|
205
|
+
for (const key of Object.keys(value)) {
|
|
206
|
+
if (!known.has(key))
|
|
207
|
+
at(`${path}.${key}`, `is not a known key (expected ${[...known].join(', ')})`);
|
|
208
|
+
}
|
|
209
|
+
for (const flag of ['prune', '$replace']) {
|
|
210
|
+
if (value[flag] !== undefined && typeof value[flag] !== 'boolean')
|
|
211
|
+
at(`${path}.${flag}`, 'must be a boolean');
|
|
212
|
+
}
|
|
213
|
+
if (value.entries === undefined) {
|
|
214
|
+
at(`${path}.entries`, 'is required when the section is written as an object');
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
entries = value.entries;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
at(path, 'must be an array of entries, or an object with an "entries" array');
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (!Array.isArray(entries)) {
|
|
224
|
+
at(Array.isArray(value) ? path : `${path}.entries`, 'must be an array');
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const base = Array.isArray(value) ? path : `${path}.entries`;
|
|
228
|
+
const seen = new Map();
|
|
229
|
+
for (const [index, entry] of entries.entries()) {
|
|
230
|
+
const where = `${base}[${index}]`;
|
|
231
|
+
if (!isPlainObject(entry)) {
|
|
232
|
+
at(where, 'must be an object');
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
for (const field of SECTION_KEYS[section]) {
|
|
236
|
+
// Only the first key field is required; `environment_scope` defaults to `*`, as GitLab does.
|
|
237
|
+
if (field !== SECTION_KEYS[section][0])
|
|
238
|
+
continue;
|
|
239
|
+
if (typeof entry[field] !== 'string' || entry[field].trim() === '') {
|
|
240
|
+
at(`${where}.${field}`, `is required and must be a non-empty string`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const key = entryKey(section, entry);
|
|
244
|
+
const first = seen.get(key);
|
|
245
|
+
if (first !== undefined) {
|
|
246
|
+
// Two entries with the same identity would silently reconcile against each other.
|
|
247
|
+
at(where, `duplicates ${base}[${first}] (same ${SECTION_KEYS[section].join(' + ')})`);
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
seen.set(key, index);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// --- Merging ------------------------------------------------------------------------------------
|
|
255
|
+
/**
|
|
256
|
+
* The effective configuration for one project: the baseline with its override merged over it.
|
|
257
|
+
*
|
|
258
|
+
* Objects merge key by key. Lists merge by the section's natural key, so an override adjusts one
|
|
259
|
+
* entry rather than restating the array — unless it sets `$replace`, which swaps the list wholesale
|
|
260
|
+
* for the project that genuinely needs different rules rather than adjusted ones.
|
|
261
|
+
*/
|
|
262
|
+
export function mergeProjectDesired(base = {}, override = {}) {
|
|
263
|
+
const merged = {};
|
|
264
|
+
for (const section of OBJECT_SECTIONS) {
|
|
265
|
+
const value = mergeObjects(base[section], override[section]);
|
|
266
|
+
if (value !== undefined)
|
|
267
|
+
merged[section] = value;
|
|
268
|
+
}
|
|
269
|
+
for (const section of LIST_SECTIONS) {
|
|
270
|
+
const value = mergeSections(section, base[section], override[section]);
|
|
271
|
+
if (value !== undefined)
|
|
272
|
+
merged[section] = value;
|
|
273
|
+
}
|
|
274
|
+
return merged;
|
|
275
|
+
}
|
|
276
|
+
export function mergeGroupDesired(base = {}, override = {}) {
|
|
277
|
+
const settings = mergeObjects(base.settings, override.settings);
|
|
278
|
+
return settings === undefined ? {} : { settings };
|
|
279
|
+
}
|
|
280
|
+
function mergeObjects(base, override) {
|
|
281
|
+
if (base === undefined && override === undefined)
|
|
282
|
+
return undefined;
|
|
283
|
+
return { ...(base ?? {}), ...(override ?? {}) };
|
|
284
|
+
}
|
|
285
|
+
function mergeSections(section, base, override) {
|
|
286
|
+
const left = normalizeSection(base);
|
|
287
|
+
const right = normalizeSection(override);
|
|
288
|
+
if (left === undefined && right === undefined)
|
|
289
|
+
return undefined;
|
|
290
|
+
if (left === undefined)
|
|
291
|
+
return { prune: right.prune, entries: right.entries };
|
|
292
|
+
if (right === undefined)
|
|
293
|
+
return { prune: left.prune, entries: left.entries };
|
|
294
|
+
// Pruning is a property of the section as configured, so an override can turn it on for one
|
|
295
|
+
// project without the baseline turning it on for all fifty.
|
|
296
|
+
if (right.replace)
|
|
297
|
+
return { prune: right.prune, entries: right.entries };
|
|
298
|
+
const byKey = new Map();
|
|
299
|
+
const order = [];
|
|
300
|
+
for (const entry of left.entries) {
|
|
301
|
+
const key = entryKey(section, entry);
|
|
302
|
+
if (!byKey.has(key))
|
|
303
|
+
order.push(key);
|
|
304
|
+
byKey.set(key, entry);
|
|
305
|
+
}
|
|
306
|
+
for (const entry of right.entries) {
|
|
307
|
+
const key = entryKey(section, entry);
|
|
308
|
+
const existing = byKey.get(key);
|
|
309
|
+
if (existing === undefined)
|
|
310
|
+
order.push(key);
|
|
311
|
+
byKey.set(key, existing === undefined ? entry : { ...existing, ...entry });
|
|
312
|
+
}
|
|
313
|
+
return { prune: right.prune || left.prune, entries: order.map((key) => byKey.get(key)) };
|
|
314
|
+
}
|
|
315
|
+
/** The effective project configuration for one path, baseline merged with any override for it. */
|
|
316
|
+
export function effectiveProject(config, path) {
|
|
317
|
+
return mergeProjectDesired(config.project, config.overrides?.[path]?.project);
|
|
318
|
+
}
|
|
319
|
+
/** The effective group configuration for one path. */
|
|
320
|
+
export function effectiveGroup(config, path) {
|
|
321
|
+
return mergeGroupDesired(config.group, config.overrides?.[path]?.group);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Reads and validates a baseline config file.
|
|
325
|
+
*
|
|
326
|
+
* A parse failure reports only the position, never the parser's snippet of the content — the same
|
|
327
|
+
* rule the rest of this package follows, because a caller can name any path and the snippet would
|
|
328
|
+
* echo the first bytes of whatever file that was.
|
|
329
|
+
*/
|
|
330
|
+
export function loadBaselineConfig(path) {
|
|
331
|
+
const source = readTextFile(path, 'Config file');
|
|
332
|
+
let parsed;
|
|
333
|
+
try {
|
|
334
|
+
parsed = JSON.parse(source);
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
const position = error instanceof Error ? /position \d+(?::? line \d+ column \d+)?/.exec(error.message)?.[0] : undefined;
|
|
338
|
+
throw new ConfigError(`${path} is not valid JSON${position === undefined ? '' : ` at ${position}`}.`);
|
|
339
|
+
}
|
|
340
|
+
return validateBaselineConfig(parsed, path);
|
|
341
|
+
}
|
|
342
|
+
//# sourceMappingURL=baseline-config.js.map
|