109 lines
5.1 KiB
TypeScript
109 lines
5.1 KiB
TypeScript
import { resolveToken } from "./gh/auth-and-safety";
|
|
import { githubRequest, isGitHubError } from "./gh/client";
|
|
|
|
export interface PacDeliveryTimingBinding {
|
|
target: Record<string, unknown>;
|
|
consumer: {
|
|
id: string;
|
|
node: string;
|
|
lane: string;
|
|
repository: string;
|
|
branch: string;
|
|
pipeline: string;
|
|
};
|
|
}
|
|
|
|
interface PacDeliveryTimingObservers {
|
|
history: () => Promise<Record<string, unknown>>;
|
|
status: () => Promise<Record<string, unknown>>;
|
|
}
|
|
|
|
function record(value: unknown): Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
}
|
|
|
|
function records(value: unknown): Record<string, unknown>[] {
|
|
return Array.isArray(value) ? value.map(record) : [];
|
|
}
|
|
|
|
function durationBetween(start: unknown, end: unknown): number | null {
|
|
if (typeof start !== "string" || typeof end !== "string") return null;
|
|
const a = Date.parse(start);
|
|
const b = Date.parse(end);
|
|
if (!Number.isFinite(a) || !Number.isFinite(b)) return null;
|
|
return Math.max(0, Math.round((b - a) / 1000));
|
|
}
|
|
|
|
export async function observePacDeliveryTiming(
|
|
binding: PacDeliveryTimingBinding,
|
|
observers: PacDeliveryTimingObservers,
|
|
): Promise<Record<string, unknown>> {
|
|
const [sourceOwner, sourceRepo] = binding.consumer.repository.split("/");
|
|
if (!sourceOwner || !sourceRepo) throw new Error(`invalid owning YAML source repository ${binding.consumer.repository}`);
|
|
const [historyResult, statusResult] = await Promise.all([observers.history(), observers.status()]);
|
|
const row = records(historyResult.rows)[0] ?? {};
|
|
const statusSummary = record(statusResult.summary);
|
|
const latest = record(statusSummary.latestPipelineRun);
|
|
const commit = typeof row.commit === "string" ? row.commit : typeof latest.sourceCommit === "string" ? latest.sourceCommit : null;
|
|
const evidenceGaps: string[] = [];
|
|
let pullRequest: Record<string, unknown> | null = null;
|
|
let githubEvidence: Record<string, unknown> = { repository: binding.consumer.repository, source: "github-commit-pulls", mutation: false };
|
|
if (commit === null) {
|
|
evidenceGaps.push("source-commit-missing");
|
|
} else {
|
|
const tokenInfo = resolveToken(binding.consumer.repository, false);
|
|
if (tokenInfo.token === null) {
|
|
evidenceGaps.push("github-token-unavailable");
|
|
githubEvidence = { ...githubEvidence, ok: false, token: tokenInfo.probe };
|
|
} else {
|
|
const response = await githubRequest<unknown>(tokenInfo.token, "GET", `/repos/${sourceOwner}/${sourceRepo}/commits/${commit}/pulls`);
|
|
if (isGitHubError(response)) {
|
|
evidenceGaps.push("github-pr-lookup-failed");
|
|
githubEvidence = { ...githubEvidence, ok: false, error: response };
|
|
} else {
|
|
const prs = Array.isArray(response) ? response : [];
|
|
const merged = prs.find((item) => record(item).merged_at !== null && record(item).merged_at !== undefined) ?? prs[0];
|
|
if (merged === undefined) {
|
|
evidenceGaps.push("github-pr-not-found");
|
|
} else {
|
|
const item = record(merged);
|
|
pullRequest = { number: item.number ?? null, title: item.title ?? null, url: item.html_url ?? null, mergedAt: item.merged_at ?? null, mergeCommit: commit };
|
|
if (typeof item.merged_at !== "string") evidenceGaps.push("github-mergedAt-missing");
|
|
}
|
|
githubEvidence = { ...githubEvidence, ok: true, count: prs.length, token: tokenInfo.probe };
|
|
}
|
|
}
|
|
}
|
|
const mergedAt = pullRequest?.mergedAt ?? null;
|
|
const startTime = row.startTime ?? null;
|
|
const completionTime = row.completionTime ?? null;
|
|
const argo = record(statusSummary.argo);
|
|
const runtime = record(statusSummary.runtime);
|
|
const provenance = record(statusSummary.provenance);
|
|
if (mergedAt === null) evidenceGaps.push("mergedAt-missing");
|
|
if (startTime === null || completionTime === null) evidenceGaps.push("pipeline-timestamps-missing");
|
|
if (argo.sync === undefined || argo.health === undefined) evidenceGaps.push("argo-closeout-missing");
|
|
if (runtime.readyReplicas === undefined) evidenceGaps.push("runtime-health-missing");
|
|
if (provenance.relation === "unknown") evidenceGaps.push("runtime-provenance-unknown");
|
|
return {
|
|
ok: commit !== null && startTime !== null && completionTime !== null,
|
|
action: "platform-infra-pipelines-as-code-delivery-timing",
|
|
mutation: false,
|
|
state: evidenceGaps.length === 0 ? "complete" : "partial",
|
|
target: binding.target,
|
|
consumer: binding.consumer,
|
|
source: { commit, pullRequest, github: githubEvidence },
|
|
timing: {
|
|
mergedAt,
|
|
pipelineStart: startTime,
|
|
pipelineCompletion: completionTime,
|
|
triggerWaitSeconds: durationBetween(mergedAt, startTime),
|
|
pipelineDurationSeconds: row.durationSeconds ?? durationBetween(startTime, completionTime),
|
|
endToEndSeconds: durationBetween(mergedAt, completionTime),
|
|
},
|
|
pipeline: { id: row.id ?? latest.name ?? null, status: row.status ?? latest.status ?? null, taskRuns: row.taskRuns ?? statusSummary.taskRuns ?? null },
|
|
runtime: { argo, health: runtime, provenance },
|
|
evidenceGaps: [...new Set(evidenceGaps)],
|
|
};
|
|
}
|