Files
pikasTech-unidesk/scripts/src/platform-infra-pipelines-as-code.ts
T
2026-07-11 11:42:16 +02:00

2515 lines
114 KiB
TypeScript

import { randomBytes } from "node:crypto";
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import type { UniDeskConfig } from "./config";
import { rootPath } from "./config";
import type { RenderedCliResult } from "./output";
import {
capture,
compactCapture,
createYamlFieldReader,
parseJsonOutput,
readYamlRecord,
shQuote,
sha256Fingerprint,
} from "./platform-infra-ops-library";
import { materializeYamlComposition } from "./yaml-composition";
import { pacNodeReadOnlyNext, pacReadOnlyNext, resolveCicdDeliveryAuthority } from "./cicd-delivery-authority";
import {
canonicalizePacPipelineSpec,
pacSourceArtifactSafeError,
pacValueEvidence,
parsePacSourceArtifactOptions,
renderPacSourceArtifactResult,
runPacSourceArtifact,
type PacSourceArtifactBinding,
type PacSourceArtifactRuntimeObservation,
type PacSourceArtifactSpec,
} from "./platform-infra-pipelines-as-code-source-artifact";
const configFile = rootPath("config", "platform-infra", "pipelines-as-code.yaml");
const configLabel = "config/platform-infra/pipelines-as-code.yaml";
const remoteScriptFile = rootPath("scripts", "src", "platform-infra-pipelines-as-code-remote.sh");
const evaluatorFile = rootPath("scripts", "native", "cicd", "pac-status-evaluator.cjs");
const sourceArtifactRuntimeObserverFile = rootPath("scripts", "native", "cicd", "pac-source-artifact-runtime.mjs");
const fieldManager = "unidesk-platform-infra-pipelines-as-code";
const y = createYamlFieldReader(configLabel);
function kubernetesLabelValue(value: string): string {
const normalized = value
.trim()
.replace(/[^A-Za-z0-9_.-]+/gu, "-")
.replace(/^-+|-+$/gu, "")
.slice(0, 63);
return normalized.length > 0 ? normalized : "unspecified";
}
interface PacTarget {
id: string;
route: string;
namespace: string;
role: string;
enabled: boolean;
}
interface PacConfig {
version: number;
kind: "platform-infra-pipelines-as-code";
metadata: {
id: string;
owner: string;
spec: string;
relatedIssues: number[];
};
defaults: {
targetId: string;
consumerId: string;
};
display: {
timeZone: string;
};
release: {
version: string;
manifestUrl: string;
namespace: string;
controllerServiceName: string;
controllerServicePort: number;
waitTimeoutSeconds: number;
};
capabilities: {
sentinelInternalPublish: {
enabled: boolean;
admissionProvenance: "unavailable";
trackingIssue: string;
};
};
closeout: {
gitOpsMirrorFlush: {
enabled: boolean;
waitTimeoutSeconds: number;
maxAttempts: number;
};
};
targets: PacTarget[];
gitea: {
configRef: string;
internalBaseUrl: string;
admin: {
sourceRoot: string;
sourceRef: string;
format: "line-pair";
usernameLine: number;
passwordLine: number;
apiUsername: string;
};
webhook: {
secretRoot: string;
secretSourceRef: string;
events: string[];
branch: string;
};
};
repositories: PacRepository[];
consumers: PacConsumer[];
}
interface PacRepository {
id: string;
name: string;
namespace: string;
providerType: "gitea";
url: string;
cloneUrl: string;
owner: string;
repo: string;
secretName: string;
tokenKey: string;
webhookSecretKey: string;
concurrencyLimit: number;
params: Record<string, string>;
}
interface PacConsumer {
id: string;
node: string;
lane: string;
namespace: string;
pipeline: string;
pipelineRunPrefix: string;
argoNamespace: string;
argoApplication: string;
argoBootstrap: {
project: string;
repoUrl: string;
targetRevision: string;
path: string;
destinationNamespace: string;
automated: boolean;
} | null;
repositoryRef: string;
closeoutGitOpsMirrorFlush: boolean;
closeoutGitOpsMirrorLane: "v02" | "v03" | null;
sourceArtifact: PacSourceArtifactSpec | null;
}
interface CommonOptions {
targetId: string | null;
consumerId: string | null;
full: boolean;
raw: boolean;
json: boolean;
}
export interface PipelinesAsCodeNodeStatusOptions {
targetId: string | null;
nodeId: string | null;
full: boolean;
raw: boolean;
}
interface HistoryOptions extends CommonOptions {
limit: number;
detailId: string | null;
}
export interface PacHistoryConsumerIdentity {
readonly id: string;
readonly node: string;
readonly pipelineRunPrefix: string;
}
export interface PacHistoryConsumerSelection {
readonly selectedConsumerIds: readonly string[];
readonly nextConsumerId: string | null;
}
interface CloseoutOptions extends CommonOptions {
sourceCommit: string | null;
wait: boolean;
}
interface ApplyOptions extends CommonOptions {
confirm: boolean;
dryRun: boolean;
wait: boolean;
}
interface SecretMaterial {
adminUsername: string;
adminPassword: string;
adminFingerprint: string;
webhookSecret: string;
webhookFingerprint: string;
webhookPath: string;
}
export async function runPlatformInfraPipelinesAsCodeCommand(config: UniDeskConfig, args: string[]): Promise<Record<string, unknown> | RenderedCliResult> {
const [action = "plan"] = args;
if (action === "help" || action === "--help") return help(args[1] ?? null);
if (action === "plan") {
const options = parseCommonOptions(args.slice(1));
const result = plan(options);
return options.full || options.raw ? result : options.json ? compactPlanJson(result) : renderPlan(result);
}
if (action === "apply") {
const options = parseApplyOptions(args.slice(1));
const result = await apply(config, options);
return options.json || options.full || options.raw ? result : renderApply(result);
}
if (action === "status") {
const options = parseCommonOptions(args.slice(1));
const result = await status(config, options);
return options.full || options.raw ? result : options.json ? compactStatusJson(result) : renderStatus(result);
}
if (action === "closeout") {
const options = parseCloseoutOptions(args.slice(1));
const result = await closeout(config, options);
return options.full || options.raw ? result : options.json ? compactCloseoutJson(result) : renderCloseout(result);
}
if (action === "history" || action === "runs") {
const options = parseHistoryOptions(args.slice(1));
const result = await history(config, options);
return options.raw ? result : options.full ? compactHistoryJson(result, true) : options.json ? compactHistoryJson(result) : renderHistory(result);
}
if (action === "debug-step") {
const options = parseHistoryOptions(args.slice(1));
const result = await debugStep(config, options);
return options.full || options.raw ? result : options.json ? result : renderDebugStep(result);
}
if (action === "source-artifact") {
if (args.length === 1 || args.slice(1).includes("--help") || args.slice(1).includes("-h")) return sourceArtifactHelp();
const structuredError = args.includes("--json") || args.includes("--full");
try {
const options = parsePacSourceArtifactOptions(args.slice(1));
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId);
const consumer = resolveConsumer(pac, options.consumerId);
if (consumer.node.toLowerCase() !== target.id.toLowerCase()) throw new Error(`Pipelines-as-Code consumer ${consumer.id} belongs to ${consumer.node}, not target ${target.id}`);
if (consumer.sourceArtifact === null) throw new Error(`Pipelines-as-Code consumer ${consumer.id} has no sourceArtifact owner in ${configLabel}`);
const repository = resolveRepository(pac, consumer.repositoryRef);
const binding: PacSourceArtifactBinding = {
target: { id: target.id },
consumer: {
id: consumer.id,
node: consumer.node,
lane: consumer.lane,
namespace: consumer.namespace,
pipeline: consumer.pipeline,
pipelineRunPrefix: consumer.pipelineRunPrefix,
sourceArtifact: consumer.sourceArtifact,
},
repository: {
id: repository.id,
url: repository.url,
cloneUrl: repository.cloneUrl,
owner: repository.owner,
repo: repository.repo,
params: repository.params,
},
};
const result = await runPacSourceArtifact(binding, options, async ({ desiredSpec, desiredWrapper, provenance, sourceCommit }) => await observeSourceArtifactRuntime(config, target, consumer, desiredSpec, desiredWrapper, provenance, sourceCommit));
return options.json || options.full ? result : renderPacSourceArtifactResult(result);
} catch (error) {
const safeError = pacSourceArtifactSafeError(error);
if (structuredError) {
return {
ok: false,
action: "platform-infra-pipelines-as-code-source-artifact-validation",
error: safeError,
next: "Fix the declared owner, exact source identity, worktree proof, or CLI arguments; rerun plan before write.",
};
}
return sourceArtifactValidationError(safeError);
}
}
return { ok: false, error: "unsupported-platform-infra-pipelines-as-code-command", args, help: help(null) };
}
function sourceArtifactHelp(): RenderedCliResult {
const lines = [
"PAC SOURCE ARTIFACT",
"Generate and verify consumer-declared PaC source artifacts from owning YAML plus the shared domain renderer.",
"",
"Usage:",
" ... source-artifact plan --target <node> --consumer <id> --source-worktree <absolute-path>",
" ... source-artifact check --target <node> --consumer <id> --source-worktree <absolute-path>",
" ... source-artifact write --target <node> --consumer <id> --source-worktree <absolute-path> --confirm",
" ... source-artifact status --target <node> --consumer <id> --source-worktree <absolute-path> [--source-commit <full-sha>]",
" ... source-artifact verify-runtime --target <node> --consumer <id> --source-worktree <absolute-path> --source-commit <full-sha>",
"",
"plan/check/write compare desired YAML-rendered state with source files and never access runtime.",
"status is non-gating runtime diagnostics; verify-runtime is fail-closed and binds the PipelineRun to the exact source commit.",
"Use --json or --full for explicit structured output; source specs and Secret values are never printed.",
];
return { ok: true, command: "platform-infra-pipelines-as-code-source-artifact-help", renderedText: `${lines.join("\n")}\n`, contentType: "text/plain" };
}
function sourceArtifactValidationError(error: ReturnType<typeof pacSourceArtifactSafeError>): RenderedCliResult {
const details = Object.entries(error.details).map(([key, value]) => `${key}=${value}`).join(" ");
return {
ok: false,
command: "platform-infra-pipelines-as-code-source-artifact-validation",
renderedText: `PAC SOURCE ARTIFACT ERROR\ncode=${error.code} evidence=${error.evidence}${details ? ` details=${details}` : ""}\nnext=Fix the declared owner, exact source identity, worktree proof, or CLI arguments; rerun plan before write.\n`,
contentType: "text/plain",
};
}
function help(scope: string | null): Record<string, unknown> {
if (scope === "platform-bootstrap") {
return {
command: "platform-infra pipelines-as-code help platform-bootstrap",
scope: "explicit-initial-platform-bootstrap",
configTruth: configLabel,
usage: [
"bun scripts/cli.ts platform-infra pipelines-as-code apply --target <NODE> --dry-run",
"bun scripts/cli.ts platform-infra pipelines-as-code apply --target <NODE> --confirm",
],
boundary: "仅用于首次安装或显式平台 bootstrap;不得在 source PR 合并后用于交付、恢复或补跑。",
mutation: "explicit-confirm-required",
};
}
if (scope === "compatibility-diagnostics") {
return {
command: "platform-infra pipelines-as-code help compatibility-diagnostics",
scope: "explicit-readonly-or-connectivity-diagnostics",
configTruth: configLabel,
usage: [
"bun scripts/cli.ts platform-infra pipelines-as-code closeout --target <NODE> --consumer <consumer> --source-commit <sha> --wait",
"bun scripts/cli.ts platform-infra pipelines-as-code status --target <NODE> --consumer <consumer> --full",
"bun scripts/cli.ts platform-infra pipelines-as-code history --target <NODE> --consumer <consumer> --limit 10 --full",
"bun scripts/cli.ts platform-infra pipelines-as-code debug-step --target <NODE> --consumer <consumer> --json",
],
boundary: "本 scope 只允许读取现有 PaC/Tekton/Argo 证据;不得 POST hook test、伪造 push、交付、恢复或补跑。",
};
}
return {
command: "platform-infra pipelines-as-code plan|status|history|debug-step|source-artifact",
configTruth: configLabel,
usage: [
"bun scripts/cli.ts platform-infra pipelines-as-code plan --target JD01",
"bun scripts/cli.ts platform-infra pipelines-as-code status --target JD01 [--json|--full|--raw]",
"bun scripts/cli.ts platform-infra pipelines-as-code history --target JD01 [--consumer hwlab-jd01-v03] [--limit 10]",
"bun scripts/cli.ts platform-infra pipelines-as-code history --target JD01 --id <pipelinerun>",
"bun scripts/cli.ts platform-infra pipelines-as-code debug-step --target JD01 [--consumer <consumer>] [--id <pipelinerun>] [--json]",
"bun scripts/cli.ts platform-infra pipelines-as-code source-artifact check --target NC01 --consumer agentrun-nc01-v02 --source-worktree /abs/worktree",
"bun scripts/cli.ts platform-infra pipelines-as-code source-artifact verify-runtime --target NC01 --consumer agentrun-nc01-v02 --source-worktree /abs/worktree --source-commit <full-sha>",
],
maintenanceHelp: "bun scripts/cli.ts platform-infra pipelines-as-code help platform-bootstrap",
compatibilityHelp: "bun scripts/cli.ts platform-infra pipelines-as-code help compatibility-diagnostics",
boundary: "唯一正式触发链:GitHub PR merge -> GitHub webhook bridge -> Gitea mirror/snapshot -> Pipelines-as-Code -> Tekton -> GitOps/Argo/k8s runtime。",
};
}
function readPacConfig(): PacConfig {
const root = materializeYamlComposition(readYamlRecord<Record<string, unknown>>(configFile, "platform-infra-pipelines-as-code"), { label: configLabel }).value;
const release = y.objectField(root, "release", "");
const capabilities = y.objectField(root, "capabilities", "");
const sentinelInternalPublish = y.objectField(capabilities, "sentinelInternalPublish", "capabilities");
const closeout = y.objectField(root, "closeout", "");
const gitOpsMirrorFlush = y.objectField(closeout, "gitOpsMirrorFlush", "closeout");
const gitea = y.objectField(root, "gitea", "");
const display = y.objectField(root, "display", "");
const admin = y.objectField(gitea, "admin", "gitea");
const webhook = y.objectField(gitea, "webhook", "gitea");
const repositories = root.repositories === undefined
? [parseRepository(y.objectField(root, "repository", ""), "repository")]
: y.arrayOfRecords(root.repositories, "repositories").map((item, index) => parseRepository(item, `repositories[${index}]`));
const consumers = root.consumers === undefined
? [parseConsumer(y.objectField(root, "consumer", ""), "consumer", repositories[0]?.id ?? "default")]
: y.arrayOfRecords(root.consumers, "consumers").map((item, index) => parseConsumer(item, `consumers[${index}]`, repositories[0]?.id ?? "default"));
const defaults = y.objectField(root, "defaults", "");
const parsed: PacConfig = {
version: y.integerField(root, "version", ""),
kind: "platform-infra-pipelines-as-code",
metadata: {
id: y.stringField(y.objectField(root, "metadata", ""), "id", "metadata"),
owner: y.stringField(y.objectField(root, "metadata", ""), "owner", "metadata"),
spec: y.stringField(y.objectField(root, "metadata", ""), "spec", "metadata"),
relatedIssues: y.numberArrayField(y.objectField(root, "metadata", ""), "relatedIssues", "metadata"),
},
defaults: {
targetId: y.stringField(defaults, "targetId", "defaults"),
consumerId: typeof defaults.consumerId === "string" && defaults.consumerId.length > 0 ? defaults.consumerId : consumers[0]?.id ?? "default",
},
display: {
timeZone: y.stringField(display, "timeZone", "display"),
},
release: {
version: y.stringField(release, "version", "release"),
manifestUrl: urlField(release, "manifestUrl", "release"),
namespace: y.kubernetesNameField(release, "namespace", "release"),
controllerServiceName: y.kubernetesNameField(release, "controllerServiceName", "release"),
controllerServicePort: y.portField(release, "controllerServicePort", "release"),
waitTimeoutSeconds: positiveInteger(release, "waitTimeoutSeconds", "release"),
},
capabilities: {
sentinelInternalPublish: {
enabled: y.booleanField(sentinelInternalPublish, "enabled", "capabilities.sentinelInternalPublish"),
admissionProvenance: y.enumField(sentinelInternalPublish, "admissionProvenance", "capabilities.sentinelInternalPublish", ["unavailable"] as const),
trackingIssue: urlField(sentinelInternalPublish, "trackingIssue", "capabilities.sentinelInternalPublish"),
},
},
closeout: {
gitOpsMirrorFlush: {
enabled: y.booleanField(gitOpsMirrorFlush, "enabled", "closeout.gitOpsMirrorFlush"),
waitTimeoutSeconds: positiveInteger(gitOpsMirrorFlush, "waitTimeoutSeconds", "closeout.gitOpsMirrorFlush"),
maxAttempts: positiveInteger(gitOpsMirrorFlush, "maxAttempts", "closeout.gitOpsMirrorFlush"),
},
},
targets: y.arrayOfRecords(root.targets, "targets").map(parseTarget),
gitea: {
configRef: y.stringField(gitea, "configRef", "gitea"),
internalBaseUrl: urlField(gitea, "internalBaseUrl", "gitea"),
admin: {
sourceRoot: y.absolutePathField(admin, "sourceRoot", "gitea.admin"),
sourceRef: y.sourceRefField(admin, "sourceRef", "gitea.admin"),
format: y.enumField(admin, "format", "gitea.admin", ["line-pair"] as const),
usernameLine: positiveInteger(admin, "usernameLine", "gitea.admin"),
passwordLine: positiveInteger(admin, "passwordLine", "gitea.admin"),
apiUsername: y.stringField(admin, "apiUsername", "gitea.admin"),
},
webhook: {
secretRoot: y.absolutePathField(webhook, "secretRoot", "gitea.webhook"),
secretSourceRef: y.sourceRefField(webhook, "secretSourceRef", "gitea.webhook"),
events: y.stringArrayField(webhook, "events", "gitea.webhook"),
branch: y.stringField(webhook, "branch", "gitea.webhook"),
},
},
repositories,
consumers,
};
validateConfig(parsed);
return parsed;
}
function parseRepository(repository: Record<string, unknown>, path: string): PacRepository {
const name = y.kubernetesNameField(repository, "name", path);
return {
id: typeof repository.id === "string" && repository.id.length > 0 ? repository.id : name,
name,
namespace: y.kubernetesNameField(repository, "namespace", path),
providerType: y.enumField(repository, "providerType", path, ["gitea"] as const),
url: urlField(repository, "url", path),
cloneUrl: urlField(repository, "cloneUrl", path),
owner: y.stringField(repository, "owner", path),
repo: y.stringField(repository, "repo", path),
secretName: y.kubernetesNameField(repository, "secretName", path),
tokenKey: y.stringField(repository, "tokenKey", path),
webhookSecretKey: y.stringField(repository, "webhookSecretKey", path),
concurrencyLimit: positiveInteger(repository, "concurrencyLimit", path),
params: stringRecord(y.objectField(repository, "params", path), `${path}.params`),
};
}
function parseConsumer(consumer: Record<string, unknown>, path: string, defaultRepositoryRef: string): PacConsumer {
const id = typeof consumer.id === "string" && consumer.id.length > 0 ? consumer.id : `${y.stringField(consumer, "node", path)}-${y.stringField(consumer, "lane", path)}`;
const argoBootstrap = consumer.argoBootstrap === undefined ? null : y.objectField(consumer, "argoBootstrap", path);
return {
id,
node: y.stringField(consumer, "node", path),
lane: y.stringField(consumer, "lane", path),
namespace: y.kubernetesNameField(consumer, "namespace", path),
pipeline: y.kubernetesNameField(consumer, "pipeline", path),
pipelineRunPrefix: y.stringField(consumer, "pipelineRunPrefix", path),
argoNamespace: y.kubernetesNameField(consumer, "argoNamespace", path),
argoApplication: y.kubernetesNameField(consumer, "argoApplication", path),
argoBootstrap: argoBootstrap === null ? null : {
project: y.kubernetesNameField(argoBootstrap, "project", `${path}.argoBootstrap`),
repoUrl: urlField(argoBootstrap, "repoUrl", `${path}.argoBootstrap`),
targetRevision: y.stringField(argoBootstrap, "targetRevision", `${path}.argoBootstrap`),
path: y.stringField(argoBootstrap, "path", `${path}.argoBootstrap`),
destinationNamespace: y.kubernetesNameField(argoBootstrap, "destinationNamespace", `${path}.argoBootstrap`),
automated: y.booleanField(argoBootstrap, "automated", `${path}.argoBootstrap`),
},
repositoryRef: typeof consumer.repositoryRef === "string" && consumer.repositoryRef.length > 0 ? consumer.repositoryRef : defaultRepositoryRef,
closeoutGitOpsMirrorFlush: y.booleanField(consumer, "closeoutGitOpsMirrorFlush", path),
closeoutGitOpsMirrorLane: consumer.closeoutGitOpsMirrorLane === undefined ? null : y.enumField(consumer, "closeoutGitOpsMirrorLane", path, ["v02", "v03"] as const),
sourceArtifact: consumer.sourceArtifact === undefined ? null : parseSourceArtifact(y.objectField(consumer, "sourceArtifact", path), `${path}.sourceArtifact`),
};
}
function parseSourceArtifact(value: Record<string, unknown>, path: string): PacSourceArtifactSpec {
const mode = y.enumField(value, "mode", path, ["embedded-pipeline-spec", "remote-pipeline-annotation"] as const);
const renderer = y.enumField(value, "renderer", path, ["agentrun-control-plane", "hwlab-runtime-lane"] as const);
const pipelineRunPath = sourceArtifactPath(y.stringField(value, "pipelineRunPath", path), `${path}.pipelineRunPath`);
const pipelinePath = value.pipelinePath === undefined ? null : sourceArtifactPath(y.stringField(value, "pipelinePath", path), `${path}.pipelinePath`);
const taskRunTemplate = y.objectField(value, "taskRunTemplate", path);
if (mode === "embedded-pipeline-spec" && pipelinePath !== null) throw new Error(`${path}.pipelinePath is forbidden for embedded-pipeline-spec`);
if (mode === "remote-pipeline-annotation" && pipelinePath === null) throw new Error(`${path}.pipelinePath is required for remote-pipeline-annotation`);
if (renderer === "agentrun-control-plane" && mode !== "embedded-pipeline-spec") throw new Error(`${path}.renderer agentrun-control-plane requires embedded-pipeline-spec`);
if (renderer === "hwlab-runtime-lane" && mode !== "remote-pipeline-annotation") throw new Error(`${path}.renderer hwlab-runtime-lane requires remote-pipeline-annotation`);
return {
mode,
renderer,
configRef: y.stringField(value, "configRef", path),
pipelineRunPath,
pipelinePath,
maxKeepRuns: positiveInteger(value, "maxKeepRuns", path),
taskRunTemplate: {
hostNetwork: y.booleanField(taskRunTemplate, "hostNetwork", `${path}.taskRunTemplate`),
dnsPolicy: y.enumField(taskRunTemplate, "dnsPolicy", `${path}.taskRunTemplate`, ["ClusterFirst", "ClusterFirstWithHostNet", "Default", "None"] as const),
fsGroup: positiveInteger(taskRunTemplate, "fsGroup", `${path}.taskRunTemplate`),
},
};
}
function sourceArtifactPath(value: string, path: string): string {
if (value.startsWith("/") || value.split(/[\\/]/u).includes("..") || !value.endsWith(".yaml")) throw new Error(`${path} must be a worktree-relative .yaml path without ..`);
return value;
}
async function observeSourceArtifactRuntime(
config: UniDeskConfig,
target: PacTarget,
consumer: PacConsumer,
desiredSpec: Record<string, unknown>,
desiredWrapper: Record<string, unknown> | null,
provenance: { readonly configRef: string; readonly effectiveConfigSha256: string; readonly renderer: string; readonly mode: string },
sourceCommit: string | null,
): Promise<PacSourceArtifactRuntimeObservation> {
const script = renderPacSourceArtifactRuntimeObserverShell({
namespace: consumer.namespace,
pipeline: consumer.pipeline,
pipelineRunPrefix: consumer.pipelineRunPrefix,
desiredSpec: canonicalizePacPipelineSpec(desiredSpec),
desiredWrapper: desiredWrapper === null ? null : canonicalizePacPipelineSpec(desiredWrapper),
provenance,
sourceCommit,
});
const captured = await capture(config, target.route, ["sh"], script);
if (captured.exitCode !== 0) {
return unavailableSourceArtifactRuntime(pacRuntimeTransportFailureReason(captured), sourceCommit);
}
const parsed = parseJsonOutput(captured.stdout);
if (parsed === null) return unavailableSourceArtifactRuntime("runtime-observer-invalid-json", sourceCommit);
return {
live: pacRuntimeSourceArtifactItem(parsed.live, {
configRef: provenance.configRef,
effectiveConfigSha256: provenance.effectiveConfigSha256,
renderer: provenance.renderer,
mode: provenance.mode,
sourceCommit: null,
exactName: consumer.pipeline,
namePrefix: null,
}),
embedded: pacRuntimeSourceArtifactItem(parsed.embedded, {
configRef: provenance.configRef,
effectiveConfigSha256: provenance.effectiveConfigSha256,
renderer: provenance.renderer,
mode: provenance.mode,
sourceCommit,
exactName: null,
namePrefix: consumer.pipelineRunPrefix,
}),
};
}
export function pacRuntimeTransportFailureReason(captured: { readonly exitCode: number; readonly stdout: string; readonly stderr: string }): string {
return `runtime-transport-exit-${captured.exitCode}:stderr(${pacValueEvidence(captured.stderr)}):stdout(${pacValueEvidence(captured.stdout)})`;
}
export function renderPacSourceArtifactRuntimeObserverShell(inputValue: Record<string, unknown>): string {
const input = JSON.stringify(inputValue);
const observer = readFileSync(sourceArtifactRuntimeObserverFile, "utf8").trimEnd();
if (observer.includes("NODE_PAC_SOURCE_ARTIFACT_STATUS") || input.includes("JSON_PAC_SOURCE_ARTIFACT_INPUT")) throw new Error("PaC source artifact observer input contains a reserved heredoc delimiter");
return `set -eu
observer_input=$(mktemp)
trap 'rm -f "$observer_input"' EXIT HUP INT TERM
cat >"$observer_input" <<'JSON_PAC_SOURCE_ARTIFACT_INPUT'
${input}
JSON_PAC_SOURCE_ARTIFACT_INPUT
PAC_SOURCE_ARTIFACT_INPUT_FILE="$observer_input" node --input-type=module <<'NODE_PAC_SOURCE_ARTIFACT_STATUS'
${observer}
NODE_PAC_SOURCE_ARTIFACT_STATUS
`;
}
export function pacRuntimeSourceArtifactItem(value: unknown, expected: {
readonly configRef: string;
readonly effectiveConfigSha256: string;
readonly renderer: string;
readonly mode: string;
readonly sourceCommit: string | null;
readonly exactName: string | null;
readonly namePrefix: string | null;
}): PacSourceArtifactRuntimeObservation["live"] {
if (typeof value !== "object" || value === null || Array.isArray(value)) return unavailableSourceArtifactRuntime("runtime-observer-invalid-item", null).live;
const item = value as Record<string, unknown>;
const status = item.status;
if (status !== "aligned" && status !== "drifted" && status !== "missing" && status !== "unavailable") return unavailableSourceArtifactRuntime("runtime-observer-invalid-status", null).live;
const drift = item.firstDrift;
const observedName = typeof item.name === "string" && item.name.length <= 253 && /^[a-z0-9](?:[-a-z0-9.]*[a-z0-9])?$/u.test(item.name) ? item.name : null;
const name = observedName !== null
&& (expected.exactName === null || observedName === expected.exactName)
&& (expected.namePrefix === null || observedName.startsWith(expected.namePrefix))
? observedName
: null;
const provenanceAligned = item.configRef === expected.configRef
&& item.effectiveConfigSha256 === expected.effectiveConfigSha256
&& item.renderer === expected.renderer
&& item.mode === expected.mode;
return {
status,
name,
canonicalSha256: typeof item.canonicalSha256 === "string" && /^sha256:[0-9a-f]{64}$/u.test(item.canonicalSha256) ? item.canonicalSha256 : null,
firstDrift: typeof drift === "object" && drift !== null && !Array.isArray(drift)
? {
path: pacRuntimeSafePath((drift as Record<string, unknown>).path),
expected: pacRuntimeSafeEvidence((drift as Record<string, unknown>).expected),
actual: pacRuntimeSafeEvidence((drift as Record<string, unknown>).actual),
}
: null,
provenanceAligned,
configRef: item.configRef === expected.configRef && expected.configRef.length <= 512 && /^[A-Za-z0-9_./-]+#[A-Za-z0-9_.-]+$/u.test(expected.configRef) ? expected.configRef : null,
effectiveConfigSha256: item.effectiveConfigSha256 === expected.effectiveConfigSha256 && /^sha256:[0-9a-f]{64}$/u.test(expected.effectiveConfigSha256) ? expected.effectiveConfigSha256 : null,
sourceCommit: item.sourceCommit === expected.sourceCommit && (expected.sourceCommit === null || /^[0-9a-f]{40}$/u.test(expected.sourceCommit)) ? expected.sourceCommit : null,
reason: typeof item.reason === "string" ? pacRuntimeSafeReason(item.reason) : null,
candidateCount: typeof item.candidateCount === "number" && Number.isInteger(item.candidateCount) && item.candidateCount >= 0 ? item.candidateCount : 0,
};
}
export function pacRuntimeSafePath(value: unknown): string {
const path = typeof value === "string" ? value : "$";
const allowedSegments = new Set([
"accessModes", "annotations", "apiVersion", "args", "command", "computeResources", "default", "description", "dnsPolicy", "env", "envFrom",
"executionMode", "finally", "fsGroup", "generateName", "hostNetwork", "image", "kind", "labels", "length", "metadata", "mode", "mountPath", "name", "namespace", "onError", "operator",
"optional", "params", "pipeline", "pipelineRef", "pipelineSpec", "podTemplate", "readinessProbe", "readOnly", "requests", "resources",
"results", "retries", "runAfter", "script", "secret", "secretName", "securityContext", "serviceAccountName", "sidecars", "spec", "steps",
"storage", "subPath", "taskRunTemplate", "taskSpec", "tasks", "timeout", "timeouts", "type", "value", "values", "volumeClaimTemplate",
"volumeMounts", "volumes", "when", "workspaces", "workingDir", "wrapper",
]);
if (!path.startsWith("$")) return `path(${pacValueEvidence(path)})`;
const token = /(?:\.([A-Za-z0-9_-]+))|(?:\[(\d+)\])/gu;
let offset = 1;
for (const match of path.slice(1).matchAll(token)) {
if (match.index !== offset - 1) return `path(${pacValueEvidence(path)})`;
if (match[1] !== undefined && !allowedSegments.has(match[1])) return `path(${pacValueEvidence(path)})`;
offset = 1 + (match.index ?? 0) + match[0].length;
}
return offset === path.length ? path : `path(${pacValueEvidence(path)})`;
}
function pacRuntimeSafeEvidence(value: unknown): string {
return typeof value === "string" && /^type=[a-z]+,length=\d+,sha256=[0-9a-f]{64}$/u.test(value)
? value
: pacValueEvidence(value);
}
export function pacRuntimeSafeReason(value: string): string {
const fixed = new Set([
"source-commit-not-specified",
"source-commit-run-not-found",
"source-commit-identity-conflict",
"pipeline-get-not-found",
"pipelinerun-list-not-found",
"pipelinerun-list-invalid-shape",
"runtime-observer-invalid-json",
"runtime-observer-invalid-item",
"runtime-observer-invalid-status",
"runtime-observer-not-configured",
]);
if (fixed.has(value)) return value;
if (/^source-commit-run-conflict:\d+$/u.test(value)) return value;
if (/^(?:pipeline-get|pipelinerun-list)-command-failed:(?:unknown|\d+):type=string,length=\d+,sha256=[0-9a-f]{64}$/u.test(value)) return value;
if (/^(?:runtime-observer-input-error|runtime-observer-reason):type=string,length=\d+,sha256=[0-9a-f]{64}$/u.test(value)) return value;
return `runtime-observer-reason:${pacValueEvidence(value)}`;
}
function unavailableSourceArtifactRuntime(reason: string, sourceCommit: string | null): PacSourceArtifactRuntimeObservation {
const item = { status: "unavailable" as const, name: null, canonicalSha256: null, firstDrift: null, provenanceAligned: false, configRef: null, effectiveConfigSha256: null, sourceCommit, reason, candidateCount: 0 };
return { live: item, embedded: item };
}
function parseTarget(record: Record<string, unknown>, index: number): PacTarget {
const path = `targets[${index}]`;
return {
id: y.stringField(record, "id", path),
route: y.stringField(record, "route", path),
namespace: y.kubernetesNameField(record, "namespace", path),
role: y.stringField(record, "role", path),
enabled: y.booleanField(record, "enabled", path),
};
}
function validateConfig(config: PacConfig): void {
if (config.version !== 1) throw new Error(`${configLabel}.version must be 1`);
resolveTarget(config, config.defaults.targetId);
if (config.release.waitTimeoutSeconds > 55) throw new Error(`${configLabel}.release.waitTimeoutSeconds must fit the 60s trans budget`);
if (config.closeout.gitOpsMirrorFlush.waitTimeoutSeconds > 55) throw new Error(`${configLabel}.closeout.gitOpsMirrorFlush.waitTimeoutSeconds must fit the 60s trans budget`);
resolveConsumer(config, config.defaults.consumerId);
const ids = new Set<string>();
for (const repository of config.repositories) {
if (ids.has(repository.id)) throw new Error(`${configLabel}.repositories id must be unique: ${repository.id}`);
ids.add(repository.id);
if (repository.providerType !== "gitea") throw new Error(`${configLabel}.repositories.${repository.id}.providerType must be gitea`);
if (isInternalRepositoryMatchUrl(repository.url)) throw new Error(`${configLabel}.repositories.${repository.id}.url must be the public Gitea URL used in webhook payloads; keep internal service URLs in cloneUrl/params.git_read_url`);
if (new URL(repository.cloneUrl).origin !== new URL(config.gitea.internalBaseUrl).origin) throw new Error(`${configLabel}.repositories.${repository.id}.cloneUrl must use the configured internal Gitea base URL`);
}
const consumerIds = new Set<string>();
for (const consumer of config.consumers) {
if (consumerIds.has(consumer.id)) throw new Error(`${configLabel}.consumers id must be unique: ${consumer.id}`);
consumerIds.add(consumer.id);
const repository = resolveRepository(config, consumer.repositoryRef);
if (repository.namespace !== consumer.namespace) throw new Error(`${configLabel}.consumers.${consumer.id}.namespace must match repository namespace`);
}
if (!config.gitea.webhook.events.includes("push")) throw new Error(`${configLabel}.gitea.webhook.events must include push`);
validateTimeZone(config.display.timeZone, `${configLabel}.display.timeZone`);
}
function resolveTarget(config: PacConfig, targetId: string | null): PacTarget {
const id = targetId ?? config.defaults.targetId;
const target = config.targets.find((item) => item.id.toLowerCase() === id.toLowerCase());
if (target === undefined) throw new Error(`unknown Pipelines-as-Code target ${id}; known targets: ${config.targets.map((item) => item.id).join(", ")}`);
if (!target.enabled) throw new Error(`Pipelines-as-Code target ${target.id} is disabled in ${configLabel}`);
return target;
}
function resolveConsumer(config: PacConfig, consumerId: string | null): PacConsumer {
const id = consumerId ?? config.defaults.consumerId;
const consumer = config.consumers.find((item) => item.id.toLowerCase() === id.toLowerCase());
if (consumer === undefined) throw new Error(`unknown Pipelines-as-Code consumer ${id}; known consumers: ${config.consumers.map((item) => item.id).join(", ")}`);
return consumer;
}
function resolveRepository(config: PacConfig, repositoryRef: string): PacRepository {
const repository = config.repositories.find((item) => item.id === repositoryRef || item.name === repositoryRef);
if (repository === undefined) throw new Error(`unknown Pipelines-as-Code repository ${repositoryRef}; known repositories: ${config.repositories.map((item) => item.id).join(", ")}`);
return repository;
}
function plan(options: CommonOptions): Record<string, unknown> {
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId);
const consumer = resolveConsumer(pac, options.consumerId);
const repository = resolveRepository(pac, consumer.repositoryRef);
const secrets = secretSummaries(pac);
return {
ok: true,
action: "platform-infra-pipelines-as-code-plan",
mutation: false,
target: targetSummary(target),
config: configSummary(pac, consumer, repository),
release: pac.release,
repository: repositorySummary(repository),
consumer: consumerObservationSummary(consumer),
secrets,
policy: policyChecks(repository),
next: nextCommands(target.id, consumer.id, pac.defaults.consumerId),
};
}
async function apply(config: UniDeskConfig, options: ApplyOptions): Promise<Record<string, unknown>> {
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId);
const consumer = resolveConsumer(pac, options.consumerId);
const repository = resolveRepository(pac, consumer.repositoryRef);
const secrets = ensureSecrets(pac, !options.dryRun, options.dryRun);
const releaseManifest = options.dryRun ? "" : await fetchReleaseManifest(pac);
const result = await capture(config, target.route, ["sh"], remoteScript("apply", pac, target, repository, consumer, options, secrets, releaseManifest));
const parsed = parseJsonOutput(result.stdout);
return {
ok: result.exitCode === 0 && parsed?.ok === true,
action: "platform-infra-pipelines-as-code-apply",
mutation: !options.dryRun,
mode: options.dryRun ? "dry-run" : "confirmed",
target: targetSummary(target),
config: compactConfigSummary(pac, consumer, repository),
release: { version: pac.release.version, manifestUrl: pac.release.manifestUrl },
repository: repositorySummary(repository),
consumer: consumerObservationSummary(consumer),
secrets: secretSummaries(pac),
remote: parsed ?? compactCapture(result, { full: true }),
next: nextCommands(target.id, consumer.id, pac.defaults.consumerId),
};
}
async function status(config: UniDeskConfig, options: CommonOptions): Promise<Record<string, unknown>> {
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId);
const consumer = resolveConsumer(pac, options.consumerId);
const repository = resolveRepository(pac, consumer.repositoryRef);
const secrets = ensureSecrets(pac, false);
const result = await capture(config, target.route, ["sh"], remoteScript("status", pac, target, repository, consumer, { ...options, confirm: false, dryRun: true, wait: false }, secrets, ""));
const parsed = parseJsonOutput(result.stdout);
const summary = parsed === null ? null : statusSummary(parsed);
const deliveryAuthority = resolveCicdDeliveryAuthority({ consumerId: consumer.id, node: consumer.node, lane: consumer.lane });
return {
ok: result.exitCode === 0 && summary?.ready === true && deliveryAuthority.kind === "pac-pr-merge",
action: "platform-infra-pipelines-as-code-status",
mutation: false,
target: targetSummary(target),
config: compactConfigSummary(pac, consumer, repository),
consumer: consumerObservationSummary(consumer),
deliveryAuthority,
coverage: consumerCoverage(pac, target.id),
summary,
remote: parsed === null ? compactCapture(result, { full: true }) : options.raw ? parsed : undefined,
next: nextCommands(target.id, consumer.id, pac.defaults.consumerId),
};
}
export async function getPlatformInfraPipelinesAsCodeNodeStatus(config: UniDeskConfig, options: PipelinesAsCodeNodeStatusOptions): Promise<Record<string, unknown>> {
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId ?? options.nodeId);
const nodeId = options.nodeId ?? target.id;
const consumers = pac.consumers.filter((consumer) => consumer.node.toLowerCase() === nodeId.toLowerCase());
if (consumers.length === 0) {
throw new Error(`no Pipelines-as-Code consumers are configured for node ${nodeId}; known nodes: ${Array.from(new Set(pac.consumers.map((item) => item.node))).sort().join(", ")}`);
}
const startedAt = Date.now();
const statuses = await Promise.all(consumers.map(async (consumer) => {
const repository = resolveRepository(pac, consumer.repositoryRef);
try {
const current = await status(config, { targetId: target.id, consumerId: consumer.id, full: false, raw: false, json: false });
return nodeStatusRow(target.id, consumer, repository, current);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return {
consumer: consumer.id,
node: consumer.node,
lane: consumer.lane,
repository: `${repository.owner}/${repository.repo}`,
namespace: consumer.namespace,
pipeline: consumer.pipeline,
argoApplication: consumer.argoApplication,
ready: false,
ciReady: false,
argoReady: false,
runtimeReady: false,
diagnosticsReady: false,
reason: "status-read-failed",
hint: message,
error: message,
valuesPrinted: false,
};
}
}));
const ready = statuses.every((row) => row.ready === true);
return {
ok: ready,
action: "cicd-node-status",
mutation: false,
node: nodeId,
target: targetSummary(target),
config: {
path: configLabel,
source: "Gitea Repository CR + Tekton PipelineRun/TaskRun + Argo + runtime live objects.",
displayTimeZone: pac.display.timeZone,
valuesPrinted: false,
},
consumers: statuses,
summary: {
ready,
total: statuses.length,
readyCount: statuses.filter((row) => row.ready === true).length,
blockedCount: statuses.filter((row) => row.ready !== true).length,
elapsedMs: Date.now() - startedAt,
valuesPrinted: false,
},
next: pacNodeReadOnlyNext(target.id),
details: options.full || options.raw ? statuses : undefined,
valuesPrinted: false,
};
}
async function closeout(config: UniDeskConfig, options: CloseoutOptions): Promise<Record<string, unknown>> {
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId);
const consumer = resolveConsumer(pac, options.consumerId);
const startedAt = Date.now();
const waitMs = Math.max(1, pac.release.waitTimeoutSeconds) * 1000;
let ciAttempts = 0;
let runtimeAttempts = 0;
let current = await observeCloseoutStatus(config, options, target, consumer, "ci", 1, startedAt);
ciAttempts += 1;
while (options.wait && !closeoutCiReady(current, options.sourceCommit) && Date.now() - startedAt < waitMs) {
await sleep(3000);
current = await observeCloseoutStatus(config, options, target, consumer, "ci", ciAttempts + 1, startedAt);
ciAttempts += 1;
}
let summary = record(current.summary);
let latest = record(summary.latestPipelineRun);
let diagnostics = record(summary.diagnostics);
let observedSourceCommit = observedCloseoutSourceCommit(latest, diagnostics);
let sourceMatched = options.sourceCommit === null || observedSourceCommit === options.sourceCommit;
const ciReady = closeoutCiReady(current, options.sourceCommit);
let gitOpsMirrorFlush: Record<string, unknown> = {
ok: true,
enabled: false,
configured: consumer.closeoutGitOpsMirrorFlush,
required: false,
applicable: false,
mode: "automatic-chain-owned-observation",
executed: false,
reason: "PaC migrated delivery owns GitOps publication and mirror persistence; compatibility closeout is read-only.",
configSource: configLabel,
valuesPrinted: false,
};
const runtimeStartedAt = Date.now();
if (ciReady) {
current = await observeCloseoutStatus(config, options, target, consumer, "runtime", 1, startedAt);
runtimeAttempts += 1;
while (options.wait && !closeoutReady(current, options.sourceCommit) && Date.now() - runtimeStartedAt < waitMs) {
await sleep(3000);
current = await observeCloseoutStatus(config, options, target, consumer, "runtime", runtimeAttempts + 1, startedAt);
runtimeAttempts += 1;
}
}
summary = record(current.summary);
latest = record(summary.latestPipelineRun);
diagnostics = record(summary.diagnostics);
observedSourceCommit = observedCloseoutSourceCommit(latest, diagnostics);
sourceMatched = options.sourceCommit === null || observedSourceCommit === options.sourceCommit;
const baseReady = current.ok === true && sourceMatched;
gitOpsMirrorFlush = {
...record(gitOpsMirrorFlush),
afterSummary: summary,
valuesPrinted: false,
};
const ready = baseReady;
printCloseoutProgress(target, consumer, "closeout", ready ? "completed" : "blocked", startedAt, {
ready,
sourceMatched,
diagnosticCode: diagnostics.code,
ciAttempts,
runtimeAttempts,
});
return {
ok: ready,
action: "platform-infra-pipelines-as-code-closeout",
mutation: false,
target: targetSummary(target),
consumer,
sourceCommit: options.sourceCommit,
observedSourceCommit,
sourceMatched,
ready,
wait: {
requested: options.wait,
attempts: ciAttempts + runtimeAttempts,
ciAttempts,
runtimeAttempts,
elapsedMs: Date.now() - startedAt,
budgetSeconds: pac.release.waitTimeoutSeconds,
budgetScope: "per-phase",
source: `${configLabel}.release.waitTimeoutSeconds`,
valuesPrinted: false,
},
summary,
status: current,
gitOpsMirrorFlush,
blocker: ready ? null : closeoutBlocker(current, options.sourceCommit, observedSourceCommit, sourceMatched, gitOpsMirrorFlush),
next: nextCommands(target.id, consumer.id, pac.defaults.consumerId),
valuesPrinted: false,
};
}
async function observeCloseoutStatus(config: UniDeskConfig, options: CloseoutOptions, target: PacTarget, consumer: PacConsumer, phase: "ci" | "runtime", attempt: number, startedAt: number): Promise<Record<string, unknown>> {
printCloseoutProgress(target, consumer, `${phase}-status`, "started", startedAt, { attempt });
const current = await status(config, options);
const summary = record(current.summary);
const latest = record(summary.latestPipelineRun);
const diagnostics = record(summary.diagnostics);
printCloseoutProgress(target, consumer, `${phase}-status`, "observed", startedAt, {
attempt,
ready: current.ok === true,
pipelineRun: latest.name,
sourceCommit: observedCloseoutSourceCommit(latest, diagnostics),
diagnosticCode: diagnostics.code,
});
return current;
}
function printCloseoutProgress(target: PacTarget, consumer: PacConsumer, stage: string, status: string, startedAt: number, detail: Record<string, unknown>): void {
process.stderr.write(`${JSON.stringify({
event: "platform-infra.pipelines-as-code.closeout.progress",
stage,
status,
target: target.id,
consumer: consumer.id,
elapsedMs: Date.now() - startedAt,
...detail,
valuesPrinted: false,
})}\n`);
}
async function history(config: UniDeskConfig, options: HistoryOptions): Promise<Record<string, unknown>> {
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId);
const selection = resolvePacHistoryConsumerSelection(pac.consumers, target.id, options.consumerId, options.detailId);
const selectedConsumers = selection.selectedConsumerIds.map((id) => resolveConsumer(pac, id));
const firstConsumer = selectedConsumers[0];
if (firstConsumer === undefined) throw new Error("no Pipelines-as-Code consumers are configured");
const firstRepository = resolveRepository(pac, firstConsumer.repositoryRef);
const secrets = ensureSecrets(pac, false);
const result = await capture(config, target.route, ["sh"], remoteScript("history", pac, target, firstRepository, firstConsumer, { ...options, confirm: false, dryRun: true, wait: false }, secrets, "", selectedConsumers));
const parsed = parseJsonOutput(result.stdout);
const remote = parsed ?? compactCapture(result, { full: true });
const historyErrors = arrayRecords(record(remote).historyErrors);
return {
ok: result.exitCode === 0 && parsed?.ok !== false && historyErrors.length === 0,
action: "platform-infra-pipelines-as-code-history",
mutation: false,
target: targetSummary(target),
config: {
path: configLabel,
source: "Gitea Repository CR + Tekton PipelineRun/TaskRun live objects; no UniDesk-owned history store is written.",
limitPerConsumer: options.limit,
displayTimeZone: pac.display.timeZone,
valuesPrinted: false,
},
consumers: selectedConsumers.map((consumer) => consumer.id),
detailId: options.detailId,
rows: arrayRecords(record(remote).rows),
historyErrors,
controlPlane: parsed === null ? undefined : {
crdPresent: parsed.crdPresent === true,
controllerReady: parsed.controllerReady,
repositoryCondition: parsed.repositoryCondition,
repository: record(parsed.repository),
consumerRows: arrayRecords(parsed.consumerRows),
webhookCount: arrayRecords(parsed.webhooks).length,
valuesPrinted: false,
},
remote: parsed === null || options.raw ? remote : undefined,
next: selection.nextConsumerId === null
? pacNodeReadOnlyNext(target.id)
: pacReadOnlyNext(target.id, selection.nextConsumerId),
};
}
export function resolvePacHistoryConsumerSelection(
consumers: readonly PacHistoryConsumerIdentity[],
targetId: string,
requestedConsumerId: string | null,
detailId: string | null,
): PacHistoryConsumerSelection {
const targetConsumers = consumers.filter((consumer) => consumer.node.toLowerCase() === targetId.toLowerCase());
if (targetConsumers.length === 0) throw new Error(`no Pipelines-as-Code consumers are configured for target ${targetId}`);
const requestedConsumer = requestedConsumerId === null
? null
: consumers.find((consumer) => consumer.id.toLowerCase() === requestedConsumerId.toLowerCase()) ?? null;
if (requestedConsumerId !== null && requestedConsumer === null) {
throw new Error(`unknown Pipelines-as-Code consumer ${requestedConsumerId}`);
}
if (requestedConsumer !== null && requestedConsumer.node.toLowerCase() !== targetId.toLowerCase()) {
throw new Error(`Pipelines-as-Code consumer ${requestedConsumer.id} belongs to ${requestedConsumer.node}, not target ${targetId}`);
}
if (detailId !== null) {
const matches = targetConsumers.filter((consumer) => detailId.startsWith(consumer.pipelineRunPrefix));
if (matches.length !== 1) {
throw new Error(`PipelineRun ${detailId} must match exactly one consumer on target ${targetId}; matched ${matches.length}`);
}
const matched = matches[0] as PacHistoryConsumerIdentity;
if (requestedConsumer !== null && requestedConsumer.id.toLowerCase() !== matched.id.toLowerCase()) {
throw new Error(`PipelineRun ${detailId} belongs to ${matched.id}, not requested consumer ${requestedConsumer.id}`);
}
return { selectedConsumerIds: [matched.id], nextConsumerId: matched.id };
}
if (requestedConsumer !== null) {
return { selectedConsumerIds: [requestedConsumer.id], nextConsumerId: requestedConsumer.id };
}
return {
selectedConsumerIds: targetConsumers.map((consumer) => consumer.id),
nextConsumerId: targetConsumers.length === 1 ? (targetConsumers[0] as PacHistoryConsumerIdentity).id : null,
};
}
async function debugStep(config: UniDeskConfig, options: HistoryOptions): Promise<Record<string, unknown>> {
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId);
const consumer = resolveConsumer(pac, options.consumerId);
const repository = resolveRepository(pac, consumer.repositoryRef);
const result = await capture(config, target.route, ["sh"], remoteScript("debug-step", pac, target, repository, consumer, { ...options, confirm: false, dryRun: true, wait: false }, emptySecretMaterial(), ""));
const parsed = parseJsonOutput(result.stdout);
return {
ok: result.exitCode === 0 && parsed?.ok === true,
action: "platform-infra-pipelines-as-code-debug-step",
mutation: false,
target: targetSummary(target),
consumer: consumer.id,
detailId: options.detailId,
evaluator: "scripts/native/cicd/pac-status-evaluator.cjs",
checks: parsed === null ? [] : arrayRecords(parsed.checks),
realRun: parsed === null ? null : parsed.realRun ?? null,
remote: parsed === null || options.raw ? parsed ?? compactCapture(result, { full: true }) : undefined,
next: nextCommands(target.id, consumer.id, pac.defaults.consumerId),
valuesPrinted: false,
};
}
async function fetchReleaseManifest(pac: PacConfig): Promise<string> {
const response = await fetch(pac.release.manifestUrl);
if (!response.ok) throw new Error(`failed to fetch ${pac.release.manifestUrl}: HTTP ${response.status}`);
const text = await response.text();
if (!text.includes("repositories.pipelinesascode.tekton.dev")) throw new Error(`${pac.release.manifestUrl} did not contain the Repository CRD`);
return text;
}
function remoteScript(action: "apply" | "status" | "history" | "debug-step", pac: PacConfig, target: PacTarget, repository: PacRepository, consumer: PacConsumer, options: ApplyOptions | HistoryOptions, secrets: SecretMaterial, releaseManifest: string, historyConsumers: PacConsumer[] = [consumer]): string {
const webhookUrl = `${pac.gitea.internalBaseUrl.replace(/\/+$/u, "").replace(/gitea-http\.[^.]+\.svc\.cluster\.local:3000/u, `${pac.release.controllerServiceName}.${pac.release.namespace}.svc.cluster.local:${pac.release.controllerServicePort}`)}`;
const env: Record<string, string> = {
UNIDESK_PAC_ACTION: action,
UNIDESK_PAC_EVALUATOR_B64: Buffer.from(readFileSync(evaluatorFile, "utf8"), "utf8").toString("base64"),
UNIDESK_PAC_FIELD_MANAGER: fieldManager,
UNIDESK_PAC_TARGET_ID: target.id,
UNIDESK_PAC_TARGET_NAMESPACE: consumer.namespace,
UNIDESK_PAC_CONSUMER_ID: consumer.id,
UNIDESK_PAC_RELEASE_NAMESPACE: pac.release.namespace,
UNIDESK_PAC_RELEASE_MANIFEST_B64: Buffer.from(releaseManifest, "utf8").toString("base64"),
UNIDESK_PAC_CONTROLLER_SERVICE_NAME: pac.release.controllerServiceName,
UNIDESK_PAC_WAIT_TIMEOUT_SECONDS: String(pac.release.waitTimeoutSeconds),
UNIDESK_PAC_DRY_RUN: "dryRun" in options && options.dryRun ? "1" : "0",
UNIDESK_PAC_GITEA_BASE_URL: pac.gitea.internalBaseUrl.replace(/\/+$/u, ""),
UNIDESK_PAC_GITEA_ADMIN_USERNAME: secrets.adminUsername,
UNIDESK_PAC_GITEA_ADMIN_PASSWORD: secrets.adminPassword,
UNIDESK_PAC_GITEA_API_USERNAME: pac.gitea.admin.apiUsername,
UNIDESK_PAC_GITEA_OWNER: repository.owner,
UNIDESK_PAC_GITEA_REPO: repository.repo,
UNIDESK_PAC_WEBHOOK_URL: webhookUrl,
UNIDESK_PAC_WEBHOOK_SECRET: secrets.webhookSecret,
UNIDESK_PAC_REPOSITORY_NAME: repository.name,
UNIDESK_PAC_REPOSITORY_URL: repository.url,
UNIDESK_PAC_SECRET_NAME: repository.secretName,
UNIDESK_PAC_TOKEN_KEY: repository.tokenKey,
UNIDESK_PAC_WEBHOOK_SECRET_KEY: repository.webhookSecretKey,
UNIDESK_PAC_CONCURRENCY_LIMIT: String(repository.concurrencyLimit),
UNIDESK_PAC_PARAMS_JSON: JSON.stringify(repository.params),
UNIDESK_PAC_PIPELINE_NAME: consumer.pipeline,
UNIDESK_PAC_PIPELINE_RUN_PREFIX: consumer.pipelineRunPrefix,
UNIDESK_PAC_HISTORY_LIMIT: "limit" in options ? String(options.limit) : "5",
UNIDESK_PAC_HISTORY_ID: "detailId" in options && options.detailId !== null ? options.detailId : "",
UNIDESK_PAC_HISTORY_CONSUMERS_B64: Buffer.from(JSON.stringify(historyConsumers.map((item) => historyConsumerConfig(pac, item))), "utf8").toString("base64"),
UNIDESK_PAC_DISPLAY_TIME_ZONE: pac.display.timeZone,
UNIDESK_PAC_ARGO_NAMESPACE: consumer.argoNamespace,
UNIDESK_PAC_ARGO_APPLICATION: consumer.argoApplication,
UNIDESK_PAC_ARGO_BOOTSTRAP_MANIFEST_B64: Buffer.from(argoBootstrapManifest(consumer), "utf8").toString("base64"),
UNIDESK_PAC_PART_OF: pacPartOf(consumer.id),
UNIDESK_PAC_SPEC: kubernetesLabelValue(pac.metadata.relatedIssues.includes(1555) ? "GH-1552-GH-1555" : pac.metadata.spec),
};
const exports = Object.entries(env).map(([key, value]) => `export ${key}=${shQuote(value)}`).join("\n");
return `${exports}\n${readFileSync(remoteScriptFile, "utf8")}`;
}
function historyConsumerConfig(pac: PacConfig, consumer: PacConsumer): Record<string, unknown> {
const repository = resolveRepository(pac, consumer.repositoryRef);
return {
id: consumer.id,
namespace: consumer.namespace,
pipeline: consumer.pipeline,
pipelineRunPrefix: consumer.pipelineRunPrefix,
repository: repository.name,
repo: `${repository.owner}/${repository.repo}`,
repoUrl: repository.url,
};
}
function credentialPath(root: string, sourceRef: string): string {
return sourceRef.startsWith("/") ? sourceRef : join(root, sourceRef);
}
function pacPartOf(consumerId: string): string {
if (consumerId.startsWith("platform-infra-")) return "platform-infra";
if (consumerId === "unidesk-host") return "unidesk-host";
if (consumerId.startsWith("sentinel")) return "hwlab-web-probe-sentinel";
if (consumerId.startsWith("hwlab-")) return "hwlab";
return "agentrun";
}
function argoBootstrapManifest(consumer: PacConsumer): string {
const bootstrap = consumer.argoBootstrap;
if (bootstrap === null) return "";
const application = {
apiVersion: "argoproj.io/v1alpha1",
kind: "Application",
metadata: {
name: consumer.argoApplication,
namespace: consumer.argoNamespace,
labels: {
"app.kubernetes.io/managed-by": "unidesk",
"app.kubernetes.io/part-of": pacPartOf(consumer.id),
},
},
spec: {
project: bootstrap.project,
source: {
repoURL: bootstrap.repoUrl,
targetRevision: bootstrap.targetRevision,
path: bootstrap.path,
},
destination: {
server: "https://kubernetes.default.svc",
namespace: bootstrap.destinationNamespace,
},
syncPolicy: bootstrap.automated ? {
automated: { prune: true, selfHeal: true },
syncOptions: ["CreateNamespace=true"],
} : undefined,
},
};
return `${Bun.YAML.stringify(application).trim()}\n`;
}
function parseLinePair(path: string, usernameLine: number, passwordLine: number): { username: string; password: string } {
const lines = readFileSync(path, "utf8").split(/\r?\n/u);
const username = lines[usernameLine - 1]?.trim() ?? "";
const password = lines[passwordLine - 1]?.trim() ?? "";
if (username.length === 0 || password.length === 0) throw new Error(`${path} must contain username on line ${usernameLine} and password on line ${passwordLine}`);
return { username, password };
}
function emptySecretMaterial(): SecretMaterial {
return {
adminUsername: "",
adminPassword: "",
adminFingerprint: "",
webhookSecret: "",
webhookFingerprint: "",
webhookPath: "",
};
}
function ensureSecrets(pac: PacConfig, createWebhookSecret: boolean, allowMissingWebhookSecret = false): SecretMaterial {
const adminPath = credentialPath(pac.gitea.admin.sourceRoot, pac.gitea.admin.sourceRef);
if (!existsSync(adminPath)) throw new Error(`${pac.gitea.admin.sourceRef} is missing`);
const admin = parseLinePair(adminPath, pac.gitea.admin.usernameLine, pac.gitea.admin.passwordLine);
const webhookPath = credentialPath(pac.gitea.webhook.secretRoot, pac.gitea.webhook.secretSourceRef);
if (!existsSync(webhookPath)) {
if (allowMissingWebhookSecret) {
const placeholder = "dry-run-webhook-secret-placeholder";
return {
adminUsername: admin.username,
adminPassword: admin.password,
adminFingerprint: secretFingerprint(`${admin.username}\0${admin.password}`),
webhookSecret: placeholder,
webhookFingerprint: secretFingerprint(placeholder),
webhookPath,
};
}
if (!createWebhookSecret) throw new Error(`${pac.gitea.webhook.secretSourceRef} is missing; run apply --confirm to create it`);
mkdirSync(dirname(webhookPath), { recursive: true });
writeFileSync(webhookPath, `${randomBytes(32).toString("hex")}\n`, { encoding: "utf8", mode: 0o600 });
chmodSync(webhookPath, 0o600);
}
const webhookSecret = readFileSync(webhookPath, "utf8").trim();
if (webhookSecret.length < 32) throw new Error(`${pac.gitea.webhook.secretSourceRef} must contain at least 32 chars`);
return {
adminUsername: admin.username,
adminPassword: admin.password,
adminFingerprint: secretFingerprint(`${admin.username}\0${admin.password}`),
webhookSecret,
webhookFingerprint: secretFingerprint(webhookSecret),
webhookPath,
};
}
function secretSummaries(pac: PacConfig): Array<Record<string, unknown>> {
const adminPath = credentialPath(pac.gitea.admin.sourceRoot, pac.gitea.admin.sourceRef);
const webhookPath = credentialPath(pac.gitea.webhook.secretRoot, pac.gitea.webhook.secretSourceRef);
const admin = existsSync(adminPath) ? parseLinePair(adminPath, pac.gitea.admin.usernameLine, pac.gitea.admin.passwordLine) : null;
const webhookSecret = existsSync(webhookPath) ? readFileSync(webhookPath, "utf8").trim() : "";
return [
{
id: "gitea-admin",
sourceRef: pac.gitea.admin.sourceRef,
sourcePath: adminPath,
present: admin !== null,
fingerprint: admin === null ? null : secretFingerprint(`${admin.username}\0${admin.password}`),
valuesPrinted: false,
},
{
id: "pac-webhook",
sourceRef: pac.gitea.webhook.secretSourceRef,
sourcePath: webhookPath,
present: webhookSecret.length > 0,
fingerprint: webhookSecret.length > 0 ? secretFingerprint(webhookSecret) : null,
valuesPrinted: false,
},
];
}
function statusSummary(payload: Record<string, unknown>): Record<string, unknown> {
const pipelineRuns = arrayRecords(payload.pipelineRuns);
const latest = pipelineRuns[0] ?? {};
const taskRuns = arrayRecords(payload.taskRuns);
const argo = record(payload.argo);
const runtime = record(payload.runtime);
const diagnostics = record(payload.diagnostics);
const webhooks = arrayRecords(payload.webhooks);
const rawArtifact = record(payload.artifact);
const sourceObservation = record(rawArtifact.sourceObservation);
const provenance = record(diagnostics.provenance);
const noRuntimeChange = stringValue(sourceObservation.mode) === "no-runtime-change"
&& sourceObservation.valid === true
&& stringValue(diagnostics.code) === "pac-ready-no-runtime-change";
const artifact = {
...rawArtifact,
imageStatus: noRuntimeChange ? "retained" : rawArtifact.imageStatus ?? (runtime.image === undefined ? undefined : "runtime"),
digest: noRuntimeChange ? provenance.runtimeDigest ?? runtime.digest : rawArtifact.digest ?? runtime.digest,
gitopsCommit: noRuntimeChange ? provenance.gitopsRevision ?? argo.revision : rawArtifact.gitopsCommit ?? argo.revision,
provenanceRelation: noRuntimeChange ? "retained" : record(argo.revisionRelation).relation,
};
const pipelineGate = pipelineRunGate(latest);
return {
ready: payload.crdPresent === true && String(payload.controllerReady ?? "0/0") !== "0/0" && pipelineGate.ok && diagnostics.ok !== false,
crdPresent: payload.crdPresent === true,
controllerReady: payload.controllerReady,
repositoryCondition: payload.repositoryCondition,
repository: record(payload.repository),
webhooks,
webhookCount: webhooks.length,
latestPipelineRun: latest,
taskRuns,
pipelineRunGate: pipelineGate,
artifact,
sourceObservation: Object.keys(sourceObservation).length === 0 ? null : sourceObservation,
provenance: Object.keys(provenance).length === 0 ? null : provenance,
argo,
runtime,
diagnostics,
valuesPrinted: false,
};
}
function nodeStatusRow(targetId: string, consumer: PacConsumer, repository: PacRepository, current: Record<string, unknown>): Record<string, unknown> {
const summary = record(current.summary);
const latest = record(summary.latestPipelineRun);
const artifact = record(summary.artifact);
const argo = record(summary.argo);
const runtime = record(summary.runtime);
const diagnostics = record(summary.diagnostics);
const pipelineGate = record(summary.pipelineRunGate);
const deliveryDisabled = stringValue(artifact.imageStatus) === "disabled";
const desired = numericValue(runtime.replicas);
const readyReplicas = numericValue(runtime.readyReplicas);
const runtimeReady = deliveryDisabled || (desired !== null && desired > 0 && readyReplicas === desired);
const argoReady = deliveryDisabled || (stringValue(argo.sync) === "Synced" && stringValue(argo.health) === "Healthy");
const ciReady = pipelineGate.ok === true;
const diagnosticsReady = diagnostics.ok !== false;
const ready = current.ok === true && ciReady && argoReady && runtimeReady && diagnosticsReady;
return {
consumer: consumer.id,
node: consumer.node,
lane: consumer.lane,
repository: `${repository.owner}/${repository.repo}`,
namespace: consumer.namespace,
pipeline: consumer.pipeline,
argoApplication: consumer.argoApplication,
ready,
ciReady,
argoReady,
runtimeReady,
diagnosticsReady,
latestPipelineRun: stringValue(latest.name),
pipelineStatus: statusText(latest),
durationSeconds: latest.durationSeconds ?? null,
sourceCommit: stringValue(latest.sourceCommit),
imageStatus: stringValue(artifact.imageStatus),
envReuse: envReuseText(record(artifact.envReuse)),
digest: stringValue(artifact.digest),
gitopsCommit: stringValue(artifact.gitopsCommit),
argo: {
sync: stringValue(argo.sync),
health: stringValue(argo.health),
revision: stringValue(argo.revision),
repoURL: stringValue(argo.repoURL),
targetRevision: stringValue(argo.targetRevision),
revisionRelation: record(argo.revisionRelation),
},
runtime: {
deployment: stringValue(runtime.deployment),
readyReplicas: runtime.readyReplicas ?? null,
replicas: runtime.replicas ?? null,
digest: stringValue(runtime.digest),
image: stringValue(runtime.image),
},
diagnostics: {
ok: diagnostics.ok !== false,
code: stringValue(diagnostics.code),
phase: stringValue(diagnostics.phase),
hint: compactLine(stringValue(diagnostics.hint)),
},
reason: ready ? "ready" : nodeStatusReason({ ciReady, argoReady, runtimeReady, diagnosticsReady, pipelineStatus: statusText(latest), diagnostics }),
statusCommand: `bun scripts/cli.ts platform-infra pipelines-as-code status --target ${targetId} --consumer ${consumer.id}`,
historyCommand: `bun scripts/cli.ts platform-infra pipelines-as-code history --target ${targetId} --consumer ${consumer.id} --limit 10`,
valuesPrinted: false,
};
}
function nodeStatusReason(input: { ciReady: boolean; argoReady: boolean; runtimeReady: boolean; diagnosticsReady: boolean; pipelineStatus: string; diagnostics: Record<string, unknown> }): string {
if (!input.ciReady) return `ci:${input.pipelineStatus}`;
if (!input.argoReady) return "argo-not-ready";
if (!input.runtimeReady) return "runtime-not-ready";
if (!input.diagnosticsReady) return stringValue(input.diagnostics.code, "diagnostics-not-ready");
return "not-ready";
}
function policyChecks(repository: PacRepository): Array<Record<string, unknown>> {
return [
{ name: "single-path", ok: true, detail: "Gitea webhook -> Pipelines-as-Code -> Tekton -> Argo/k8s runtime; no Gitea Actions/act_runner/branch-follower fallback." },
{ name: "yaml-source-of-truth", ok: true, detail: `${configLabel} owns PaC release, Repository CR, Gitea webhook and consumer params.` },
{ name: "public-repository-url", ok: !isInternalRepositoryMatchUrl(repository.url), detail: "Repository CR url matches the public Gitea webhook payload URL; internal service URLs remain clone/read URLs only." },
{ name: "gitea-internal-source", ok: repository.cloneUrl.includes(".svc.cluster.local"), detail: "Tekton source clone uses the internal k3s Gitea service URL." },
{ name: "runtime-zero-docker", ok: true, detail: "Runtime starts at k8s pulling already-built images; CI build may use Tekton/BuildKit." },
];
}
function isInternalRepositoryMatchUrl(value: string): boolean {
const parsed = new URL(value);
return parsed.hostname.endsWith(".svc.cluster.local")
|| parsed.hostname === "localhost"
|| parsed.hostname === "127.0.0.1"
|| /^10\./u.test(parsed.hostname)
|| /^192\.168\./u.test(parsed.hostname)
|| /^172\.(1[6-9]|2\d|3[0-1])\./u.test(parsed.hostname);
}
function targetSummary(target: PacTarget): Record<string, unknown> {
return { id: target.id, route: target.route, namespace: target.namespace, role: target.role };
}
function configSummary(pac: PacConfig, consumer: PacConsumer, repository: PacRepository): Record<string, unknown> {
return {
path: configLabel,
metadata: pac.metadata,
release: pac.release,
capabilities: capabilitySummary(pac),
gitea: { configRef: pac.gitea.configRef, internalBaseUrl: pac.gitea.internalBaseUrl, webhookBranch: pac.gitea.webhook.branch },
display: pac.display,
repositories: pac.repositories.map(repositorySummary),
consumers: pac.consumers.map(consumerObservationSummary),
repository: repositorySummary(repository),
consumer,
valuesPrinted: false,
};
}
function compactConfigSummary(pac: PacConfig, consumer: PacConsumer, repository: PacRepository): Record<string, unknown> {
return {
path: configLabel,
releaseVersion: pac.release.version,
capabilities: capabilitySummary(pac),
repository: repository.name,
providerType: repository.providerType,
sourceUrl: repository.cloneUrl,
displayTimeZone: pac.display.timeZone,
consumer: `${consumer.node}/${consumer.lane}`,
consumerId: consumer.id,
pipeline: consumer.pipeline,
valuesPrinted: false,
};
}
function capabilitySummary(pac: PacConfig): Record<string, unknown> {
return {
sentinelInternalPublish: {
...pac.capabilities.sentinelInternalPublish,
configRef: `${configLabel}#capabilities.sentinelInternalPublish`,
valuesPrinted: false,
},
valuesPrinted: false,
};
}
function repositorySummary(repository: PacRepository): Record<string, unknown> {
return {
id: repository.id,
name: repository.name,
namespace: repository.namespace,
providerType: repository.providerType,
url: repository.url,
cloneUrl: repository.cloneUrl,
owner: repository.owner,
repo: repository.repo,
secretName: repository.secretName,
concurrencyLimit: repository.concurrencyLimit,
params: Object.keys(repository.params).sort(),
};
}
function consumerObservationSummary(consumer: PacConsumer): Record<string, unknown> {
return {
id: consumer.id,
node: consumer.node,
lane: consumer.lane,
namespace: consumer.namespace,
pipeline: consumer.pipeline,
pipelineRunPrefix: consumer.pipelineRunPrefix,
argoNamespace: consumer.argoNamespace,
argoApplication: consumer.argoApplication,
repositoryRef: consumer.repositoryRef,
valuesPrinted: false,
};
}
function consumerCoverage(pac: PacConfig, targetId: string): Record<string, unknown>[] {
return pac.consumers.filter((consumer) => consumer.node.toLowerCase() === targetId.toLowerCase()).map((consumer) => {
const repository = resolveRepository(pac, consumer.repositoryRef);
return {
consumer: consumer.id,
source: `${repository.owner}/${repository.repo}@${repository.params.source_branch ?? "-"}`,
namespace: consumer.namespace,
pipeline: consumer.pipeline,
argoApplication: consumer.argoApplication,
statusCommand: `bun scripts/cli.ts platform-infra pipelines-as-code status --target ${targetId} --consumer ${consumer.id}`,
historyCommand: `bun scripts/cli.ts platform-infra pipelines-as-code history --target ${targetId} --consumer ${consumer.id}`,
};
});
}
function nextCommands(targetId: string, consumerId: string, _defaultConsumerId: string): Record<string, unknown> {
return pacReadOnlyNext(targetId, consumerId);
}
function compactPlanJson(result: Record<string, unknown>): Record<string, unknown> {
const config = record(result.config);
const consumer = record(result.consumer);
return {
ok: result.ok === true,
action: result.action,
mutation: false,
target: result.target,
config: {
path: config.path,
metadata: config.metadata,
release: config.release,
capabilities: config.capabilities,
gitea: config.gitea,
display: config.display,
valuesPrinted: false,
},
repository: result.repository,
consumer: {
id: consumer.id,
node: consumer.node,
lane: consumer.lane,
namespace: consumer.namespace,
pipeline: consumer.pipeline,
pipelineRunPrefix: consumer.pipelineRunPrefix,
argoNamespace: consumer.argoNamespace,
argoApplication: consumer.argoApplication,
repositoryRef: consumer.repositoryRef,
},
secrets: result.secrets,
policy: result.policy,
next: result.next,
valuesPrinted: false,
};
}
function compactStatusJson(result: Record<string, unknown>): Record<string, unknown> {
const consumer = record(result.consumer);
return {
ok: result.ok === true,
action: result.action,
mutation: false,
target: result.target,
consumer: {
id: consumer.id,
node: consumer.node,
lane: consumer.lane,
namespace: consumer.namespace,
pipeline: consumer.pipeline,
argoApplication: consumer.argoApplication,
},
deliveryAuthority: result.deliveryAuthority,
capabilities: record(result.config).capabilities,
summary: compactStatusSummary(record(result.summary)),
next: result.next,
valuesPrinted: false,
};
}
function compactStatusSummary(summary: Record<string, unknown>): Record<string, unknown> {
const latest = record(summary.latestPipelineRun);
const artifact = record(summary.artifact);
const argo = record(summary.argo);
const runtime = record(summary.runtime);
const diagnostics = record(summary.diagnostics);
const taskRuns = arrayRecords(summary.taskRuns);
const longest = longestTaskRun(taskRuns);
return {
ready: summary.ready === true,
crdPresent: summary.crdPresent === true,
controllerReady: summary.controllerReady,
repositoryCondition: summary.repositoryCondition,
webhookCount: summary.webhookCount,
latestPipelineRun: {
name: latest.name,
status: latest.status,
reason: latest.reason,
durationSeconds: latest.durationSeconds,
sourceCommit: latest.sourceCommit,
},
taskRuns: {
count: taskRuns.length,
longest: longest === null ? null : {
name: longest.name,
status: longest.status,
reason: longest.reason,
durationSeconds: longest.durationSeconds,
},
},
pipelineRunGate: summary.pipelineRunGate,
sourceObservation: summary.sourceObservation,
artifact: {
imageStatus: artifact.imageStatus,
envReuse: artifact.envReuse,
envIdentity: artifact.envIdentity,
digest: artifact.digest,
gitopsCommit: artifact.gitopsCommit,
provenanceRelation: artifact.provenanceRelation,
},
argo: {
sync: argo.sync,
health: argo.health,
revision: argo.revision,
repoURL: argo.repoURL,
targetRevision: argo.targetRevision,
revisionRelation: argo.revisionRelation,
},
runtime: {
deployment: runtime.deployment,
readyReplicas: runtime.readyReplicas,
replicas: runtime.replicas,
image: runtime.image,
digest: runtime.digest,
},
diagnostics: {
ok: diagnostics.ok !== false,
code: diagnostics.code,
phase: diagnostics.phase,
hint: diagnostics.hint,
deliveryMode: diagnostics.deliveryMode,
registry: diagnostics.registry,
},
provenance: summary.provenance,
valuesPrinted: false,
};
}
function compactCloseoutJson(result: Record<string, unknown>): Record<string, unknown> {
const consumer = record(result.consumer);
const flush = record(result.gitOpsMirrorFlush);
return {
ok: result.ok === true,
action: result.action,
mutation: result.mutation === true,
target: result.target,
consumer: { id: consumer.id, node: consumer.node, lane: consumer.lane },
sourceCommit: result.sourceCommit,
observedSourceCommit: result.observedSourceCommit,
sourceMatched: result.sourceMatched === true,
ready: result.ready === true,
wait: result.wait,
summary: compactStatusSummary(record(result.summary)),
gitOpsMirrorFlush: {
ok: flush.ok !== false,
enabled: flush.enabled,
required: flush.required,
applicable: flush.applicable,
mode: flush.mode,
executed: flush.executed === true,
reason: flush.reason,
},
blocker: result.blocker,
next: result.next,
valuesPrinted: false,
};
}
function compactHistoryJson(result: Record<string, unknown>, includeTaskDetails = false): Record<string, unknown> {
const detailRequested = stringValue(result.detailId) !== "-";
return {
ok: result.ok === true,
action: result.action,
mutation: false,
target: result.target,
config: result.config,
consumers: result.consumers,
detailId: result.detailId,
rows: arrayRecords(result.rows).map((row) => {
const taskRuns = record(row.taskRuns);
const artifact = record(row.artifact);
const catalog = record(artifact.catalog);
const collector = record(row.collector);
return {
id: row.id ?? row.pipelineRun,
consumer: row.consumer,
repo: row.repo,
pipeline: row.pipeline,
triggeredAt: row.triggeredAt,
startTime: row.startTime,
completionTime: row.completionTime,
displayTime: row.displayTime,
displayTimeZone: row.displayTimeZone,
durationSeconds: row.durationSeconds,
status: row.status,
reason: row.reason,
commit: row.commit,
branch: row.branch,
envReuse: row.envReuse,
sourceObservation: row.sourceObservation,
artifact: detailRequested ? {
imageStatus: artifact.imageStatus,
envReuse: artifact.envReuse,
digest: artifact.digest,
digests: artifact.digests,
gitopsCommit: artifact.gitopsCommit,
sourceCommit: artifact.sourceCommit,
action: artifact.action,
reason: artifact.reason,
catalog: {
present: catalog.present === true,
status: catalog.status,
sourceCommit: catalog.sourceCommit,
registryVerified: catalog.registryVerified === true,
publishedCount: catalog.publishedCount,
reusedCount: catalog.reusedCount,
requiredServiceCount: catalog.requiredServiceCount,
digestCount: catalog.digestCount,
source: catalog.source,
valuesPrinted: false,
},
valuesPrinted: false,
} : undefined,
collector: detailRequested ? {
mode: collector.mode,
contractTaskCount: collector.contractTaskCount,
terminalRecordCount: collector.terminalRecordCount,
logErrorCount: collector.logErrorCount,
logErrors: collector.logErrors,
valuesPrinted: false,
} : undefined,
taskRuns: {
count: taskRuns.count,
longest: taskRuns.longest,
failed: taskRuns.failed,
details: includeTaskDetails ? taskRuns.details : undefined,
logsCommand: taskRuns.logsCommand,
},
};
}),
historyErrors: result.historyErrors,
next: result.next,
valuesPrinted: false,
};
}
function renderPlan(result: Record<string, unknown>): RenderedCliResult {
const target = record(result.target);
const config = record(result.config);
const repository = record(result.repository);
const consumer = record(result.consumer);
const internalPublish = record(record(config.capabilities).sentinelInternalPublish);
const secrets = arrayRecords(result.secrets);
const policy = arrayRecords(result.policy);
const lines = [
"PLATFORM-INFRA PIPELINES-AS-CODE PLAN",
...table(["TARGET", "NAMESPACE", "RELEASE", "REPOSITORY", "CONSUMER"], [[stringValue(target.id), stringValue(repository.namespace), stringValue(record(config.release).version), stringValue(repository.name), stringValue(consumer.id)]]),
"",
"PATH",
...table(["STAGE", "AUTHORITY", "DETAIL"], [
["source", "Gitea", stringValue(repository.cloneUrl)],
["trigger", "Pipelines-as-Code", "Gitea push webhook"],
["ci", "Tekton", stringValue(consumer.pipeline)],
["cd", "Argo", stringValue(consumer.argoApplication)],
]),
"",
"SECRETS",
...table(["ID", "PRESENT", "FINGERPRINT", "VALUES"], secrets.map((item) => [stringValue(item.id), boolText(item.present), stringValue(item.fingerprint), "false"])),
"",
"POLICY",
...table(["NAME", "OK", "DETAIL"], policy.map((item) => [stringValue(item.name), boolText(item.ok), stringValue(item.detail)])),
"",
"CAPABILITIES",
...table(["NAME", "ENABLED", "PROVENANCE", "TRACKING"], [[
"sentinelInternalPublish",
boolText(internalPublish.enabled),
stringValue(internalPublish.admissionProvenance),
stringValue(internalPublish.trackingIssue),
]]),
"",
"READ-ONLY NEXT",
` status: ${stringValue(record(result.next).status)}`,
` history: ${stringValue(record(result.next).history)}`,
];
return rendered(result, "platform-infra pipelines-as-code plan", lines);
}
function renderApply(result: Record<string, unknown>): RenderedCliResult {
const target = record(result.target);
const remote = record(result.remote);
const errorText = compactLine(stringValue(remote.stderrTail, stringValue(remote.error, stringValue(result.error, ""))));
const lines = [
"PLATFORM-INFRA PIPELINES-AS-CODE APPLY",
...table(["TARGET", "MODE", "MUTATION", "OK"], [[stringValue(target.id), stringValue(result.mode), boolText(result.mutation), boolText(result.ok)]]),
"",
"REMOTE",
...table(["CONTROLLER", "WEBHOOK", "REPOSITORY", "EXIT", "VALUES"], [[stringValue(remote.controllerReady), stringValue(record(remote.webhook).present), stringValue(remote.repository), stringValue(remote.exitCode, "0"), "false"]]),
...(result.ok === false && errorText.length > 0 ? ["", `ERROR ${errorText}`] : []),
"",
"NEXT",
` status: ${stringValue(record(result.next).status)}`,
];
return rendered(result, "platform-infra pipelines-as-code apply", lines);
}
function renderStatus(result: Record<string, unknown>): RenderedCliResult {
const summary = record(result.summary);
const consumer = record(result.consumer);
const deliveryAuthority = record(result.deliveryAuthority);
const internalPublish = record(record(record(result.config).capabilities).sentinelInternalPublish);
const coverage = arrayRecords(result.coverage);
const latest = record(summary.latestPipelineRun);
const taskRuns = arrayRecords(summary.taskRuns);
const artifact = record(summary.artifact);
const argo = record(summary.argo);
const diagnostics = record(summary.diagnostics);
const sourceObservation = record(summary.sourceObservation);
const deliveryPlan = record(sourceObservation.plan);
const repository = record(summary.repository);
const webhooks = arrayRecords(summary.webhooks);
const lines = [
"PLATFORM-INFRA PIPELINES-AS-CODE STATUS",
...(coverage.length === 0 ? [] : [
"COVERAGE",
...table(["CONSUMER", "SOURCE", "NAMESPACE", "PIPELINE", "ARGO_APP"], coverage.map((item) => [stringValue(item.consumer), stringValue(item.source), stringValue(item.namespace), stringValue(item.pipeline), stringValue(item.argoApplication)])),
"",
]),
...table(["CONSUMER", "READY", "AUTHORITY", "CRD", "CONTROLLER", "WEBHOOKS", "REPO_URL"], [[stringValue(consumer.id), boolText(summary.ready), stringValue(deliveryAuthority.kind), boolText(summary.crdPresent), stringValue(summary.controllerReady), stringValue(summary.webhookCount), short(stringValue(repository.url), 56)]]),
` repository-condition: ${compactLine(stringValue(summary.repositoryCondition))}`,
` sentinel-internal-publish: enabled=${boolText(internalPublish.enabled)} provenance=${stringValue(internalPublish.admissionProvenance)} tracking=${stringValue(internalPublish.trackingIssue)}`,
"",
"GITEA HOOKS",
...(webhooks.length === 0 ? ["-"] : table(["HOOK", "ACTIVE", "EVENTS", "URL"], webhooks.map((item) => [stringValue(item.id), boolText(item.active), Array.isArray(item.events) ? item.events.join(",") : stringValue(item.events), short(stringValue(item.url), 56)]))),
"",
"LATEST PIPELINERUN",
...table(["NAME", "STATUS", "REASON", "DURATION_S", "SOURCE"], [[stringValue(latest.name), stringValue(latest.status), stringValue(latest.reason), stringValue(latest.durationSeconds), short(stringValue(latest.sourceCommit))]]),
"",
"DELIVERY OBSERVATION",
...table(["MODE", "VALID", "SOURCE_MATCHED", "AFFECTED", "ROLLOUT", "BUILD", "REUSED", "REASON"], [[
stringValue(sourceObservation.mode),
boolText(sourceObservation.valid),
boolText(sourceObservation.sourceMatched ?? (stringValue(sourceObservation.sourceCommit) === stringValue(latest.sourceCommit))),
stringValue(deliveryPlan.affectedServiceCount),
stringValue(deliveryPlan.rolloutServiceCount),
stringValue(deliveryPlan.buildServiceCount),
stringValue(deliveryPlan.reusedServiceCount),
stringValue(sourceObservation.reason),
]]),
"",
"TASKRUN DURATIONS",
...(taskRuns.length === 0 ? ["-"] : table(["TASKRUN", "STATUS", "REASON", "FAILED_STEP", "EXIT", "DURATION_S"], taskRuns.map((item) => {
const failedStep = record(item.failedStep);
return [short(stringValue(item.name), 48), stringValue(item.status), stringValue(item.reason), stringValue(failedStep.name), stringValue(failedStep.exitCode), stringValue(item.durationSeconds)];
}))),
"",
"IMAGE / GITOPS",
...table(["IMAGE_STATUS", "ENV_REUSE", "ENV_ID", "DIGEST", "GITOPS"], [[stringValue(artifact.imageStatus), stringValue(artifact.envReuse), stringValue(artifact.envIdentity), short(stringValue(artifact.digest), 18), short(stringValue(artifact.gitopsCommit))]]),
"",
"ARGO",
...table(["SYNC", "HEALTH", "REVISION", "RELATION", "TARGET_REVISION"], [[
stringValue(argo.sync),
stringValue(argo.health),
short(stringValue(argo.revision)),
stringValue(record(argo.revisionRelation).relation),
stringValue(argo.targetRevision),
]]),
` repo-url: ${compactLine(stringValue(argo.repoURL))}`,
"",
"CICD DIAGNOSIS",
...table(["OK", "CODE", "PHASE", "SOURCE", "REGISTRY", "GITOPS", "RUNTIME"], [[
boolText(diagnostics.ok !== false),
stringValue(diagnostics.code),
stringValue(diagnostics.phase),
short(stringValue(diagnostics.sourceCommit)),
registryText(record(diagnostics.registry)),
short(stringValue(record(diagnostics.gitops).commit)),
runtimeText(record(diagnostics.runtime)),
]]),
` hint: ${compactLine(stringValue(diagnostics.hint))}`,
` pipeline-run: ${latest.name === undefined ? "-" : `bun scripts/cli.ts platform-infra pipelines-as-code history --target ${stringValue(record(result.target).id)} --id ${stringValue(latest.name)}`}`,
"",
"NEXT",
` full: ${stringValue(record(result.next).status)} --full`,
` all-history: ${stringValue(record(result.next).history).replace(/ --consumer [^ ]+/u, "")} --limit 10`,
` fix-automatic-delivery: ${stringValue(record(record(result.next).fixAutomaticDelivery).reference)}`,
];
return rendered(result, "platform-infra pipelines-as-code status", lines);
}
function renderCloseout(result: Record<string, unknown>): RenderedCliResult {
const target = record(result.target);
const consumer = record(result.consumer);
const wait = record(result.wait);
const summary = record(result.summary);
const latest = record(summary.latestPipelineRun);
const artifact = record(summary.artifact);
const argo = record(summary.argo);
const runtime = record(summary.runtime);
const diagnostics = record(summary.diagnostics);
const sourceObservation = record(summary.sourceObservation);
const blocker = record(result.blocker);
const gitOpsMirrorFlush = record(result.gitOpsMirrorFlush);
const lines = [
"PLATFORM-INFRA PIPELINES-AS-CODE CLOSEOUT",
...table(["TARGET", "CONSUMER", "READY", "SOURCE_MATCHED", "MUTATION"], [[stringValue(target.id), stringValue(consumer.id), boolText(result.ready), boolText(result.sourceMatched), boolText(result.mutation)]]),
"",
"WAIT",
...table(["REQUESTED", "CI_ATTEMPTS", "RUNTIME_ATTEMPTS", "ELAPSED_MS", "BUDGET_S", "SCOPE", "SOURCE"], [[
stringValue(wait.requested),
stringValue(wait.ciAttempts),
stringValue(wait.runtimeAttempts),
stringValue(wait.elapsedMs),
stringValue(wait.budgetSeconds),
stringValue(wait.budgetScope),
stringValue(wait.source),
]]),
"",
"SOURCE / PIPELINERUN",
...table(["EXPECTED", "OBSERVED", "PIPELINERUN", "STATUS", "DURATION_S"], [[short(stringValue(result.sourceCommit), 16), short(stringValue(result.observedSourceCommit), 16), stringValue(latest.name), stringValue(latest.reason ?? latest.status), stringValue(latest.durationSeconds)]]),
` delivery: ${stringValue(sourceObservation.mode)} (${stringValue(sourceObservation.reason)})`,
"",
"RUNTIME",
...table(["IMAGE_STATUS", "ENV_REUSE", "DIGEST", "GITOPS", "ARGO", "RELATION", "RUNTIME"], [[
stringValue(artifact.imageStatus),
stringValue(artifact.envReuse),
short(stringValue(artifact.digest), 18),
short(stringValue(artifact.gitopsCommit), 12),
`${stringValue(argo.sync)}/${stringValue(argo.health)}`,
stringValue(record(argo.revisionRelation).relation),
runtimeText(runtime),
]]),
"",
"CICD DIAGNOSIS",
...table(["OK", "CODE", "PHASE", "HINT"], [[boolText(diagnostics.ok !== false), stringValue(diagnostics.code), stringValue(diagnostics.phase), compactLine(stringValue(diagnostics.hint))]]),
"",
"AUTOMATIC DELIVERY OBSERVATION",
...table(["ENABLED", "REQUIRED", "OK", "MODE", "EXECUTED", "PENDING", "IN_SYNC"], [[
boolText(gitOpsMirrorFlush.enabled),
boolText(gitOpsMirrorFlush.required),
boolText(gitOpsMirrorFlush.ok !== false),
stringValue(gitOpsMirrorFlush.mode),
boolText(gitOpsMirrorFlush.executed),
stringValue(record(gitOpsMirrorFlush.afterSummary ?? gitOpsMirrorFlush.beforeSummary).pendingFlush),
stringValue(record(gitOpsMirrorFlush.afterSummary ?? gitOpsMirrorFlush.beforeSummary).githubInSync),
]]),
"",
...(Object.keys(blocker).length === 0
? ["BLOCKER", "-"]
: ["BLOCKER", ...table(["CODE", "REASON"], [[stringValue(blocker.code), compactLine(stringValue(blocker.reason))]])]),
"",
"NEXT",
` status: ${stringValue(record(result.next).status)}`,
` history: ${stringValue(record(result.next).history)} --limit 10`,
latest.name === undefined ? " pipeline-run: -" : ` pipeline-run: ${stringValue(record(result.next).history)} --id ${stringValue(latest.name)}`,
];
return rendered(result, "platform-infra pipelines-as-code closeout", lines);
}
function renderHistory(result: Record<string, unknown>): RenderedCliResult {
const rows = arrayRecords(result.rows);
const config = record(result.config);
const historyErrors = arrayRecords(result.historyErrors);
const detailId = stringValue(result.detailId);
const timeHeader = `TIME(${stringValue(config.displayTimeZone)})`;
const lines = [
"PLATFORM-INFRA PIPELINES-AS-CODE HISTORY",
`DATA: ${stringValue(config.source)}`,
`ROWS: ${stringValue(rows.length)} LIMIT_PER_CONSUMER: ${stringValue(config.limitPerConsumer)} READ_ERRORS: ${stringValue(historyErrors.length)}`,
...(historyErrors.length === 0 ? [] : [
"",
"READ ERRORS",
...table(["CONTEXT", "STATUS", "ERROR"], historyErrors.map((item) => [stringValue(item.context), stringValue(item.status), compactLine(stringValue(item.error ?? item.stderr))])),
]),
"",
"TRIGGERS",
...(rows.length === 0 ? ["-"] : table([timeHeader, "CONSUMER", "CLASS", "OWNER", "STATUS", "DUR_S", "COMMIT", "ENV_REUSE", "ID"], rows.map((row) => [
stringValue(row.displayTime) === "-" ? isoShort(stringValue(row.triggeredAt)) : stringValue(row.displayTime),
stringValue(row.consumer),
stringValue(row.deliveryClass),
stringValue(row.deliveryOwner ?? row.executionOwner),
statusText(row),
stringValue(row.durationSeconds),
short(stringValue(row.commit)),
envReuseText(record(row.envReuse)),
stringValue(row.id ?? row.pipelineRun),
]))),
...(detailId === "-" || rows.length !== 1 ? [] : renderHistoryDetail(rows[0] ?? {})),
"",
"NEXT",
rows.length > 0 && detailId === "-" ? ` detail: ${stringValue(record(result.next).history)} --id ${stringValue(rows[0]?.id ?? rows[0]?.pipelineRun)}` : null,
` full: ${stringValue(record(result.next).history)}${detailId === "-" ? "" : ` --id ${detailId}`} --full`,
];
return rendered(result, "platform-infra pipelines-as-code history", lines.filter((line): line is string => line !== null));
}
function renderDebugStep(result: Record<string, unknown>): RenderedCliResult {
const checks = arrayRecords(result.checks);
const realRun = record(result.realRun);
const pipelineRun = record(realRun.pipelineRun);
const artifact = record(realRun.artifact);
const catalog = record(artifact.catalog);
const terminal = arrayRecords(realRun.terminal);
const firstBreak = record(realRun.firstBreak);
const fixturePassed = checks.filter((item) => item.ok === true).length;
const lines = [
"PLATFORM-INFRA PIPELINES-AS-CODE DEBUG STEP",
...table(["TARGET", "CONSUMER", "PIPELINERUN", "OK", "FIXTURES", "MUTATION"], [[
stringValue(record(result.target).id),
stringValue(result.consumer),
stringValue(result.detailId),
boolText(result.ok),
`${fixturePassed}/${checks.length}`,
boolText(result.mutation),
]]),
...(Object.keys(realRun).length === 0 ? [] : [
"",
"REAL PIPELINERUN EVIDENCE",
...table(["FOUND", "CLASS", "AUTHORITY", "STATUS", "SOURCE", "MODE", "VALID", "FIRST_BREAK"], [[
boolText(realRun.found),
stringValue(pipelineRun.deliveryClass),
boolText(pipelineRun.deliveryAuthorityEligible),
stringValue(pipelineRun.status ?? pipelineRun.reason),
short(stringValue(pipelineRun.sourceCommit), 16),
stringValue(realRun.mode),
boolText(realRun.valid),
stringValue(firstBreak.code),
]]),
"",
"TERMINAL EVIDENCE",
...(terminal.length === 0 ? ["-"] : table(["STEP", "PRESENT", "STATUS", "SOURCE", "MARKER", "TASKRUN", "RESULTS"], terminal.map((item) => [
stringValue(item.step),
boolText(item.present),
stringValue(item.status),
stringValue(item.source),
item.markerPresent === true ? stringValue(item.markerSource) : "-",
short(stringValue(item.taskRun), 52),
compactResultNames(item.results),
]))),
"",
"ARTIFACT / GITOPS",
...table(["CATALOG", "VERIFIED", "REUSED", "DIGESTS", "GITOPS"], [[
stringValue(catalog.status),
boolText(catalog.registryVerified),
stringValue(catalog.reusedCount),
stringValue(catalog.digestCount),
short(stringValue(artifact.gitopsCommit), 16),
]]),
]),
"",
"STATUS EVALUATOR FIXTURES",
...(checks.length === 0 ? ["-"] : table(["CASE", "OK", "EXPECTED", "ACTUAL"], checks.map((item) => [
stringValue(item.id),
boolText(item.ok),
`${stringValue(item.expectedOk)}/${stringValue(item.expectedCode)}`,
`${stringValue(item.actualOk)}/${stringValue(item.actualCode)}`,
]))),
"",
"NEXT",
` status: ${stringValue(record(result.next).status)}`,
];
return rendered(result, "platform-infra pipelines-as-code debug-step", lines);
}
function parseApplyOptions(args: string[]): ApplyOptions {
const commonArgs: string[] = [];
let confirm = false;
let dryRun = false;
let wait = false;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === "--confirm") confirm = true;
else if (arg === "--dry-run") dryRun = true;
else if (arg === "--wait") wait = true;
else {
commonArgs.push(arg);
if (arg === "--target" || arg === "--node" || arg === "--consumer") {
commonArgs.push(args[index + 1] ?? "");
index += 1;
}
}
}
if (confirm && dryRun) throw new Error("pipelines-as-code apply accepts only one of --confirm or --dry-run");
return { ...parseCommonOptions(commonArgs), confirm, dryRun: dryRun || !confirm, wait };
}
function parseCloseoutOptions(args: string[]): CloseoutOptions {
const commonArgs: string[] = [];
let sourceCommit: string | null = null;
let wait = false;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === "--source-commit" || arg === "--commit") {
const value = args[index + 1];
if (value === undefined || value.startsWith("--")) throw new Error(`${arg} requires a value`);
if (!/^[0-9a-f]{40}$/iu.test(value)) throw new Error(`${arg} must be a full git sha`);
sourceCommit = value;
index += 1;
} else if (arg === "--wait") {
wait = true;
} else {
commonArgs.push(arg);
if (arg === "--target" || arg === "--node" || arg === "--consumer") {
commonArgs.push(args[index + 1] ?? "");
index += 1;
}
}
}
return { ...parseCommonOptions(commonArgs), sourceCommit, wait };
}
function parseHistoryOptions(args: string[]): HistoryOptions {
const commonArgs: string[] = [];
let limit = 5;
let detailId: string | null = null;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === "--limit") {
const value = args[index + 1];
if (value === undefined || value.startsWith("--")) throw new Error("--limit requires a value");
const parsed = Number.parseInt(value, 10);
if (!Number.isInteger(parsed) || parsed < 1 || parsed > 50) throw new Error("--limit must be an integer from 1 to 50");
limit = parsed;
index += 1;
} else if (arg === "--id" || arg === "--run" || arg === "--pipeline-run") {
const value = args[index + 1];
if (value === undefined || value.startsWith("--")) throw new Error(`${arg} requires a value`);
if (!/^[A-Za-z0-9._:-]+$/u.test(value)) throw new Error(`${arg} must be a PipelineRun id`);
detailId = value;
index += 1;
} else {
commonArgs.push(arg);
if (arg === "--target" || arg === "--node" || arg === "--consumer") {
commonArgs.push(args[index + 1] ?? "");
index += 1;
}
}
}
return { ...parseCommonOptions(commonArgs), limit, detailId };
}
function parseCommonOptions(args: string[]): CommonOptions {
let targetId: string | null = null;
let consumerId: string | null = null;
let full = false;
let raw = false;
let json = false;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === "--target" || arg === "--node" || arg === "--consumer") {
const value = args[index + 1];
if (value === undefined || value.startsWith("--")) throw new Error(`${arg} requires a value`);
if (!/^[A-Za-z0-9._-]+$/u.test(value)) throw new Error(`${arg} must be a simple id`);
if (arg === "--consumer") consumerId = value;
else targetId = value;
index += 1;
} else if (arg === "--full") {
full = true;
} else if (arg === "--raw") {
raw = true;
full = true;
} else if (arg === "--json") {
json = true;
} else {
throw new Error(`unsupported pipelines-as-code option: ${arg}`);
}
}
return { targetId, consumerId, full, raw, json };
}
function closeoutReady(value: Record<string, unknown>, sourceCommit: string | null): boolean {
if (value.ok !== true) return false;
const summary = record(value.summary);
const latest = record(summary.latestPipelineRun);
const diagnostics = record(summary.diagnostics);
const observedSourceCommit = observedCloseoutSourceCommit(latest, diagnostics);
if (sourceCommit === null) return true;
return observedSourceCommit === sourceCommit;
}
function closeoutCiReady(value: Record<string, unknown>, sourceCommit: string | null): boolean {
const summary = record(value.summary);
const latest = record(summary.latestPipelineRun);
const diagnostics = record(summary.diagnostics);
const pipelineGate = record(summary.pipelineRunGate);
if (pipelineGate.ok !== true) return false;
const observedSourceCommit = observedCloseoutSourceCommit(latest, diagnostics);
if (sourceCommit !== null && observedSourceCommit !== sourceCommit) return false;
const sourceObservation = record(summary.sourceObservation);
if (Object.keys(sourceObservation).length > 0) {
if (sourceObservation.valid !== true || stringValue(sourceObservation.sourceCommit) !== observedSourceCommit) return false;
if (stringValue(sourceObservation.contract) === "hwlab-g14-ci-plan" && stringValue(sourceObservation.mode) === "delivery") {
const deliveryArtifact = record(summary.artifact);
const catalog = record(deliveryArtifact.catalog);
const digests = Array.isArray(deliveryArtifact.digests) ? deliveryArtifact.digests : [];
if (catalog.present !== true
|| stringValue(catalog.status) !== "published"
|| catalog.registryVerified !== true
|| stringValue(catalog.sourceCommit) !== observedSourceCommit
|| digests.length === 0) return false;
}
}
if (stringValue(diagnostics.code) === "pac-ready-no-runtime-change") return true;
const artifact = record(summary.artifact);
if (stringValue(artifact.imageStatus) === "disabled") return true;
return stringValue(artifact.gitopsCommit) !== "-";
}
function observedCloseoutSourceCommit(latest: Record<string, unknown>, diagnostics: Record<string, unknown>): string {
return stringValue(latest.sourceCommit) !== "-" ? stringValue(latest.sourceCommit) : stringValue(diagnostics.sourceCommit);
}
function closeoutBlocker(value: Record<string, unknown>, expected: string | null, observed: string, sourceMatched: boolean, gitOpsMirrorFlush: unknown): Record<string, unknown> {
if (!sourceMatched) {
return {
code: "pac-closeout-source-mismatch",
reason: `expected source commit ${expected ?? "-"} but latest observed source commit is ${observed}`,
valuesPrinted: false,
};
}
const summary = record(value.summary);
const latest = record(summary.latestPipelineRun);
const pipelineGate = pipelineRunGate(latest);
if (!pipelineGate.ok) {
const longestTask = longestTaskRun(arrayRecords(summary.taskRuns));
const longestTaskText = longestTask === null
? "longestTask=-"
: `longestTask=${stringValue(longestTask.name)} status=${statusText(longestTask)} duration=${stringValue(longestTask.durationSeconds)}s`;
return {
code: pipelineGate.code,
reason: `${pipelineGate.reason}; pipelineRun=${stringValue(latest.name)} duration=${stringValue(latest.durationSeconds)}s; ${longestTaskText}`,
valuesPrinted: false,
};
}
const flush = record(gitOpsMirrorFlush);
if (flush.ok === false) {
const next = record(flush.next);
return {
code: stringValue(flush.degradedReason, "pac-closeout-gitops-mirror-flush-failed"),
reason: `GitOps mirror flush did not complete: mode=${stringValue(flush.mode)} required=${stringValue(flush.required)} next=${stringValue(next.flush ?? next.status)}`,
valuesPrinted: false,
};
}
const diagnostics = record(summary.diagnostics);
return {
code: stringValue(diagnostics.code, "pac-closeout-not-ready"),
reason: stringValue(diagnostics.hint, "PaC status is not ready; inspect status/history for the selected consumer"),
valuesPrinted: false,
};
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function pipelineRunGate(latest: Record<string, unknown>): Record<string, unknown> {
const name = stringValue(latest.name);
if (name === "-") {
return {
ok: false,
code: "pipeline-missing",
reason: "no matching PipelineRun is visible yet",
valuesPrinted: false,
};
}
const status = stringValue(latest.status);
const reason = stringValue(latest.reason);
if (status === "True") {
return {
ok: true,
code: "pipeline-succeeded",
reason: reason === "-" ? "PipelineRun completed successfully" : `PipelineRun ${reason}`,
valuesPrinted: false,
};
}
if (status === "False") {
return {
ok: false,
code: "pipeline-failed",
reason: reason === "-" ? "PipelineRun completed with failure" : `PipelineRun failed: ${reason}`,
valuesPrinted: false,
};
}
return {
ok: false,
code: "pipeline-running",
reason: statusText(latest) === "-" ? "PipelineRun is not terminal" : `PipelineRun is ${statusText(latest)}`,
valuesPrinted: false,
};
}
function longestTaskRun(taskRuns: Record<string, unknown>[]): Record<string, unknown> | null {
let selected: Record<string, unknown> | null = null;
let selectedDuration = -1;
for (const taskRun of taskRuns) {
const duration = numericValue(taskRun.durationSeconds);
if (duration === null || duration < selectedDuration) continue;
selected = taskRun;
selectedDuration = duration;
}
return selected;
}
function positiveInteger(obj: Record<string, unknown>, key: string, path: string): number {
const value = y.integerField(obj, key, path);
if (value < 1) throw new Error(`${configLabel}.${path}.${key} must be positive`);
return value;
}
function urlField(obj: Record<string, unknown>, key: string, path: string): string {
const value = y.stringField(obj, key, path);
const parsed = new URL(value);
if (!["http:", "https:"].includes(parsed.protocol) || parsed.search || parsed.hash) throw new Error(`${configLabel}.${path}.${key} must be an http(s) URL without query or hash`);
return value.replace(/\/+$/u, "");
}
function validateTimeZone(value: string, path: string): void {
try {
new Intl.DateTimeFormat("en-US", { timeZone: value }).format(new Date(0));
} catch (error) {
throw new Error(`${path} must be a valid IANA time zone: ${(error as Error).message}`);
}
}
function stringRecord(obj: Record<string, unknown>, path: string): Record<string, string> {
const out: Record<string, string> = {};
for (const [key, value] of Object.entries(obj)) {
if (typeof value !== "string" || value.length === 0) throw new Error(`${configLabel}.${path}.${key} must be a non-empty string`);
out[key] = value;
}
return out;
}
function secretFingerprint(value: string): string {
return sha256Fingerprint(value).slice(0, 23);
}
function rendered(result: Record<string, unknown>, command: string, lines: string[]): RenderedCliResult {
return { ok: result.ok !== false, command, renderedText: lines.join("\n"), contentType: "text/plain" };
}
function record(value: unknown): Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value) ? value as Record<string, unknown> : {};
}
function arrayRecords(value: unknown): Record<string, unknown>[] {
return Array.isArray(value) ? value.filter((item) => typeof item === "object" && item !== null && !Array.isArray(item)) as Record<string, unknown>[] : [];
}
function stringValue(value: unknown, fallback = "-"): string {
if (typeof value === "string" && value.length > 0) return value;
if (typeof value === "number" || typeof value === "boolean") return String(value);
return fallback;
}
function numericValue(value: unknown): number | null {
if (typeof value === "number" && Number.isFinite(value)) return value;
if (typeof value !== "string" || value.trim().length === 0) return null;
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : null;
}
function boolText(value: unknown): string {
return value === true ? "true" : "false";
}
function short(value: string, length = 12): string {
if (value === "-" || value.length <= length) return value;
return value.slice(0, length);
}
function isoShort(value: string): string {
if (value === "-") return value;
return value.replace(/:\d\d(?:\.\d+)?Z$/u, "Z");
}
function statusText(row: Record<string, unknown>): string {
const status = stringValue(row.status);
const reason = stringValue(row.reason);
if (status === "-" && reason === "-") return "-";
if (status === "True" && reason !== "-") return reason;
if (reason === "-") return status;
if (status === "-") return reason;
return `${status}/${reason}`;
}
function envReuseText(envReuse: Record<string, unknown>): string {
const status = stringValue(envReuse.status);
const skipped = stringValue(envReuse.buildSkippedCount);
const reused = stringValue(envReuse.serviceReusedCount);
const cache = stringValue(envReuse.buildCache);
const parts = [
status === "-" ? null : status,
skipped === "-" ? null : `skip=${skipped}`,
reused === "-" ? null : `reuse=${reused}`,
cache === "-" ? null : `cache=${cache}`,
].filter((item): item is string => item !== null);
return parts.length === 0 ? "-" : parts.join(",");
}
function registryText(registry: Record<string, unknown>): string {
if (Object.keys(registry).length === 0) return "-";
const present = registry.present === true ? "present" : "missing";
const digest = short(stringValue(registry.digest), 18);
return digest === "-" ? present : `${present}:${digest}`;
}
function runtimeText(runtime: Record<string, unknown>): string {
if (Object.keys(runtime).length === 0) return "-";
const ready = `${stringValue(runtime.readyReplicas)}/${stringValue(runtime.replicas)}`;
const digest = short(stringValue(runtime.digest), 18);
return digest === "-" ? ready : `${ready}:${digest}`;
}
function renderHistoryDetail(row: Record<string, unknown>): string[] {
const envReuse = record(row.envReuse);
const sourceObservation = record(row.sourceObservation);
const deliveryPlan = record(sourceObservation.plan);
const terminalSources = record(sourceObservation.terminalSources);
const artifact = record(row.artifact);
const catalog = record(artifact.catalog);
const collector = record(row.collector);
const taskRuns = record(row.taskRuns);
const longest = record(taskRuns.longest);
const failed = record(taskRuns.failed);
const failedStep = record(failed.failedStep);
return [
"",
"DETAIL",
...table(["FIELD", "VALUE"], [
["id", stringValue(row.id ?? row.pipelineRun)],
["deliveryClass", stringValue(row.deliveryClass)],
["deliveryAuthorityEligible", stringValue(row.deliveryAuthorityEligible)],
["deliveryOwner", stringValue(row.deliveryOwner)],
["executionOwner", stringValue(row.executionOwner)],
["parentRelation", stringValue(row.parentRelation)],
["pipeline", stringValue(row.pipeline)],
["branch", stringValue(row.branch)],
["commit", stringValue(row.commit)],
["started", stringValue(row.displayStartTime) === "-" ? stringValue(row.startTime) : stringValue(row.displayStartTime)],
["completed", stringValue(row.displayCompletionTime) === "-" ? stringValue(row.completionTime) : stringValue(row.displayCompletionTime)],
["taskRuns", stringValue(taskRuns.count)],
["longestTask", stringValue(longest.name)],
["longestTaskDurationS", stringValue(longest.durationSeconds)],
["failedTask", stringValue(failed.name)],
["failedStep", stringValue(failedStep.name)],
["failedStepExit", stringValue(failedStep.exitCode)],
["failedStepTermination", stringValue(failedStep.terminationReason ?? failedStep.reason)],
["failure", compactLine(stringValue(failed.message))],
["envReuseSource", stringValue(envReuse.source)],
["deliveryMode", stringValue(sourceObservation.mode)],
["deliveryValid", stringValue(sourceObservation.valid)],
["deliverySourceMatched", stringValue(sourceObservation.sourceMatched)],
["deliveryReason", stringValue(sourceObservation.reason)],
["affectedServices", stringValue(deliveryPlan.affectedServiceCount)],
["rolloutServices", stringValue(deliveryPlan.rolloutServiceCount)],
["buildServices", stringValue(deliveryPlan.buildServiceCount)],
["reusedServices", stringValue(deliveryPlan.reusedServiceCount)],
]),
"",
"TERMINAL EVIDENCE",
...table(["STEP", "PRESENT", "STATUS", "SOURCE", "MARKER", "TASKRUN"], [
["plan-artifacts", boolText(record(terminalSources.planArtifacts).present), stringValue(record(terminalSources.planArtifacts).status), stringValue(record(terminalSources.planArtifacts).source), record(terminalSources.planArtifacts).markerPresent === true ? stringValue(record(terminalSources.planArtifacts).markerSource) : "-", short(stringValue(record(terminalSources.planArtifacts).taskRun), 52)],
["collect-artifacts", boolText(record(terminalSources.collectArtifacts).present), stringValue(record(terminalSources.collectArtifacts).status), stringValue(record(terminalSources.collectArtifacts).source), record(terminalSources.collectArtifacts).markerPresent === true ? stringValue(record(terminalSources.collectArtifacts).markerSource) : "-", short(stringValue(record(terminalSources.collectArtifacts).taskRun), 52)],
["gitops-promote", boolText(record(terminalSources.gitopsPromote).present), stringValue(record(terminalSources.gitopsPromote).status), stringValue(record(terminalSources.gitopsPromote).source), record(terminalSources.gitopsPromote).markerPresent === true ? stringValue(record(terminalSources.gitopsPromote).markerSource) : "-", short(stringValue(record(terminalSources.gitopsPromote).taskRun), 52)],
]),
"",
"ARTIFACT / COLLECTOR",
...table(["CATALOG", "VERIFIED", "REUSED", "DIGESTS", "GITOPS", "TASKS", "TERMINALS", "LOG_ERRORS"], [[
stringValue(catalog.status),
boolText(catalog.registryVerified),
stringValue(catalog.reusedCount),
stringValue(catalog.digestCount),
short(stringValue(artifact.gitopsCommit), 16),
stringValue(collector.contractTaskCount),
stringValue(collector.terminalRecordCount),
stringValue(collector.logErrorCount),
]]),
"",
"LOGS",
` ${stringValue(taskRuns.logsCommand)}`,
];
}
function compactLine(value: string): string {
const trimmed = value.replace(/\s+/gu, " ").trim();
return trimmed.length > 0 ? trimmed.slice(0, 120) : "-";
}
function compactResultNames(value: unknown): string {
if (!Array.isArray(value) || value.length === 0) return "-";
const names = value.filter((item): item is string => typeof item === "string");
if (names.length <= 2) return names.join(",");
return `${names.length}:${names.slice(0, 2).join(",")},...`;
}
function table(headers: string[], rows: string[][]): string[] {
const widths = headers.map((header, index) => Math.max(header.length, ...rows.map((row) => (row[index] ?? "").length)));
const format = (row: string[]) => row.map((cell, index) => cell.padEnd(widths[index])).join(" ");
return [format(headers), format(headers.map((header, index) => "-".repeat(Math.max(header.length, widths[index])))), ...rows.map(format)];
}