A test run pins a validated draft, runs with forge and push writes suppressed and recorded per step, and is excluded from workflow stats. The History tab lists revisions that changed a workflow, diffs them as YAML and on the canvas, restores into the editor, and ties runs to the revision they used.
116 lines
4.5 KiB
TypeScript
116 lines
4.5 KiB
TypeScript
import { parseDocument } from "yaml";
|
|
|
|
import type { ResourceRevision } from "../stores/settings.ts";
|
|
import { workflowGraph } from "./workflow-graph.ts";
|
|
|
|
/** One revision of the definitions that changed a single workflow. */
|
|
export interface WorkflowRevision {
|
|
version: number;
|
|
change: "added" | "edited" | "removed";
|
|
/** The workflow's YAML at this revision; null when the revision removed it. */
|
|
yaml: string | null;
|
|
actor: string;
|
|
source: string;
|
|
at?: string;
|
|
}
|
|
|
|
function entryYaml(value: unknown, id: string): string | null {
|
|
const definitions = (value as { definitions?: { id: string; yaml: string }[] } | null)?.definitions;
|
|
return definitions?.find((entry) => entry.id === id)?.yaml ?? null;
|
|
}
|
|
|
|
/**
|
|
* The revisions in which workflow `id` was added, edited or removed, newest
|
|
* first. All workflows share one resource, so a revision that left this one's
|
|
* YAML as it was is not part of its history.
|
|
*/
|
|
export function workflowRevisions(id: string, revisions: ResourceRevision[]): WorkflowRevision[] {
|
|
const changes: WorkflowRevision[] = [];
|
|
let previous: string | null = null;
|
|
for (const revision of [...revisions].sort((a, b) => a.version - b.version)) {
|
|
const yaml = entryYaml(revision.value, id);
|
|
if (yaml !== previous) {
|
|
changes.push({
|
|
version: revision.version,
|
|
change: yaml === null ? "removed" : previous === null ? "added" : "edited",
|
|
yaml,
|
|
actor: revision.actor,
|
|
source: revision.source,
|
|
at: revision.at,
|
|
});
|
|
}
|
|
previous = yaml;
|
|
}
|
|
return changes.reverse();
|
|
}
|
|
|
|
/**
|
|
* The revision a run used: the newest one holding the YAML the run pinned that
|
|
* was already in place when the run was created. A test run pins a draft that
|
|
* no revision holds.
|
|
*/
|
|
export function revisionOfRun(revisions: WorkflowRevision[], pinned?: string, createdAt?: string): WorkflowRevision | undefined {
|
|
if (!pinned) return undefined;
|
|
const matches = revisions.filter((revision) => revision.yaml === pinned);
|
|
const created = createdAt ? Date.parse(createdAt) : NaN;
|
|
return matches.find((revision) => !revision.at || Number.isNaN(created) || Date.parse(revision.at) <= created) ?? matches.at(-1);
|
|
}
|
|
|
|
export interface DiffLine {
|
|
kind: "same" | "add" | "del";
|
|
text: string;
|
|
}
|
|
|
|
/** A line diff from `before` to `after`: `del` lines are only in `before`. */
|
|
export function lineDiff(before: string, after: string): DiffLine[] {
|
|
const a = before.split("\n");
|
|
const b = after.split("\n");
|
|
const lcs: number[][] = Array.from({ length: a.length + 1 }, () => new Array<number>(b.length + 1).fill(0));
|
|
for (let i = a.length - 1; i >= 0; i--)
|
|
for (let j = b.length - 1; j >= 0; j--)
|
|
lcs[i]![j] = a[i] === b[j] ? lcs[i + 1]![j + 1]! + 1 : Math.max(lcs[i + 1]![j]!, lcs[i]![j + 1]!);
|
|
const out: DiffLine[] = [];
|
|
let i = 0;
|
|
let j = 0;
|
|
while (i < a.length && j < b.length) {
|
|
if (a[i] === b[j]) out.push({ kind: "same", text: a[i++]! }), j++;
|
|
else if (lcs[i + 1]![j]! >= lcs[i]![j + 1]!) out.push({ kind: "del", text: a[i++]! });
|
|
else out.push({ kind: "add", text: b[j++]! });
|
|
}
|
|
while (i < a.length) out.push({ kind: "del", text: a[i++]! });
|
|
while (j < b.length) out.push({ kind: "add", text: b[j++]! });
|
|
return out;
|
|
}
|
|
|
|
export type Mark = "added" | "removed" | "changed";
|
|
|
|
function stepsOf(source: string): Map<string, { node: string; signature: string }> {
|
|
const value: unknown = parseDocument(source).toJS();
|
|
const steps = new Map<string, { node: string; signature: string }>();
|
|
for (const node of workflowGraph(source).nodes) {
|
|
const { kind, path, stage, occurrence } = node.data;
|
|
if (node.type !== "step" || kind !== "step" || !path || stage === undefined) continue;
|
|
let at: unknown = value;
|
|
for (const part of path) at = (at as Record<string | number, unknown> | undefined)?.[part];
|
|
steps.set(`${stage}#${occurrence ?? 0}`, { node: node.id, signature: JSON.stringify(at ?? null) });
|
|
}
|
|
return steps;
|
|
}
|
|
|
|
/**
|
|
* Marks the steps of `source` that differ from `other`, by canvas node id. A
|
|
* step `other` lacks is marked `missing`: "removed" when `source` is the older
|
|
* definition, "added" when it is the newer.
|
|
*/
|
|
export function stepMarks(source: string, other: string, missing: "added" | "removed"): Record<string, Mark> {
|
|
const here = stepsOf(source);
|
|
const there = stepsOf(other);
|
|
const marks: Record<string, Mark> = {};
|
|
for (const [identity, step] of here) {
|
|
const counterpart = there.get(identity);
|
|
if (!counterpart) marks[step.node] = missing;
|
|
else if (counterpart.signature !== step.signature) marks[step.node] = "changed";
|
|
}
|
|
return marks;
|
|
}
|