@gate-forge/cli 0.10.3 → 0.11.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/CHANGELOG.md +30 -0
- package/README.md +2 -0
- package/dist/commands/adopt.d.ts +1 -1
- package/dist/commands/adopt.d.ts.map +1 -1
- package/dist/commands/adopt.js +5 -2
- package/dist/commands/adopt.js.map +1 -1
- package/dist/commands/check.d.ts.map +1 -1
- package/dist/commands/check.js +95 -23
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/classify.d.ts.map +1 -1
- package/dist/commands/classify.js +24 -11
- package/dist/commands/classify.js.map +1 -1
- package/dist/commands/init.d.ts +14 -14
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +120 -80
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/migrate-moved-documents.d.ts +44 -0
- package/dist/commands/migrate-moved-documents.d.ts.map +1 -0
- package/dist/commands/migrate-moved-documents.js +333 -0
- package/dist/commands/migrate-moved-documents.js.map +1 -0
- package/dist/commands/migrate.d.ts.map +1 -1
- package/dist/commands/migrate.js +98 -75
- package/dist/commands/migrate.js.map +1 -1
- package/dist/commands/next.d.ts.map +1 -1
- package/dist/commands/next.js +50 -17
- package/dist/commands/next.js.map +1 -1
- package/dist/docs-exclusions.js +1 -2
- package/dist/docs-exclusions.js.map +1 -1
- package/dist/endpoint-compiler.d.ts +82 -9
- package/dist/endpoint-compiler.d.ts.map +1 -1
- package/dist/endpoint-compiler.js +86 -41
- package/dist/endpoint-compiler.js.map +1 -1
- package/dist/endpoint-config.d.ts +32 -13
- package/dist/endpoint-config.d.ts.map +1 -1
- package/dist/endpoint-config.js +37 -27
- package/dist/endpoint-config.js.map +1 -1
- package/dist/execution.d.ts.map +1 -1
- package/dist/execution.js +6 -8
- package/dist/execution.js.map +1 -1
- package/dist/gateforge-owned.d.ts.map +1 -1
- package/dist/gateforge-owned.js +2 -6
- package/dist/gateforge-owned.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/input-snapshot.d.ts +0 -2
- package/dist/input-snapshot.d.ts.map +1 -1
- package/dist/input-snapshot.js +9 -6
- package/dist/input-snapshot.js.map +1 -1
- package/dist/moved-owner-documents.d.ts +51 -0
- package/dist/moved-owner-documents.d.ts.map +1 -0
- package/dist/moved-owner-documents.js +92 -0
- package/dist/moved-owner-documents.js.map +1 -0
- package/dist/owner-answers.d.ts +31 -0
- package/dist/owner-answers.d.ts.map +1 -0
- package/dist/owner-answers.js +56 -0
- package/dist/owner-answers.js.map +1 -0
- package/dist/pipeline.d.ts +0 -2
- package/dist/pipeline.d.ts.map +1 -1
- package/dist/pipeline.js +34 -41
- package/dist/pipeline.js.map +1 -1
- package/dist/planes-inference.d.ts +3 -2
- package/dist/planes-inference.d.ts.map +1 -1
- package/dist/planes-inference.js +3 -2
- package/dist/planes-inference.js.map +1 -1
- package/dist/plugins.d.ts +25 -3
- package/dist/plugins.d.ts.map +1 -1
- package/dist/plugins.js +23 -7
- package/dist/plugins.js.map +1 -1
- package/dist/route-plane-proposals.d.ts +6 -3
- package/dist/route-plane-proposals.d.ts.map +1 -1
- package/dist/route-plane-proposals.js +26 -7
- package/dist/route-plane-proposals.js.map +1 -1
- package/dist/run-cache.d.ts +8 -2
- package/dist/run-cache.d.ts.map +1 -1
- package/dist/run-cache.js +9 -3
- package/dist/run-cache.js.map +1 -1
- package/dist/scope.d.ts.map +1 -1
- package/dist/scope.js +1 -3
- package/dist/scope.js.map +1 -1
- package/dist/yaml-section.d.ts +49 -0
- package/dist/yaml-section.d.ts.map +1 -0
- package/dist/yaml-section.js +443 -0
- package/dist/yaml-section.js.map +1 -0
- package/dist/yaml.d.ts +10 -0
- package/dist/yaml.d.ts.map +1 -0
- package/dist/yaml.js +35 -0
- package/dist/yaml.js.map +1 -0
- package/guides/CONNECT-YOUR-PROJECT.md +1 -1
- package/guides/QUICKSTART.md +13 -8
- package/guides/REFERENCE.md +140 -24
- package/package.json +10 -10
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type PreMigrationConfig, type ScanConfig } from '@gate-forge/core';
|
|
2
|
+
/** What one migration step found, before anything is written. */
|
|
3
|
+
export interface MigrationStep {
|
|
4
|
+
/** Stable id, printed in the preview and on success. */
|
|
5
|
+
readonly id: string;
|
|
6
|
+
/** One owner-facing line describing what changes. */
|
|
7
|
+
readonly describe: string;
|
|
8
|
+
/** Repo-relative files this step deletes once its rewrite lands. */
|
|
9
|
+
readonly removals: readonly string[];
|
|
10
|
+
/** The preview diff, one line per added/removed text line. */
|
|
11
|
+
readonly diff: string;
|
|
12
|
+
/** Applies the rewrite, then deletes the old files. */
|
|
13
|
+
apply(cwd: string): void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The scanner settings as the repository will read them once every
|
|
17
|
+
* migration step is applied.
|
|
18
|
+
*
|
|
19
|
+
* This is the composition every run performs, run EARLY so that everything
|
|
20
|
+
* downstream — the exclusion validators, which refuse to hide a declared
|
|
21
|
+
* gate input — validates against the config the migration will produce and
|
|
22
|
+
* not against the half-migrated one on disk.
|
|
23
|
+
*
|
|
24
|
+
* @param cwd - absolute repository root.
|
|
25
|
+
* @param config - the pre-0.11 config (its `scan:` is absent or partial).
|
|
26
|
+
* @returns the composed scanner settings.
|
|
27
|
+
* @throws UsageError when no `scan:` can be composed — a repository with
|
|
28
|
+
* no scanner answer anywhere cannot be migrated, and inventing one would
|
|
29
|
+
* write a scan scope the owner never wrote.
|
|
30
|
+
*/
|
|
31
|
+
export declare function composeScanSection(cwd: string, config: PreMigrationConfig): ScanConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Every pre-0.11 owner-answer file still on disk becomes one migration
|
|
34
|
+
* step, in the order they are applied.
|
|
35
|
+
*
|
|
36
|
+
* @param cwd - absolute repository root.
|
|
37
|
+
* @param config - the pre-0.11 config, read leniently (no `scan:` yet).
|
|
38
|
+
* @returns one step per moved file present, answers document first.
|
|
39
|
+
* @throws UsageError naming the offending file when a document's bytes
|
|
40
|
+
* would not validate in their new home, or when two owner documents
|
|
41
|
+
* answer the same fact differently.
|
|
42
|
+
*/
|
|
43
|
+
export declare function movedDocumentSteps(cwd: string, config: PreMigrationConfig): MigrationStep[];
|
|
44
|
+
//# sourceMappingURL=migrate-moved-documents.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate-moved-documents.d.ts","sourceRoot":"","sources":["../../src/commands/migrate-moved-documents.ts"],"names":[],"mappings":"AAyBA,OAAO,EAKL,KAAK,kBAAkB,EACvB,KAAK,UAAU,EAChB,MAAM,kBAAkB,CAAC;AAS1B,iEAAiE;AACjE,MAAM,WAAW,aAAa;IAC5B,wDAAwD;IACxD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,qDAAqD;IACrD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,oEAAoE;IACpE,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,8DAA8D;IAC9D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uDAAuD;IACvD,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,kBAAkB,GAAG,UAAU,CAoBtF;AA0HD;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,kBAAkB,GAAG,aAAa,EAAE,CAmF3F"}
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `gateforge migrate` steps that fold the 0.10 owner-answer FILES into
|
|
3
|
+
* the two documents 0.11.0 reads.
|
|
4
|
+
*
|
|
5
|
+
* One step per moved file, each reading the old bytes deliberately and
|
|
6
|
+
* writing them as TEXT into their new home:
|
|
7
|
+
*
|
|
8
|
+
* - `.gateforge/planes.json` / `.gateforge/endpoints.json` become the
|
|
9
|
+
* `planes:` / `endpoints:` SECTIONS of `.gateforge/classification-policy.yml`;
|
|
10
|
+
* - `.gateforge/http-clients.json` / `.gateforge/fastapi.json` become
|
|
11
|
+
* `scan.httpClients` / `scan.fastapi` in `.gateforge.yml`;
|
|
12
|
+
* - the four scanner keys move OUT of the answers document into `scan:`.
|
|
13
|
+
*
|
|
14
|
+
* Why a text splice and not a re-serialization: the answers document is an
|
|
15
|
+
* owner-authored review artifact whose comments and reasons ARE the review
|
|
16
|
+
* record, and the comment above a moved key travels with it
|
|
17
|
+
* ({@link removeTopLevelSection} hands it back for exactly that reason).
|
|
18
|
+
*
|
|
19
|
+
* Every old value is validated by the SAME reader that will read it after
|
|
20
|
+
* the move, BEFORE anything is written: a file whose contents the new home
|
|
21
|
+
* would reject is refused here, with the file named, instead of producing a
|
|
22
|
+
* repository that cannot run.
|
|
23
|
+
*/
|
|
24
|
+
import { existsSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
25
|
+
import { join } from 'node:path';
|
|
26
|
+
import { FastapiScanSectionSchema, HttpClientsScanSectionSchema, isMovedScannerKey, ScanConfigSchema, } from '@gate-forge/core';
|
|
27
|
+
import { parsePlanesConfigDocument } from '@gate-forge/pack-sqlalchemy';
|
|
28
|
+
import { parse as parseYaml } from 'yaml';
|
|
29
|
+
import { UsageError } from '../errors.js';
|
|
30
|
+
import { parseEndpointsConfigDocument } from '../endpoint-config.js';
|
|
31
|
+
import { configTextDiff } from '../evidence-config-text.js';
|
|
32
|
+
import { movedOwnerDocumentsPresent } from '../moved-owner-documents.js';
|
|
33
|
+
import { declaresSection, removeTopLevelSection, setSection } from '../yaml-section.js';
|
|
34
|
+
/**
|
|
35
|
+
* The scanner settings as the repository will read them once every
|
|
36
|
+
* migration step is applied.
|
|
37
|
+
*
|
|
38
|
+
* This is the composition every run performs, run EARLY so that everything
|
|
39
|
+
* downstream — the exclusion validators, which refuse to hide a declared
|
|
40
|
+
* gate input — validates against the config the migration will produce and
|
|
41
|
+
* not against the half-migrated one on disk.
|
|
42
|
+
*
|
|
43
|
+
* @param cwd - absolute repository root.
|
|
44
|
+
* @param config - the pre-0.11 config (its `scan:` is absent or partial).
|
|
45
|
+
* @returns the composed scanner settings.
|
|
46
|
+
* @throws UsageError when no `scan:` can be composed — a repository with
|
|
47
|
+
* no scanner answer anywhere cannot be migrated, and inventing one would
|
|
48
|
+
* write a scan scope the owner never wrote.
|
|
49
|
+
*/
|
|
50
|
+
export function composeScanSection(cwd, config) {
|
|
51
|
+
const answersPath = config.classificationPolicy;
|
|
52
|
+
const absolute = join(cwd, ...answersPath.split('/'));
|
|
53
|
+
const answersText = existsSync(absolute) ? readFileSync(absolute, 'utf8') : '';
|
|
54
|
+
const fromAnswers = readMovedScannerKeys(answersText, answersPath);
|
|
55
|
+
const declared = readMovedScannerKeys(readFileSync(join(cwd, '.gateforge.yml'), 'utf8'), '.gateforge.yml', 'scan');
|
|
56
|
+
const merged = { ...config.scan, ...declared, ...fromAnswers };
|
|
57
|
+
const parsed = ScanConfigSchema.safeParse(merged);
|
|
58
|
+
if (!parsed.success) {
|
|
59
|
+
throw new UsageError('this repository declares no complete `scan:` section, so there is nothing to migrate ' +
|
|
60
|
+
'into: add `scanRoots`, `declarations` and `volatileFields` to .gateforge.yml by hand ' +
|
|
61
|
+
`(first missing: ${parsed.error.issues[0]?.path.join('.') ?? 'scan'})`);
|
|
62
|
+
}
|
|
63
|
+
return parsed.data;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The scanner keys a document declares under its own top level (or under
|
|
67
|
+
* `prefix`), as raw values — this reader never validates them: the caller
|
|
68
|
+
* composes and then validates the whole section once.
|
|
69
|
+
*
|
|
70
|
+
* @param text - the document's exact text.
|
|
71
|
+
* @param path - repo-relative document path, named in refusals.
|
|
72
|
+
* @param prefix - the section the keys live under, when not top level.
|
|
73
|
+
* @returns the raw moved-key values found.
|
|
74
|
+
*/
|
|
75
|
+
function readMovedScannerKeys(text, path, prefix) {
|
|
76
|
+
let document;
|
|
77
|
+
try {
|
|
78
|
+
document = parseYaml(text);
|
|
79
|
+
}
|
|
80
|
+
catch (cause) {
|
|
81
|
+
const detail = cause instanceof Error ? cause.message.split('\n')[0] ?? '' : String(cause);
|
|
82
|
+
throw new UsageError(`${path} is not valid YAML, so nothing was migrated: ${detail}`);
|
|
83
|
+
}
|
|
84
|
+
if (document === null || typeof document !== 'object' || Array.isArray(document))
|
|
85
|
+
return {};
|
|
86
|
+
const top = document;
|
|
87
|
+
const scope = prefix === undefined
|
|
88
|
+
? top
|
|
89
|
+
: top[prefix] !== null && typeof top[prefix] === 'object' && !Array.isArray(top[prefix])
|
|
90
|
+
? top[prefix]
|
|
91
|
+
: {};
|
|
92
|
+
const keys = {};
|
|
93
|
+
for (const [key, value] of Object.entries(scope)) {
|
|
94
|
+
if (isMovedScannerKey(key))
|
|
95
|
+
keys[key] = value;
|
|
96
|
+
}
|
|
97
|
+
return keys;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Reads one moved JSON document, failing closed on unreadable bytes.
|
|
101
|
+
*
|
|
102
|
+
* @param cwd - absolute repository root.
|
|
103
|
+
* @param relative - repo-relative path of the old file.
|
|
104
|
+
* @returns the parsed document.
|
|
105
|
+
* @throws UsageError naming the file when its bytes are not valid JSON.
|
|
106
|
+
*/
|
|
107
|
+
function readMovedJson(cwd, relative) {
|
|
108
|
+
const absolute = join(cwd, ...relative.split('/'));
|
|
109
|
+
if (!existsSync(absolute)) {
|
|
110
|
+
throw new UsageError(`${relative} disappeared between planning and reading it`);
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
return JSON.parse(readFileSync(absolute, 'utf8'));
|
|
114
|
+
}
|
|
115
|
+
catch (cause) {
|
|
116
|
+
const detail = cause instanceof Error ? cause.message.split('\n')[0] ?? '' : String(cause);
|
|
117
|
+
throw new UsageError(`${relative} is not valid JSON, so nothing was migrated: ${detail}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Writes a document through a sibling temporary file, so an interrupted
|
|
122
|
+
* migrate never leaves a half-written owner artifact behind.
|
|
123
|
+
*
|
|
124
|
+
* @param cwd - absolute repository root.
|
|
125
|
+
* @param relative - repo-relative target path.
|
|
126
|
+
* @param text - the exact content to write.
|
|
127
|
+
*/
|
|
128
|
+
function writeDocument(cwd, relative, text) {
|
|
129
|
+
const absolute = join(cwd, ...relative.split('/'));
|
|
130
|
+
const temporary = `${absolute}.gateforge-migrate.tmp`;
|
|
131
|
+
try {
|
|
132
|
+
writeFileSync(temporary, text, { flag: 'wx', encoding: 'utf8' });
|
|
133
|
+
renameSync(temporary, absolute);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (existsSync(temporary))
|
|
137
|
+
unlinkSync(temporary);
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/** One step that deletes one old file after its rewrite landed. */
|
|
142
|
+
function stepWithRemoval(id, describe, diff, removal, write) {
|
|
143
|
+
return {
|
|
144
|
+
id,
|
|
145
|
+
describe,
|
|
146
|
+
removals: [removal],
|
|
147
|
+
diff,
|
|
148
|
+
apply: (cwd) => {
|
|
149
|
+
write(cwd);
|
|
150
|
+
const absolute = join(cwd, ...removal.split('/'));
|
|
151
|
+
if (existsSync(absolute))
|
|
152
|
+
unlinkSync(absolute);
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The section body a moved file declares, validated by the SAME reader
|
|
158
|
+
* that will read it after the move.
|
|
159
|
+
*
|
|
160
|
+
* @param section - the answers-document section key the file becomes.
|
|
161
|
+
* @param parsed - the old file's parsed JSON.
|
|
162
|
+
* @param relative - repo-relative path of the old file, named in refusals.
|
|
163
|
+
* @returns the validated section body.
|
|
164
|
+
* @throws UsageError naming the file when the body would be refused.
|
|
165
|
+
*/
|
|
166
|
+
function validatedSection(section, parsed, relative) {
|
|
167
|
+
try {
|
|
168
|
+
return section === 'planes'
|
|
169
|
+
? parsePlanesConfigDocument(parsed, relative)
|
|
170
|
+
: parseEndpointsConfigDocument(parsed, relative);
|
|
171
|
+
}
|
|
172
|
+
catch (cause) {
|
|
173
|
+
const detail = cause instanceof Error ? cause.message : String(cause);
|
|
174
|
+
throw new UsageError(`${relative} would not be readable in its new home: ${detail}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Every pre-0.11 owner-answer file still on disk becomes one migration
|
|
179
|
+
* step, in the order they are applied.
|
|
180
|
+
*
|
|
181
|
+
* @param cwd - absolute repository root.
|
|
182
|
+
* @param config - the pre-0.11 config, read leniently (no `scan:` yet).
|
|
183
|
+
* @returns one step per moved file present, answers document first.
|
|
184
|
+
* @throws UsageError naming the offending file when a document's bytes
|
|
185
|
+
* would not validate in their new home, or when two owner documents
|
|
186
|
+
* answer the same fact differently.
|
|
187
|
+
*/
|
|
188
|
+
export function movedDocumentSteps(cwd, config) {
|
|
189
|
+
const present = movedOwnerDocumentsPresent(cwd);
|
|
190
|
+
if (present.length === 0)
|
|
191
|
+
return [];
|
|
192
|
+
const answersPath = config.classificationPolicy;
|
|
193
|
+
const answersAbsolute = join(cwd, ...answersPath.split('/'));
|
|
194
|
+
const answersBefore = existsSync(answersAbsolute) ? readFileSync(answersAbsolute, 'utf8') : '';
|
|
195
|
+
if (!existsSync(answersAbsolute)) {
|
|
196
|
+
throw new UsageError(`${answersPath} is absent, so the plane and endpoint answers have nowhere to go — run ` +
|
|
197
|
+
'`gateforge init` first, then rerun `gateforge migrate`');
|
|
198
|
+
}
|
|
199
|
+
const configPath = '.gateforge.yml';
|
|
200
|
+
const configBefore = readFileSync(join(cwd, configPath), 'utf8');
|
|
201
|
+
// The two JSON documents that become `planes:` / `endpoints:` sections.
|
|
202
|
+
// Both write the SAME document, so their steps compose onto one running
|
|
203
|
+
// text: the second must see what the first wrote, or the second write
|
|
204
|
+
// would silently drop the first section.
|
|
205
|
+
let answersRunning = answersBefore;
|
|
206
|
+
const answersSteps = present
|
|
207
|
+
.filter((document) => document.destination === answersPath)
|
|
208
|
+
.map((document) => {
|
|
209
|
+
const parsed = readMovedJson(cwd, document.path);
|
|
210
|
+
// Validate through the SAME reader that will read the section after
|
|
211
|
+
// the move: a file whose bytes the new home refuses is refused here,
|
|
212
|
+
// with the file named, instead of producing a repository that cannot
|
|
213
|
+
// load.
|
|
214
|
+
const body = validatedSection(document.section, parsed, document.path);
|
|
215
|
+
if (declaresSection(answersRunning, [document.section], answersPath)) {
|
|
216
|
+
throw new UsageError(`${answersPath} already declares a \`${document.section}:\` section, so migrating ` +
|
|
217
|
+
`${document.path} would overwrite an answer the owner already made — delete or ` +
|
|
218
|
+
'merge it by hand');
|
|
219
|
+
}
|
|
220
|
+
const before = answersRunning;
|
|
221
|
+
const after = setSection(before, [document.section], body, answersPath);
|
|
222
|
+
answersRunning = after;
|
|
223
|
+
return stepWithRemoval(`${document.section}-section`, `${document.path} becomes the \`${document.section}:\` section of ${answersPath}`, configTextDiff(answersPath, before, after), document.path, (repoRoot) => {
|
|
224
|
+
writeDocument(repoRoot, answersPath, after);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
// The two detector documents that become `scan:` subsections. Both live
|
|
228
|
+
// in `.gateforge.yml`, so their steps compose onto the SAME running text:
|
|
229
|
+
// the second must see what the first wrote.
|
|
230
|
+
let configRunning = configBefore;
|
|
231
|
+
const scanSteps = present
|
|
232
|
+
.filter((document) => document.destination === configPath)
|
|
233
|
+
.map((document) => {
|
|
234
|
+
const key = document.section.split('.')[1] ?? document.section;
|
|
235
|
+
const parsed = readMovedJson(cwd, document.path);
|
|
236
|
+
const schema = key === 'httpClients' ? HttpClientsScanSectionSchema : FastapiScanSectionSchema;
|
|
237
|
+
const validated = schema.safeParse(parsed);
|
|
238
|
+
if (!validated.success) {
|
|
239
|
+
throw new UsageError(`${document.path} does not validate as \`scan.${key}\`, so nothing was migrated: ` +
|
|
240
|
+
`${validated.error.issues[0]?.message ?? 'invalid document'}`);
|
|
241
|
+
}
|
|
242
|
+
const before = configRunning;
|
|
243
|
+
const after = setSection(before, ['scan', key], validated.data, configPath);
|
|
244
|
+
configRunning = after;
|
|
245
|
+
return stepWithRemoval(`scan-${key}`, `${document.path} becomes \`scan.${key}\` in ${configPath}`, configTextDiff(configPath, before, after), document.path, (repoRoot) => {
|
|
246
|
+
writeDocument(repoRoot, configPath, after);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
const scannerStep = scannerKeysStep(cwd, answersPath, answersRunning, configRunning);
|
|
250
|
+
return [...answersSteps, ...scanSteps, ...(scannerStep === null ? [] : [scannerStep])];
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* The step that moves the four scanner keys OUT of the answers document
|
|
254
|
+
* and into `scan:` in `.gateforge.yml`, or null when the answers document
|
|
255
|
+
* declares none of them.
|
|
256
|
+
*
|
|
257
|
+
* @param cwd - absolute repository root.
|
|
258
|
+
* @param answersPath - repo-relative path of the answers document.
|
|
259
|
+
* @param answersBefore - the answers document's exact current text.
|
|
260
|
+
* @param configText - `.gateforge.yml` as the earlier steps left it.
|
|
261
|
+
* @returns the step, or null when there is nothing to move.
|
|
262
|
+
* @throws UsageError when a key the config already declares under `scan:`
|
|
263
|
+
* disagrees with the one the answers document carries, or when the
|
|
264
|
+
* composed section would not satisfy the schema a run validates.
|
|
265
|
+
*/
|
|
266
|
+
function scannerKeysStep(cwd, answersPath, answersBefore, configText) {
|
|
267
|
+
let parsed;
|
|
268
|
+
try {
|
|
269
|
+
parsed = parseYaml(answersBefore);
|
|
270
|
+
}
|
|
271
|
+
catch (cause) {
|
|
272
|
+
const detail = cause instanceof Error ? cause.message.split('\n')[0] ?? '' : String(cause);
|
|
273
|
+
throw new UsageError(`${answersPath} is not valid YAML, so nothing was migrated: ${detail}`);
|
|
274
|
+
}
|
|
275
|
+
const moved = (parsed === null ? [] : Object.keys(parsed))
|
|
276
|
+
.filter((key) => isMovedScannerKey(key))
|
|
277
|
+
.sort();
|
|
278
|
+
if (moved.length === 0)
|
|
279
|
+
return null;
|
|
280
|
+
const existingScan = parseYaml(configText)?.scan ?? {};
|
|
281
|
+
const section = {};
|
|
282
|
+
// A key already migrated into `scan:` is skipped ONLY when it agrees; a
|
|
283
|
+
// disagreement is two owner documents answering the same fact and is
|
|
284
|
+
// refused by name rather than resolved by precedence.
|
|
285
|
+
for (const key of moved) {
|
|
286
|
+
const value = parsed?.[key];
|
|
287
|
+
const present = existingScan[key];
|
|
288
|
+
if (present !== undefined && JSON.stringify(present) !== JSON.stringify(value)) {
|
|
289
|
+
throw new UsageError(`${answersPath} declares \`${key}\` and ${'.gateforge.yml'} already declares a different ` +
|
|
290
|
+
`\`scan.${key}\` — two answers to one fact. Fix one by hand, then rerun migrate`);
|
|
291
|
+
}
|
|
292
|
+
if (present !== undefined)
|
|
293
|
+
continue;
|
|
294
|
+
section[key] = value;
|
|
295
|
+
}
|
|
296
|
+
if (Object.keys(section).length === 0)
|
|
297
|
+
return null;
|
|
298
|
+
// The composed `scan:` must satisfy the SAME schema a run validates, so
|
|
299
|
+
// a migrated repository that still cannot load is impossible.
|
|
300
|
+
const composed = ScanConfigSchema.safeParse({ ...existingScan, ...section });
|
|
301
|
+
if (!composed.success) {
|
|
302
|
+
throw new UsageError(`moving ${moved.join(', ')} into \`scan:\` in .gateforge.yml would produce a section that ` +
|
|
303
|
+
`fails the schema a run validates, so nothing was migrated: ` +
|
|
304
|
+
`${composed.error.issues[0]?.message ?? 'invalid section'}`);
|
|
305
|
+
}
|
|
306
|
+
// Cut the moved keys out of the answers document, carrying each key's
|
|
307
|
+
// own comment block to its new home.
|
|
308
|
+
let answersAfter = answersBefore;
|
|
309
|
+
const carried = new Map();
|
|
310
|
+
for (const key of moved) {
|
|
311
|
+
const removal = removeTopLevelSection(answersAfter, key, answersPath);
|
|
312
|
+
if (removal === null)
|
|
313
|
+
continue;
|
|
314
|
+
answersAfter = removal.text;
|
|
315
|
+
if (removal.comments.length > 0)
|
|
316
|
+
carried.set(key, removal.comments);
|
|
317
|
+
}
|
|
318
|
+
const configAfter = setSection(configText, ['scan'], composed.data, '.gateforge.yml', carried);
|
|
319
|
+
return {
|
|
320
|
+
id: 'scanner-settings',
|
|
321
|
+
describe: `${moved.join(', ')} move from ${answersPath} into \`scan:\` in .gateforge.yml`,
|
|
322
|
+
removals: [],
|
|
323
|
+
diff: [
|
|
324
|
+
configTextDiff('.gateforge.yml', configText, configAfter),
|
|
325
|
+
configTextDiff(answersPath, answersBefore, answersAfter),
|
|
326
|
+
].join('\n'),
|
|
327
|
+
apply: (repoRoot) => {
|
|
328
|
+
writeDocument(repoRoot, '.gateforge.yml', configAfter);
|
|
329
|
+
writeDocument(repoRoot, answersPath, answersAfter);
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
//# sourceMappingURL=migrate-moved-documents.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate-moved-documents.js","sourceRoot":"","sources":["../../src/commands/migrate-moved-documents.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC1F,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,iBAAiB,EACjB,gBAAgB,GAGjB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,EAAE,KAAK,IAAI,SAAS,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,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AkB;QACtB,QAAQ,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,WAAW,mCAAmC;QACzF,QAAQ,EAAE,EAAE;QACZ,IAAI,EAAE;YACJ,cAAc,CAAC,gBAAgB,EAAE,UAAU,EAAE,WAAW,CAAC;YACzD,cAAc,CAAC,WAAW,EAAE,aAAa,EAAE,YAAY,CAAC;SACzD,CAAC,IAAI,CAAC,IAAI,CAAC;QACZ,KAAK,EAAE,CAAC,QAAgB,EAAE,EAAE;YAC1B,aAAa,CAAC,QAAQ,EAAE,gBAAgB,EAAE,WAAW,CAAC,CAAC;YACvD,aAAa,CAAC,QAAQ,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;QACrD,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../../src/commands/migrate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../../src/commands/migrate.ts"],"names":[],"mappings":"AAuCA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,UAAU,CAAC;AAanC,eAAO,MAAM,aAAa,QAiBoB,CAAC;AA+E/C;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAsDtE"}
|
package/dist/commands/migrate.js
CHANGED
|
@@ -1,31 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `gateforge migrate`: move owner declarations that used to live in their
|
|
3
|
-
* own files into
|
|
3
|
+
* own files into the documents that read them, without touching anything
|
|
4
|
+
* else.
|
|
4
5
|
*
|
|
5
6
|
* 0.10 removed `.gateforge/docs-exclusions.yml` and
|
|
6
|
-
* `.gateforge/cache-exclusions.yml`;
|
|
7
|
-
* `.gateforge.yml` under `evidence.exclude`. Every command refuses a
|
|
8
|
-
* repository that still carries either file (reading it would silently
|
|
9
|
-
* drop the owner's exclusions), so this command is the way through.
|
|
7
|
+
* `.gateforge/cache-exclusions.yml`; 0.11.0 folds four more files away:
|
|
10
8
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* -
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
9
|
+
* - `.gateforge/planes.json` and `.gateforge/endpoints.json` become the
|
|
10
|
+
* `planes:` / `endpoints:` SECTIONS of the owner-answers document;
|
|
11
|
+
* - `.gateforge/http-clients.json` and `.gateforge/fastapi.json` become
|
|
12
|
+
* `scan.httpClients` / `scan.fastapi` in `.gateforge.yml`;
|
|
13
|
+
* - the four scanner keys leave the answers document for `scan:`.
|
|
14
|
+
*
|
|
15
|
+
* Contract (unchanged, extended):
|
|
16
|
+
*
|
|
17
|
+
* - **Fail closed.** A file whose bytes its new home would refuse is
|
|
18
|
+
* refused BY NAME here, and nothing is written — a migrate that half
|
|
19
|
+
* succeeded would leave a repository that cannot load.
|
|
20
|
+
* - **Text-level writes.** The answers document is an owner review
|
|
21
|
+
* artifact: every section spliced into it preserves the bytes around it,
|
|
22
|
+
* comments included (see `yaml-section.ts`).
|
|
20
23
|
* - **Idempotent.** Nothing to migrate prints `nothing to migrate` and
|
|
21
24
|
* exits 0, so a re-run after a partial adoption is free.
|
|
22
25
|
*
|
|
23
26
|
* Migrating changes a policy input, so it ends with the re-pin reminder:
|
|
24
|
-
* the
|
|
27
|
+
* the bytes moved between documents are still owner-pinned, and a
|
|
28
|
+
* repository that trusts a digest computed over the old files would be
|
|
29
|
+
* trusting a set of inputs that no longer exists.
|
|
25
30
|
*/
|
|
26
31
|
import { existsSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
27
32
|
import { join } from 'node:path';
|
|
28
|
-
import {
|
|
33
|
+
import { loadPreMigrationConfig, } from '@gate-forge/core';
|
|
29
34
|
import { parseArgs } from '../args.js';
|
|
30
35
|
import { UsageError } from '../errors.js';
|
|
31
36
|
import { writeLine } from '../io.js';
|
|
@@ -34,32 +39,66 @@ import { readLegacyCacheExclusions } from '../cache-exclusions.js';
|
|
|
34
39
|
import { readLegacyDocsExclusions } from '../docs-exclusions.js';
|
|
35
40
|
import { LEGACY_CACHE_EXCLUSIONS_PATH, LEGACY_DOCS_EXCLUSIONS_PATH, legacyExclusionPathsPresent, } from '../legacy-exclusion-paths.js';
|
|
36
41
|
import { rejectUnknownFlags } from './common.js';
|
|
42
|
+
import { composeScanSection, movedDocumentSteps } from './migrate-moved-documents.js';
|
|
37
43
|
export const MIGRATE_USAGE = 'usage: gateforge migrate [--confirm]\n' +
|
|
38
|
-
' Moves owner declarations out of their own files
|
|
39
|
-
' Today that is
|
|
40
|
-
`
|
|
41
|
-
'
|
|
44
|
+
' Moves owner declarations out of their own files into the documents that read them.\n' +
|
|
45
|
+
' Today that is:\n' +
|
|
46
|
+
` .gateforge/docs-exclusions.yml and ${LEGACY_CACHE_EXCLUSIONS_PATH}\n` +
|
|
47
|
+
' become `evidence.exclude.docs` / `evidence.exclude.cache` in .gateforge.yml\n' +
|
|
48
|
+
' .gateforge/planes.json and .gateforge/endpoints.json\n' +
|
|
49
|
+
' become the `planes:` / `endpoints:` sections of the owner-answers document\n' +
|
|
50
|
+
' .gateforge/http-clients.json and .gateforge/fastapi.json\n' +
|
|
51
|
+
' become `scan.httpClients` / `scan.fastapi` in .gateforge.yml\n' +
|
|
52
|
+
' `scanRoots`, `coverage`, `declarations`, `volatileFields`\n' +
|
|
53
|
+
' move out of the owner-answers document into `scan:` in .gateforge.yml\n' +
|
|
42
54
|
' Preview by default (prints the exact diff, writes nothing, exit 0);\n' +
|
|
43
|
-
' --confirm writes
|
|
44
|
-
' with nothing to migrate prints `nothing to
|
|
45
|
-
'
|
|
55
|
+
' --confirm writes each section as TEXT (every other byte survives) and deletes the\n' +
|
|
56
|
+
' old files. Idempotent: a repository with nothing to migrate prints `nothing to\n' +
|
|
57
|
+
' migrate` and exits 0.\n' +
|
|
58
|
+
' The old files are NOT read by any other command: while any of them is present every\n' +
|
|
46
59
|
' command refuses and names this one.';
|
|
47
60
|
/** Flags the command accepts (plus the implicit `help`). */
|
|
48
61
|
const MIGRATE_FLAGS = ['confirm', 'help'];
|
|
62
|
+
/**
|
|
63
|
+
* Writes a file through a sibling temporary file, so an interrupted
|
|
64
|
+
* migrate never leaves a half-written document behind.
|
|
65
|
+
*
|
|
66
|
+
* Args:
|
|
67
|
+
* cwd: absolute repository root.
|
|
68
|
+
* path: repo-relative target path.
|
|
69
|
+
* text: the exact content to write.
|
|
70
|
+
*
|
|
71
|
+
* Returns:
|
|
72
|
+
* void.
|
|
73
|
+
*/
|
|
74
|
+
function writeFileAtomic(cwd, path, text) {
|
|
75
|
+
const absolute = join(cwd, ...path.split('/'));
|
|
76
|
+
const temporary = `${absolute}.gateforge-migrate.tmp`;
|
|
77
|
+
try {
|
|
78
|
+
writeFileSync(temporary, text, { flag: 'wx', encoding: 'utf8' });
|
|
79
|
+
renameSync(temporary, absolute);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (existsSync(temporary))
|
|
83
|
+
unlinkSync(temporary);
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
49
87
|
/**
|
|
50
88
|
* Builds the evidence-exclusions step, or null when there is nothing to do.
|
|
51
89
|
*
|
|
52
90
|
* Args:
|
|
53
91
|
* cwd: absolute repository root.
|
|
54
|
-
* config: the
|
|
55
|
-
*
|
|
92
|
+
* config: the config as the repository will read it AFTER this migration
|
|
93
|
+
* (the pre-0.11 file plus the composed `scan:`), because the exclusion
|
|
94
|
+
* validators refuse to hide a declared gate input.
|
|
56
95
|
*
|
|
57
96
|
* Returns:
|
|
58
|
-
* MigrationStep
|
|
97
|
+
* MigrationStep[]: the step, or an empty list when no old file is present.
|
|
59
98
|
*/
|
|
60
|
-
function
|
|
99
|
+
function evidenceExclusionsSteps(cwd, config) {
|
|
61
100
|
if (legacyExclusionPathsPresent(cwd).length === 0)
|
|
62
|
-
return
|
|
101
|
+
return [];
|
|
63
102
|
const docs = existsSync(join(cwd, ...LEGACY_DOCS_EXCLUSIONS_PATH.split('/')))
|
|
64
103
|
? readLegacyDocsExclusions(cwd, config)
|
|
65
104
|
: undefined;
|
|
@@ -79,49 +118,22 @@ function evidenceExclusionsStep(cwd, config) {
|
|
|
79
118
|
...(docs === undefined ? [] : [LEGACY_DOCS_EXCLUSIONS_PATH]),
|
|
80
119
|
...(cache === undefined ? [] : [LEGACY_CACHE_EXCLUSIONS_PATH]),
|
|
81
120
|
];
|
|
82
|
-
return
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
121
|
+
return [
|
|
122
|
+
{
|
|
123
|
+
id: 'evidence-exclusions',
|
|
124
|
+
describe: `${declared.join(', ')} move into ${configPath}; ${removals.join(' and ')} deleted`,
|
|
125
|
+
removals,
|
|
126
|
+
diff: configTextDiff(configPath, before, after),
|
|
127
|
+
apply: (repoRoot) => {
|
|
128
|
+
writeFileAtomic(repoRoot, configPath, after);
|
|
129
|
+
for (const removal of removals) {
|
|
130
|
+
const absolute = join(repoRoot, ...removal.split('/'));
|
|
131
|
+
if (existsSync(absolute))
|
|
132
|
+
unlinkSync(absolute);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
94
135
|
},
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Writes a file through a sibling temporary file, so an interrupted
|
|
99
|
-
* migrate never leaves a half-written `.gateforge.yml` behind.
|
|
100
|
-
*
|
|
101
|
-
* Args:
|
|
102
|
-
* cwd: absolute repository root.
|
|
103
|
-
* path: repo-relative target path.
|
|
104
|
-
* text: the exact content to write.
|
|
105
|
-
*
|
|
106
|
-
* Returns:
|
|
107
|
-
* void.
|
|
108
|
-
*/
|
|
109
|
-
function writeFileAtomic(cwd, path, text) {
|
|
110
|
-
const absolute = join(cwd, ...path.split('/'));
|
|
111
|
-
const temporary = `${absolute}.gateforge-migrate.tmp`;
|
|
112
|
-
try {
|
|
113
|
-
writeFileSync(temporary, text, { flag: 'wx', encoding: 'utf8' });
|
|
114
|
-
renameSync(temporary, absolute);
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
if (existsSync(temporary))
|
|
118
|
-
unlinkSync(temporary);
|
|
119
|
-
throw error;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
/** Every migration step, in the order they are applied. */
|
|
123
|
-
function planSteps(cwd, config) {
|
|
124
|
-
return [evidenceExclusionsStep(cwd, config)].filter((step) => step !== null);
|
|
136
|
+
];
|
|
125
137
|
}
|
|
126
138
|
/**
|
|
127
139
|
* Runs `gateforge migrate`.
|
|
@@ -143,14 +155,25 @@ export function migrateCommand(io, argv) {
|
|
|
143
155
|
if (options['confirm'] !== undefined && options['confirm'] !== true) {
|
|
144
156
|
throw new UsageError("migrate: '--confirm' must be a boolean flag");
|
|
145
157
|
}
|
|
146
|
-
// Read the config
|
|
147
|
-
//
|
|
158
|
+
// Read the config LENIENTLY, without either refusal: a repository that
|
|
159
|
+
// needs migrating is exactly the one whose `.gateforge.yml` has no `scan:`
|
|
160
|
+
// yet and whose answers document still carries the scanner keys the
|
|
161
|
+
// strict loader refuses by name. This command is the ONE place those
|
|
162
|
+
// shapes are readable.
|
|
148
163
|
const configPath = join(io.cwd, '.gateforge.yml');
|
|
149
164
|
if (!existsSync(configPath)) {
|
|
150
165
|
throw new UsageError('migrate: no .gateforge.yml in this repository — run `gateforge init` first');
|
|
151
166
|
}
|
|
152
|
-
const
|
|
153
|
-
|
|
167
|
+
const preMigration = loadPreMigrationConfig(configPath);
|
|
168
|
+
// The scanner settings as the repository will read them once this command
|
|
169
|
+
// is done. Everything downstream validates against them, so a migration
|
|
170
|
+
// that would produce a config a run rejects fails here instead.
|
|
171
|
+
const scan = composeScanSection(io.cwd, preMigration);
|
|
172
|
+
const effective = { ...preMigration, scan };
|
|
173
|
+
const steps = [
|
|
174
|
+
...evidenceExclusionsSteps(io.cwd, effective),
|
|
175
|
+
...movedDocumentSteps(io.cwd, preMigration),
|
|
176
|
+
];
|
|
154
177
|
if (steps.length === 0) {
|
|
155
178
|
writeLine(io.stdout, 'nothing to migrate');
|
|
156
179
|
return 0;
|
|
@@ -168,7 +191,7 @@ export function migrateCommand(io, argv) {
|
|
|
168
191
|
return 0;
|
|
169
192
|
}
|
|
170
193
|
for (const step of steps) {
|
|
171
|
-
step.apply(io.cwd
|
|
194
|
+
step.apply(io.cwd);
|
|
172
195
|
writeLine(io.stdout, `migrated: ${step.describe}`);
|
|
173
196
|
}
|
|
174
197
|
writeLine(io.stdout, 'policy inputs changed: re-pin the approved digest (gateforge enforcement doctor)');
|