@mmerterden/multi-agent-pipeline 23.3.0 → 23.5.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 +73 -0
- package/README.md +27 -8
- package/README.tr.md +28 -9
- package/SECURITY.md +16 -0
- package/docs/adr/0008-installer-modularization-and-secret-leak-defense.md +1 -1
- package/docs/adr/0010-own-code-graph.md +299 -8
- package/docs/architecture.md +3 -3
- package/docs/ecosystem.md +10 -10
- package/docs/engineering.md +4 -4
- package/docs/facts.json +8 -8
- package/docs/features.md +5 -5
- package/docs/performance.md +17 -17
- package/install/_dev-only-files.mjs +9 -0
- package/install/_platform-filter.mjs +2 -55
- package/install/claude.mjs +28 -2
- package/install/codex.mjs +1 -3
- package/install/copilot.mjs +4 -8
- package/install/templates/claude-hooks.json +32 -1
- package/manifest.json +236 -167
- package/package.json +11 -3
- package/pipeline/commands/multi-agent/SKILL.md +1 -1
- package/pipeline/commands/multi-agent/design-check/SKILL.md +3 -2
- package/pipeline/commands/multi-agent/diff-explain/SKILL.md +3 -3
- package/pipeline/commands/multi-agent/graph/SKILL.md +57 -24
- package/pipeline/commands/multi-agent/pair/SKILL.md +92 -0
- package/pipeline/commands/multi-agent/prune-prompts/SKILL.md +1 -1
- package/pipeline/commands/multi-agent/search/SKILL.md +7 -1
- package/pipeline/commands/multi-agent/status/SKILL.md +4 -0
- package/pipeline/commands/multi-agent/sync/SKILL.md +3 -3
- package/pipeline/contract/CHANGELOG.md +63 -0
- package/pipeline/contract/README.md +1 -0
- package/pipeline/contract/build.mjs +13 -1
- package/pipeline/contract/fixtures/error-invalid-request.json +1 -1
- package/pipeline/contract/fixtures/error-unauthorized.json +1 -1
- package/pipeline/contract/fixtures/runs-awaiting-question.json +4 -1
- package/pipeline/contract/fixtures/runs-empty.json +1 -1
- package/pipeline/contract/fixtures/runs-failed.json +4 -1
- package/pipeline/contract/fixtures/runs-old-schema.json +3 -0
- package/pipeline/contract/fixtures/runs-pr-opened-redacted.json +4 -1
- package/pipeline/contract/fixtures/runs-pr-opened.json +4 -1
- package/pipeline/contract/fixtures/runs-running.json +4 -1
- package/pipeline/contract/manifest.json +41 -15
- package/pipeline/contract/types/index.d.ts +81 -3
- package/pipeline/lib/detach-self.mjs +95 -0
- package/pipeline/lib/public-mcp-servers.mjs +33 -0
- package/pipeline/lib/rotated-log.mjs +93 -0
- package/pipeline/lib/stack-detect.sh +1 -1
- package/pipeline/lib/tls-trust.mjs +29 -0
- package/pipeline/lib/usage-lifecycle.mjs +9 -11
- package/pipeline/lib/usage-names.generated.mjs +613 -0
- package/pipeline/lib/usage-run-key.mjs +235 -0
- package/pipeline/lib/usage-vocab.mjs +210 -0
- package/pipeline/multi-agent-refs/analysis/intake.md +2 -2
- package/pipeline/multi-agent-refs/analysis/render.md +1 -1
- package/pipeline/multi-agent-refs/channels/generator.md +4 -5
- package/pipeline/multi-agent-refs/cross-cli-contract.md +3 -3
- package/pipeline/multi-agent-refs/design-check/init-inventory.md +11 -1
- package/pipeline/multi-agent-refs/design-check/scope-and-ledger.md +1 -1
- package/pipeline/multi-agent-refs/features/code-graph.md +146 -11
- package/pipeline/multi-agent-refs/features/code-intelligence.md +16 -14
- package/pipeline/multi-agent-refs/features/dev-critic.md +7 -7
- package/pipeline/multi-agent-refs/features/model-fallback.md +6 -4
- package/pipeline/multi-agent-refs/features/remote-control.md +253 -0
- package/pipeline/multi-agent-refs/features/review-delta.md +1 -1
- package/pipeline/multi-agent-refs/features/review-file-set.md +3 -3
- package/pipeline/multi-agent-refs/features/review-multi-repo.md +1 -1
- package/pipeline/multi-agent-refs/features/session-brief.md +42 -0
- package/pipeline/multi-agent-refs/features/skill-conformance.md +3 -3
- package/pipeline/multi-agent-refs/features/usage-reporting.md +255 -27
- package/pipeline/multi-agent-refs/help/en.md +3 -2
- package/pipeline/multi-agent-refs/help/tr.md +3 -2
- package/pipeline/multi-agent-refs/knowledge.md +1 -1
- package/pipeline/multi-agent-refs/orchestrator/dispatcher.md +2 -2
- package/pipeline/multi-agent-refs/phases/operations.md +2 -2
- package/pipeline/multi-agent-refs/phases/phase-0-init.md +13 -12
- package/pipeline/multi-agent-refs/phases/phase-1-plan.md +4 -4
- package/pipeline/multi-agent-refs/phases/phase-3-review.md +2 -0
- package/pipeline/multi-agent-refs/phases/phase-5-report.md +1 -1
- package/pipeline/multi-agent-refs/platform-parity.md +2 -2
- package/pipeline/multi-agent-refs/wiki-capture.md +3 -17
- package/pipeline/office/index.html +1309 -236
- package/pipeline/office/toolkit.json +1 -1
- package/pipeline/rules/outside-the-pipeline.md +1 -1
- package/pipeline/schemas/activity.schema.json +23 -0
- package/pipeline/schemas/agent-state.schema.json +6 -1
- package/pipeline/schemas/code-graph-calls.schema.json +254 -0
- package/pipeline/schemas/code-graph-docs.schema.json +223 -0
- package/pipeline/schemas/code-graph-infra.schema.json +227 -0
- package/pipeline/schemas/code-graph.schema.json +2 -2
- package/pipeline/schemas/examples/design-check-config.json +27 -0
- package/pipeline/schemas/learning-curve.schema.json +67 -0
- package/pipeline/schemas/mcp-health.schema.json +96 -0
- package/pipeline/schemas/office-jobs.json +69 -2
- package/pipeline/schemas/office-jobs.schema.json +8 -1
- package/pipeline/schemas/phases.json +5 -0
- package/pipeline/schemas/phases.schema.json +8 -0
- package/pipeline/schemas/prefs.schema.json +46 -12
- package/pipeline/schemas/runs-index.schema.json +17 -0
- package/pipeline/schemas/toolkit-path-params.json +78 -8
- package/pipeline/scripts/README.md +1 -0
- package/pipeline/scripts/_code-graph.mjs +184 -103
- package/pipeline/scripts/_contract-core.mjs +1 -1
- package/pipeline/scripts/_contract-routes-office.mjs +33 -0
- package/pipeline/scripts/_diff-impact.mjs +21 -2
- package/pipeline/scripts/_graph-sidecars.mjs +372 -0
- package/pipeline/scripts/_http-post.mjs +103 -115
- package/pipeline/scripts/_safe-read.mjs +57 -0
- package/pipeline/scripts/_skill-practices.mjs +26 -0
- package/pipeline/scripts/_strip-code.mjs +380 -0
- package/pipeline/scripts/activity-snapshot.mjs +13 -1
- package/pipeline/scripts/aggregate-metrics.mjs +3 -5
- package/pipeline/scripts/build-stack-plugins.mjs +1 -47
- package/pipeline/scripts/call-graph/index.mjs +78 -0
- package/pipeline/scripts/call-graph/lang/cpp.mjs +308 -0
- package/pipeline/scripts/call-graph/lang/csharp.mjs +130 -0
- package/pipeline/scripts/call-graph/lang/go.mjs +303 -0
- package/pipeline/scripts/call-graph/lang/java.mjs +370 -0
- package/pipeline/scripts/call-graph/lang/kotlin.mjs +397 -0
- package/pipeline/scripts/call-graph/lang/python.mjs +399 -0
- package/pipeline/scripts/call-graph/lang/rust.mjs +345 -0
- package/pipeline/scripts/call-graph/lang/shared.mjs +230 -0
- package/pipeline/scripts/call-graph/lang/swift.mjs +432 -0
- package/pipeline/scripts/call-graph/lang/typescript.mjs +692 -0
- package/pipeline/scripts/call-graph/lex.mjs +396 -0
- package/pipeline/scripts/call-graph/platform.mjs +86 -0
- package/pipeline/scripts/call-graph/profiles.mjs +89 -0
- package/pipeline/scripts/call-graph/resolve.mjs +1125 -0
- package/pipeline/scripts/call-graph/scan.mjs +267 -0
- package/pipeline/scripts/code-graph-rules/android.json +7 -2
- package/pipeline/scripts/code-graph-rules/cpp.json +99 -0
- package/pipeline/scripts/code-graph-rules/csharp.json +102 -0
- package/pipeline/scripts/code-graph-rules/go.json +2 -1
- package/pipeline/scripts/code-graph-rules/ios.json +3 -1
- package/pipeline/scripts/code-graph-rules/java.json +76 -0
- package/pipeline/scripts/code-graph-rules/node.json +7 -2
- package/pipeline/scripts/code-graph-rules/python.json +6 -2
- package/pipeline/scripts/code-graph-rules/rust.json +105 -0
- package/pipeline/scripts/contract-server.mjs +3 -0
- package/pipeline/scripts/design-check-config.mjs +205 -0
- package/pipeline/scripts/feedback-send.mjs +26 -26
- package/pipeline/scripts/gate-ledger.mjs +2 -1
- package/pipeline/scripts/gen-facts.mjs +1 -2
- package/pipeline/scripts/gen-usage-names.mjs +124 -0
- package/pipeline/scripts/graph-affected.mjs +109 -20
- package/pipeline/scripts/graph-build.mjs +95 -15
- package/pipeline/scripts/graph-export.mjs +272 -0
- package/pipeline/scripts/graph-hint.mjs +15 -2
- package/pipeline/scripts/graph-query.mjs +305 -26
- package/pipeline/scripts/graph-report.mjs +187 -1
- package/pipeline/scripts/health-report.mjs +3 -4
- package/pipeline/scripts/infra/docs.mjs +317 -0
- package/pipeline/scripts/infra/extract.mjs +998 -0
- package/pipeline/scripts/infra/graph.mjs +245 -0
- package/pipeline/scripts/infra/yaml-lite.mjs +311 -0
- package/pipeline/scripts/job-launch.mjs +67 -2
- package/pipeline/scripts/learning-curve.mjs +288 -178
- package/pipeline/scripts/log-metric.sh +10 -1
- package/pipeline/scripts/mcp-health.mjs +309 -0
- package/pipeline/scripts/note-agent-event.mjs +3 -4
- package/pipeline/scripts/note-command-use.mjs +333 -0
- package/pipeline/scripts/note-command-use.sh +126 -0
- package/pipeline/scripts/note-mcp-call.sh +89 -0
- package/pipeline/scripts/note-mcp-failure.mjs +272 -1
- package/pipeline/scripts/note-session.sh +62 -3
- package/pipeline/scripts/note-skill-use.mjs +58 -2
- package/pipeline/scripts/office-build.mjs +115 -1
- package/pipeline/scripts/office-memory.mjs +14 -5
- package/pipeline/scripts/pair.mjs +333 -0
- package/pipeline/scripts/pre-push-check.sh +1 -1
- package/pipeline/scripts/remote/cbor.mjs +128 -0
- package/pipeline/scripts/remote/contract.mjs +98 -0
- package/pipeline/scripts/remote/handler.mjs +330 -0
- package/pipeline/scripts/remote/handles.mjs +57 -0
- package/pipeline/scripts/remote/lifecycle.mjs +186 -0
- package/pipeline/scripts/remote/listener.mjs +204 -0
- package/pipeline/scripts/remote/lockout.mjs +103 -0
- package/pipeline/scripts/remote/nonces.mjs +61 -0
- package/pipeline/scripts/remote/pairing.mjs +519 -0
- package/pipeline/scripts/remote/qr-encode.mjs +459 -0
- package/pipeline/scripts/remote/store.mjs +191 -0
- package/pipeline/scripts/remote/transport.mjs +211 -0
- package/pipeline/scripts/remote/verbs.json +70 -0
- package/pipeline/scripts/remote/verbs.mjs +217 -0
- package/pipeline/scripts/remote/webauthn.mjs +169 -0
- package/pipeline/scripts/remote/wire.mjs +364 -0
- package/pipeline/scripts/remote-listener.mjs +83 -0
- package/pipeline/scripts/route-learn.mjs +4 -4
- package/pipeline/scripts/run-kill.mjs +1 -1
- package/pipeline/scripts/run-write.mjs +13 -2
- package/pipeline/scripts/runs-index.mjs +30 -3
- package/pipeline/scripts/search-logs.sh +9 -0
- package/pipeline/scripts/session-brief.mjs +458 -0
- package/pipeline/scripts/session-brief.sh +130 -0
- package/pipeline/scripts/skill-usage.mjs +16 -5
- package/pipeline/scripts/test-gap-rules/cpp.json +25 -0
- package/pipeline/scripts/test-gap-rules/csharp.json +20 -0
- package/pipeline/scripts/test-gap-rules/java.json +20 -0
- package/pipeline/scripts/test-gap-rules/rust.json +14 -0
- package/pipeline/scripts/token-budget-report.mjs +3 -2
- package/pipeline/scripts/uninstall.mjs +29 -0
- package/pipeline/scripts/usage-access.mjs +16 -31
- package/pipeline/scripts/usage-health.mjs +31 -62
- package/pipeline/scripts/usage-register-beacon.mjs +11 -22
- package/pipeline/scripts/usage-register.mjs +39 -29
- package/pipeline/scripts/usage-report.mjs +579 -178
- package/pipeline/scripts/validate-code-graph.mjs +361 -111
- package/pipeline/scripts/write-state.mjs +39 -0
- package/pipeline/skills/.skill-manifest.json +21 -17
- package/pipeline/skills/.skills-index.json +13 -2
- package/pipeline/skills/shared/README.md +5 -4
- package/pipeline/skills/shared/core/multi-agent-design-check/SKILL.md +1 -1
- package/pipeline/skills/shared/core/multi-agent-diff-explain/SKILL.md +3 -3
- package/pipeline/skills/shared/core/multi-agent-graph/SKILL.md +55 -21
- package/pipeline/skills/shared/core/multi-agent-pair/SKILL.md +41 -0
- package/pipeline/skills/shared/core/multi-agent-prune-prompts/SKILL.md +1 -1
- package/pipeline/skills/shared/core/multi-agent-sync/SKILL.md +3 -3
- package/pipeline/skills/shared/external/app-store-optimization/SKILL.md +2 -3
- package/pipeline/skills/shared/external/app-store-optimization/references/keyword-research-methodology.md +2 -2
- package/pipeline/skills/shared/external/app-store-optimization/references/product-page-variants.md +2 -2
- package/pipeline/skills/shared/external/app-store-review/SKILL.md +11 -4
- package/pipeline/skills/shared/external/build-settings-audit/SKILL.md +1 -1
- package/pipeline/skills/shared/external/build-time-benchmark/SKILL.md +5 -5
- package/pipeline/skills/shared/external/build-time-fixes/SKILL.md +5 -5
- package/pipeline/skills/shared/external/build-time-optimizer/SKILL.md +6 -6
- package/pipeline/skills/shared/external/free-llm-apis/references/providers.md +1 -0
- package/pipeline/skills/shared/external/ios-coding-standard/references/STANDARD.md +2 -2
- package/pipeline/skills/shared/external/ios-haptics/references/core-haptics.md +1 -1
- package/pipeline/skills/shared/external/localization-reuse-map/references/publish-and-snapshot.md +1 -1
- package/pipeline/skills/shared/external/package-build-audit/SKILL.md +3 -3
- package/pipeline/skills/shared/external/reverse-engineer-own-app/SKILL.md +2 -3
- package/pipeline/skills/shared/external/swift-compile-hotspots/SKILL.md +2 -2
- package/pipeline/skills/shared/external/weatherkit/SKILL.md +5 -6
- package/pipeline/skills/skills-index.md +3 -2
- package/docs/FIGMA_PIPELINE.md +0 -137
- package/pipeline/lib/identifier-scan.mjs +0 -156
- package/pipeline/scripts/_renamed-skills.mjs +0 -164
- package/pipeline/scripts/publish-staged.mjs +0 -324
- package/pipeline/skills/renamed.json +0 -30
- package/pipeline/skills/shared/external/ios-coding-standard/scripts/check_structure.py +0 -765
|
@@ -0,0 +1,998 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file extract.mjs - infrastructure and configuration files as graph facts.
|
|
3
|
+
*
|
|
4
|
+
* Reads Dockerfiles, Compose files, Kubernetes manifests, Helm charts,
|
|
5
|
+
* Kustomize files, Terraform, CI workflows, `.env` files and Gradle build
|
|
6
|
+
* configuration, and reports what each declares and what it points at:
|
|
7
|
+
*
|
|
8
|
+
* nodes infra (a stage, service, resource, job...), env (a variable name),
|
|
9
|
+
* config (a key: a Helm value, a Gradle BuildConfig field)
|
|
10
|
+
* edges sets (infra sets an env variable), uses (infra uses infra, an
|
|
11
|
+
* image, or a file), builds (a service builds from a Dockerfile),
|
|
12
|
+
* runs (a job or stage runs a script), reads (a template reads a
|
|
13
|
+
* value; code reads an env variable or a config key), defines (a file
|
|
14
|
+
* defines an env variable or a key)
|
|
15
|
+
*
|
|
16
|
+
* Only names are recorded. A value is never stored: an `.env` line keeps its key,
|
|
17
|
+
* a Compose `environment` entry keeps its variable name, and nothing that looks
|
|
18
|
+
* like a secret reaches the graph because nothing but names is read.
|
|
19
|
+
*
|
|
20
|
+
* Every file is untrusted input: size, line, nesting and node counts are capped
|
|
21
|
+
* (see yaml-lite.mjs for the YAML limits), and a file that exceeds a cap yields
|
|
22
|
+
* nothing instead of a partial guess.
|
|
23
|
+
*
|
|
24
|
+
* Targets that name a file are written as `path:<repo-relative path>`; the graph
|
|
25
|
+
* builder keeps the edge only when that file exists.
|
|
26
|
+
*
|
|
27
|
+
* @module pipeline/scripts/infra/extract
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { parseYamlDocuments } from "./yaml-lite.mjs";
|
|
31
|
+
|
|
32
|
+
export const MAX_INFRA_BYTES = 512 * 1024;
|
|
33
|
+
export const MAX_ITEMS_PER_FILE = 4000;
|
|
34
|
+
|
|
35
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What kind of configuration file a path is, from its name alone; null when the
|
|
39
|
+
* name does not say (a bare YAML file is sniffed for Kubernetes by the caller).
|
|
40
|
+
*
|
|
41
|
+
* @param {string} path
|
|
42
|
+
* @param {Set<string>} chartDirs - directories that hold a Chart.yaml
|
|
43
|
+
* @returns {string|null}
|
|
44
|
+
*/
|
|
45
|
+
export function classify(path, chartDirs = new Set()) {
|
|
46
|
+
const base = path.split("/").pop();
|
|
47
|
+
const dir = path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : "";
|
|
48
|
+
if (base === "Dockerfile" || base.startsWith("Dockerfile.") || base.endsWith(".dockerfile"))
|
|
49
|
+
return "dockerfile";
|
|
50
|
+
if (base === "Containerfile") return "dockerfile";
|
|
51
|
+
if (/^(docker-)?compose([.-][\w.-]+)?\.ya?ml$/.test(base)) return "compose";
|
|
52
|
+
if (/^\.github\/workflows\/[^/]+\.ya?ml$/.test(path)) return "ci-github";
|
|
53
|
+
if (base === ".gitlab-ci.yml" || base === ".gitlab-ci.yaml") return "ci-gitlab";
|
|
54
|
+
if (base === "Chart.yaml") return "helm-chart";
|
|
55
|
+
if (/^kustomization\.ya?ml$/.test(base)) return "kustomize";
|
|
56
|
+
if (base.endsWith(".tf")) return "terraform";
|
|
57
|
+
if (base === "terragrunt.hcl") return "terragrunt";
|
|
58
|
+
if (base === ".env" || base.startsWith(".env.") || base.endsWith(".env")) return "env-file";
|
|
59
|
+
if (base.endsWith(".gradle") || base.endsWith(".gradle.kts")) return "gradle";
|
|
60
|
+
if (/^(application|bootstrap)(-[\w.]+)?\.(ya?ml|properties)$/.test(base)) return "app-config";
|
|
61
|
+
if (/^appsettings(\.[\w-]+)?\.json$/.test(base)) return "dotnet-config";
|
|
62
|
+
if (base === "package.json" && !path.includes("node_modules/")) return "manifest-npm";
|
|
63
|
+
if (base === "Cargo.toml") return "manifest-cargo";
|
|
64
|
+
if (base === "go.mod") return "manifest-go";
|
|
65
|
+
if (/^requirements([-.][\w.-]+)?\.txt$/.test(base)) return "manifest-pip";
|
|
66
|
+
if (base === "pyproject.toml") return "manifest-pyproject";
|
|
67
|
+
if (base === "Package.swift") return "manifest-swiftpm";
|
|
68
|
+
if (base === "Podfile") return "manifest-pods";
|
|
69
|
+
if (base === "pom.xml") return "manifest-maven";
|
|
70
|
+
if (base.endsWith(".csproj")) return "manifest-nuget";
|
|
71
|
+
for (const chart of chartDirs) {
|
|
72
|
+
const prefix = chart ? `${chart}/` : "";
|
|
73
|
+
if (!path.startsWith(prefix)) continue;
|
|
74
|
+
const rest = path.slice(prefix.length);
|
|
75
|
+
if (/^values[^/]*\.ya?ml$/.test(rest)) return "helm-values";
|
|
76
|
+
if (/^templates\/.+\.(ya?ml|tpl|txt)$/.test(rest)) return "helm-template";
|
|
77
|
+
}
|
|
78
|
+
void dir;
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
83
|
+
const asList = (v) => (Array.isArray(v) ? v : v === null || v === undefined ? [] : [v]);
|
|
84
|
+
|
|
85
|
+
function relTo(dir, target) {
|
|
86
|
+
if (
|
|
87
|
+
typeof target !== "string" ||
|
|
88
|
+
!target ||
|
|
89
|
+
/^[a-z]+:\/\//i.test(target) ||
|
|
90
|
+
target.startsWith("$")
|
|
91
|
+
)
|
|
92
|
+
return null;
|
|
93
|
+
const parts = (target.startsWith("/") ? [] : dir ? dir.split("/") : []).slice();
|
|
94
|
+
for (const seg of target.split("/")) {
|
|
95
|
+
if (seg === "" || seg === ".") continue;
|
|
96
|
+
if (seg === "..") {
|
|
97
|
+
if (!parts.length) return null;
|
|
98
|
+
parts.pop();
|
|
99
|
+
} else parts.push(seg);
|
|
100
|
+
}
|
|
101
|
+
return parts.join("/");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
class Sink {
|
|
105
|
+
constructor(path) {
|
|
106
|
+
this.path = path;
|
|
107
|
+
this.dir = path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : "";
|
|
108
|
+
this.nodes = [];
|
|
109
|
+
this.edges = [];
|
|
110
|
+
this.seen = new Set();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
full() {
|
|
114
|
+
return this.nodes.length + this.edges.length >= MAX_ITEMS_PER_FILE;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
node(id, name, sk, kind = "infra", line) {
|
|
118
|
+
if (this.full() || this.seen.has(id)) return id;
|
|
119
|
+
this.seen.add(id);
|
|
120
|
+
this.nodes.push({ id, kind, name, sk, ...(line ? { line } : {}) });
|
|
121
|
+
return id;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
env(name) {
|
|
125
|
+
if (!ENV_NAME.test(String(name))) return null;
|
|
126
|
+
return this.node(`env:${name}`, String(name), "env", "env");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
edge(from, to, kind, via, confidence = "certain") {
|
|
130
|
+
if (this.full() || !from || !to) return;
|
|
131
|
+
this.edges.push({ from, to, kind, via, confidence });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* A reference to a file. Paths in a Compose file, Terraform or Kustomize are
|
|
136
|
+
* relative to the file that holds them; paths in a CI workflow are relative
|
|
137
|
+
* to the repository root, and a Dockerfile's depend on the build context, so
|
|
138
|
+
* it tries both.
|
|
139
|
+
*/
|
|
140
|
+
file(target, fromRoot = false) {
|
|
141
|
+
const rel = relTo(fromRoot ? "" : this.dir, target);
|
|
142
|
+
return rel ? `path:${rel}` : null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
result() {
|
|
146
|
+
return { nodes: this.nodes, edges: this.edges };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const fileId = (path) => `file:${path}`;
|
|
151
|
+
|
|
152
|
+
function dockerfile(path, text) {
|
|
153
|
+
const s = new Sink(path);
|
|
154
|
+
const lines = text.replace(/\\\r?\n/g, " ").split(/\r?\n/);
|
|
155
|
+
let stage = null;
|
|
156
|
+
let stageIdx = 0;
|
|
157
|
+
const stageIds = new Map();
|
|
158
|
+
lines.forEach((raw, i) => {
|
|
159
|
+
const line = raw.trim();
|
|
160
|
+
if (!line || line.startsWith("#")) return;
|
|
161
|
+
const m = line.match(/^(\w+)\s+(.*)$/);
|
|
162
|
+
if (!m) return;
|
|
163
|
+
const op = m[1].toUpperCase();
|
|
164
|
+
const rest = m[2];
|
|
165
|
+
if (op === "FROM") {
|
|
166
|
+
const f = rest.match(/^(?:--\S+\s+)*(\S+)(?:\s+AS\s+(\S+))?/i);
|
|
167
|
+
if (!f) return;
|
|
168
|
+
const name = f[2] || `stage${stageIdx}`;
|
|
169
|
+
stageIdx++;
|
|
170
|
+
stage = s.node(`infra:docker:${path}#${name}`, name, "docker-stage", "infra", i + 1);
|
|
171
|
+
stageIds.set(name, stage);
|
|
172
|
+
s.edge(fileId(path), stage, "defines", "declaration");
|
|
173
|
+
const parent = stageIds.get(f[1]);
|
|
174
|
+
if (parent) s.edge(stage, parent, "uses", "from-stage");
|
|
175
|
+
else
|
|
176
|
+
s.edge(
|
|
177
|
+
stage,
|
|
178
|
+
s.node(`module:image:${f[1].split(":")[0]}`, f[1].split(":")[0], "image", "module"),
|
|
179
|
+
"uses",
|
|
180
|
+
"from-image",
|
|
181
|
+
);
|
|
182
|
+
} else if (op === "ENV" || op === "ARG") {
|
|
183
|
+
const pairs = rest.includes("=")
|
|
184
|
+
? [...rest.matchAll(/(?:^|\s)([A-Za-z_]\w*)=/g)].map((x) => x[1])
|
|
185
|
+
: [rest.split(/\s+/)[0]];
|
|
186
|
+
for (const name of pairs) {
|
|
187
|
+
const id = s.env(name);
|
|
188
|
+
if (id && stage) s.edge(stage, id, "sets", op === "ARG" ? "arg" : "env");
|
|
189
|
+
else if (id) s.edge(fileId(path), id, "defines", "declaration");
|
|
190
|
+
}
|
|
191
|
+
} else if ((op === "COPY" || op === "ADD") && stage) {
|
|
192
|
+
const parts = rest.split(/\s+/).filter((x) => !x.startsWith("--"));
|
|
193
|
+
const from = rest.match(/--from=(\S+)/);
|
|
194
|
+
if (from && stageIds.has(from[1])) s.edge(stage, stageIds.get(from[1]), "uses", "copy-from");
|
|
195
|
+
for (const src of parts.slice(0, -1)) {
|
|
196
|
+
if (from) continue;
|
|
197
|
+
for (const t of new Set([
|
|
198
|
+
s.file(src.replace(/^\.\//, "")),
|
|
199
|
+
s.file(src.replace(/^\.\//, ""), true),
|
|
200
|
+
])) {
|
|
201
|
+
if (t) s.edge(stage, t, "uses", "copy", "high");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
} else if ((op === "RUN" || op === "CMD" || op === "ENTRYPOINT") && stage) {
|
|
205
|
+
for (const sc of rest.matchAll(
|
|
206
|
+
/(?:^|[\s"[,;&|])(\.{0,2}\/?[\w./-]+\.(?:sh|py|js|mjs|rb|pl))(?=$|[\s"\],;&|])/g,
|
|
207
|
+
)) {
|
|
208
|
+
for (const t of new Set([s.file(sc[1]), s.file(sc[1], true)])) {
|
|
209
|
+
if (t) s.edge(stage, t, "runs", "script", "high");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
return s.result();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function envKeyList(value) {
|
|
218
|
+
if (isObj(value)) return Object.keys(value);
|
|
219
|
+
return asList(value)
|
|
220
|
+
.map((x) => (typeof x === "string" ? x.split("=")[0].trim() : isObj(x) ? x.name : null))
|
|
221
|
+
.filter(Boolean);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function interpolations(text) {
|
|
225
|
+
return [...text.matchAll(/\$\{([A-Za-z_]\w*)(?:[:?+-][^}]*)?\}/g)].map((m) => m[1]);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function compose(path, text) {
|
|
229
|
+
const s = new Sink(path);
|
|
230
|
+
const doc = parseYamlDocuments(text).find(isObj);
|
|
231
|
+
const services = doc && isObj(doc.services) ? doc.services : null;
|
|
232
|
+
if (!services) return s.result();
|
|
233
|
+
const ids = new Map();
|
|
234
|
+
for (const name of Object.keys(services)) {
|
|
235
|
+
ids.set(name, s.node(`infra:compose:${path}#${name}`, name, "compose-service"));
|
|
236
|
+
s.edge(fileId(path), ids.get(name), "defines", "declaration");
|
|
237
|
+
}
|
|
238
|
+
for (const [name, svc] of Object.entries(services)) {
|
|
239
|
+
if (!isObj(svc)) continue;
|
|
240
|
+
const id = ids.get(name);
|
|
241
|
+
if (typeof svc.image === "string") {
|
|
242
|
+
const img = svc.image.split(":")[0].replace(/\$\{[^}]*\}/g, "");
|
|
243
|
+
if (img) s.edge(id, s.node(`module:image:${img}`, img, "image", "module"), "uses", "image");
|
|
244
|
+
}
|
|
245
|
+
const build = svc.build;
|
|
246
|
+
if (build !== undefined && build !== null) {
|
|
247
|
+
const ctx = typeof build === "string" ? build : isObj(build) ? build.context || "." : null;
|
|
248
|
+
const df = isObj(build) && build.dockerfile ? build.dockerfile : "Dockerfile";
|
|
249
|
+
if (ctx !== null) {
|
|
250
|
+
const t = s.file(`${String(ctx).replace(/\/$/, "")}/${df}`);
|
|
251
|
+
if (t) s.edge(id, t, "builds", "build-context", "high");
|
|
252
|
+
}
|
|
253
|
+
if (isObj(build) && isObj(build.args)) {
|
|
254
|
+
for (const k of Object.keys(build.args)) {
|
|
255
|
+
const e = s.env(k);
|
|
256
|
+
if (e) s.edge(id, e, "sets", "build-arg");
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
for (const k of envKeyList(svc.environment)) {
|
|
261
|
+
const e = s.env(k);
|
|
262
|
+
if (e) s.edge(id, e, "sets", "environment");
|
|
263
|
+
}
|
|
264
|
+
for (const f of asList(svc.env_file)) {
|
|
265
|
+
const t = s.file(typeof f === "string" ? f : f?.path);
|
|
266
|
+
if (t) s.edge(id, t, "uses", "env-file", "high");
|
|
267
|
+
}
|
|
268
|
+
for (const dep of isObj(svc.depends_on)
|
|
269
|
+
? Object.keys(svc.depends_on)
|
|
270
|
+
: asList(svc.depends_on)) {
|
|
271
|
+
if (ids.has(dep)) s.edge(id, ids.get(dep), "uses", "depends-on");
|
|
272
|
+
}
|
|
273
|
+
for (const v of [
|
|
274
|
+
...asList(svc.environment),
|
|
275
|
+
...Object.values(isObj(svc.environment) ? svc.environment : {}),
|
|
276
|
+
]) {
|
|
277
|
+
if (typeof v !== "string") continue;
|
|
278
|
+
for (const name of interpolations(v)) {
|
|
279
|
+
const e = s.env(name);
|
|
280
|
+
if (e) s.edge(id, e, "reads", "interpolation");
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return s.result();
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function k8s(path, text) {
|
|
288
|
+
const s = new Sink(path);
|
|
289
|
+
const byName = new Map();
|
|
290
|
+
const docs = parseYamlDocuments(text).filter(
|
|
291
|
+
(d) => isObj(d) && typeof d.kind === "string" && isObj(d.metadata) && d.metadata.name,
|
|
292
|
+
);
|
|
293
|
+
for (const d of docs) {
|
|
294
|
+
const nm = String(d.metadata.name);
|
|
295
|
+
const id = s.node(`infra:k8s:${path}#${d.kind}/${nm}`, `${d.kind}/${nm}`, `k8s-${d.kind}`);
|
|
296
|
+
byName.set(`${d.kind}/${nm}`, id);
|
|
297
|
+
s.edge(fileId(path), id, "defines", "declaration");
|
|
298
|
+
}
|
|
299
|
+
for (const d of docs) {
|
|
300
|
+
const id = byName.get(`${d.kind}/${d.metadata.name}`);
|
|
301
|
+
const pod =
|
|
302
|
+
d.spec?.template?.spec ||
|
|
303
|
+
d.spec?.jobTemplate?.spec?.template?.spec ||
|
|
304
|
+
(d.kind === "Pod" ? d.spec : null);
|
|
305
|
+
if (!isObj(pod)) continue;
|
|
306
|
+
for (const c of [...asList(pod.containers), ...asList(pod.initContainers)]) {
|
|
307
|
+
if (!isObj(c)) continue;
|
|
308
|
+
if (typeof c.image === "string") {
|
|
309
|
+
const img = c.image.split(":")[0].split("@")[0];
|
|
310
|
+
s.edge(id, s.node(`module:image:${img}`, img, "image", "module"), "uses", "image");
|
|
311
|
+
}
|
|
312
|
+
for (const e of asList(c.env)) {
|
|
313
|
+
if (!isObj(e) || !e.name) continue;
|
|
314
|
+
const envId = s.env(e.name);
|
|
315
|
+
if (!envId) continue;
|
|
316
|
+
s.edge(id, envId, "sets", "env");
|
|
317
|
+
const ref = e.valueFrom?.configMapKeyRef || e.valueFrom?.secretKeyRef;
|
|
318
|
+
if (ref?.name) {
|
|
319
|
+
const kind = e.valueFrom.configMapKeyRef ? "ConfigMap" : "Secret";
|
|
320
|
+
const target = byName.get(`${kind}/${ref.name}`);
|
|
321
|
+
if (target) s.edge(id, target, "uses", "env-from-ref");
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
for (const ef of asList(c.envFrom)) {
|
|
325
|
+
const ref = ef?.configMapRef || ef?.secretRef;
|
|
326
|
+
if (!ref?.name) continue;
|
|
327
|
+
const kind = ef.configMapRef ? "ConfigMap" : "Secret";
|
|
328
|
+
const target = byName.get(`${kind}/${ref.name}`);
|
|
329
|
+
if (target) s.edge(id, target, "uses", "env-from");
|
|
330
|
+
const cm = docs.find((x) => x.kind === kind && x.metadata.name === ref.name);
|
|
331
|
+
if (cm && isObj(cm.data)) {
|
|
332
|
+
for (const key of Object.keys(cm.data)) {
|
|
333
|
+
const e = s.env(key);
|
|
334
|
+
if (e) s.edge(id, e, "sets", "env-from", "medium");
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return s.result();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function helmChart(path, text) {
|
|
344
|
+
const s = new Sink(path);
|
|
345
|
+
const doc = parseYamlDocuments(text).find(isObj);
|
|
346
|
+
if (!doc || typeof doc.name !== "string") return s.result();
|
|
347
|
+
const id = s.node(`infra:helm:${path}#${doc.name}`, doc.name, "helm-chart");
|
|
348
|
+
s.edge(fileId(path), id, "defines", "declaration");
|
|
349
|
+
for (const dep of asList(doc.dependencies)) {
|
|
350
|
+
if (isObj(dep) && dep.name) {
|
|
351
|
+
s.edge(
|
|
352
|
+
id,
|
|
353
|
+
s.node(`module:chart:${dep.name}`, String(dep.name), "chart", "module"),
|
|
354
|
+
"uses",
|
|
355
|
+
"chart-dependency",
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return s.result();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function flatten(value, prefix, out, depth = 0) {
|
|
363
|
+
if (depth > 12 || out.length > 2000) return;
|
|
364
|
+
if (isObj(value)) {
|
|
365
|
+
for (const [k, v] of Object.entries(value))
|
|
366
|
+
flatten(v, prefix ? `${prefix}.${k}` : k, out, depth + 1);
|
|
367
|
+
} else if (prefix) out.push(prefix);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function helmValues(path, text, chartName) {
|
|
371
|
+
const s = new Sink(path);
|
|
372
|
+
const doc = parseYamlDocuments(text).find(isObj);
|
|
373
|
+
if (!doc) return s.result();
|
|
374
|
+
const keys = [];
|
|
375
|
+
flatten(doc, "", keys);
|
|
376
|
+
for (const k of keys) {
|
|
377
|
+
const id = s.node(`config:helm:${chartName}.${k}`, k, "helm-value", "config");
|
|
378
|
+
s.edge(fileId(path), id, "defines", "declaration");
|
|
379
|
+
}
|
|
380
|
+
return s.result();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function helmTemplate(path, text, chartName) {
|
|
384
|
+
const s = new Sink(path);
|
|
385
|
+
const seen = new Set();
|
|
386
|
+
for (const m of text.matchAll(/\.Values\.([A-Za-z_][\w.]*)/g)) {
|
|
387
|
+
const key = m[1].replace(/\.$/, "");
|
|
388
|
+
if (seen.has(key)) continue;
|
|
389
|
+
seen.add(key);
|
|
390
|
+
const id = `config:helm:${chartName}.${key}`;
|
|
391
|
+
s.edge(fileId(path), id, "reads", "helm-values", "high");
|
|
392
|
+
}
|
|
393
|
+
for (const m of text.matchAll(/^[ \t]*(?:-[ \t]*)?name:[ \t]*([A-Za-z_]\w*)[ \t]*$/gm)) {
|
|
394
|
+
if (/^[A-Z][A-Z0-9_]+$/.test(m[1])) {
|
|
395
|
+
const e = s.env(m[1]);
|
|
396
|
+
if (e) s.edge(fileId(path), e, "sets", "template-env", "medium");
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return s.result();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function kustomize(path, text) {
|
|
403
|
+
const s = new Sink(path);
|
|
404
|
+
const doc = parseYamlDocuments(text).find(isObj);
|
|
405
|
+
if (!doc) return s.result();
|
|
406
|
+
const id = s.node(`infra:kustomize:${path}#kustomization`, "kustomization", "kustomization");
|
|
407
|
+
s.edge(fileId(path), id, "defines", "declaration");
|
|
408
|
+
for (const key of ["resources", "bases", "components", "patches", "patchesStrategicMerge"]) {
|
|
409
|
+
for (const r of asList(doc[key])) {
|
|
410
|
+
const target = typeof r === "string" ? r : isObj(r) ? r.path : null;
|
|
411
|
+
const t = target ? s.file(target) : null;
|
|
412
|
+
if (t) s.edge(id, t, "uses", key, "high");
|
|
413
|
+
else if (typeof target === "string" && target && !/^[a-z]+:\/\//.test(target)) {
|
|
414
|
+
const dirT = s.file(`${target}/kustomization.yaml`);
|
|
415
|
+
if (dirT) s.edge(id, dirT, "uses", key, "medium");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
for (const gen of [...asList(doc.configMapGenerator), ...asList(doc.secretGenerator)]) {
|
|
420
|
+
for (const lit of asList(gen?.literals)) {
|
|
421
|
+
const e = s.env(String(lit).split("=")[0]);
|
|
422
|
+
if (e) s.edge(id, e, "sets", "generator", "medium");
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return s.result();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const MAX_HCL_SCAN = 4_000_000;
|
|
429
|
+
|
|
430
|
+
/** `text` with every closed `/* ... *\/` block replaced by a space; an unclosed one runs to the end. */
|
|
431
|
+
function withoutBlockComments(text) {
|
|
432
|
+
let at = text.indexOf("/*");
|
|
433
|
+
if (at === -1) return text;
|
|
434
|
+
let out = "";
|
|
435
|
+
let from = 0;
|
|
436
|
+
while (at !== -1) {
|
|
437
|
+
out += text.slice(from, at);
|
|
438
|
+
const end = text.indexOf("*/", at + 2);
|
|
439
|
+
if (end === -1) return `${out} `;
|
|
440
|
+
out += " ";
|
|
441
|
+
from = end + 2;
|
|
442
|
+
at = text.indexOf("/*", from);
|
|
443
|
+
}
|
|
444
|
+
return out + text.slice(from);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function terraform(path, text) {
|
|
448
|
+
const s = new Sink(path);
|
|
449
|
+
const stripped = withoutBlockComments(text).replace(/(^|\s)(#|\/\/).*$/gm, "$1");
|
|
450
|
+
const declared = new Map();
|
|
451
|
+
const blocks = [];
|
|
452
|
+
const re =
|
|
453
|
+
/^[ \t]*(resource|data|module|variable|output|provider|locals)\b(?:[ \t]+"([^"\n]{1,200})")?(?:[ \t]+"([^"\n]{1,200})")?[ \t]*\{/gm;
|
|
454
|
+
let m;
|
|
455
|
+
let scanned = 0;
|
|
456
|
+
let lineAt = 1;
|
|
457
|
+
let lineIndex = 0;
|
|
458
|
+
while ((m = re.exec(stripped))) {
|
|
459
|
+
const kind = m[1];
|
|
460
|
+
let depth = 1;
|
|
461
|
+
let i = m.index + m[0].length;
|
|
462
|
+
const start = i;
|
|
463
|
+
while (i < stripped.length && depth > 0 && i - start < 200_000) {
|
|
464
|
+
const c = stripped[i];
|
|
465
|
+
if (c === "{") depth++;
|
|
466
|
+
else if (c === "}") depth--;
|
|
467
|
+
i++;
|
|
468
|
+
}
|
|
469
|
+
scanned += i - start;
|
|
470
|
+
if (scanned > MAX_HCL_SCAN) break;
|
|
471
|
+
re.lastIndex = Math.max(re.lastIndex, i);
|
|
472
|
+
const body = stripped.slice(start, i - 1);
|
|
473
|
+
for (let at = stripped.indexOf("\n", lineIndex); at !== -1 && at < m.index;) {
|
|
474
|
+
lineAt++;
|
|
475
|
+
lineIndex = at + 1;
|
|
476
|
+
at = stripped.indexOf("\n", lineIndex);
|
|
477
|
+
}
|
|
478
|
+
const line = lineAt;
|
|
479
|
+
let name = null;
|
|
480
|
+
let sk = `tf-${kind}`;
|
|
481
|
+
let ref = null;
|
|
482
|
+
if (kind === "resource") {
|
|
483
|
+
name = `${m[2]}.${m[3]}`;
|
|
484
|
+
ref = name;
|
|
485
|
+
} else if (kind === "data") {
|
|
486
|
+
name = `data.${m[2]}.${m[3]}`;
|
|
487
|
+
ref = name;
|
|
488
|
+
} else if (kind === "module") {
|
|
489
|
+
name = `module.${m[2]}`;
|
|
490
|
+
ref = name;
|
|
491
|
+
} else if (kind === "variable") {
|
|
492
|
+
name = `var.${m[2]}`;
|
|
493
|
+
ref = name;
|
|
494
|
+
} else if (kind === "output") {
|
|
495
|
+
name = `output.${m[2]}`;
|
|
496
|
+
} else if (kind === "provider") {
|
|
497
|
+
name = `provider.${m[2]}`;
|
|
498
|
+
sk = "tf-provider";
|
|
499
|
+
}
|
|
500
|
+
if (!name) continue;
|
|
501
|
+
const id = s.node(`infra:tf:${path}#${name}`, name, sk, "infra", line);
|
|
502
|
+
s.edge(fileId(path), id, "defines", "declaration");
|
|
503
|
+
if (ref) declared.set(ref, id);
|
|
504
|
+
blocks.push({ id, kind, body, name });
|
|
505
|
+
if (blocks.length > 1500) break;
|
|
506
|
+
}
|
|
507
|
+
const dirScope = path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : "";
|
|
508
|
+
void dirScope;
|
|
509
|
+
for (const b of blocks) {
|
|
510
|
+
for (const r of b.body.matchAll(
|
|
511
|
+
/\b(var\.[A-Za-z_]\w*|module\.[A-Za-z_][\w-]*|data\.[A-Za-z_]\w*\.[A-Za-z_][\w-]*|[a-z][a-z0-9]*_[a-z0-9_]+\.[A-Za-z_][\w-]*)/g,
|
|
512
|
+
)) {
|
|
513
|
+
const target = declared.get(r[1]);
|
|
514
|
+
if (target && target !== b.id) s.edge(b.id, target, "uses", "reference", "high");
|
|
515
|
+
}
|
|
516
|
+
if (b.kind === "module") {
|
|
517
|
+
const src = b.body.match(/^[ \t]*source[ \t]*=[ \t]*"([^"\n]{1,300})"/m);
|
|
518
|
+
if (src && /^\.{1,2}\//.test(src[1])) {
|
|
519
|
+
const t = s.file(`${src[1]}/main.tf`);
|
|
520
|
+
if (t) s.edge(b.id, t, "uses", "module-source", "medium");
|
|
521
|
+
} else if (src) {
|
|
522
|
+
s.edge(
|
|
523
|
+
b.id,
|
|
524
|
+
s.node(`module:tf:${src[1]}`, src[1], "tf-module-source", "module"),
|
|
525
|
+
"uses",
|
|
526
|
+
"module-source",
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (b.kind === "variable") {
|
|
531
|
+
const e = s.env(`TF_VAR_${b.name.slice(4)}`);
|
|
532
|
+
if (e) s.edge(b.id, e, "reads", "tf-var-env", "medium");
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return s.result();
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function terragrunt(path, text) {
|
|
539
|
+
const s = new Sink(path);
|
|
540
|
+
const id = s.node(`infra:terragrunt:${path}#config`, "terragrunt", "terragrunt");
|
|
541
|
+
s.edge(fileId(path), id, "defines", "declaration");
|
|
542
|
+
for (const m of text.matchAll(/(?:config_path|path)[ \t]{0,8}=[ \t]{0,8}"([^"\n]{1,300})"/g)) {
|
|
543
|
+
const t = s.file(`${m[1]}/terragrunt.hcl`);
|
|
544
|
+
if (t) s.edge(id, t, "uses", "dependency", "medium");
|
|
545
|
+
}
|
|
546
|
+
for (const m of text.matchAll(/source[ \t]{0,8}=[ \t]{0,8}"(\.{1,2}\/[^"\n]{1,300})"/g)) {
|
|
547
|
+
const t = s.file(`${m[1]}/main.tf`);
|
|
548
|
+
if (t) s.edge(id, t, "uses", "module-source", "medium");
|
|
549
|
+
}
|
|
550
|
+
return s.result();
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function scriptsIn(text) {
|
|
554
|
+
return [
|
|
555
|
+
...text.matchAll(
|
|
556
|
+
/(?:^|[\s"'`(;&|])(\.{0,2}\/?[A-Za-z0-9_][\w./-]*\.(?:sh|py|js|mjs|cjs|rb|ps1|bash))(?=$|[\s"'`);&|])/gm,
|
|
557
|
+
),
|
|
558
|
+
].map((m) => m[1]);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function githubWorkflow(path, text) {
|
|
562
|
+
const s = new Sink(path);
|
|
563
|
+
const doc = parseYamlDocuments(text).find(isObj);
|
|
564
|
+
if (!doc) return s.result();
|
|
565
|
+
const wfName = typeof doc.name === "string" ? doc.name : path.split("/").pop();
|
|
566
|
+
const wf = s.node(`infra:ci:${path}#workflow`, wfName, "ci-workflow");
|
|
567
|
+
s.edge(fileId(path), wf, "defines", "declaration");
|
|
568
|
+
for (const k of envKeyList(doc.env)) {
|
|
569
|
+
const e = s.env(k);
|
|
570
|
+
if (e) s.edge(wf, e, "sets", "env");
|
|
571
|
+
}
|
|
572
|
+
const jobs = isObj(doc.jobs) ? doc.jobs : {};
|
|
573
|
+
const jobIds = new Map();
|
|
574
|
+
for (const name of Object.keys(jobs))
|
|
575
|
+
jobIds.set(name, s.node(`infra:ci:${path}#job:${name}`, name, "ci-job"));
|
|
576
|
+
for (const [name, job] of Object.entries(jobs)) {
|
|
577
|
+
if (!isObj(job)) continue;
|
|
578
|
+
const id = jobIds.get(name);
|
|
579
|
+
s.edge(wf, id, "defines", "declaration");
|
|
580
|
+
for (const need of asList(job.needs))
|
|
581
|
+
if (jobIds.has(need)) s.edge(id, jobIds.get(need), "uses", "needs");
|
|
582
|
+
for (const k of envKeyList(job.env)) {
|
|
583
|
+
const e = s.env(k);
|
|
584
|
+
if (e) s.edge(id, e, "sets", "env");
|
|
585
|
+
}
|
|
586
|
+
if (typeof job.uses === "string") reusable(s, id, job.uses);
|
|
587
|
+
for (const step of asList(job.steps)) {
|
|
588
|
+
if (!isObj(step)) continue;
|
|
589
|
+
for (const k of envKeyList(step.env)) {
|
|
590
|
+
const e = s.env(k);
|
|
591
|
+
if (e) s.edge(id, e, "sets", "env");
|
|
592
|
+
}
|
|
593
|
+
if (typeof step.uses === "string") reusable(s, id, step.uses);
|
|
594
|
+
if (typeof step.run === "string") {
|
|
595
|
+
for (const sc of scriptsIn(step.run)) {
|
|
596
|
+
const t = s.file(sc.replace(/^\.\//, ""), true);
|
|
597
|
+
if (t) s.edge(id, t, "runs", "script", "high");
|
|
598
|
+
}
|
|
599
|
+
for (const r of step.run.matchAll(
|
|
600
|
+
/\$\{\{\s*env\.([A-Za-z_]\w*)\s*\}\}|\$\{?([A-Z][A-Z0-9_]{2,})\}?/g,
|
|
601
|
+
)) {
|
|
602
|
+
const e = s.env(r[1] || r[2]);
|
|
603
|
+
if (e) s.edge(id, e, "reads", "ci-run", r[1] ? "high" : "medium");
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
if (isObj(step.with)) {
|
|
607
|
+
for (const v of Object.values(step.with)) {
|
|
608
|
+
if (typeof v !== "string") continue;
|
|
609
|
+
for (const r of v.matchAll(/\$\{\{\s*env\.([A-Za-z_]\w*)\s*\}\}/g)) {
|
|
610
|
+
const e = s.env(r[1]);
|
|
611
|
+
if (e) s.edge(id, e, "reads", "ci-with", "high");
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return s.result();
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function reusable(s, from, uses) {
|
|
621
|
+
if (uses.startsWith("./")) {
|
|
622
|
+
const t = s.file(uses.replace(/^\.\//, "").replace(/^\/+/, ""), true);
|
|
623
|
+
if (t) s.edge(from, t.startsWith("path:") ? t : null, "uses", "local-action", "high");
|
|
624
|
+
const act = s.file(`${uses.replace(/^\.\//, "")}/action.yml`, true);
|
|
625
|
+
if (act) s.edge(from, act, "uses", "local-action", "medium");
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
if (uses.startsWith("docker://")) return;
|
|
629
|
+
const name = uses.split("@")[0];
|
|
630
|
+
s.edge(from, s.node(`module:action:${name}`, name, "action", "module"), "uses", "action");
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function gitlabCi(path, text) {
|
|
634
|
+
const s = new Sink(path);
|
|
635
|
+
const doc = parseYamlDocuments(text).find(isObj);
|
|
636
|
+
if (!doc) return s.result();
|
|
637
|
+
const wf = s.node(`infra:ci:${path}#pipeline`, "pipeline", "ci-workflow");
|
|
638
|
+
s.edge(fileId(path), wf, "defines", "declaration");
|
|
639
|
+
for (const k of envKeyList(doc.variables)) {
|
|
640
|
+
const e = s.env(k);
|
|
641
|
+
if (e) s.edge(wf, e, "sets", "variables");
|
|
642
|
+
}
|
|
643
|
+
for (const inc of asList(doc.include)) {
|
|
644
|
+
const target = typeof inc === "string" ? inc : isObj(inc) ? inc.local : null;
|
|
645
|
+
const t = target ? s.file(String(target).replace(/^\//, ""), true) : null;
|
|
646
|
+
if (t) s.edge(wf, t, "uses", "include", "high");
|
|
647
|
+
}
|
|
648
|
+
const reserved = new Set([
|
|
649
|
+
"stages",
|
|
650
|
+
"variables",
|
|
651
|
+
"include",
|
|
652
|
+
"default",
|
|
653
|
+
"workflow",
|
|
654
|
+
"image",
|
|
655
|
+
"services",
|
|
656
|
+
"cache",
|
|
657
|
+
"before_script",
|
|
658
|
+
"after_script",
|
|
659
|
+
]);
|
|
660
|
+
for (const [name, job] of Object.entries(doc)) {
|
|
661
|
+
if (reserved.has(name) || name.startsWith(".") || !isObj(job)) continue;
|
|
662
|
+
const id = s.node(`infra:ci:${path}#job:${name}`, name, "ci-job");
|
|
663
|
+
s.edge(wf, id, "defines", "declaration");
|
|
664
|
+
for (const k of envKeyList(job.variables)) {
|
|
665
|
+
const e = s.env(k);
|
|
666
|
+
if (e) s.edge(id, e, "sets", "variables");
|
|
667
|
+
}
|
|
668
|
+
for (const line of [...asList(job.script), ...asList(job.before_script)]) {
|
|
669
|
+
if (typeof line !== "string") continue;
|
|
670
|
+
for (const sc of scriptsIn(line)) {
|
|
671
|
+
const t = s.file(sc.replace(/^\.\//, ""), true);
|
|
672
|
+
if (t) s.edge(id, t, "runs", "script", "high");
|
|
673
|
+
}
|
|
674
|
+
for (const r of line.matchAll(/\$\{?([A-Z][A-Z0-9_]{2,})\}?/g)) {
|
|
675
|
+
const e = s.env(r[1]);
|
|
676
|
+
if (e) s.edge(id, e, "reads", "ci-script", "medium");
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return s.result();
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function envFile(path, text) {
|
|
684
|
+
const s = new Sink(path);
|
|
685
|
+
for (const line of text.split(/\r?\n/)) {
|
|
686
|
+
const m = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/);
|
|
687
|
+
if (!m) continue;
|
|
688
|
+
const e = s.env(m[1]);
|
|
689
|
+
if (e) s.edge(fileId(path), e, "defines", "declaration");
|
|
690
|
+
}
|
|
691
|
+
return s.result();
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function flattenKeys(value, prefix, sep, out, depth = 0) {
|
|
695
|
+
if (depth > 12 || out.length > 2000) return;
|
|
696
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
697
|
+
for (const [k, v] of Object.entries(value)) {
|
|
698
|
+
flattenKeys(v, prefix ? `${prefix}${sep}${k}` : k, sep, out, depth + 1);
|
|
699
|
+
}
|
|
700
|
+
} else if (prefix) out.push(prefix);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function appConfig(path, text, kind) {
|
|
704
|
+
const s = new Sink(path);
|
|
705
|
+
const keys = [];
|
|
706
|
+
if (kind === "dotnet-config") {
|
|
707
|
+
try {
|
|
708
|
+
flattenKeys(JSON.parse(text), "", ":", keys);
|
|
709
|
+
} catch {
|
|
710
|
+
return s.result();
|
|
711
|
+
}
|
|
712
|
+
} else if (path.endsWith(".properties")) {
|
|
713
|
+
for (const line of text.split(/\r?\n/)) {
|
|
714
|
+
const m = line.match(/^\s*([A-Za-z_][\w.-]*)\s*[=:]/);
|
|
715
|
+
if (m) keys.push(m[1]);
|
|
716
|
+
}
|
|
717
|
+
} else {
|
|
718
|
+
for (const doc of parseYamlDocuments(text)) flattenKeys(doc, "", ".", keys);
|
|
719
|
+
}
|
|
720
|
+
for (const key of keys) {
|
|
721
|
+
const id = s.node(`config:app:${path}#${key}`, key, "app-config-key", "config");
|
|
722
|
+
s.edge(fileId(path), id, "defines", "declaration");
|
|
723
|
+
}
|
|
724
|
+
return s.result();
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
function dependencies(s, path, eco, names, via = "dependency") {
|
|
728
|
+
const seen = new Set();
|
|
729
|
+
for (const name of names) {
|
|
730
|
+
if (!name || seen.has(name) || seen.size >= 500) continue;
|
|
731
|
+
seen.add(name);
|
|
732
|
+
const id = s.node(`module:dep:${eco}:${name}`, name, "dependency", "module");
|
|
733
|
+
s.edge(fileId(path), id, "uses", via);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
const PACKAGE_CALL_MAX = 600;
|
|
738
|
+
|
|
739
|
+
/** Dependency names a Package.swift declares with `.package(url: "...", ...)`. */
|
|
740
|
+
function swiftPackageNames(text) {
|
|
741
|
+
const names = [];
|
|
742
|
+
let at = text.indexOf(".package(");
|
|
743
|
+
while (at !== -1 && names.length < 500) {
|
|
744
|
+
const call = text.slice(at, at + PACKAGE_CALL_MAX);
|
|
745
|
+
const m = call.match(/url:[ \t\n]{0,8}"([^"\n]{1,300})"/);
|
|
746
|
+
if (m) {
|
|
747
|
+
const last = m[1]
|
|
748
|
+
.replace(/\.git$/, "")
|
|
749
|
+
.split("/")
|
|
750
|
+
.pop();
|
|
751
|
+
if (last) names.push(last);
|
|
752
|
+
}
|
|
753
|
+
at = text.indexOf(".package(", at + 9);
|
|
754
|
+
}
|
|
755
|
+
return names;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
function manifest(kind, path, text) {
|
|
759
|
+
const s = new Sink(path);
|
|
760
|
+
const eco = kind.slice("manifest-".length);
|
|
761
|
+
if (kind === "manifest-npm") {
|
|
762
|
+
let doc;
|
|
763
|
+
try {
|
|
764
|
+
doc = JSON.parse(text);
|
|
765
|
+
} catch {
|
|
766
|
+
return s.result();
|
|
767
|
+
}
|
|
768
|
+
if (!isObj(doc)) return s.result();
|
|
769
|
+
dependencies(s, path, eco, Object.keys(doc.dependencies || {}));
|
|
770
|
+
dependencies(s, path, eco, Object.keys(doc.peerDependencies || {}), "peer-dependency");
|
|
771
|
+
dependencies(s, path, eco, Object.keys(doc.optionalDependencies || {}), "optional-dependency");
|
|
772
|
+
dependencies(s, path, eco, Object.keys(doc.devDependencies || {}), "dev-dependency");
|
|
773
|
+
} else if (kind === "manifest-cargo") {
|
|
774
|
+
const prod = [];
|
|
775
|
+
const dev = [];
|
|
776
|
+
let section = "";
|
|
777
|
+
for (const line of text.split(/\r?\n/)) {
|
|
778
|
+
const h = line.match(/^\s*\[([^\]]+)\]/);
|
|
779
|
+
if (h) {
|
|
780
|
+
section = h[1].trim();
|
|
781
|
+
const dotted = section.match(/^(dev-|build-)?dependencies\.([\w-]+)$/);
|
|
782
|
+
if (dotted) (dotted[1] === "dev-" ? dev : prod).push(dotted[2]);
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
const kv = line.match(/^\s*([A-Za-z0-9_-]+)\s*=/);
|
|
786
|
+
if (!kv) continue;
|
|
787
|
+
if (section === "dependencies" || section === "build-dependencies") prod.push(kv[1]);
|
|
788
|
+
else if (section === "dev-dependencies") dev.push(kv[1]);
|
|
789
|
+
}
|
|
790
|
+
dependencies(s, path, eco, prod);
|
|
791
|
+
dependencies(s, path, eco, dev, "dev-dependency");
|
|
792
|
+
} else if (kind === "manifest-go") {
|
|
793
|
+
const names = [];
|
|
794
|
+
let inBlock = false;
|
|
795
|
+
for (const line of text.split(/\r?\n/)) {
|
|
796
|
+
if (/^\s*require\s*\(/.test(line)) inBlock = true;
|
|
797
|
+
else if (inBlock && /^\s*\)/.test(line)) inBlock = false;
|
|
798
|
+
else {
|
|
799
|
+
const m = inBlock
|
|
800
|
+
? line.match(/^\s*(\S+)\s+v\S+/)
|
|
801
|
+
: line.match(/^\s*require\s+(\S+)\s+v\S+/);
|
|
802
|
+
if (m) names.push(m[1]);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
dependencies(s, path, eco, names);
|
|
806
|
+
} else if (kind === "manifest-pip") {
|
|
807
|
+
const names = text
|
|
808
|
+
.split(/\r?\n/)
|
|
809
|
+
.map((l) => l.trim())
|
|
810
|
+
.filter((l) => l && !l.startsWith("#") && !l.startsWith("-"))
|
|
811
|
+
.map((l) => l.match(/^([A-Za-z0-9][\w.-]*)/)?.[1]);
|
|
812
|
+
dependencies(s, path, eco, names);
|
|
813
|
+
} else if (kind === "manifest-pyproject") {
|
|
814
|
+
const names = [];
|
|
815
|
+
for (const m of text.matchAll(/^[ \t]*dependencies[ \t]*=[ \t]*\[([^\]]{0,4000})\]/gm)) {
|
|
816
|
+
for (const q of m[1].matchAll(/["']([A-Za-z0-9][\w.-]*)/g)) names.push(q[1]);
|
|
817
|
+
}
|
|
818
|
+
const poetry = text.match(/\[tool\.poetry\.dependencies\]([\s\S]*?)(?:\n\[|$)/);
|
|
819
|
+
if (poetry)
|
|
820
|
+
for (const m of poetry[1].matchAll(/^[ \t]*([A-Za-z0-9][\w.-]*)[ \t]*=/gm)) names.push(m[1]);
|
|
821
|
+
dependencies(
|
|
822
|
+
s,
|
|
823
|
+
path,
|
|
824
|
+
eco,
|
|
825
|
+
names.filter((n) => n !== "python"),
|
|
826
|
+
);
|
|
827
|
+
} else if (kind === "manifest-swiftpm") {
|
|
828
|
+
dependencies(s, path, eco, swiftPackageNames(text));
|
|
829
|
+
} else if (kind === "manifest-pods") {
|
|
830
|
+
dependencies(
|
|
831
|
+
s,
|
|
832
|
+
path,
|
|
833
|
+
eco,
|
|
834
|
+
[...text.matchAll(/^[ \t]*pod[ \t]+['"]([^'"/\n]{1,120})/gm)].map((m) => m[1]),
|
|
835
|
+
);
|
|
836
|
+
} else if (kind === "manifest-maven") {
|
|
837
|
+
const deps = [...text.matchAll(/<dependency>([\s\S]{0,2000}?)<\/dependency>/g)].map(
|
|
838
|
+
(m) => m[1].match(/<artifactId>[ \t\n]{0,8}([^<\s]{1,200})/)?.[1],
|
|
839
|
+
);
|
|
840
|
+
dependencies(s, path, eco, deps);
|
|
841
|
+
} else if (kind === "manifest-nuget") {
|
|
842
|
+
dependencies(
|
|
843
|
+
s,
|
|
844
|
+
path,
|
|
845
|
+
eco,
|
|
846
|
+
[...text.matchAll(/<PackageReference[ \t\n]{1,8}Include="([^"\n]{1,200})"/g)].map(
|
|
847
|
+
(m) => m[1],
|
|
848
|
+
),
|
|
849
|
+
);
|
|
850
|
+
}
|
|
851
|
+
return s.result();
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
function gradle(path, text) {
|
|
855
|
+
const s = new Sink(path);
|
|
856
|
+
dependencies(
|
|
857
|
+
s,
|
|
858
|
+
path,
|
|
859
|
+
"gradle",
|
|
860
|
+
[
|
|
861
|
+
...text.matchAll(
|
|
862
|
+
/\b(?:implementation|api|compileOnly|runtimeOnly|kapt|ksp|annotationProcessor)[ \t]{0,8}\(?[ \t]{0,8}["']([\w.-]{1,200}):([\w.-]{1,200})(?::[^"'\n]{0,200})?["']/g,
|
|
863
|
+
),
|
|
864
|
+
].map((m) => m[2]),
|
|
865
|
+
);
|
|
866
|
+
for (const m of text.matchAll(
|
|
867
|
+
/buildConfigField[ \t]{0,8}\(?[ \t]{0,8}["']?\w{1,60}["']?[ \t]{0,8},[ \t]{0,8}["']([A-Z_][A-Z0-9_]{0,100})["']/g,
|
|
868
|
+
)) {
|
|
869
|
+
const id = s.node(
|
|
870
|
+
`config:BuildConfig.${m[1]}`,
|
|
871
|
+
`BuildConfig.${m[1]}`,
|
|
872
|
+
"buildconfig-field",
|
|
873
|
+
"config",
|
|
874
|
+
);
|
|
875
|
+
s.edge(fileId(path), id, "defines", "declaration");
|
|
876
|
+
}
|
|
877
|
+
for (const m of text.matchAll(/System\.getenv\(\s*["']([A-Za-z_]\w*)["']\s*\)/g)) {
|
|
878
|
+
const e = s.env(m[1]);
|
|
879
|
+
if (e) s.edge(fileId(path), e, "reads", "env-api", "high");
|
|
880
|
+
}
|
|
881
|
+
return s.result();
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Facts one configuration file declares.
|
|
886
|
+
*
|
|
887
|
+
* @param {string} kind - result of classify()
|
|
888
|
+
* @param {string} path - repo-relative path
|
|
889
|
+
* @param {string} text
|
|
890
|
+
* @param {{chart?: string}} [ctx] - for Helm files, the chart name
|
|
891
|
+
* @returns {{nodes: Array, edges: Array}}
|
|
892
|
+
*/
|
|
893
|
+
export function extractConfigFile(kind, path, text, ctx = {}) {
|
|
894
|
+
if (text.length > MAX_INFRA_BYTES || text.includes("\0")) return { nodes: [], edges: [] };
|
|
895
|
+
switch (kind) {
|
|
896
|
+
case "dockerfile":
|
|
897
|
+
return dockerfile(path, text);
|
|
898
|
+
case "compose":
|
|
899
|
+
return compose(path, text);
|
|
900
|
+
case "k8s":
|
|
901
|
+
return k8s(path, text);
|
|
902
|
+
case "helm-chart":
|
|
903
|
+
return helmChart(path, text);
|
|
904
|
+
case "helm-values":
|
|
905
|
+
return helmValues(path, text, ctx.chart || "chart");
|
|
906
|
+
case "helm-template":
|
|
907
|
+
return helmTemplate(path, text, ctx.chart || "chart");
|
|
908
|
+
case "kustomize":
|
|
909
|
+
return kustomize(path, text);
|
|
910
|
+
case "terraform":
|
|
911
|
+
return terraform(path, text);
|
|
912
|
+
case "terragrunt":
|
|
913
|
+
return terragrunt(path, text);
|
|
914
|
+
case "ci-github":
|
|
915
|
+
return githubWorkflow(path, text);
|
|
916
|
+
case "ci-gitlab":
|
|
917
|
+
return gitlabCi(path, text);
|
|
918
|
+
case "env-file":
|
|
919
|
+
return envFile(path, text);
|
|
920
|
+
case "gradle":
|
|
921
|
+
return gradle(path, text);
|
|
922
|
+
case "app-config":
|
|
923
|
+
case "dotnet-config":
|
|
924
|
+
return appConfig(path, text, kind);
|
|
925
|
+
default:
|
|
926
|
+
return kind.startsWith("manifest-") ? manifest(kind, path, text) : { nodes: [], edges: [] };
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/** True when a YAML text is a Kubernetes manifest (a kind and an apiVersion near the top). */
|
|
931
|
+
export function looksLikeK8s(text) {
|
|
932
|
+
const head = text.slice(0, 4096);
|
|
933
|
+
return /^apiVersion:\s*\S+/m.test(head) && /^kind:\s*\w+/m.test(head);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
const ENV_READS = [
|
|
937
|
+
/process\.env\.([A-Za-z_][A-Za-z0-9_]*)/g,
|
|
938
|
+
/process\.env\[\s*["']([A-Za-z_][A-Za-z0-9_]*)["']\s*\]/g,
|
|
939
|
+
/import\.meta\.env\.([A-Za-z_][A-Za-z0-9_]*)/g,
|
|
940
|
+
/Deno\.env\.get\(\s*["']([A-Za-z_]\w*)["']/g,
|
|
941
|
+
/os\.environ\[\s*["']([A-Za-z_]\w*)["']\s*\]/g,
|
|
942
|
+
/os\.environ\.get\(\s*["']([A-Za-z_]\w*)["']/g,
|
|
943
|
+
/os\.getenv\(\s*["']([A-Za-z_]\w*)["']/g,
|
|
944
|
+
/os\.Getenv\(\s*"([A-Za-z_]\w*)"/g,
|
|
945
|
+
/os\.LookupEnv\(\s*"([A-Za-z_]\w*)"/g,
|
|
946
|
+
/\benv::var(?:_os)?\(\s*"([A-Za-z_]\w*)"/g,
|
|
947
|
+
/\b(?:option_)?env!\(\s*"([A-Za-z_]\w*)"/g,
|
|
948
|
+
/System\.getenv\(\s*"([A-Za-z_]\w*)"/g,
|
|
949
|
+
/Environment\.GetEnvironmentVariable\(\s*"([A-Za-z_]\w*)"/g,
|
|
950
|
+
/\b(?:std::)?getenv\(\s*"([A-Za-z_]\w*)"/g,
|
|
951
|
+
/ProcessInfo\.processInfo\.environment\[\s*"([A-Za-z_]\w*)"\s*\]/g,
|
|
952
|
+
];
|
|
953
|
+
|
|
954
|
+
const CONFIG_READS = [
|
|
955
|
+
[/BuildConfig\.([A-Z_][A-Z0-9_]*)/g, (k) => `BuildConfig.${k}`],
|
|
956
|
+
[/Configuration\[\s*"([\w:.-]+)"\s*\]/g, (k) => k],
|
|
957
|
+
[/GetSection\(\s*"([\w:.-]+)"\s*\)/g, (k) => k],
|
|
958
|
+
[/@Value\(\s*"\$\{([A-Za-z_][\w.-]{0,200})(?::[^}\n]{0,200})?\}"/g, (k) => k],
|
|
959
|
+
[/System\.getProperty\(\s*"([A-Za-z_][\w.-]*)"/g, (k) => k],
|
|
960
|
+
[/Bundle\.main\.object\(forInfoDictionaryKey:\s*"([A-Za-z_]\w*)"/g, (k) => `Info.plist.${k}`],
|
|
961
|
+
[/infoDictionary\??\[\s*"([A-Za-z_]\w*)"\s*\]/g, (k) => `Info.plist.${k}`],
|
|
962
|
+
];
|
|
963
|
+
|
|
964
|
+
export const mayReadConfig = (text) => PREFILTER.test(text);
|
|
965
|
+
|
|
966
|
+
const PREFILTER =
|
|
967
|
+
/env|Env|getenv|BuildConfig|@Value|getProperty|infoDictionary|InfoDictionary|Configuration|GetSection/;
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Environment variables and configuration keys a source file reads by a literal name.
|
|
971
|
+
*
|
|
972
|
+
* @param {string} text - source with comments removed, strings kept
|
|
973
|
+
* @returns {{env: string[], config: string[]}}
|
|
974
|
+
*/
|
|
975
|
+
export function codeReads(text) {
|
|
976
|
+
const out = { env: [], config: [] };
|
|
977
|
+
if (!PREFILTER.test(text)) return out;
|
|
978
|
+
const seen = new Set();
|
|
979
|
+
for (let i = 0; i < ENV_READS.length; i++) {
|
|
980
|
+
for (const m of text.matchAll(ENV_READS[i])) {
|
|
981
|
+
if (!seen.has(m[1]) && seen.size < 200) {
|
|
982
|
+
seen.add(m[1]);
|
|
983
|
+
out.env.push(m[1]);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
const cfg = new Set();
|
|
988
|
+
for (const [re, fmt] of CONFIG_READS) {
|
|
989
|
+
for (const m of text.matchAll(re)) {
|
|
990
|
+
const key = fmt(m[1]);
|
|
991
|
+
if (!cfg.has(key) && cfg.size < 200) {
|
|
992
|
+
cfg.add(key);
|
|
993
|
+
out.config.push(key);
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return out;
|
|
998
|
+
}
|