@mark1russell7/workflow 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +16 -2
- package/build/index.d.ts +409 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1451 -0
- package/build/index.js.map +1 -0
- package/package.json +44 -3
- package/src/check.ts +430 -0
- package/src/edit.ts +267 -0
- package/src/index.ts +21 -0
- package/src/language.ts +225 -0
- package/src/sample.ts +64 -0
- package/src/world.ts +158 -0
package/src/check.ts
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { type Key, type Machine, type NodeGraph, check, cycles, isEnvelope, isRecord, nodeLayers } from "@mark1russell7/coeval";
|
|
2
|
+
import { workflowMachine } from "./language.ts";
|
|
3
|
+
|
|
4
|
+
/** One problem of a workflow, at the JSON path of its value. */
|
|
5
|
+
export interface WorkflowIssue {
|
|
6
|
+
readonly level: "error" | "warning";
|
|
7
|
+
/** A stable code, for example `needs-unknown`. A kernel issue keeps its code, for example `#NAME?`. */
|
|
8
|
+
readonly code: string;
|
|
9
|
+
/** The keys from the root JSON value of the document to the value with the problem. */
|
|
10
|
+
readonly at: readonly Key[];
|
|
11
|
+
readonly message: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The answer of a grant check: `ok`, and the words of the reason. */
|
|
15
|
+
export interface AccessAnswer {
|
|
16
|
+
readonly ok: boolean;
|
|
17
|
+
readonly why: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The grant check of the workspaces: can the workspace `from` read or write the workspace `target`? The host binds its
|
|
22
|
+
* model of the workspaces. A workflow runs with the grants of its own workspace only.
|
|
23
|
+
*/
|
|
24
|
+
export type Access = (from: string, access: "read" | "write", target: string) => AccessAnswer;
|
|
25
|
+
|
|
26
|
+
/** The facts of one procedure for a check. */
|
|
27
|
+
export interface ProcedureInfo {
|
|
28
|
+
/** The inputs that name a workspace, by the access of the call to that workspace. */
|
|
29
|
+
readonly workspaces?: { readonly read?: readonly string[]; readonly write?: readonly string[] };
|
|
30
|
+
/** The names of the inputs that the procedure needs. */
|
|
31
|
+
readonly required?: readonly string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The options of `checkWorkflow`. */
|
|
35
|
+
export interface WorkflowCheckOptions {
|
|
36
|
+
/** The machine. The default is `workflowMachine()`. An add-on gives the machine with its language. */
|
|
37
|
+
readonly machine?: Machine;
|
|
38
|
+
/** The grant check. Without it, the check finds no grant problem. */
|
|
39
|
+
readonly access?: Access;
|
|
40
|
+
/** The procedures that a step can call, by the path with dots, for example `git.clone`. Without it, each path is known. */
|
|
41
|
+
readonly procedures?: Readonly<Record<string, ProcedureInfo>>;
|
|
42
|
+
/** The names that the host binds, for example the contexts of a host. A reference to such a name is always valid. */
|
|
43
|
+
readonly hostNames?: readonly string[];
|
|
44
|
+
/** The namespaces of calls, besides `procedure`. An input of such a call can hold a secret. */
|
|
45
|
+
readonly callNamespaces?: readonly string[];
|
|
46
|
+
/** With `true`, a secret needs no declaration: the host gives the secrets of the workspace. The default is `false`. */
|
|
47
|
+
readonly openSecrets?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const ID = /^[A-Za-z_][A-Za-z0-9_-]*$/;
|
|
51
|
+
const CRON_FIELD = /^(\*|\d+(-\d+)?)(\/\d+)?(,(\*|\d+(-\d+)?)(\/\d+)?)*$|^[A-Za-z]{3}(-[A-Za-z]{3})?$/;
|
|
52
|
+
|
|
53
|
+
type Record_ = Readonly<Record<string, unknown>>;
|
|
54
|
+
const recordOr = (value: unknown): Record_ => (isRecord(value) ? value : {});
|
|
55
|
+
const listOr = (value: unknown): readonly unknown[] => (Array.isArray(value) ? value : []);
|
|
56
|
+
|
|
57
|
+
/** The names that an expression at a position can read, besides the root `event` and the names of the host. */
|
|
58
|
+
export interface WorkflowScope {
|
|
59
|
+
/** The names, for example `inputs` and `steps`. */
|
|
60
|
+
readonly names: ReadonlySet<string>;
|
|
61
|
+
/** The ids of the steps that the position can read: the earlier steps of the job. */
|
|
62
|
+
readonly steps?: ReadonlySet<string>;
|
|
63
|
+
/** The ids of the jobs that the position can read: the needs of the job. */
|
|
64
|
+
readonly needs?: ReadonlySet<string>;
|
|
65
|
+
/** The axes of the matrix of the job. `any` is a computed matrix. */
|
|
66
|
+
readonly matrix?: ReadonlySet<string> | "any" | undefined;
|
|
67
|
+
}
|
|
68
|
+
type Scope = WorkflowScope;
|
|
69
|
+
|
|
70
|
+
/** This function gives the axes of the matrix of a job: `undefined` with no matrix, `any` for a computed one. */
|
|
71
|
+
function matrixAxes(matrix: unknown): ReadonlySet<string> | "any" | undefined {
|
|
72
|
+
if (matrix === undefined) return undefined;
|
|
73
|
+
if (!isEnvelope(matrix) || matrix.type !== "matrix") return "any";
|
|
74
|
+
const m = recordOr(matrix.value);
|
|
75
|
+
const keys = new Set(Object.keys(recordOr(m["axes"])));
|
|
76
|
+
// A row of include can add a key. A computed include can add each key.
|
|
77
|
+
const include = m["include"];
|
|
78
|
+
if (include === undefined) return keys;
|
|
79
|
+
if (Array.isArray(include)) {
|
|
80
|
+
for (const row of include) for (const k of Object.keys(recordOr(row))) keys.add(k);
|
|
81
|
+
return keys;
|
|
82
|
+
}
|
|
83
|
+
if (isEnvelope(include) && include.type === "list" && Array.isArray(include.value)) {
|
|
84
|
+
for (const row of include.value) if (isEnvelope(row) && row.type === "record") for (const k of Object.keys(recordOr(row.value))) keys.add(k);
|
|
85
|
+
return keys;
|
|
86
|
+
}
|
|
87
|
+
return "any";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The part of a job that holds an expression. A step part gives the index of the step. */
|
|
91
|
+
type JobPart = "if" | "env" | "matrix" | "outputs" | { readonly step: number };
|
|
92
|
+
|
|
93
|
+
/** This function gives the scope of an expression in a part of a job. */
|
|
94
|
+
function jobScopeOf(job: Readonly<Record<string, unknown>>, part: JobPart): WorkflowScope {
|
|
95
|
+
const needs = new Set(listOr(job["needs"]).filter((need): need is string => typeof need === "string"));
|
|
96
|
+
if (part === "matrix") return { names: new Set(["inputs", "needs"]), needs };
|
|
97
|
+
const axes = matrixAxes(job["matrix"]);
|
|
98
|
+
const steps = listOr(job["steps"]);
|
|
99
|
+
const count = part === "outputs" ? steps.length : typeof part === "object" ? part.step : 0;
|
|
100
|
+
const seen = new Set<string>();
|
|
101
|
+
for (const step of steps.slice(0, count)) {
|
|
102
|
+
const id = isEnvelope(step) ? recordOr(step.value)["id"] : undefined;
|
|
103
|
+
if (typeof id === "string") seen.add(id);
|
|
104
|
+
}
|
|
105
|
+
const names = ["inputs", "env", "needs", "job", ...(axes === undefined ? [] : ["matrix"]), ...(seen.size > 0 ? ["steps"] : [])];
|
|
106
|
+
return { names: new Set(names), steps: seen, needs, matrix: axes };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* This function gives the scope of an expression at a JSON path of a workflow document: the names that it can read.
|
|
111
|
+
* The check uses the same rules. A builder uses it to offer only the values that a position can read.
|
|
112
|
+
*
|
|
113
|
+
* @param doc - The workflow document.
|
|
114
|
+
* @param at - The JSON path of the expression, for example `["value", "jobs", "build", "value", "steps", 1, "value", "do"]`.
|
|
115
|
+
* @returns The scope, or `undefined` when the path is not in a workflow.
|
|
116
|
+
*/
|
|
117
|
+
export function scopeAt(doc: unknown, at: readonly Key[]): WorkflowScope | undefined {
|
|
118
|
+
if (!isEnvelope(doc) || doc.type !== "workflow" || !isRecord(doc.value) || at[0] !== "value") return undefined;
|
|
119
|
+
const [, field, id, , part, index] = at;
|
|
120
|
+
if (field === "triggers") return { names: new Set() };
|
|
121
|
+
if (field === "env") return { names: new Set(["inputs"]) };
|
|
122
|
+
if (field === "outputs") return { names: new Set(["inputs", "jobs"]) };
|
|
123
|
+
if (field !== "jobs" || typeof id !== "string") return undefined;
|
|
124
|
+
const job = recordOr(doc.value["jobs"])[id];
|
|
125
|
+
if (!isEnvelope(job)) return undefined;
|
|
126
|
+
const j = recordOr(job.value);
|
|
127
|
+
if (part === "steps" && typeof index === "number") return jobScopeOf(j, { step: index });
|
|
128
|
+
if (part === "if" || part === "env" || part === "matrix" || part === "outputs") return jobScopeOf(j, part);
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** This function gives the reference paths in a document: each envelope `ref` and its path. */
|
|
133
|
+
function refsIn(value: unknown, at: readonly Key[], out: { path: readonly unknown[]; at: readonly Key[] }[] = []): { path: readonly unknown[]; at: readonly Key[] }[] {
|
|
134
|
+
if (Array.isArray(value)) value.forEach((item, index) => refsIn(item, [...at, index], out));
|
|
135
|
+
else if (isRecord(value)) {
|
|
136
|
+
if (value["type"] === "ref" && Array.isArray(value["value"])) out.push({ path: value["value"], at });
|
|
137
|
+
for (const [key, item] of Object.entries(value)) refsIn(item, [...at, key], out);
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const isEmptyText = (doc: unknown): boolean => doc === "" || (isEnvelope(doc) && doc.type === "lit" && doc.value === "");
|
|
143
|
+
const isSecretDoc = (doc: unknown): boolean => isRecord(doc) && doc["type"] === "secret";
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* This function tells if a document is the test of the presence of a secret: `eq` or `ne` of a secret and the empty
|
|
147
|
+
* text. The test reads one bit, not the value. The host answers it.
|
|
148
|
+
*
|
|
149
|
+
* @param doc - The document.
|
|
150
|
+
* @returns `true` for a presence test.
|
|
151
|
+
*/
|
|
152
|
+
export function isPresenceTest(doc: unknown): boolean {
|
|
153
|
+
if (!isEnvelope(doc) || !Array.isArray(doc.type) || doc.type.length !== 2 || doc.type[0] !== "ops") return false;
|
|
154
|
+
if (doc.type[1] !== "eq" && doc.type[1] !== "ne") return false;
|
|
155
|
+
const args = doc.value;
|
|
156
|
+
if (!Array.isArray(args) || args.length !== 2) return false;
|
|
157
|
+
return (isSecretDoc(args[0]) && isEmptyText(args[1])) || (isSecretDoc(args[1]) && isEmptyText(args[0]));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* This function gives the secret documents in a value, and tells if each one is in a permitted place. A place is
|
|
162
|
+
* permitted in the value of a call, in a map `env` and in a presence test, through plain records and lists only. Another document around
|
|
163
|
+
* the secret, for example a template or an operation, makes the place not permitted.
|
|
164
|
+
*/
|
|
165
|
+
function secretsIn(value: unknown, at: readonly Key[], allowed: boolean, calls: ReadonlySet<string>, out: { name: unknown; at: readonly Key[]; inCall: boolean }[] = []): { name: unknown; at: readonly Key[]; inCall: boolean }[] {
|
|
166
|
+
if (Array.isArray(value)) value.forEach((item, index) => secretsIn(item, [...at, index], allowed, calls, out));
|
|
167
|
+
else if (isRecord(value)) {
|
|
168
|
+
if (value["type"] === "secret") {
|
|
169
|
+
out.push({ name: value["value"], at, inCall: allowed });
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
if (isEnvelope(value)) {
|
|
173
|
+
const head = Array.isArray(value.type) ? value.type[0] : value.type;
|
|
174
|
+
// A presence test compares a secret with the empty text. It reads one bit, not the value.
|
|
175
|
+
const call = isPresenceTest(value) || (typeof head === "string" && calls.has(head));
|
|
176
|
+
secretsIn(value.value, [...at, "value"], call, calls, out);
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
for (const [key, item] of Object.entries(value)) secretsIn(item, [...at, key], key === "env" ? true : allowed, calls, out);
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* This function checks a workflow document. It gives each problem with its JSON path, and it does not throw.
|
|
186
|
+
*
|
|
187
|
+
* - The kernel checks the kinds, the shapes and the operations (`check`).
|
|
188
|
+
* - The workflow rules check the ids, the needs of the jobs and their cycles, and each reference of each expression.
|
|
189
|
+
* - They also check the secrets, the cron lines and the grants of the workspace.
|
|
190
|
+
*
|
|
191
|
+
* @param doc - The document `{ type: "workflow", value }`.
|
|
192
|
+
* @param options - The machine, the grant check and the procedures.
|
|
193
|
+
* @returns The problems. An empty list means a good workflow.
|
|
194
|
+
*/
|
|
195
|
+
export function checkWorkflow(doc: unknown, options: WorkflowCheckOptions = {}): readonly WorkflowIssue[] {
|
|
196
|
+
const issues: WorkflowIssue[] = [];
|
|
197
|
+
const error = (code: string, at: readonly Key[], message: string): void => void issues.push({ level: "error", code, at, message });
|
|
198
|
+
const warn = (code: string, at: readonly Key[], message: string): void => void issues.push({ level: "warning", code, at, message });
|
|
199
|
+
if (!isEnvelope(doc) || doc.type !== "workflow" || !isRecord(doc.value)) {
|
|
200
|
+
error("workflow-form", [], "A workflow is the document { type: \"workflow\", value: { name, workspace, triggers, jobs } }.");
|
|
201
|
+
return issues;
|
|
202
|
+
}
|
|
203
|
+
const machine = options.machine ?? workflowMachine();
|
|
204
|
+
const names = ["inputs", "env", "steps", "needs", "job", "jobs", "matrix", ...(options.hostNames ?? [])];
|
|
205
|
+
for (const issue of check(machine, doc, { names })) {
|
|
206
|
+
// A kernel issue has the JSON path of its value when it has one, else the place through the parts.
|
|
207
|
+
error(issue.code, issue.at ?? issue.place, issue.reason);
|
|
208
|
+
}
|
|
209
|
+
const value = doc.value;
|
|
210
|
+
const root: readonly Key[] = ["value"];
|
|
211
|
+
const workspace = typeof value["workspace"] === "string" ? value["workspace"] : "";
|
|
212
|
+
const inputs = new Set(Object.keys(recordOr(value["inputs"])));
|
|
213
|
+
const secretNames = new Set(Object.keys(recordOr(value["secrets"])));
|
|
214
|
+
const jobs = recordOr(value["jobs"]);
|
|
215
|
+
const jobIds = new Set(Object.keys(jobs));
|
|
216
|
+
const hostNames = options.hostNames ?? [];
|
|
217
|
+
|
|
218
|
+
const checkRefs = (expr: unknown, at: readonly Key[], scope: Scope): void => {
|
|
219
|
+
for (const ref of refsIn(expr, at)) {
|
|
220
|
+
const [first, name, key] = ref.path;
|
|
221
|
+
if (first === "event") continue;
|
|
222
|
+
if (first !== "names") {
|
|
223
|
+
error("ref-root", ref.at, `A workflow reads only names and the event. The root ${JSON.stringify(first)} is not one.`);
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (typeof name !== "string") continue;
|
|
227
|
+
if (hostNames.includes(name)) continue;
|
|
228
|
+
if (name === "secrets") {
|
|
229
|
+
error("secret-ref", ref.at, "A secret is not a name. Use a document { type: \"secret\", value: NAME } as an input of a call.");
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (!scope.names.has(name)) {
|
|
233
|
+
error("ref-name", ref.at, `The name ${name} is not available here. Here a workflow can read: ${[...scope.names].join(", ") || "only the event"}.`);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (typeof key !== "string") continue;
|
|
237
|
+
if (name === "inputs" && !inputs.has(key)) error("ref-input", ref.at, `The workflow has no input ${key}.`);
|
|
238
|
+
if (name === "steps" && scope.steps !== undefined && !scope.steps.has(key)) error("ref-step", ref.at, `No earlier step of this job has the id ${key}.`);
|
|
239
|
+
if (name === "needs" && scope.needs !== undefined && !scope.needs.has(key)) error("ref-needs", ref.at, `The job does not need the job ${key}. Add ${key} to its needs.`);
|
|
240
|
+
if (name === "jobs" && !jobIds.has(key)) error("ref-job", ref.at, `The workflow has no job ${key}.`);
|
|
241
|
+
if (name === "matrix" && scope.matrix !== undefined && scope.matrix !== "any" && !scope.matrix.has(key)) error("ref-matrix", ref.at, `The matrix of the job has no axis ${key}.`);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// The triggers.
|
|
246
|
+
listOr(value["triggers"]).forEach((trigger, index) => {
|
|
247
|
+
const at = [...root, "triggers", index];
|
|
248
|
+
if (!isEnvelope(trigger)) return;
|
|
249
|
+
const data = recordOr(trigger.value);
|
|
250
|
+
const kind = Array.isArray(trigger.type) ? trigger.type.join(".") : String(trigger.type);
|
|
251
|
+
if (kind === "trigger.schedule" && typeof data["cron"] === "string") {
|
|
252
|
+
const fields = data["cron"].trim().split(/\s+/);
|
|
253
|
+
if (fields.length !== 5 || !fields.every((field) => CRON_FIELD.test(field))) {
|
|
254
|
+
error("cron", [...at, "value", "cron"], `A cron line has five fields: minute, hour, day, month and weekday. ${JSON.stringify(data["cron"])} is not one.`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (kind === "trigger.feed" && typeof data["from"] === "string" && options.access !== undefined) {
|
|
258
|
+
const answer = options.access(workspace, "read", data["from"]);
|
|
259
|
+
if (!answer.ok) error("grant", [...at, "value", "from"], `The workspace ${workspace} cannot read ${data["from"]}: ${answer.why}`);
|
|
260
|
+
}
|
|
261
|
+
if (data["when"] !== undefined) checkRefs(data["when"], [...at, "value", "when"], { names: new Set() });
|
|
262
|
+
});
|
|
263
|
+
if (listOr(value["triggers"]).length === 0) warn("triggers-none", [...root, "triggers"], "The workflow has no trigger, thus nothing starts it.");
|
|
264
|
+
|
|
265
|
+
const top: Scope = { names: new Set(["inputs"]) };
|
|
266
|
+
for (const [key, expr] of Object.entries(recordOr(value["env"]))) checkRefs(expr, [...root, "env", key], top);
|
|
267
|
+
for (const [key, expr] of Object.entries(recordOr(value["outputs"]))) checkRefs(expr, [...root, "outputs", key], { names: new Set(["inputs", "jobs"]) });
|
|
268
|
+
|
|
269
|
+
// The jobs.
|
|
270
|
+
const graph = new Map<string, { edges: { to: string; lazy: boolean }[] }>();
|
|
271
|
+
for (const [id, job] of Object.entries(jobs)) {
|
|
272
|
+
const at = [...root, "jobs", id];
|
|
273
|
+
if (!ID.test(id)) error("job-id", at, `A job id has letters, digits, _ and -, and it does not start with a digit or -. ${JSON.stringify(id)} does not.`);
|
|
274
|
+
if (!isEnvelope(job) || job.type !== "job") {
|
|
275
|
+
error("job-form", at, "A job is the document { type: \"job\", value: { steps } }.");
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
const j = recordOr(job.value);
|
|
279
|
+
const needs = listOr(j["needs"]).filter((need): need is string => typeof need === "string");
|
|
280
|
+
graph.set(id, { edges: needs.map((to) => ({ to, lazy: false })) });
|
|
281
|
+
needs.forEach((need, index) => {
|
|
282
|
+
if (need === id) error("needs-self", [...at, "value", "needs", index], `The job ${id} cannot need itself.`);
|
|
283
|
+
else if (!jobIds.has(need)) error("needs-unknown", [...at, "value", "needs", index], `The workflow has no job ${need}.`);
|
|
284
|
+
});
|
|
285
|
+
if (j["matrix"] !== undefined) checkRefs(j["matrix"], [...at, "value", "matrix"], jobScopeOf(j, "matrix"));
|
|
286
|
+
if (j["if"] !== undefined) checkRefs(j["if"], [...at, "value", "if"], jobScopeOf(j, "if"));
|
|
287
|
+
for (const [key, expr] of Object.entries(recordOr(j["env"]))) checkRefs(expr, [...at, "value", "env", key], jobScopeOf(j, "env"));
|
|
288
|
+
const seen = new Set<string>();
|
|
289
|
+
listOr(j["steps"]).forEach((step, index) => {
|
|
290
|
+
const sat = [...at, "value", "steps", index];
|
|
291
|
+
if (!isEnvelope(step) || step.type !== "step") {
|
|
292
|
+
error("step-form", sat, "A step is the document { type: \"step\", value: { do } }.");
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const s = recordOr(step.value);
|
|
296
|
+
const scope = jobScopeOf(j, { step: index });
|
|
297
|
+
for (const field of ["if", "do", "env"] as const) if (s[field] !== undefined) checkRefs(s[field], [...sat, "value", field], scope);
|
|
298
|
+
const call = s["do"];
|
|
299
|
+
if (isEnvelope(call) && Array.isArray(call.type) && call.type[0] === "procedure") checkCall(call.type.slice(1).join("."), recordOr(call.value), [...sat, "value", "do"]);
|
|
300
|
+
if (typeof s["id"] === "string") {
|
|
301
|
+
if (!ID.test(s["id"])) error("step-id", [...sat, "value", "id"], `A step id has letters, digits, _ and -. ${JSON.stringify(s["id"])} does not.`);
|
|
302
|
+
else if (seen.has(s["id"])) error("step-id-twice", [...sat, "value", "id"], `Two steps of the job ${id} have the id ${s["id"]}.`);
|
|
303
|
+
seen.add(s["id"]);
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
if (listOr(j["steps"]).length === 0) warn("steps-none", [...at, "value", "steps"], `The job ${id} has no step.`);
|
|
307
|
+
for (const [key, expr] of Object.entries(recordOr(j["outputs"]))) checkRefs(expr, [...at, "value", "outputs", key], jobScopeOf(j, "outputs"));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function checkCall(path: string, inputsOfCall: Record_, at: readonly Key[]): void {
|
|
311
|
+
const info = options.procedures === undefined ? undefined : options.procedures[path];
|
|
312
|
+
if (options.procedures !== undefined && info === undefined) {
|
|
313
|
+
warn("procedure-unknown", at, `No procedure has the path ${path}.`);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
for (const name of info?.required ?? []) if (!(name in inputsOfCall)) error("input-missing", [...at, "value"], `The procedure ${path} needs the input ${name}.`);
|
|
317
|
+
if (options.access === undefined) return;
|
|
318
|
+
for (const access of ["read", "write"] as const) {
|
|
319
|
+
for (const name of info?.workspaces?.[access] ?? []) {
|
|
320
|
+
const target = inputsOfCall[name];
|
|
321
|
+
if (typeof target !== "string") {
|
|
322
|
+
warn("grant-at-run", [...at, "value", name], `The input ${name} is computed, thus the runner checks the grant at the run.`);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const answer = options.access(workspace, access, target);
|
|
326
|
+
if (!answer.ok) error("grant", [...at, "value", name], `The workspace ${workspace} cannot ${access} ${target}: ${answer.why}`);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// The cycles of the needs.
|
|
332
|
+
for (const cycle of cycles(graph as NodeGraph<string>)) {
|
|
333
|
+
if (cycle.certain) error("needs-cycle", [...root, "jobs", cycle.nodes[0] as string, "value", "needs"], `The jobs ${cycle.nodes.join(", ")} need each other in a cycle.`);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// The secrets: each one is declared, and only a call reads it.
|
|
337
|
+
const calls = new Set(["procedure", ...(options.callNamespaces ?? [])]);
|
|
338
|
+
for (const secret of secretsIn(value, root, false, calls)) {
|
|
339
|
+
if (options.openSecrets !== true && typeof secret.name === "string" && !secretNames.has(secret.name)) error("secret-unknown", secret.at, `The workflow declares no secret ${secret.name}.`);
|
|
340
|
+
if (!secret.inCall) error("secret-place", secret.at, "A secret can only be an input of a call, a value of env or a test of its presence (secret != ''). A text, a condition or an output cannot hold it.");
|
|
341
|
+
}
|
|
342
|
+
return issues;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** One node of the graph of a workflow. */
|
|
346
|
+
export interface WorkflowNode {
|
|
347
|
+
readonly id: string;
|
|
348
|
+
readonly kind: "trigger" | "job" | "step";
|
|
349
|
+
/** The job of a step. */
|
|
350
|
+
readonly job?: string;
|
|
351
|
+
readonly label: string;
|
|
352
|
+
/** The JSON path of the node in the document. */
|
|
353
|
+
readonly at: readonly Key[];
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** One edge of the graph of a workflow. `needs` orders two jobs, `data` carries a value, `start` joins a trigger to a job. */
|
|
357
|
+
export interface WorkflowEdge {
|
|
358
|
+
readonly from: string;
|
|
359
|
+
readonly to: string;
|
|
360
|
+
readonly kind: "needs" | "data" | "start" | "next";
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** The graph of a workflow: its nodes, its edges, and the layers of its jobs in the order of their needs. */
|
|
364
|
+
export interface WorkflowGraph {
|
|
365
|
+
readonly nodes: readonly WorkflowNode[];
|
|
366
|
+
readonly edges: readonly WorkflowEdge[];
|
|
367
|
+
readonly layers: readonly (readonly string[])[];
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function labelOf(value: Record_, fallback: string): string {
|
|
371
|
+
return typeof value["name"] === "string" && value["name"] !== "" ? value["name"] : fallback;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** This function gives the text of the path of a call, for example `git.clone`, or an empty text. */
|
|
375
|
+
export function callPath(doc: unknown): string {
|
|
376
|
+
return isEnvelope(doc) && Array.isArray(doc.type) && doc.type[0] === "procedure" ? doc.type.slice(1).join(".") : "";
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* This function gives the graph of a workflow from its document. The data edges come from the references of the
|
|
381
|
+
* expressions. Thus the graph shows each value that moves from a step or a job to another.
|
|
382
|
+
*
|
|
383
|
+
* @param doc - The workflow document.
|
|
384
|
+
* @returns The nodes, the edges, and the layers of the jobs.
|
|
385
|
+
*/
|
|
386
|
+
export function workflowGraph(doc: unknown): WorkflowGraph {
|
|
387
|
+
const nodes: WorkflowNode[] = [];
|
|
388
|
+
const edges: WorkflowEdge[] = [];
|
|
389
|
+
if (!isEnvelope(doc) || !isRecord(doc.value)) return { nodes, edges, layers: [] };
|
|
390
|
+
const value = doc.value;
|
|
391
|
+
const jobs = recordOr(value["jobs"]);
|
|
392
|
+
const jobGraph = new Map<string, { edges: { to: string; lazy: boolean }[] }>();
|
|
393
|
+
listOr(value["triggers"]).forEach((trigger, index) => {
|
|
394
|
+
const kind = isEnvelope(trigger) && Array.isArray(trigger.type) ? String(trigger.type[1]) : "trigger";
|
|
395
|
+
const data = isEnvelope(trigger) ? recordOr(trigger.value) : {};
|
|
396
|
+
const detail = typeof data["event"] === "string" ? data["event"] : typeof data["cron"] === "string" ? data["cron"] : typeof data["from"] === "string" ? data["from"] : "";
|
|
397
|
+
nodes.push({ id: `trigger:${index}`, kind: "trigger", label: detail === "" ? kind : `${kind}: ${detail}`, at: ["value", "triggers", index] });
|
|
398
|
+
});
|
|
399
|
+
for (const [id, job] of Object.entries(jobs)) {
|
|
400
|
+
const j = isEnvelope(job) ? recordOr(job.value) : {};
|
|
401
|
+
const needs = listOr(j["needs"]).filter((need): need is string => typeof need === "string" && need in jobs);
|
|
402
|
+
jobGraph.set(id, { edges: needs.map((to) => ({ to, lazy: false })) });
|
|
403
|
+
nodes.push({ id: `job:${id}`, kind: "job", label: labelOf(j, id), at: ["value", "jobs", id] });
|
|
404
|
+
for (const need of needs) edges.push({ from: `job:${need}`, to: `job:${id}`, kind: "needs" });
|
|
405
|
+
if (needs.length === 0) listOr(value["triggers"]).forEach((_, index) => edges.push({ from: `trigger:${index}`, to: `job:${id}`, kind: "start" }));
|
|
406
|
+
const stepIds = new Map<string, string>();
|
|
407
|
+
let previous: string | undefined;
|
|
408
|
+
listOr(j["steps"]).forEach((step, index) => {
|
|
409
|
+
const s = isEnvelope(step) ? recordOr(step.value) : {};
|
|
410
|
+
const node = `step:${id}:${index}`;
|
|
411
|
+
const path = callPath(s["do"]);
|
|
412
|
+
nodes.push({ id: node, kind: "step", job: id, label: labelOf(s, typeof s["id"] === "string" ? s["id"] : path || `step ${index + 1}`), at: ["value", "jobs", id, "value", "steps", index] });
|
|
413
|
+
edges.push({ from: previous ?? `job:${id}`, to: node, kind: "next" });
|
|
414
|
+
previous = node;
|
|
415
|
+
const seen = new Set<string>();
|
|
416
|
+
for (const ref of refsIn(step, [])) {
|
|
417
|
+
const [first, name, key] = ref.path;
|
|
418
|
+
if (first !== "names" || typeof key !== "string") continue;
|
|
419
|
+
const from = name === "steps" ? stepIds.get(key) : name === "needs" && key in jobs ? `job:${key}` : undefined;
|
|
420
|
+
if (from !== undefined && !seen.has(from)) {
|
|
421
|
+
seen.add(from);
|
|
422
|
+
edges.push({ from, to: node, kind: "data" });
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (typeof s["id"] === "string") stepIds.set(s["id"], node);
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
const layers = nodeLayers(jobGraph as NodeGraph<string>);
|
|
429
|
+
return { nodes, edges, layers };
|
|
430
|
+
}
|