@dataparade/cli 0.0.32 → 0.0.33
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/README.md +56 -1
- package/dist/package.json +4 -3
- package/dist/src/cli.js +47 -4
- package/dist/src/diff/register-diff-command.d.ts +4 -0
- package/dist/src/diff/register-diff-command.js +79 -0
- package/dist/src/diff/run-diff.d.ts +80 -0
- package/dist/src/diff/run-diff.js +206 -0
- package/dist/src/diff/upload-diff.d.ts +26 -0
- package/dist/src/diff/upload-diff.js +69 -0
- package/dist/src/facts/apply-answers.js +1 -1
- package/dist/src/facts/build-facts.d.ts +3 -1
- package/dist/src/facts/build-facts.js +10 -1
- package/dist/src/facts/identity.d.ts +27 -0
- package/dist/src/facts/identity.js +203 -0
- package/dist/src/facts/io.d.ts +2 -0
- package/dist/src/facts/io.js +7 -0
- package/dist/src/facts/scan-stamps.d.ts +55 -0
- package/dist/src/facts/scan-stamps.js +165 -0
- package/dist/src/facts/schema.d.ts +243 -7
- package/dist/src/facts/schema.js +71 -1
- package/dist/src/kb/answer-fingerprint.d.ts +44 -0
- package/dist/src/kb/answer-fingerprint.js +173 -0
- package/dist/src/kb/brief.d.ts +8 -1
- package/dist/src/kb/brief.js +18 -2
- package/dist/src/kb/land.js +3 -0
- package/dist/src/kb/register-kb-commands.js +7 -1
- package/dist/src/output/git-context.d.ts +8 -0
- package/dist/src/output/git-context.js +18 -0
- package/dist/src/output/scan-output-excludes.js +5 -1
- package/dist/src/output/write-scan-knowledge.d.ts +2 -0
- package/dist/src/output/write-scan-knowledge.js +1 -0
- package/dist/src/platform-api/dataparade-app-base-url.d.ts +8 -4
- package/dist/src/platform-api/dataparade-app-base-url.js +10 -5
- package/dist/src/platform-api/upload-client.d.ts +1 -1
- package/dist/src/platform-api/upload-client.js +32 -1
- package/dist/src/platform-api/upload.types.d.ts +55 -0
- package/dist/src/sensitive-scan/process.d.ts +5 -0
- package/dist/src/sensitive-scan/process.js +48 -0
- package/dist/src/sensitive-scan/project.d.ts +12 -0
- package/dist/src/sensitive-scan/project.js +177 -0
- package/dist/src/sensitive-scan/report.d.ts +50 -0
- package/dist/src/sensitive-scan/report.js +32 -0
- package/dist/src/sensitive-scan/sessions.d.ts +2 -0
- package/dist/src/sensitive-scan/sessions.js +120 -0
- package/dist/src/ui/scan-progress-renderer.d.ts +1 -0
- package/dist/src/ui/scan-progress-renderer.js +8 -0
- package/dist/src/upload/build-preview-url.d.ts +2 -2
- package/dist/src/upload/build-preview-url.js +4 -4
- package/dist/src/upload/run-upload.js +6 -2
- package/dist/src/upload/types.d.ts +2 -0
- package/dist/tests/unit/diff/run-diff.spec.d.ts +1 -0
- package/dist/tests/unit/diff/run-diff.spec.js +189 -0
- package/dist/tests/unit/diff/upload-diff.spec.d.ts +1 -0
- package/dist/tests/unit/diff/upload-diff.spec.js +81 -0
- package/dist/tests/unit/facts/apply-answers.spec.js +1 -1
- package/dist/tests/unit/facts/build-facts.spec.js +67 -1
- package/dist/tests/unit/facts/identity.spec.d.ts +1 -0
- package/dist/tests/unit/facts/identity.spec.js +148 -0
- package/dist/tests/unit/facts/scan-stamps.spec.d.ts +1 -0
- package/dist/tests/unit/facts/scan-stamps.spec.js +95 -0
- package/dist/tests/unit/kb/answer-fingerprint.spec.d.ts +1 -0
- package/dist/tests/unit/kb/answer-fingerprint.spec.js +157 -0
- package/dist/tests/unit/kb/kb-interview-loop.spec.js +16 -0
- package/dist/tests/unit/output/git-context.spec.js +19 -0
- package/dist/tests/unit/platform-api/dataparade-app-base-url.spec.js +9 -0
- package/dist/tests/unit/platform-api/upload-client.spec.js +101 -1
- package/dist/tests/unit/sensitive-scan/project.spec.d.ts +1 -0
- package/dist/tests/unit/sensitive-scan/project.spec.js +98 -0
- package/dist/tests/unit/upload/build-preview-url.spec.js +4 -0
- package/dist/tests/unit/upload/collect-scan-sidecars.spec.js +1 -0
- package/dist/tests/unit/upload/run-upload.spec.js +32 -0
- package/dist/vendor/kb-compare/dist/src/canonical.d.ts +80 -0
- package/dist/vendor/kb-compare/dist/src/canonical.js +119 -0
- package/dist/vendor/kb-compare/dist/src/change-set.d.ts +144 -0
- package/dist/vendor/kb-compare/dist/src/change-set.js +80 -0
- package/dist/vendor/kb-compare/dist/src/comparable.d.ts +23 -0
- package/dist/vendor/kb-compare/dist/src/comparable.js +48 -0
- package/dist/vendor/kb-compare/dist/src/compare.d.ts +52 -0
- package/dist/vendor/kb-compare/dist/src/compare.js +370 -0
- package/dist/vendor/kb-compare/dist/src/fingerprint.d.ts +40 -0
- package/dist/vendor/kb-compare/dist/src/fingerprint.js +88 -0
- package/dist/vendor/kb-compare/dist/src/index.d.ts +15 -0
- package/dist/vendor/kb-compare/dist/src/index.js +34 -0
- package/dist/vendor/kb-compare/dist/src/input.d.ts +128 -0
- package/dist/vendor/kb-compare/dist/src/input.js +75 -0
- package/dist/vendor/kb-compare/dist/src/slot-rules.d.ts +69 -0
- package/dist/vendor/kb-compare/dist/src/slot-rules.js +203 -0
- package/dist/vendor/kb-compare/dist/src/summary.d.ts +3 -0
- package/dist/vendor/kb-compare/dist/src/summary.js +47 -0
- package/dist/vendor/kb-compare/package.json +6 -0
- package/dist/vendor/scanner/dist/src/classifier/application-injection.js +5 -0
- package/dist/vendor/scanner/dist/src/core/pipeline/stable-component-ids.d.ts +8 -0
- package/dist/vendor/scanner/dist/src/core/pipeline/stable-component-ids.js +11 -1
- package/dist/vendor/scanner/dist/src/index.d.ts +1 -1
- package/dist/vendor/scanner/dist/src/index.js +4 -2
- package/dist/vendor/scanner/package.json +3 -1
- package/dist/vendor/scanner/patterns/classifier/third-party.classifier.yaml +10 -0
- package/dist/vendor/scanner/patterns/component-taxonomy.yaml +1 -1
- package/dist/vendor/scanner/patterns/third-party.patterns.yaml +25 -0
- package/package.json +4 -3
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.uploadDiffScans = uploadDiffScans;
|
|
7
|
+
exports.describeUploadedComparison = describeUploadedComparison;
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const upload_client_1 = require("../platform-api/upload-client");
|
|
11
|
+
const git_context_1 = require("../output/git-context");
|
|
12
|
+
const collect_scan_sidecars_1 = require("../upload/collect-scan-sidecars");
|
|
13
|
+
function readDataflow(dir) {
|
|
14
|
+
const dataflowPath = path_1.default.join(dir, "dataflow.json");
|
|
15
|
+
const dataflow = JSON.parse(fs_1.default.readFileSync(dataflowPath, "utf8"));
|
|
16
|
+
const name = dataflow?.metadata?.projectName;
|
|
17
|
+
return { dataflow, dataflowPath, ...(typeof name === "string" && name.trim() ? { projectName: name.trim() } : {}) };
|
|
18
|
+
}
|
|
19
|
+
async function uploadDiffScans(options) {
|
|
20
|
+
const { result } = options;
|
|
21
|
+
if (!result.scanDirs)
|
|
22
|
+
throw new Error("The diff kept no scans to upload; run it with keepScans.");
|
|
23
|
+
const upload = options.upload ?? upload_client_1.cliUploadPreview;
|
|
24
|
+
const log = options.log ?? (() => { });
|
|
25
|
+
const remoteUrl = (0, git_context_1.readOriginRemoteUrl)(options.repo);
|
|
26
|
+
const send = async (side, sha) => {
|
|
27
|
+
const { dataflow, dataflowPath, projectName } = readDataflow(result.scanDirs[side]);
|
|
28
|
+
log(`Uploading the scan of ${sha.slice(0, 8)} (${side})...`);
|
|
29
|
+
return upload({
|
|
30
|
+
apiKey: options.apiKey,
|
|
31
|
+
dataflow,
|
|
32
|
+
projectName,
|
|
33
|
+
command: "upload",
|
|
34
|
+
files: (0, collect_scan_sidecars_1.collectScanUploadFiles)(dataflowPath),
|
|
35
|
+
remoteUrl,
|
|
36
|
+
...(side === "head"
|
|
37
|
+
? { compare: { baseCommitSha: result.baseSha, rootCommitSha: result.rootCommit, renames: result.renames } }
|
|
38
|
+
: {}),
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
// The base first: the head's completion compares with the stored base scan.
|
|
42
|
+
const base = await send("base", result.baseSha);
|
|
43
|
+
const head = await send("head", result.headSha);
|
|
44
|
+
const complete = head.complete;
|
|
45
|
+
const comparison = complete && complete.stored ? (complete.comparison ?? null) : null;
|
|
46
|
+
return { base, head, comparison };
|
|
47
|
+
}
|
|
48
|
+
/** One line on what the backend did with the pair, for the terminal. */
|
|
49
|
+
function describeUploadedComparison(upload) {
|
|
50
|
+
for (const [side, response] of [
|
|
51
|
+
["base", upload.base],
|
|
52
|
+
["head", upload.head],
|
|
53
|
+
]) {
|
|
54
|
+
if (!response.complete)
|
|
55
|
+
return `The ${side} scan was uploaded but not stored for comparison.`;
|
|
56
|
+
if (!response.complete.stored)
|
|
57
|
+
return `The ${side} scan was uploaded but not stored for comparison (${response.complete.reason}).`;
|
|
58
|
+
}
|
|
59
|
+
const comparison = upload.comparison;
|
|
60
|
+
if (!comparison)
|
|
61
|
+
return "The scans were stored; the backend did not compare them.";
|
|
62
|
+
if (!comparison.compared)
|
|
63
|
+
return `The backend did not compare the scans (${comparison.reason}): ${comparison.message}`;
|
|
64
|
+
const counts = new Map();
|
|
65
|
+
for (const binding of comparison.bindings)
|
|
66
|
+
counts.set(binding.status, (counts.get(binding.status) ?? 0) + 1);
|
|
67
|
+
const answers = [...counts].map(([status, count]) => `${count} ${status}`).join(", ");
|
|
68
|
+
return `Stored comparison ${comparison.comparison.id}${answers ? ` (answers: ${answers})` : ""}.`;
|
|
69
|
+
}
|
|
@@ -5,7 +5,7 @@ exports.applyAnswers = applyAnswers;
|
|
|
5
5
|
/** The Fact id for facts about the whole system, such as its confirmed scope. */
|
|
6
6
|
exports.SYSTEM_FACT_ID = "dp:a0/system";
|
|
7
7
|
/** Properties an answer may not change: they identify the Fact or link it to the code. */
|
|
8
|
-
const PROTECTED_PROPERTIES = new Set(["id", "file", "codeRefs", "groups", "occurrences", "schemaVersion"]);
|
|
8
|
+
const PROTECTED_PROPERTIES = new Set(["id", "identity", "file", "codeRefs", "groups", "occurrences", "schemaVersion"]);
|
|
9
9
|
function clone(value) {
|
|
10
10
|
return JSON.parse(JSON.stringify(value));
|
|
11
11
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { DataParadeGraph } from "../../vendor/scanner/dist/src/index.js";
|
|
2
2
|
import type { OrchestratorScanResult } from "../core/pipeline/orchestrator-result";
|
|
3
|
-
import { type DataItemFile, type FactsFile } from "./schema";
|
|
3
|
+
import { type DataItemFile, type FactsFile, type ScanStamps } from "./schema";
|
|
4
4
|
export interface BuildFactsInput {
|
|
5
5
|
result: OrchestratorScanResult;
|
|
6
6
|
/** The knowledge graph of the scan; without it, Facts have no code refs and no relations. */
|
|
@@ -11,6 +11,8 @@ export interface BuildFactsInput {
|
|
|
11
11
|
cliVersion: string;
|
|
12
12
|
scannerVersion: string;
|
|
13
13
|
scannedAt?: string;
|
|
14
|
+
/** Scan stamps (DATAP-876); left out of facts.json when not given. */
|
|
15
|
+
stamps?: ScanStamps;
|
|
14
16
|
}
|
|
15
17
|
export interface BuiltFacts {
|
|
16
18
|
facts: FactsFile;
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.buildFacts = buildFacts;
|
|
7
7
|
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const identity_1 = require("./identity");
|
|
8
9
|
const slug_1 = require("./slug");
|
|
9
10
|
const schema_1 = require("./schema");
|
|
10
11
|
const DP_PREFIX = "dp::";
|
|
@@ -96,8 +97,10 @@ function buildFacts(input) {
|
|
|
96
97
|
}
|
|
97
98
|
const refsFor = (id) => refs.get(id) ?? [];
|
|
98
99
|
const locations = (list) => (list ?? []).map((l) => ({ filePath: scrub(l.filePath), startLine: l.startLine, endLine: l.endLine }));
|
|
100
|
+
const identities = (0, identity_1.buildScanIdentities)(result.scanResult.components, result.scanResult.dataFlows, scrub);
|
|
99
101
|
const components = result.scanResult.components.map((c) => ({
|
|
100
102
|
id: scrub(c.id),
|
|
103
|
+
...(identities.components.has(scrub(c.id)) ? { identity: identities.components.get(scrub(c.id)) } : {}),
|
|
101
104
|
name: c.name,
|
|
102
105
|
type: c.type,
|
|
103
106
|
subType: c.subType ?? "",
|
|
@@ -107,6 +110,7 @@ function buildFacts(input) {
|
|
|
107
110
|
}));
|
|
108
111
|
const dataFlows = result.scanResult.dataFlows.map((f) => ({
|
|
109
112
|
id: scrub(f.id),
|
|
113
|
+
...(identities.dataFlows.has(scrub(f.id)) ? { identity: identities.dataFlows.get(scrub(f.id)) } : {}),
|
|
110
114
|
sourceComponentId: scrub(f.sourceComponentId),
|
|
111
115
|
targetComponentId: scrub(f.targetComponentId),
|
|
112
116
|
type: f.type,
|
|
@@ -130,7 +134,8 @@ function buildFacts(input) {
|
|
|
130
134
|
return o ? [o] : [];
|
|
131
135
|
});
|
|
132
136
|
const groupOf = new Map();
|
|
133
|
-
const
|
|
137
|
+
const groupIdentities = (0, identity_1.numberGroupDuplicates)((item.groups ?? []).map((g) => [scrub(g.id), (0, identity_1.groupIdentity)(name, scrub(g.id))]));
|
|
138
|
+
const groups = (item.groups ?? []).map((g, index) => {
|
|
134
139
|
const groupId = scrub(`group:${g.id}`);
|
|
135
140
|
for (const id of g.occurrenceIds)
|
|
136
141
|
groupOf.set(id, groupId);
|
|
@@ -138,6 +143,7 @@ function buildFacts(input) {
|
|
|
138
143
|
const prefix = `${name}:`;
|
|
139
144
|
return {
|
|
140
145
|
id: groupId,
|
|
146
|
+
identity: groupIdentities[index],
|
|
141
147
|
label: g.id.startsWith(prefix) ? g.id.slice(prefix.length) || g.id : g.id,
|
|
142
148
|
...(basis ? { basis } : {}),
|
|
143
149
|
occurrenceCount: g.occurrenceIds.length,
|
|
@@ -163,6 +169,7 @@ function buildFacts(input) {
|
|
|
163
169
|
occurrenceFacts.sort((a, b) => a.filePath.localeCompare(b.filePath) || a.startLine - b.startLine || a.id.localeCompare(b.id));
|
|
164
170
|
dataItems.push({
|
|
165
171
|
id: itemId,
|
|
172
|
+
identity: (0, identity_1.dataItemIdentity)(name),
|
|
166
173
|
name,
|
|
167
174
|
labels: [...item.labels],
|
|
168
175
|
occurrenceCount: occurrenceFacts.length,
|
|
@@ -172,6 +179,7 @@ function buildFacts(input) {
|
|
|
172
179
|
dataItemFiles.set(file, {
|
|
173
180
|
schemaVersion: schema_1.SCHEMA_VERSION,
|
|
174
181
|
id: itemId,
|
|
182
|
+
identity: (0, identity_1.dataItemIdentity)(name),
|
|
175
183
|
name,
|
|
176
184
|
labels: [...item.labels],
|
|
177
185
|
groups: byId(groups.map((g) => ({ ...g }))),
|
|
@@ -187,6 +195,7 @@ function buildFacts(input) {
|
|
|
187
195
|
scannedAt: input.scannedAt ?? new Date().toISOString(),
|
|
188
196
|
cliVersion: input.cliVersion,
|
|
189
197
|
scannerVersion: input.scannerVersion,
|
|
198
|
+
...(input.stamps ? { stamps: input.stamps } : {}),
|
|
190
199
|
},
|
|
191
200
|
components: byId(components),
|
|
192
201
|
dataFlows: byId(dataFlows),
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type DetectedComponent, type DetectedDataFlow } from "../../vendor/scanner/dist/src/index.js";
|
|
2
|
+
import type { Identity } from "./schema";
|
|
3
|
+
export interface ScanIdentities {
|
|
4
|
+
/** By the component's id in facts.json. */
|
|
5
|
+
components: Map<string, Identity>;
|
|
6
|
+
/** By the flow's id in facts.json. */
|
|
7
|
+
dataFlows: Map<string, Identity>;
|
|
8
|
+
}
|
|
9
|
+
export declare function buildScanIdentities(components: DetectedComponent[], dataFlows: DetectedDataFlow[], scrub: (value: string) => string): ScanIdentities;
|
|
10
|
+
/** `data_item:<name>` → key `<name>`. Data item names carry no path or line. */
|
|
11
|
+
export declare function dataItemIdentity(name: string): Identity;
|
|
12
|
+
/**
|
|
13
|
+
* Group ids (relative to the scan root) are `<data item>:<qualifier>`, `<data item>@<column>`,
|
|
14
|
+
* or, for a group named after the declaration its occurrences share,
|
|
15
|
+
* `<data item>@<file>:<line>` or `<data item>@<file>:<line>#<name>` (scanner
|
|
16
|
+
* `declarationNodeId`). The key drops the declaration's line, so it does not change when code
|
|
17
|
+
* above it moves: `email@src/a.js#email`. Location-named groups (`<item>~file:line`) are never
|
|
18
|
+
* listed as groups. Two unnamed declarations in one file share a key; `numberGroupDuplicates`
|
|
19
|
+
* numbers them.
|
|
20
|
+
*/
|
|
21
|
+
export declare function groupIdentity(dataItemName: string, groupId: string): Identity;
|
|
22
|
+
/**
|
|
23
|
+
* Numbers groups of one data item that share a key (`#2`, `#3`, …, with `duplicateIndex`), by
|
|
24
|
+
* the declaration line in their ids, then by id. Takes `[groupId, identity]` pairs and returns
|
|
25
|
+
* the identities in the same order.
|
|
26
|
+
*/
|
|
27
|
+
export declare function numberGroupDuplicates(groups: Array<[string, Identity]>): Identity[];
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildScanIdentities = buildScanIdentities;
|
|
4
|
+
exports.dataItemIdentity = dataItemIdentity;
|
|
5
|
+
exports.groupIdentity = groupIdentity;
|
|
6
|
+
exports.numberGroupDuplicates = numberGroupDuplicates;
|
|
7
|
+
const scanner_1 = require("../../vendor/scanner/dist/src/index.js");
|
|
8
|
+
/**
|
|
9
|
+
* Identity of every Fact (DATAP-843, DATAP-844): the canonical key the scanner hashes
|
|
10
|
+
* into a component or flow id, or the name-based key of a data item or group, plus the
|
|
11
|
+
* parts the key is made of.
|
|
12
|
+
*
|
|
13
|
+
* Keys never contain the scan root or a line number, so two scans of the same commit in
|
|
14
|
+
* two folders give the same keys. Duplicate keys get `#2`, `#3`, … and `duplicateIndex`, in
|
|
15
|
+
* the order the scanner suffixes ids (`_2`, `_3`, …); see `identitySchema` for the rule.
|
|
16
|
+
* The number comes from the id suffix, never from the key text, because a name can itself
|
|
17
|
+
* end in `#<digits>`.
|
|
18
|
+
*/
|
|
19
|
+
const SECTION_FALLBACK = "<unsectioned>";
|
|
20
|
+
function compareStrings(a, b) {
|
|
21
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
22
|
+
}
|
|
23
|
+
function normalizeName(name) {
|
|
24
|
+
return name.trim().toLowerCase().replace(/\s+/g, " ");
|
|
25
|
+
}
|
|
26
|
+
function sectionOf(properties) {
|
|
27
|
+
const raw = properties?.section_id;
|
|
28
|
+
return typeof raw === "string" && raw.trim() ? raw.trim() : SECTION_FALLBACK;
|
|
29
|
+
}
|
|
30
|
+
function firstPath(paths) {
|
|
31
|
+
return [...paths].sort(compareStrings)[0] ?? "";
|
|
32
|
+
}
|
|
33
|
+
const STABLE_ID = /^(?:cmp|flow)_[0-9a-f]{12}(?:_(\d+))?$/;
|
|
34
|
+
/**
|
|
35
|
+
* The scanner's duplicate number for a stable id: `n` for `<prefix>_<hash>_n`, `undefined`
|
|
36
|
+
* for a plain stable id, and `null` for an id that is not a stable hash (older or test data).
|
|
37
|
+
*/
|
|
38
|
+
function idSuffix(id) {
|
|
39
|
+
const match = STABLE_ID.exec(id);
|
|
40
|
+
if (!match)
|
|
41
|
+
return null;
|
|
42
|
+
const n = match[1] === undefined ? undefined : Number(match[1]);
|
|
43
|
+
return n !== undefined && n >= 2 ? n : undefined;
|
|
44
|
+
}
|
|
45
|
+
function withDuplicate(identity, duplicateIndex) {
|
|
46
|
+
return duplicateIndex ? { ...identity, key: `${identity.key}#${duplicateIndex}`, duplicateIndex } : identity;
|
|
47
|
+
}
|
|
48
|
+
/** Copies of the scan's components and flows with file paths relative to the scan root. */
|
|
49
|
+
function scrubLocations(item, scrub) {
|
|
50
|
+
const next = { ...item };
|
|
51
|
+
if (item.sourceLocations) {
|
|
52
|
+
next.sourceLocations = item.sourceLocations.map((loc) => ({ ...loc, filePath: scrub(loc.filePath) }));
|
|
53
|
+
}
|
|
54
|
+
const single = item.sourceLocation;
|
|
55
|
+
if (single)
|
|
56
|
+
next.sourceLocation = { ...single, filePath: scrub(single.filePath) };
|
|
57
|
+
return next;
|
|
58
|
+
}
|
|
59
|
+
function componentParts(component, baseKey,
|
|
60
|
+
/** Base keys (without `#n`), as the scanner embeds a provider's key in a managed key. */
|
|
61
|
+
baseKeyById) {
|
|
62
|
+
const props = component.properties ?? {};
|
|
63
|
+
if (baseKey.startsWith("tf:")) {
|
|
64
|
+
return { scheme: "tf", terraformAddress: String(props.terraform_address ?? "").trim() };
|
|
65
|
+
}
|
|
66
|
+
if (baseKey.startsWith("managed:")) {
|
|
67
|
+
const providerId = String(props.managed_by_provider ?? "").trim();
|
|
68
|
+
const providerKey = baseKeyById.get(providerId);
|
|
69
|
+
return {
|
|
70
|
+
scheme: "managed",
|
|
71
|
+
provider: providerKey ?? providerId,
|
|
72
|
+
serviceKey: String(props.managed_service_key ?? "").trim(),
|
|
73
|
+
section: sectionOf(props),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
scheme: "app",
|
|
78
|
+
section: sectionOf(props),
|
|
79
|
+
type: component.type,
|
|
80
|
+
subType: component.subType ?? "",
|
|
81
|
+
file: firstPath((component.sourceLocations ?? []).map((loc) => loc.filePath.replace(/\\/g, "/"))),
|
|
82
|
+
// The root Application named after the project or scan folder: the scanner keys it by a fixed token (DATAP-858).
|
|
83
|
+
name: props[scanner_1.NAME_FROM_PROJECT_PROPERTY] === true ? scanner_1.PROJECT_NAME_KEY_TOKEN : normalizeName(component.name),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function buildScanIdentities(components, dataFlows, scrub) {
|
|
87
|
+
const scrubbedComponents = components.map((c) => scrubLocations(c, scrub));
|
|
88
|
+
// Unique keys (with `#n`), in the order the scanner suffixes ids.
|
|
89
|
+
const uniqueKeyById = (0, scanner_1.buildComponentKeyById)(scrubbedComponents);
|
|
90
|
+
// Base key and duplicate number by id. The scanner gives the n-th duplicate `_n` on the id
|
|
91
|
+
// and `#n` on the unique key; read n from the id so a name ending in `#2` is not a duplicate.
|
|
92
|
+
const baseKeyById = new Map();
|
|
93
|
+
const duplicateById = new Map();
|
|
94
|
+
for (const component of scrubbedComponents) {
|
|
95
|
+
const uniqueKey = uniqueKeyById.get(component.id);
|
|
96
|
+
if (uniqueKey === undefined)
|
|
97
|
+
continue;
|
|
98
|
+
const n = idSuffix(component.id);
|
|
99
|
+
const duplicate = n && uniqueKey.endsWith(`#${n}`) ? n : undefined;
|
|
100
|
+
baseKeyById.set(component.id, duplicate ? uniqueKey.slice(0, -`#${duplicate}`.length) : uniqueKey);
|
|
101
|
+
duplicateById.set(component.id, duplicate);
|
|
102
|
+
}
|
|
103
|
+
const componentIdentities = new Map();
|
|
104
|
+
for (const component of scrubbedComponents) {
|
|
105
|
+
const key = baseKeyById.get(component.id);
|
|
106
|
+
if (key === undefined || componentIdentities.has(scrub(component.id)))
|
|
107
|
+
continue;
|
|
108
|
+
componentIdentities.set(scrub(component.id), withDuplicate({ kind: "component", key, parts: componentParts(component, key, baseKeyById) }, duplicateById.get(component.id)));
|
|
109
|
+
}
|
|
110
|
+
const flowEntries = dataFlows.map((flow) => {
|
|
111
|
+
const scrubbed = scrubLocations(flow, scrub);
|
|
112
|
+
const paths = [
|
|
113
|
+
...(scrubbed.sourceLocation ? [scrubbed.sourceLocation.filePath] : []),
|
|
114
|
+
...(scrubbed.sourceLocations ?? []).map((loc) => loc.filePath),
|
|
115
|
+
].map((p) => p.replace(/\\/g, "/"));
|
|
116
|
+
const identity = {
|
|
117
|
+
kind: "dataFlow",
|
|
118
|
+
key: (0, scanner_1.stableFlowKey)(scrubbed, uniqueKeyById),
|
|
119
|
+
parts: {
|
|
120
|
+
source: uniqueKeyById.get(flow.sourceComponentId) ?? `id:${flow.sourceComponentId}`,
|
|
121
|
+
target: uniqueKeyById.get(flow.targetComponentId) ?? `id:${flow.targetComponentId}`,
|
|
122
|
+
type: flow.type,
|
|
123
|
+
method: (flow.method ?? "").trim().toUpperCase(),
|
|
124
|
+
endpoint: (0, scanner_1.normalizeFlowEndpoint)(flow.endpoint),
|
|
125
|
+
file: firstPath(paths),
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
return { id: scrub(flow.id), identity };
|
|
129
|
+
});
|
|
130
|
+
// Flows that share a key: number them in id order, which is the scanner's suffix order.
|
|
131
|
+
const suffix = (id) => idSuffix(id) ?? 1;
|
|
132
|
+
const byKey = new Map();
|
|
133
|
+
for (const entry of flowEntries) {
|
|
134
|
+
const list = byKey.get(entry.identity.key) ?? [];
|
|
135
|
+
list.push(entry);
|
|
136
|
+
byKey.set(entry.identity.key, list);
|
|
137
|
+
}
|
|
138
|
+
const flowIdentities = new Map();
|
|
139
|
+
for (const list of byKey.values()) {
|
|
140
|
+
list.sort((a, b) => suffix(a.id) - suffix(b.id) || compareStrings(a.id, b.id));
|
|
141
|
+
list.forEach((entry, index) => {
|
|
142
|
+
if (!flowIdentities.has(entry.id)) {
|
|
143
|
+
flowIdentities.set(entry.id, withDuplicate(entry.identity, index === 0 ? undefined : index + 1));
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return { components: componentIdentities, dataFlows: flowIdentities };
|
|
148
|
+
}
|
|
149
|
+
/** `data_item:<name>` → key `<name>`. Data item names carry no path or line. */
|
|
150
|
+
function dataItemIdentity(name) {
|
|
151
|
+
return { kind: "dataItem", key: name, parts: { name } };
|
|
152
|
+
}
|
|
153
|
+
/** `<file>:<line>` or `<file>:<line>#<name>` after the `@` of a declaration-named group. */
|
|
154
|
+
const DECLARATION = /^(.+):(\d+)(?:#(.*))?$/;
|
|
155
|
+
/**
|
|
156
|
+
* Group ids (relative to the scan root) are `<data item>:<qualifier>`, `<data item>@<column>`,
|
|
157
|
+
* or, for a group named after the declaration its occurrences share,
|
|
158
|
+
* `<data item>@<file>:<line>` or `<data item>@<file>:<line>#<name>` (scanner
|
|
159
|
+
* `declarationNodeId`). The key drops the declaration's line, so it does not change when code
|
|
160
|
+
* above it moves: `email@src/a.js#email`. Location-named groups (`<item>~file:line`) are never
|
|
161
|
+
* listed as groups. Two unnamed declarations in one file share a key; `numberGroupDuplicates`
|
|
162
|
+
* numbers them.
|
|
163
|
+
*/
|
|
164
|
+
function groupIdentity(dataItemName, groupId) {
|
|
165
|
+
const rest = groupId.startsWith(dataItemName) ? groupId.slice(dataItemName.length) : "";
|
|
166
|
+
if (rest.startsWith("@")) {
|
|
167
|
+
const declaration = DECLARATION.exec(rest.slice(1));
|
|
168
|
+
if (declaration) {
|
|
169
|
+
const [, file, , name] = declaration;
|
|
170
|
+
return {
|
|
171
|
+
kind: "group",
|
|
172
|
+
key: `${dataItemName}@${file}${name !== undefined ? `#${name}` : ""}`,
|
|
173
|
+
parts: { dataItem: dataItemName, declarationFile: file, ...(name !== undefined ? { declarationName: name } : {}) },
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return { kind: "group", key: groupId, parts: { dataItem: dataItemName, column: rest.slice(1) } };
|
|
177
|
+
}
|
|
178
|
+
const parts = rest.startsWith(":")
|
|
179
|
+
? { dataItem: dataItemName, qualifier: rest.slice(1) }
|
|
180
|
+
: { dataItem: dataItemName, group: groupId };
|
|
181
|
+
return { kind: "group", key: groupId, parts };
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Numbers groups of one data item that share a key (`#2`, `#3`, …, with `duplicateIndex`), by
|
|
185
|
+
* the declaration line in their ids, then by id. Takes `[groupId, identity]` pairs and returns
|
|
186
|
+
* the identities in the same order.
|
|
187
|
+
*/
|
|
188
|
+
function numberGroupDuplicates(groups) {
|
|
189
|
+
const line = (id) => Number(/:(\d+)(?:#.*)?$/.exec(id)?.[1] ?? 0);
|
|
190
|
+
const byKey = new Map();
|
|
191
|
+
groups.forEach(([, identity], index) => byKey.set(identity.key, [...(byKey.get(identity.key) ?? []), index]));
|
|
192
|
+
const result = groups.map(([, identity]) => identity);
|
|
193
|
+
for (const indexes of byKey.values()) {
|
|
194
|
+
if (indexes.length < 2)
|
|
195
|
+
continue;
|
|
196
|
+
indexes.sort((a, b) => line(groups[a][0]) - line(groups[b][0]) || compareStrings(groups[a][0], groups[b][0]));
|
|
197
|
+
indexes.forEach((index, n) => {
|
|
198
|
+
if (n > 0)
|
|
199
|
+
result[index] = withDuplicate(result[index], n + 1);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return result;
|
|
203
|
+
}
|
package/dist/src/facts/io.d.ts
CHANGED
|
@@ -13,5 +13,7 @@ export declare function readFacts(layout: OutputLayout): FactsFile;
|
|
|
13
13
|
/** All data item files named by facts.json, in the order of facts.dataItems. */
|
|
14
14
|
export declare function readDataItemFiles(layout: OutputLayout, facts: FactsFile): DataItemFile[];
|
|
15
15
|
export declare function readAnswers(layout: OutputLayout): AnswersFile;
|
|
16
|
+
/** Replace answers.json with these answers (used by migrations that add fields to existing answers). */
|
|
17
|
+
export declare function writeAnswers(layout: OutputLayout, file: AnswersFile): AnswersFile;
|
|
16
18
|
/** Append answers to answers.json (created when missing). Existing answers are kept as they are. */
|
|
17
19
|
export declare function appendAnswers(layout: OutputLayout, answers: Answer[]): AnswersFile;
|
package/dist/src/facts/io.js
CHANGED
|
@@ -8,6 +8,7 @@ exports.writeFactsOutput = writeFactsOutput;
|
|
|
8
8
|
exports.readFacts = readFacts;
|
|
9
9
|
exports.readDataItemFiles = readDataItemFiles;
|
|
10
10
|
exports.readAnswers = readAnswers;
|
|
11
|
+
exports.writeAnswers = writeAnswers;
|
|
11
12
|
exports.appendAnswers = appendAnswers;
|
|
12
13
|
const fs_1 = __importDefault(require("fs"));
|
|
13
14
|
const path_1 = __importDefault(require("path"));
|
|
@@ -58,6 +59,12 @@ function readAnswers(layout) {
|
|
|
58
59
|
return { schemaVersion: schema_1.SCHEMA_VERSION, answers: [] };
|
|
59
60
|
return schema_1.answersFileSchema.parse(JSON.parse(fs_1.default.readFileSync(layout.answersPath, "utf8")));
|
|
60
61
|
}
|
|
62
|
+
/** Replace answers.json with these answers (used by migrations that add fields to existing answers). */
|
|
63
|
+
function writeAnswers(layout, file) {
|
|
64
|
+
const next = schema_1.answersFileSchema.parse({ ...file, schemaVersion: schema_1.SCHEMA_VERSION });
|
|
65
|
+
writeJson(layout.answersPath, next);
|
|
66
|
+
return next;
|
|
67
|
+
}
|
|
61
68
|
/** Append answers to answers.json (created when missing). Existing answers are kept as they are. */
|
|
62
69
|
function appendAnswers(layout, answers) {
|
|
63
70
|
const file = readAnswers(layout);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ScanStamps } from "./schema";
|
|
2
|
+
/** The settings that change the scan output. AI, upload and display settings are not here. */
|
|
3
|
+
export interface OutputSettings {
|
|
4
|
+
organizationDomains?: string[];
|
|
5
|
+
excludePaths?: string[];
|
|
6
|
+
enableAPIDetection?: boolean;
|
|
7
|
+
enableDatabaseDetection?: boolean;
|
|
8
|
+
enableDataFlowDetection?: boolean;
|
|
9
|
+
languages?: string[];
|
|
10
|
+
minimumConfidence?: number;
|
|
11
|
+
deepAnalysis?: boolean;
|
|
12
|
+
terraformJsonPath?: string;
|
|
13
|
+
terraformPlanPath?: string;
|
|
14
|
+
terraformStackSectionPathDepth?: number;
|
|
15
|
+
autoInferTerraformStackSectionPathDepth?: boolean;
|
|
16
|
+
monorepoPackageSectionPathDepth?: number;
|
|
17
|
+
autoInferMonorepoPackageSectionPathDepth?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export declare function configHash(config: OutputSettings): string;
|
|
20
|
+
/** Hash of every file under `patterns/`, by sorted relative path and content. */
|
|
21
|
+
export declare function catalogHash(patternsDir: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* The bundled scanner (dist/vendor/scanner, written by scripts/vendor-scanner.mjs) records its
|
|
24
|
+
* commit and catalog hash. Running from source (tests), the installed scanner package is used.
|
|
25
|
+
*/
|
|
26
|
+
export declare function scannerBuildInfo(cliPackageRoot: string, pinnedCommit: string): {
|
|
27
|
+
scannerCommit: string;
|
|
28
|
+
catalogHash: string;
|
|
29
|
+
};
|
|
30
|
+
/** The @dataparade/cli package root, from a file inside it (src/ when run from source, dist/src/ when built). */
|
|
31
|
+
export declare function findCliPackageRoot(start: string): string;
|
|
32
|
+
export interface GitStamps {
|
|
33
|
+
commitSha: string | null;
|
|
34
|
+
dirty: boolean | null;
|
|
35
|
+
rootCommitSha: string | null;
|
|
36
|
+
scanRoot: string | null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Git facts for the scanned folder, read before the scan writes anything. `outputDir` is left
|
|
40
|
+
* out of the dirty check, so the scan's own output never makes a checkout dirty. In a shallow
|
|
41
|
+
* clone the root commit is unknown (null); the repository is then matched by its remote URL.
|
|
42
|
+
* `fallbackCommitSha` comes from --git-context, for scans of archives that have no .git.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readGitStamps(scanRoot: string, outputDir: string | null, fallbackCommitSha?: string): GitStamps;
|
|
45
|
+
export declare function buildScanStamps(input: {
|
|
46
|
+
cliVersion: string;
|
|
47
|
+
scanner: {
|
|
48
|
+
scannerCommit: string;
|
|
49
|
+
catalogHash: string;
|
|
50
|
+
};
|
|
51
|
+
config: OutputSettings;
|
|
52
|
+
aiInference: boolean;
|
|
53
|
+
structureGraph: boolean;
|
|
54
|
+
git: GitStamps;
|
|
55
|
+
}): ScanStamps;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.configHash = configHash;
|
|
7
|
+
exports.catalogHash = catalogHash;
|
|
8
|
+
exports.scannerBuildInfo = scannerBuildInfo;
|
|
9
|
+
exports.findCliPackageRoot = findCliPackageRoot;
|
|
10
|
+
exports.readGitStamps = readGitStamps;
|
|
11
|
+
exports.buildScanStamps = buildScanStamps;
|
|
12
|
+
const child_process_1 = require("child_process");
|
|
13
|
+
const crypto_1 = require("crypto");
|
|
14
|
+
const fs_1 = __importDefault(require("fs"));
|
|
15
|
+
const path_1 = __importDefault(require("path"));
|
|
16
|
+
/**
|
|
17
|
+
* Scan stamps (DATAP-876): what a comparison needs to know to decide whether two scans can be
|
|
18
|
+
* compared. Two scans of the same commit with the same CLI, scanner, catalog and configuration
|
|
19
|
+
* have equal stamps.
|
|
20
|
+
*/
|
|
21
|
+
function sha256(text) {
|
|
22
|
+
return (0, crypto_1.createHash)("sha256").update(text).digest("hex");
|
|
23
|
+
}
|
|
24
|
+
/** JSON with sorted keys, so equal settings always hash the same. */
|
|
25
|
+
function canonical(value) {
|
|
26
|
+
if (Array.isArray(value))
|
|
27
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
28
|
+
if (value && typeof value === "object") {
|
|
29
|
+
return `{${Object.entries(value)
|
|
30
|
+
.filter(([, v]) => v !== undefined)
|
|
31
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
32
|
+
.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`)
|
|
33
|
+
.join(",")}}`;
|
|
34
|
+
}
|
|
35
|
+
return JSON.stringify(value ?? null);
|
|
36
|
+
}
|
|
37
|
+
function configHash(config) {
|
|
38
|
+
const sorted = (list) => (list ? [...list].sort() : undefined);
|
|
39
|
+
return `sha256:${sha256(canonical({
|
|
40
|
+
organizationDomains: sorted(config.organizationDomains?.map((d) => d.trim().toLowerCase())),
|
|
41
|
+
excludePaths: sorted(config.excludePaths),
|
|
42
|
+
enableAPIDetection: config.enableAPIDetection,
|
|
43
|
+
enableDatabaseDetection: config.enableDatabaseDetection,
|
|
44
|
+
enableDataFlowDetection: config.enableDataFlowDetection,
|
|
45
|
+
languages: sorted(config.languages),
|
|
46
|
+
minimumConfidence: config.minimumConfidence,
|
|
47
|
+
deepAnalysis: config.deepAnalysis,
|
|
48
|
+
terraformJsonPath: config.terraformJsonPath,
|
|
49
|
+
terraformPlanPath: config.terraformPlanPath,
|
|
50
|
+
terraformStackSectionPathDepth: config.terraformStackSectionPathDepth,
|
|
51
|
+
autoInferTerraformStackSectionPathDepth: config.autoInferTerraformStackSectionPathDepth,
|
|
52
|
+
monorepoPackageSectionPathDepth: config.monorepoPackageSectionPathDepth,
|
|
53
|
+
autoInferMonorepoPackageSectionPathDepth: config.autoInferMonorepoPackageSectionPathDepth,
|
|
54
|
+
})).slice(0, 16)}`;
|
|
55
|
+
}
|
|
56
|
+
/** Hash of every file under `patterns/`, by sorted relative path and content. */
|
|
57
|
+
function catalogHash(patternsDir) {
|
|
58
|
+
const files = [];
|
|
59
|
+
const walk = (dir) => {
|
|
60
|
+
for (const name of fs_1.default.readdirSync(dir)) {
|
|
61
|
+
const full = path_1.default.join(dir, name);
|
|
62
|
+
if (fs_1.default.statSync(full).isDirectory())
|
|
63
|
+
walk(full);
|
|
64
|
+
else
|
|
65
|
+
files.push(full);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
walk(patternsDir);
|
|
69
|
+
const hash = (0, crypto_1.createHash)("sha256");
|
|
70
|
+
for (const file of files.map((f) => path_1.default.relative(patternsDir, f).split(path_1.default.sep).join("/")).sort()) {
|
|
71
|
+
hash.update(`${file}\0`);
|
|
72
|
+
hash.update(fs_1.default.readFileSync(path_1.default.join(patternsDir, file)));
|
|
73
|
+
hash.update("\0");
|
|
74
|
+
}
|
|
75
|
+
return `sha256:${hash.digest("hex").slice(0, 16)}`;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The bundled scanner (dist/vendor/scanner, written by scripts/vendor-scanner.mjs) records its
|
|
79
|
+
* commit and catalog hash. Running from source (tests), the installed scanner package is used.
|
|
80
|
+
*/
|
|
81
|
+
function scannerBuildInfo(cliPackageRoot, pinnedCommit) {
|
|
82
|
+
// The build copies package.json into dist/, so the root found from dist/src may be dist/ itself.
|
|
83
|
+
for (const vendored of [path_1.default.join(cliPackageRoot, "dist", "vendor", "scanner"), path_1.default.join(cliPackageRoot, "vendor", "scanner")]) {
|
|
84
|
+
const manifest = path_1.default.join(vendored, "package.json");
|
|
85
|
+
if (!fs_1.default.existsSync(manifest))
|
|
86
|
+
continue;
|
|
87
|
+
const info = JSON.parse(fs_1.default.readFileSync(manifest, "utf8"));
|
|
88
|
+
if (info.gitHead && info.catalogHash)
|
|
89
|
+
return { scannerCommit: info.gitHead, catalogHash: info.catalogHash };
|
|
90
|
+
}
|
|
91
|
+
const installed = path_1.default.join(cliPackageRoot, "node_modules", "@dataparade", "scanner", "patterns");
|
|
92
|
+
return { scannerCommit: pinnedCommit, catalogHash: fs_1.default.existsSync(installed) ? catalogHash(installed) : "unknown" };
|
|
93
|
+
}
|
|
94
|
+
/** The @dataparade/cli package root, from a file inside it (src/ when run from source, dist/src/ when built). */
|
|
95
|
+
function findCliPackageRoot(start) {
|
|
96
|
+
let dir = start;
|
|
97
|
+
for (;;) {
|
|
98
|
+
const manifest = path_1.default.join(dir, "package.json");
|
|
99
|
+
if (fs_1.default.existsSync(manifest)) {
|
|
100
|
+
try {
|
|
101
|
+
if (JSON.parse(fs_1.default.readFileSync(manifest, "utf8")).name === "@dataparade/cli")
|
|
102
|
+
return dir;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// keep looking
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const parent = path_1.default.dirname(dir);
|
|
109
|
+
if (parent === dir)
|
|
110
|
+
return start;
|
|
111
|
+
dir = parent;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** realpath of a path that may not exist yet: the nearest existing ancestor, resolved, plus the rest. */
|
|
115
|
+
function realpathOfNearest(target) {
|
|
116
|
+
let dir = path_1.default.resolve(target);
|
|
117
|
+
const rest = [];
|
|
118
|
+
while (!fs_1.default.existsSync(dir)) {
|
|
119
|
+
rest.unshift(path_1.default.basename(dir));
|
|
120
|
+
const parent = path_1.default.dirname(dir);
|
|
121
|
+
if (parent === dir)
|
|
122
|
+
break;
|
|
123
|
+
dir = parent;
|
|
124
|
+
}
|
|
125
|
+
return path_1.default.join(fs_1.default.realpathSync(dir), ...rest);
|
|
126
|
+
}
|
|
127
|
+
function git(cwd, args) {
|
|
128
|
+
return (0, child_process_1.execFileSync)("git", ["-C", cwd, ...args], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Git facts for the scanned folder, read before the scan writes anything. `outputDir` is left
|
|
132
|
+
* out of the dirty check, so the scan's own output never makes a checkout dirty. In a shallow
|
|
133
|
+
* clone the root commit is unknown (null); the repository is then matched by its remote URL.
|
|
134
|
+
* `fallbackCommitSha` comes from --git-context, for scans of archives that have no .git.
|
|
135
|
+
*/
|
|
136
|
+
function readGitStamps(scanRoot, outputDir, fallbackCommitSha) {
|
|
137
|
+
try {
|
|
138
|
+
const top = git(scanRoot, ["rev-parse", "--show-toplevel"]);
|
|
139
|
+
const commitSha = git(top, ["rev-parse", "HEAD"]);
|
|
140
|
+
const realTop = fs_1.default.realpathSync(top);
|
|
141
|
+
const realOutput = outputDir ? realpathOfNearest(outputDir) : null;
|
|
142
|
+
const exclude = realOutput && realOutput.startsWith(`${realTop}${path_1.default.sep}`)
|
|
143
|
+
? [`:(exclude)${path_1.default.relative(realTop, realOutput).split(path_1.default.sep).join("/")}`]
|
|
144
|
+
: [];
|
|
145
|
+
const dirty = git(top, ["status", "--porcelain", "--", ".", ...exclude]) !== "";
|
|
146
|
+
const shallow = git(top, ["rev-parse", "--is-shallow-repository"]) === "true";
|
|
147
|
+
const roots = shallow ? [] : git(top, ["rev-list", "--max-parents=0", "HEAD"]).split("\n").filter(Boolean);
|
|
148
|
+
const relative = path_1.default.relative(fs_1.default.realpathSync(top), fs_1.default.realpathSync(scanRoot)).split(path_1.default.sep).join("/");
|
|
149
|
+
return { commitSha, dirty, rootCommitSha: roots.length > 0 ? roots[roots.length - 1] : null, scanRoot: relative };
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return { commitSha: fallbackCommitSha ?? null, dirty: fallbackCommitSha ? false : null, rootCommitSha: null, scanRoot: null };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function buildScanStamps(input) {
|
|
156
|
+
return {
|
|
157
|
+
scannerCommit: input.scanner.scannerCommit,
|
|
158
|
+
cliVersion: input.cliVersion,
|
|
159
|
+
catalogHash: input.scanner.catalogHash,
|
|
160
|
+
configHash: configHash(input.config),
|
|
161
|
+
aiInference: input.aiInference,
|
|
162
|
+
structureGraph: input.structureGraph,
|
|
163
|
+
...input.git,
|
|
164
|
+
};
|
|
165
|
+
}
|