fix(ui): show workflow validation errors on the failing step and link to it
This commit is contained in:
3 files changed
+29
-32
No files matched your search
@@ -315,20 +315,15 @@ const problems = computed(() =>
|
||||
}),
|
||||
),
|
||||
);
|
||||
// The parse stops at the first failing step; its node carries that failure too.
|
||||
const nodeProblems = computed(() => {
|
||||
const result = parsed.value;
|
||||
const failed = result.ok ? null : /^workflow \S+ step (\d+)\b/.exec(result.message);
|
||||
if (result.ok || !failed) return problems.value;
|
||||
return { [JSON.stringify(["steps", Number(failed[1]) - 1])]: result.message, ...problems.value };
|
||||
});
|
||||
// A parse failure on a step shows on that node too.
|
||||
const failedPath = computed(() => (parsed.value.ok ? undefined : (parsed.value.path as StepPath | undefined)));
|
||||
const nodeProblems = computed(() =>
|
||||
failedPath.value && !parsed.value.ok ? { ...problems.value, [JSON.stringify(failedPath.value)]: parsed.value.message } : problems.value,
|
||||
);
|
||||
// Every error the editor knows, each linked to its step when it has one.
|
||||
const errors = computed(() => {
|
||||
const list: { text: string; path?: StepPath }[] = [];
|
||||
if (!parsed.value.ok) {
|
||||
const step = /^workflow \S+ step (\d+)\b/.exec(parsed.value.message)?.[1];
|
||||
list.push({ text: validationLabel(parsed.value), path: step ? ["steps", Number(step) - 1] : undefined });
|
||||
}
|
||||
if (!parsed.value.ok) list.push({ text: validationLabel(parsed.value), path: failedPath.value });
|
||||
for (const [key, text] of Object.entries(problems.value)) list.push({ text, path: JSON.parse(key) as StepPath });
|
||||
return list;
|
||||
});
|
||||
|
||||
@@ -41,7 +41,7 @@ export interface WorkflowStepChip {
|
||||
|
||||
export type WorkflowParse =
|
||||
| { ok: true; id: string; steps: WorkflowStepChip[] }
|
||||
| { ok: false; message: string; line?: number };
|
||||
| { ok: false; message: string; line?: number; path?: (string | number)[] };
|
||||
|
||||
/** What the editor's indicator reads. */
|
||||
export function validationLabel(parsed: WorkflowParse): string {
|
||||
@@ -87,26 +87,25 @@ export function parseWorkflowYaml(
|
||||
const chips: WorkflowStepChip[] = [];
|
||||
const declared = new Set<string>();
|
||||
for (const [i, step] of steps.entries()) {
|
||||
const where = `workflow ${id} step ${i + 1}`;
|
||||
const path = ["steps", i];
|
||||
const controls = stepControlsProblem(step);
|
||||
if (controls) return { ok: false, message: `${where}: ${controls}` };
|
||||
if (controls) return { ok: false, message: controls, path };
|
||||
const branching = branchingOf(step);
|
||||
if (branching) {
|
||||
if (branching.kind === "switch" && !branching.on)
|
||||
return { ok: false, message: `${where}: switch.on names nothing` };
|
||||
return { ok: false, message: "switch.on names nothing", path };
|
||||
if (branching.on && !declared.has(branching.on.split(".")[1] ?? "") && branching.on.startsWith("steps."))
|
||||
return { ok: false, message: `${where}: no earlier step has id "${branching.on.split(".")[1]}"` };
|
||||
return { ok: false, message: notEarlier(branching.on.split(".")[1]!), path };
|
||||
for (const [branch, branchSteps] of branching.branches) {
|
||||
if (!branchSteps.length)
|
||||
return { ok: false, message: `${where}: ${branching.kind === "switch" ? "case" : "branch"} ${branch} needs a list of steps` };
|
||||
return { ok: false, message: `${branching.kind === "switch" ? "Case" : "Branch"} ${branch} needs a list of steps`, path };
|
||||
const visible = new Set(declared);
|
||||
for (const [j, branchStep] of branchSteps.entries()) {
|
||||
const branchWhere = `${where} branch ${branch} step ${j + 1}`;
|
||||
const problem =
|
||||
stepControlsProblem(branchStep) ||
|
||||
checkStepType(branchStep, branchWhere, known, repository) ||
|
||||
laterReference(branchStep, branchWhere, visible);
|
||||
if (problem) return { ok: false, message: problem };
|
||||
checkStepType(branchStep, known, repository) ||
|
||||
laterReference(branchStep, visible);
|
||||
if (problem) return { ok: false, message: problem, path: [...path, ...branching.base, branch, j] };
|
||||
declareID(branchStep, visible);
|
||||
}
|
||||
for (const branchStep of branchSteps) declareID(branchStep, declared);
|
||||
@@ -115,8 +114,8 @@ export function parseWorkflowYaml(
|
||||
chips.push({ index: i + 1, type: branching.kind });
|
||||
continue;
|
||||
}
|
||||
const problem = checkStepType(step, where, known, repository) || laterReference(step, where, declared);
|
||||
if (problem) return { ok: false, message: problem };
|
||||
const problem = checkStepType(step, known, repository) || laterReference(step, declared);
|
||||
if (problem) return { ok: false, message: problem, path };
|
||||
declareID(step, declared);
|
||||
chips.push({ index: i + 1, type: (step as { type: string }).type.trim() });
|
||||
}
|
||||
@@ -127,14 +126,18 @@ const STEP_REFERENCE = /steps\.([A-Za-z0-9_-]+)/g;
|
||||
|
||||
/** A reference to a step that has not run yet: the server refuses it, so the
|
||||
* page says so before save. */
|
||||
function laterReference(step: unknown, where: string, declared: Set<string>): string {
|
||||
function laterReference(step: unknown, declared: Set<string>): string {
|
||||
if (!isMapping(step)) return "";
|
||||
const text = JSON.stringify(step.settings ?? {}) + " " + (typeof step.when === "string" ? step.when : "");
|
||||
for (const [, ref] of text.matchAll(STEP_REFERENCE))
|
||||
if (!declared.has(ref)) return `${where}: no earlier step has id "${ref}"`;
|
||||
if (!declared.has(ref)) return notEarlier(ref);
|
||||
return "";
|
||||
}
|
||||
|
||||
function notEarlier(ref: string): string {
|
||||
return `No step "${ref}" runs before this one`;
|
||||
}
|
||||
|
||||
function declareID(step: unknown, declared: Set<string>): void {
|
||||
if (isMapping(step) && typeof step.id === "string") declared.add(step.id);
|
||||
}
|
||||
@@ -166,19 +169,18 @@ export function stepControlsProblem(step: unknown): string {
|
||||
/** What is wrong with one typed step, or "" when nothing the page can see. */
|
||||
function checkStepType(
|
||||
step: unknown,
|
||||
where: string,
|
||||
known: Map<string, StepTypeInfo>,
|
||||
repository: string,
|
||||
): string {
|
||||
if (!isMapping(step) || !isNonEmptyString(step.type)) return `${where} needs a type`;
|
||||
if (!isMapping(step) || !isNonEmptyString(step.type)) return "Needs a type";
|
||||
const type = step.type.trim();
|
||||
const info = known.get(type);
|
||||
if (known.size && !info) return `${where}: unknown step type "${type}"`;
|
||||
if (known.size && !info) return `Unknown step type "${type}"`;
|
||||
if (info?.needs_repository && repository !== "required")
|
||||
return `${where}: "${type}" needs a repository, but the workflow's repository is ${repository}`;
|
||||
return `"${type}" needs a repository, but the workflow's repository is ${repository}`;
|
||||
if (!info || step.settings === undefined || step.settings === null) return "";
|
||||
if (!info.settings) return `${where}: "${type}" takes no settings`;
|
||||
return settingsProblem(step.settings, info.settings, `${where} settings`);
|
||||
if (!info.settings) return `"${type}" takes no settings`;
|
||||
return settingsProblem(step.settings, info.settings, "settings");
|
||||
}
|
||||
|
||||
/** The first way a settings value breaks its schema, or "". A string holding a
|
||||
|
||||
@@ -48,7 +48,7 @@ test("invalid workflow structures identify the field that prevents saving", () =
|
||||
for (const [source, message] of [
|
||||
["steps:\n - type: a\n", /id/],
|
||||
["id: x\n", /steps/],
|
||||
["id: x\nsteps:\n - type: a\n - settings: {}\n", /step 2/],
|
||||
["id: x\nsteps:\n - type: a\n - settings: {}\n", /type/],
|
||||
["- a\n- b\n", /mapping/],
|
||||
['"just a string"\n', /mapping/],
|
||||
["id: x\nsteps: 3\n", /steps/],
|
||||
|
||||
Reference in new issue
Block a user