@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/build/index.js
ADDED
|
@@ -0,0 +1,1451 @@
|
|
|
1
|
+
import { STD_OPS, check, compose, cycles, defineLanguage, fail, formulaMachine, isEnvelope, isRecord, nodeLayers } from "@mark1russell7/coeval";
|
|
2
|
+
// src/world.ts
|
|
3
|
+
/** The name of the format of a workflow document. */
|
|
4
|
+
const WORKFLOW_FORMAT = "workflow/1";
|
|
5
|
+
const text = { shape: "text" };
|
|
6
|
+
/** An expression: a document, or a bare JSON value that is a literal. */
|
|
7
|
+
const term = {
|
|
8
|
+
shape: "term",
|
|
9
|
+
of: "lit"
|
|
10
|
+
};
|
|
11
|
+
/** A document of the workflow: a trigger, a job, a step or a call. */
|
|
12
|
+
const node = { shape: "term" };
|
|
13
|
+
const data = { shape: "none" };
|
|
14
|
+
const terms = {
|
|
15
|
+
shape: "map",
|
|
16
|
+
item: term
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The world of the workflow language. The formula kit of the kernel gives the expressions: `lit`, `ref`, `if`,
|
|
20
|
+
* `and`, `or` and the operations. This world adds the structure, the calls, the text templates and the secrets.
|
|
21
|
+
*/
|
|
22
|
+
const WORKFLOW_WORLD = { kinds: {
|
|
23
|
+
workflow: {
|
|
24
|
+
verbs: { eval: "workflow.workflow" },
|
|
25
|
+
parts: {
|
|
26
|
+
shape: "record",
|
|
27
|
+
fields: {
|
|
28
|
+
name: text,
|
|
29
|
+
summary: text,
|
|
30
|
+
workspace: text,
|
|
31
|
+
triggers: {
|
|
32
|
+
shape: "list",
|
|
33
|
+
item: node
|
|
34
|
+
},
|
|
35
|
+
inputs: data,
|
|
36
|
+
secrets: data,
|
|
37
|
+
env: terms,
|
|
38
|
+
jobs: {
|
|
39
|
+
shape: "map",
|
|
40
|
+
item: node
|
|
41
|
+
},
|
|
42
|
+
outputs: terms,
|
|
43
|
+
layers: data
|
|
44
|
+
},
|
|
45
|
+
required: [
|
|
46
|
+
"name",
|
|
47
|
+
"workspace",
|
|
48
|
+
"triggers",
|
|
49
|
+
"jobs"
|
|
50
|
+
],
|
|
51
|
+
closed: true
|
|
52
|
+
},
|
|
53
|
+
summary: "A workflow: its triggers, its inputs, the names of its secrets, and its jobs. It belongs to one workspace."
|
|
54
|
+
},
|
|
55
|
+
job: {
|
|
56
|
+
verbs: { eval: "workflow.job" },
|
|
57
|
+
parts: {
|
|
58
|
+
shape: "record",
|
|
59
|
+
fields: {
|
|
60
|
+
name: text,
|
|
61
|
+
needs: {
|
|
62
|
+
shape: "list",
|
|
63
|
+
item: text
|
|
64
|
+
},
|
|
65
|
+
if: term,
|
|
66
|
+
matrix: node,
|
|
67
|
+
env: terms,
|
|
68
|
+
steps: {
|
|
69
|
+
shape: "list",
|
|
70
|
+
item: node
|
|
71
|
+
},
|
|
72
|
+
outputs: terms,
|
|
73
|
+
timeout: {
|
|
74
|
+
shape: "number",
|
|
75
|
+
min: 0
|
|
76
|
+
},
|
|
77
|
+
layers: data
|
|
78
|
+
},
|
|
79
|
+
required: ["steps"],
|
|
80
|
+
closed: true
|
|
81
|
+
},
|
|
82
|
+
summary: "A job: steps in order. The jobs run at the same time, unless one needs another."
|
|
83
|
+
},
|
|
84
|
+
step: {
|
|
85
|
+
verbs: { eval: "workflow.step" },
|
|
86
|
+
parts: {
|
|
87
|
+
shape: "record",
|
|
88
|
+
fields: {
|
|
89
|
+
id: text,
|
|
90
|
+
name: text,
|
|
91
|
+
if: term,
|
|
92
|
+
do: node,
|
|
93
|
+
env: terms,
|
|
94
|
+
retry: data,
|
|
95
|
+
timeout: {
|
|
96
|
+
shape: "number",
|
|
97
|
+
min: 0
|
|
98
|
+
},
|
|
99
|
+
continue: {
|
|
100
|
+
shape: "one",
|
|
101
|
+
values: [true, false]
|
|
102
|
+
},
|
|
103
|
+
layers: data
|
|
104
|
+
},
|
|
105
|
+
required: ["do"],
|
|
106
|
+
closed: true
|
|
107
|
+
},
|
|
108
|
+
summary: "One step of a job: a call of a procedure, with a condition, a retry rule and a time limit."
|
|
109
|
+
},
|
|
110
|
+
matrix: {
|
|
111
|
+
verbs: { eval: "workflow.matrix" },
|
|
112
|
+
parts: {
|
|
113
|
+
shape: "record",
|
|
114
|
+
fields: {
|
|
115
|
+
axes: terms,
|
|
116
|
+
include: term,
|
|
117
|
+
exclude: term,
|
|
118
|
+
parallel: {
|
|
119
|
+
shape: "number",
|
|
120
|
+
whole: true,
|
|
121
|
+
min: 1
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
closed: true
|
|
125
|
+
},
|
|
126
|
+
summary: "A job runs one time for each combination of the values of its axes."
|
|
127
|
+
},
|
|
128
|
+
trigger: { kinds: {
|
|
129
|
+
manual: {
|
|
130
|
+
verbs: { eval: "workflow.trigger" },
|
|
131
|
+
parts: {
|
|
132
|
+
shape: "record",
|
|
133
|
+
fields: {
|
|
134
|
+
summary: text,
|
|
135
|
+
layers: data
|
|
136
|
+
},
|
|
137
|
+
closed: true
|
|
138
|
+
},
|
|
139
|
+
summary: "A person starts the workflow by a click, with the inputs of the workflow."
|
|
140
|
+
},
|
|
141
|
+
schedule: {
|
|
142
|
+
verbs: { eval: "workflow.trigger" },
|
|
143
|
+
parts: {
|
|
144
|
+
shape: "record",
|
|
145
|
+
fields: {
|
|
146
|
+
cron: text,
|
|
147
|
+
zone: text,
|
|
148
|
+
layers: data
|
|
149
|
+
},
|
|
150
|
+
required: ["cron"],
|
|
151
|
+
closed: true
|
|
152
|
+
},
|
|
153
|
+
summary: "The clock starts the workflow at each time of a cron line, in a time zone."
|
|
154
|
+
},
|
|
155
|
+
event: {
|
|
156
|
+
verbs: { eval: "workflow.trigger" },
|
|
157
|
+
parts: {
|
|
158
|
+
shape: "record",
|
|
159
|
+
fields: {
|
|
160
|
+
event: text,
|
|
161
|
+
types: {
|
|
162
|
+
shape: "list",
|
|
163
|
+
item: text
|
|
164
|
+
},
|
|
165
|
+
when: term,
|
|
166
|
+
layers: data
|
|
167
|
+
},
|
|
168
|
+
required: ["event"],
|
|
169
|
+
closed: true
|
|
170
|
+
},
|
|
171
|
+
summary: "An event of the host starts the workflow. The condition when selects the events."
|
|
172
|
+
},
|
|
173
|
+
feed: {
|
|
174
|
+
verbs: { eval: "workflow.trigger" },
|
|
175
|
+
parts: {
|
|
176
|
+
shape: "record",
|
|
177
|
+
fields: {
|
|
178
|
+
from: text,
|
|
179
|
+
collection: text,
|
|
180
|
+
when: term,
|
|
181
|
+
layers: data
|
|
182
|
+
},
|
|
183
|
+
required: ["from", "collection"],
|
|
184
|
+
closed: true
|
|
185
|
+
},
|
|
186
|
+
summary: "A new or changed record of a collection of a workspace starts the workflow. This is a read of that workspace."
|
|
187
|
+
},
|
|
188
|
+
call: {
|
|
189
|
+
verbs: { eval: "workflow.trigger" },
|
|
190
|
+
parts: {
|
|
191
|
+
shape: "record",
|
|
192
|
+
fields: {
|
|
193
|
+
summary: text,
|
|
194
|
+
layers: data
|
|
195
|
+
},
|
|
196
|
+
closed: true
|
|
197
|
+
},
|
|
198
|
+
summary: "Another workflow calls this workflow as a procedure, with its inputs and its outputs."
|
|
199
|
+
}
|
|
200
|
+
} },
|
|
201
|
+
procedure: {
|
|
202
|
+
kinds: {},
|
|
203
|
+
fallback: {
|
|
204
|
+
verbs: { eval: "workflow.call" },
|
|
205
|
+
parts: {
|
|
206
|
+
shape: "record",
|
|
207
|
+
fields: {},
|
|
208
|
+
rest: term
|
|
209
|
+
},
|
|
210
|
+
side: "run",
|
|
211
|
+
effects: "type",
|
|
212
|
+
summary: "A call of a procedure. The path after procedure names the procedure, and the value holds its inputs."
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
template: {
|
|
216
|
+
verbs: { eval: "workflow.template" },
|
|
217
|
+
parts: {
|
|
218
|
+
shape: "list",
|
|
219
|
+
of: "lit"
|
|
220
|
+
},
|
|
221
|
+
pure: true,
|
|
222
|
+
summary: "A text with documents in it. Each text part stays, and each document gives its value as text."
|
|
223
|
+
},
|
|
224
|
+
secret: {
|
|
225
|
+
verbs: { eval: "workflow.secret" },
|
|
226
|
+
pure: true,
|
|
227
|
+
summary: "A reference to a secret by its name. It never holds the value, and only a call can use it."
|
|
228
|
+
}
|
|
229
|
+
} };
|
|
230
|
+
// src/language.ts
|
|
231
|
+
/** The version of the workflow language. */
|
|
232
|
+
const WORKFLOW_VERSION = "0.1.0";
|
|
233
|
+
/** This function tells if a value is a reference to a secret. */
|
|
234
|
+
function isSecretRef(value) {
|
|
235
|
+
return isRecord(value) && typeof value["secret"] === "string" && Object.keys(value).length === 1 && Object.isFrozen(value);
|
|
236
|
+
}
|
|
237
|
+
const ok = (value) => ({
|
|
238
|
+
ok: true,
|
|
239
|
+
value
|
|
240
|
+
});
|
|
241
|
+
function scalarText(value) {
|
|
242
|
+
if (typeof value === "string") return value;
|
|
243
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
244
|
+
if (value === null) return "";
|
|
245
|
+
}
|
|
246
|
+
function noSecret(op, args) {
|
|
247
|
+
if (args.some(isSecretRef)) throw new TypeError(`The operation ${op} cannot read a secret. Only a call can use a secret.`);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* The operations of the workflow language, beside the operations of the formula kit. Each one is a general operation on
|
|
251
|
+
* texts and lists. No operation reads a secret.
|
|
252
|
+
*/
|
|
253
|
+
const OPS = {
|
|
254
|
+
contains: (...args) => {
|
|
255
|
+
noSecret("contains", args);
|
|
256
|
+
const [where, what] = args;
|
|
257
|
+
if (Array.isArray(where)) return where.some((item) => item === what);
|
|
258
|
+
const a = scalarText(where);
|
|
259
|
+
const b = scalarText(what);
|
|
260
|
+
if (a === void 0 || b === void 0) throw new TypeError("contains takes a list or a text, and a value.");
|
|
261
|
+
return a.toLowerCase().includes(b.toLowerCase());
|
|
262
|
+
},
|
|
263
|
+
startsWith: (...args) => {
|
|
264
|
+
noSecret("startsWith", args);
|
|
265
|
+
const [a, b] = args.map(scalarText);
|
|
266
|
+
if (a === void 0 || b === void 0) throw new TypeError("startsWith takes two texts.");
|
|
267
|
+
return a.toLowerCase().startsWith(b.toLowerCase());
|
|
268
|
+
},
|
|
269
|
+
endsWith: (...args) => {
|
|
270
|
+
noSecret("endsWith", args);
|
|
271
|
+
const [a, b] = args.map(scalarText);
|
|
272
|
+
if (a === void 0 || b === void 0) throw new TypeError("endsWith takes two texts.");
|
|
273
|
+
return a.toLowerCase().endsWith(b.toLowerCase());
|
|
274
|
+
},
|
|
275
|
+
join: (...args) => {
|
|
276
|
+
noSecret("join", args);
|
|
277
|
+
const [list, separator = ","] = args;
|
|
278
|
+
const by = scalarText(separator) ?? ",";
|
|
279
|
+
if (!Array.isArray(list)) return scalarText(list) ?? "";
|
|
280
|
+
return list.map((item) => scalarText(item) ?? "").join(by);
|
|
281
|
+
},
|
|
282
|
+
toJSON: (...args) => {
|
|
283
|
+
noSecret("toJSON", args);
|
|
284
|
+
return JSON.stringify(args[0] ?? null, null, 2);
|
|
285
|
+
},
|
|
286
|
+
fromJSON: (...args) => {
|
|
287
|
+
const text = scalarText(args[0]);
|
|
288
|
+
if (text === void 0) throw new TypeError("fromJSON takes a text.");
|
|
289
|
+
return JSON.parse(text);
|
|
290
|
+
},
|
|
291
|
+
format: (...args) => {
|
|
292
|
+
noSecret("format", args);
|
|
293
|
+
const [pattern, ...values] = args;
|
|
294
|
+
const text = scalarText(pattern);
|
|
295
|
+
if (text === void 0) throw new TypeError("format takes a text first.");
|
|
296
|
+
return text.replace(/\{\{|\}\}|\{(\d+)\}/g, (match, index) => {
|
|
297
|
+
if (match === "{{") return "{";
|
|
298
|
+
if (match === "}}") return "}";
|
|
299
|
+
const value = values[Number(index)];
|
|
300
|
+
return scalarText(value) ?? JSON.stringify(value);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
const TEXT_2 = {
|
|
305
|
+
params: ["unknown", "unknown"],
|
|
306
|
+
to: "bool"
|
|
307
|
+
};
|
|
308
|
+
const SIGS = {
|
|
309
|
+
contains: TEXT_2,
|
|
310
|
+
startsWith: TEXT_2,
|
|
311
|
+
endsWith: TEXT_2,
|
|
312
|
+
join: {
|
|
313
|
+
params: ["unknown", "unknown"],
|
|
314
|
+
to: "text"
|
|
315
|
+
},
|
|
316
|
+
toJSON: {
|
|
317
|
+
params: ["unknown"],
|
|
318
|
+
to: "text"
|
|
319
|
+
},
|
|
320
|
+
fromJSON: {
|
|
321
|
+
params: ["text"],
|
|
322
|
+
to: "unknown"
|
|
323
|
+
},
|
|
324
|
+
format: {
|
|
325
|
+
params: [
|
|
326
|
+
"text",
|
|
327
|
+
"unknown",
|
|
328
|
+
"unknown",
|
|
329
|
+
"unknown",
|
|
330
|
+
"unknown"
|
|
331
|
+
],
|
|
332
|
+
to: "text"
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
/** This function gives the combinations of the axes of a matrix, without the excluded ones, with the included ones. */
|
|
336
|
+
function expandMatrix(axes, include = [], exclude = []) {
|
|
337
|
+
const lists = Object.entries(axes);
|
|
338
|
+
let rows = lists.length === 0 ? [] : [{}];
|
|
339
|
+
for (const [key, values] of lists) {
|
|
340
|
+
if (!Array.isArray(values)) throw new TypeError(`The axis ${key} of a matrix must be a list.`);
|
|
341
|
+
rows = rows.flatMap((row) => values.map((value) => ({
|
|
342
|
+
...row,
|
|
343
|
+
[key]: value
|
|
344
|
+
})));
|
|
345
|
+
}
|
|
346
|
+
const fits = (entry, row, keys) => isRecord(entry) && [...keys].every((key) => !(key in entry) || JSON.stringify(entry[key]) === JSON.stringify(row[key]));
|
|
347
|
+
rows = rows.filter((row) => !exclude.some((entry) => isRecord(entry) && fits(entry, row, Object.keys(entry))));
|
|
348
|
+
const axisKeys = lists.map(([key]) => key);
|
|
349
|
+
for (const entry of include) {
|
|
350
|
+
if (!isRecord(entry)) continue;
|
|
351
|
+
const matches = rows.filter((row) => fits(entry, row, axisKeys.filter((key) => key in entry)) && axisKeys.some((key) => key in entry));
|
|
352
|
+
if (matches.length === 0) rows.push({ ...entry });
|
|
353
|
+
else for (const row of matches) Object.assign(row, entry);
|
|
354
|
+
}
|
|
355
|
+
return rows;
|
|
356
|
+
}
|
|
357
|
+
/** The workflow language: the kinds of `WORKFLOW_WORLD` and their code, on the formula kit of the kernel. */
|
|
358
|
+
const WORKFLOW_LANGUAGE = defineLanguage({
|
|
359
|
+
name: "workflow",
|
|
360
|
+
version: WORKFLOW_VERSION,
|
|
361
|
+
world: WORKFLOW_WORLD,
|
|
362
|
+
code: {
|
|
363
|
+
evaluators: {
|
|
364
|
+
"workflow.secret": ({ data }) => typeof data === "string" && /^[A-Za-z_][A-Za-z0-9_]*$/.test(data) ? ok(Object.freeze({ secret: data })) : fail("#VALUE!", "A secret holds the name of a secret: letters, digits and _, not first a digit."),
|
|
365
|
+
"workflow.template": ({ parts }) => {
|
|
366
|
+
let out = "";
|
|
367
|
+
for (const part of parts) {
|
|
368
|
+
const result = part.evaluate();
|
|
369
|
+
if (!result.ok) return result;
|
|
370
|
+
if (isSecretRef(result.value)) return fail("#VALUE!", "A secret cannot be part of a text. Give it to a call as an input.");
|
|
371
|
+
const text = scalarText(result.value);
|
|
372
|
+
out += text ?? JSON.stringify(result.value);
|
|
373
|
+
}
|
|
374
|
+
return ok(out);
|
|
375
|
+
},
|
|
376
|
+
"workflow.matrix": ({ parts }) => {
|
|
377
|
+
const axes = {};
|
|
378
|
+
let include = [];
|
|
379
|
+
let exclude = [];
|
|
380
|
+
for (const part of parts) {
|
|
381
|
+
const result = part.evaluate();
|
|
382
|
+
if (!result.ok) return result;
|
|
383
|
+
const [field, key] = part.path;
|
|
384
|
+
if (field === "axes" && typeof key === "string") axes[key] = result.value;
|
|
385
|
+
else if (field === "include") include = Array.isArray(result.value) ? result.value : [];
|
|
386
|
+
else if (field === "exclude") exclude = Array.isArray(result.value) ? result.value : [];
|
|
387
|
+
}
|
|
388
|
+
try {
|
|
389
|
+
return ok(expandMatrix(axes, include, exclude));
|
|
390
|
+
} catch (error) {
|
|
391
|
+
return fail("#VALUE!", error instanceof Error ? error.message : String(error));
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
"workflow.trigger": ({ kind, data }) => ok({
|
|
395
|
+
kind,
|
|
396
|
+
...isRecord(data) ? data : {}
|
|
397
|
+
}),
|
|
398
|
+
"workflow.workflow": () => fail("#CALC!", "A workflow runs through a runner, not through an evaluation."),
|
|
399
|
+
"workflow.job": () => fail("#CALC!", "A job runs through a runner, not through an evaluation."),
|
|
400
|
+
"workflow.step": () => fail("#CALC!", "A step runs through a runner, not through an evaluation."),
|
|
401
|
+
"workflow.call": () => fail("#CALC!", "A call runs through a runner, not through an evaluation.")
|
|
402
|
+
},
|
|
403
|
+
runners: { "workflow.call": function* ({ kind, parts, effect }) {
|
|
404
|
+
const inputs = {};
|
|
405
|
+
for (const part of parts) {
|
|
406
|
+
const result = yield* part.evaluate();
|
|
407
|
+
if (!result.ok) return result;
|
|
408
|
+
inputs[part.key] = result.value;
|
|
409
|
+
}
|
|
410
|
+
return yield* effect(kind, inputs);
|
|
411
|
+
} }
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
/** The formula profile of the expressions of a workflow: the operations of the kernel and the text operations. */
|
|
415
|
+
const WORKFLOW_OPS = {
|
|
416
|
+
ops: {
|
|
417
|
+
...STD_OPS.ops,
|
|
418
|
+
...OPS
|
|
419
|
+
},
|
|
420
|
+
lazy: STD_OPS.lazy,
|
|
421
|
+
sigs: {
|
|
422
|
+
...STD_OPS.sigs,
|
|
423
|
+
...SIGS
|
|
424
|
+
},
|
|
425
|
+
pure: [...STD_OPS.pure, ...Object.keys(OPS)]
|
|
426
|
+
};
|
|
427
|
+
/**
|
|
428
|
+
* This function gives the machine of the workflow language: the formula kit with the operations of a workflow, and
|
|
429
|
+
* the kinds of the workflow world. An add-on composes its own language after it.
|
|
430
|
+
*
|
|
431
|
+
* @param more - The languages of add-ons, in order.
|
|
432
|
+
* @returns The machine.
|
|
433
|
+
*/
|
|
434
|
+
function workflowMachine(...more) {
|
|
435
|
+
return workflowMachineWith(WORKFLOW_OPS, ...more);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* This function gives the machine of the workflow language with another formula profile. An add-on gives the
|
|
439
|
+
* operations of its host here, for example `WORKFLOW_OPS` with more operations.
|
|
440
|
+
*
|
|
441
|
+
* @param profile - The formula profile of the expressions.
|
|
442
|
+
* @param more - The languages of add-ons, in order.
|
|
443
|
+
* @returns The machine.
|
|
444
|
+
*/
|
|
445
|
+
function workflowMachineWith(profile, ...more) {
|
|
446
|
+
const { world, ...code } = formulaMachine(profile);
|
|
447
|
+
const kit = defineLanguage({
|
|
448
|
+
name: "formula",
|
|
449
|
+
version: "1.0.0",
|
|
450
|
+
world,
|
|
451
|
+
code
|
|
452
|
+
});
|
|
453
|
+
return compose(kit, WORKFLOW_LANGUAGE, ...more);
|
|
454
|
+
}
|
|
455
|
+
// src/check.ts
|
|
456
|
+
const ID = /^[A-Za-z_][A-Za-z0-9_-]*$/;
|
|
457
|
+
const CRON_FIELD = /^(\*|\d+(-\d+)?)(\/\d+)?(,(\*|\d+(-\d+)?)(\/\d+)?)*$|^[A-Za-z]{3}(-[A-Za-z]{3})?$/;
|
|
458
|
+
const recordOr$1 = (value) => isRecord(value) ? value : {};
|
|
459
|
+
const listOr$1 = (value) => Array.isArray(value) ? value : [];
|
|
460
|
+
/** This function gives the axes of the matrix of a job: `undefined` with no matrix, `any` for a computed one. */
|
|
461
|
+
function matrixAxes(matrix) {
|
|
462
|
+
if (matrix === void 0) return void 0;
|
|
463
|
+
if (!isEnvelope(matrix) || matrix.type !== "matrix") return "any";
|
|
464
|
+
const m = recordOr$1(matrix.value);
|
|
465
|
+
const keys = new Set(Object.keys(recordOr$1(m["axes"])));
|
|
466
|
+
const include = m["include"];
|
|
467
|
+
if (include === void 0) return keys;
|
|
468
|
+
if (Array.isArray(include)) {
|
|
469
|
+
for (const row of include) for (const k of Object.keys(recordOr$1(row))) keys.add(k);
|
|
470
|
+
return keys;
|
|
471
|
+
}
|
|
472
|
+
if (isEnvelope(include) && include.type === "list" && Array.isArray(include.value)) {
|
|
473
|
+
for (const row of include.value) if (isEnvelope(row) && row.type === "record") for (const k of Object.keys(recordOr$1(row.value))) keys.add(k);
|
|
474
|
+
return keys;
|
|
475
|
+
}
|
|
476
|
+
return "any";
|
|
477
|
+
}
|
|
478
|
+
/** This function gives the scope of an expression in a part of a job. */
|
|
479
|
+
function jobScopeOf(job, part) {
|
|
480
|
+
const needs = new Set(listOr$1(job["needs"]).filter((need) => typeof need === "string"));
|
|
481
|
+
if (part === "matrix") return {
|
|
482
|
+
names: /* @__PURE__ */ new Set(["inputs", "needs"]),
|
|
483
|
+
needs
|
|
484
|
+
};
|
|
485
|
+
const axes = matrixAxes(job["matrix"]);
|
|
486
|
+
const steps = listOr$1(job["steps"]);
|
|
487
|
+
const count = part === "outputs" ? steps.length : typeof part === "object" ? part.step : 0;
|
|
488
|
+
const seen = /* @__PURE__ */ new Set();
|
|
489
|
+
for (const step of steps.slice(0, count)) {
|
|
490
|
+
const id = isEnvelope(step) ? recordOr$1(step.value)["id"] : void 0;
|
|
491
|
+
if (typeof id === "string") seen.add(id);
|
|
492
|
+
}
|
|
493
|
+
const names = [
|
|
494
|
+
"inputs",
|
|
495
|
+
"env",
|
|
496
|
+
"needs",
|
|
497
|
+
"job",
|
|
498
|
+
...axes === void 0 ? [] : ["matrix"],
|
|
499
|
+
...seen.size > 0 ? ["steps"] : []
|
|
500
|
+
];
|
|
501
|
+
return {
|
|
502
|
+
names: new Set(names),
|
|
503
|
+
steps: seen,
|
|
504
|
+
needs,
|
|
505
|
+
matrix: axes
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* This function gives the scope of an expression at a JSON path of a workflow document: the names that it can read.
|
|
510
|
+
* The check uses the same rules. A builder uses it to offer only the values that a position can read.
|
|
511
|
+
*
|
|
512
|
+
* @param doc - The workflow document.
|
|
513
|
+
* @param at - The JSON path of the expression, for example `["value", "jobs", "build", "value", "steps", 1, "value", "do"]`.
|
|
514
|
+
* @returns The scope, or `undefined` when the path is not in a workflow.
|
|
515
|
+
*/
|
|
516
|
+
function scopeAt(doc, at) {
|
|
517
|
+
if (!isEnvelope(doc) || doc.type !== "workflow" || !isRecord(doc.value) || at[0] !== "value") return void 0;
|
|
518
|
+
const [, field, id, , part, index] = at;
|
|
519
|
+
if (field === "triggers") return { names: /* @__PURE__ */ new Set() };
|
|
520
|
+
if (field === "env") return { names: /* @__PURE__ */ new Set(["inputs"]) };
|
|
521
|
+
if (field === "outputs") return { names: /* @__PURE__ */ new Set(["inputs", "jobs"]) };
|
|
522
|
+
if (field !== "jobs" || typeof id !== "string") return void 0;
|
|
523
|
+
const job = recordOr$1(doc.value["jobs"])[id];
|
|
524
|
+
if (!isEnvelope(job)) return void 0;
|
|
525
|
+
const j = recordOr$1(job.value);
|
|
526
|
+
if (part === "steps" && typeof index === "number") return jobScopeOf(j, { step: index });
|
|
527
|
+
if (part === "if" || part === "env" || part === "matrix" || part === "outputs") return jobScopeOf(j, part);
|
|
528
|
+
}
|
|
529
|
+
/** This function gives the reference paths in a document: each envelope `ref` and its path. */
|
|
530
|
+
function refsIn(value, at, out = []) {
|
|
531
|
+
if (Array.isArray(value)) value.forEach((item, index) => refsIn(item, [...at, index], out));
|
|
532
|
+
else if (isRecord(value)) {
|
|
533
|
+
if (value["type"] === "ref" && Array.isArray(value["value"])) out.push({
|
|
534
|
+
path: value["value"],
|
|
535
|
+
at
|
|
536
|
+
});
|
|
537
|
+
for (const [key, item] of Object.entries(value)) refsIn(item, [...at, key], out);
|
|
538
|
+
}
|
|
539
|
+
return out;
|
|
540
|
+
}
|
|
541
|
+
const isEmptyText = (doc) => doc === "" || isEnvelope(doc) && doc.type === "lit" && doc.value === "";
|
|
542
|
+
const isSecretDoc = (doc) => isRecord(doc) && doc["type"] === "secret";
|
|
543
|
+
/**
|
|
544
|
+
* This function tells if a document is the test of the presence of a secret: `eq` or `ne` of a secret and the empty
|
|
545
|
+
* text. The test reads one bit, not the value. The host answers it.
|
|
546
|
+
*
|
|
547
|
+
* @param doc - The document.
|
|
548
|
+
* @returns `true` for a presence test.
|
|
549
|
+
*/
|
|
550
|
+
function isPresenceTest(doc) {
|
|
551
|
+
if (!isEnvelope(doc) || !Array.isArray(doc.type) || doc.type.length !== 2 || doc.type[0] !== "ops") return false;
|
|
552
|
+
if (doc.type[1] !== "eq" && doc.type[1] !== "ne") return false;
|
|
553
|
+
const args = doc.value;
|
|
554
|
+
if (!Array.isArray(args) || args.length !== 2) return false;
|
|
555
|
+
return isSecretDoc(args[0]) && isEmptyText(args[1]) || isSecretDoc(args[1]) && isEmptyText(args[0]);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* This function gives the secret documents in a value, and tells if each one is in a permitted place. A place is
|
|
559
|
+
* 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
|
|
560
|
+
* the secret, for example a template or an operation, makes the place not permitted.
|
|
561
|
+
*/
|
|
562
|
+
function secretsIn(value, at, allowed, calls, out = []) {
|
|
563
|
+
if (Array.isArray(value)) value.forEach((item, index) => secretsIn(item, [...at, index], allowed, calls, out));
|
|
564
|
+
else if (isRecord(value)) {
|
|
565
|
+
if (value["type"] === "secret") {
|
|
566
|
+
out.push({
|
|
567
|
+
name: value["value"],
|
|
568
|
+
at,
|
|
569
|
+
inCall: allowed
|
|
570
|
+
});
|
|
571
|
+
return out;
|
|
572
|
+
}
|
|
573
|
+
if (isEnvelope(value)) {
|
|
574
|
+
const head = Array.isArray(value.type) ? value.type[0] : value.type;
|
|
575
|
+
const call = isPresenceTest(value) || typeof head === "string" && calls.has(head);
|
|
576
|
+
secretsIn(value.value, [...at, "value"], call, calls, out);
|
|
577
|
+
return out;
|
|
578
|
+
}
|
|
579
|
+
for (const [key, item] of Object.entries(value)) secretsIn(item, [...at, key], key === "env" ? true : allowed, calls, out);
|
|
580
|
+
}
|
|
581
|
+
return out;
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* This function checks a workflow document. It gives each problem with its JSON path, and it does not throw.
|
|
585
|
+
*
|
|
586
|
+
* - The kernel checks the kinds, the shapes and the operations (`check`).
|
|
587
|
+
* - The workflow rules check the ids, the needs of the jobs and their cycles, and each reference of each expression.
|
|
588
|
+
* - They also check the secrets, the cron lines and the grants of the workspace.
|
|
589
|
+
*
|
|
590
|
+
* @param doc - The document `{ type: "workflow", value }`.
|
|
591
|
+
* @param options - The machine, the grant check and the procedures.
|
|
592
|
+
* @returns The problems. An empty list means a good workflow.
|
|
593
|
+
*/
|
|
594
|
+
function checkWorkflow(doc, options = {}) {
|
|
595
|
+
const issues = [];
|
|
596
|
+
const error = (code, at, message) => void issues.push({
|
|
597
|
+
level: "error",
|
|
598
|
+
code,
|
|
599
|
+
at,
|
|
600
|
+
message
|
|
601
|
+
});
|
|
602
|
+
const warn = (code, at, message) => void issues.push({
|
|
603
|
+
level: "warning",
|
|
604
|
+
code,
|
|
605
|
+
at,
|
|
606
|
+
message
|
|
607
|
+
});
|
|
608
|
+
if (!isEnvelope(doc) || doc.type !== "workflow" || !isRecord(doc.value)) {
|
|
609
|
+
error("workflow-form", [], "A workflow is the document { type: \"workflow\", value: { name, workspace, triggers, jobs } }.");
|
|
610
|
+
return issues;
|
|
611
|
+
}
|
|
612
|
+
const machine = options.machine ?? workflowMachine();
|
|
613
|
+
const names = [
|
|
614
|
+
"inputs",
|
|
615
|
+
"env",
|
|
616
|
+
"steps",
|
|
617
|
+
"needs",
|
|
618
|
+
"job",
|
|
619
|
+
"jobs",
|
|
620
|
+
"matrix",
|
|
621
|
+
...options.hostNames ?? []
|
|
622
|
+
];
|
|
623
|
+
for (const issue of check(machine, doc, { names })) error(issue.code, issue.at ?? issue.place, issue.reason);
|
|
624
|
+
const value = doc.value;
|
|
625
|
+
const root = ["value"];
|
|
626
|
+
const workspace = typeof value["workspace"] === "string" ? value["workspace"] : "";
|
|
627
|
+
const inputs = new Set(Object.keys(recordOr$1(value["inputs"])));
|
|
628
|
+
const secretNames = new Set(Object.keys(recordOr$1(value["secrets"])));
|
|
629
|
+
const jobs = recordOr$1(value["jobs"]);
|
|
630
|
+
const jobIds = new Set(Object.keys(jobs));
|
|
631
|
+
const hostNames = options.hostNames ?? [];
|
|
632
|
+
const checkRefs = (expr, at, scope) => {
|
|
633
|
+
for (const ref of refsIn(expr, at)) {
|
|
634
|
+
const [first, name, key] = ref.path;
|
|
635
|
+
if (first === "event") continue;
|
|
636
|
+
if (first !== "names") {
|
|
637
|
+
error("ref-root", ref.at, `A workflow reads only names and the event. The root ${JSON.stringify(first)} is not one.`);
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
if (typeof name !== "string") continue;
|
|
641
|
+
if (hostNames.includes(name)) continue;
|
|
642
|
+
if (name === "secrets") {
|
|
643
|
+
error("secret-ref", ref.at, "A secret is not a name. Use a document { type: \"secret\", value: NAME } as an input of a call.");
|
|
644
|
+
continue;
|
|
645
|
+
}
|
|
646
|
+
if (!scope.names.has(name)) {
|
|
647
|
+
error("ref-name", ref.at, `The name ${name} is not available here. Here a workflow can read: ${[...scope.names].join(", ") || "only the event"}.`);
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
if (typeof key !== "string") continue;
|
|
651
|
+
if (name === "inputs" && !inputs.has(key)) error("ref-input", ref.at, `The workflow has no input ${key}.`);
|
|
652
|
+
if (name === "steps" && scope.steps !== void 0 && !scope.steps.has(key)) error("ref-step", ref.at, `No earlier step of this job has the id ${key}.`);
|
|
653
|
+
if (name === "needs" && scope.needs !== void 0 && !scope.needs.has(key)) error("ref-needs", ref.at, `The job does not need the job ${key}. Add ${key} to its needs.`);
|
|
654
|
+
if (name === "jobs" && !jobIds.has(key)) error("ref-job", ref.at, `The workflow has no job ${key}.`);
|
|
655
|
+
if (name === "matrix" && scope.matrix !== void 0 && scope.matrix !== "any" && !scope.matrix.has(key)) error("ref-matrix", ref.at, `The matrix of the job has no axis ${key}.`);
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
listOr$1(value["triggers"]).forEach((trigger, index) => {
|
|
659
|
+
const at = [
|
|
660
|
+
...root,
|
|
661
|
+
"triggers",
|
|
662
|
+
index
|
|
663
|
+
];
|
|
664
|
+
if (!isEnvelope(trigger)) return;
|
|
665
|
+
const data = recordOr$1(trigger.value);
|
|
666
|
+
const kind = Array.isArray(trigger.type) ? trigger.type.join(".") : String(trigger.type);
|
|
667
|
+
if (kind === "trigger.schedule" && typeof data["cron"] === "string") {
|
|
668
|
+
const fields = data["cron"].trim().split(/\s+/);
|
|
669
|
+
if (fields.length !== 5 || !fields.every((field) => CRON_FIELD.test(field))) error("cron", [
|
|
670
|
+
...at,
|
|
671
|
+
"value",
|
|
672
|
+
"cron"
|
|
673
|
+
], `A cron line has five fields: minute, hour, day, month and weekday. ${JSON.stringify(data["cron"])} is not one.`);
|
|
674
|
+
}
|
|
675
|
+
if (kind === "trigger.feed" && typeof data["from"] === "string" && options.access !== void 0) {
|
|
676
|
+
const answer = options.access(workspace, "read", data["from"]);
|
|
677
|
+
if (!answer.ok) error("grant", [
|
|
678
|
+
...at,
|
|
679
|
+
"value",
|
|
680
|
+
"from"
|
|
681
|
+
], `The workspace ${workspace} cannot read ${data["from"]}: ${answer.why}`);
|
|
682
|
+
}
|
|
683
|
+
if (data["when"] !== void 0) checkRefs(data["when"], [
|
|
684
|
+
...at,
|
|
685
|
+
"value",
|
|
686
|
+
"when"
|
|
687
|
+
], { names: /* @__PURE__ */ new Set() });
|
|
688
|
+
});
|
|
689
|
+
if (listOr$1(value["triggers"]).length === 0) warn("triggers-none", [...root, "triggers"], "The workflow has no trigger, thus nothing starts it.");
|
|
690
|
+
const top = { names: /* @__PURE__ */ new Set(["inputs"]) };
|
|
691
|
+
for (const [key, expr] of Object.entries(recordOr$1(value["env"]))) checkRefs(expr, [
|
|
692
|
+
...root,
|
|
693
|
+
"env",
|
|
694
|
+
key
|
|
695
|
+
], top);
|
|
696
|
+
for (const [key, expr] of Object.entries(recordOr$1(value["outputs"]))) checkRefs(expr, [
|
|
697
|
+
...root,
|
|
698
|
+
"outputs",
|
|
699
|
+
key
|
|
700
|
+
], { names: /* @__PURE__ */ new Set(["inputs", "jobs"]) });
|
|
701
|
+
const graph = /* @__PURE__ */ new Map();
|
|
702
|
+
for (const [id, job] of Object.entries(jobs)) {
|
|
703
|
+
const at = [
|
|
704
|
+
...root,
|
|
705
|
+
"jobs",
|
|
706
|
+
id
|
|
707
|
+
];
|
|
708
|
+
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.`);
|
|
709
|
+
if (!isEnvelope(job) || job.type !== "job") {
|
|
710
|
+
error("job-form", at, "A job is the document { type: \"job\", value: { steps } }.");
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
const j = recordOr$1(job.value);
|
|
714
|
+
const needs = listOr$1(j["needs"]).filter((need) => typeof need === "string");
|
|
715
|
+
graph.set(id, { edges: needs.map((to) => ({
|
|
716
|
+
to,
|
|
717
|
+
lazy: false
|
|
718
|
+
})) });
|
|
719
|
+
needs.forEach((need, index) => {
|
|
720
|
+
if (need === id) error("needs-self", [
|
|
721
|
+
...at,
|
|
722
|
+
"value",
|
|
723
|
+
"needs",
|
|
724
|
+
index
|
|
725
|
+
], `The job ${id} cannot need itself.`);
|
|
726
|
+
else if (!jobIds.has(need)) error("needs-unknown", [
|
|
727
|
+
...at,
|
|
728
|
+
"value",
|
|
729
|
+
"needs",
|
|
730
|
+
index
|
|
731
|
+
], `The workflow has no job ${need}.`);
|
|
732
|
+
});
|
|
733
|
+
if (j["matrix"] !== void 0) checkRefs(j["matrix"], [
|
|
734
|
+
...at,
|
|
735
|
+
"value",
|
|
736
|
+
"matrix"
|
|
737
|
+
], jobScopeOf(j, "matrix"));
|
|
738
|
+
if (j["if"] !== void 0) checkRefs(j["if"], [
|
|
739
|
+
...at,
|
|
740
|
+
"value",
|
|
741
|
+
"if"
|
|
742
|
+
], jobScopeOf(j, "if"));
|
|
743
|
+
for (const [key, expr] of Object.entries(recordOr$1(j["env"]))) checkRefs(expr, [
|
|
744
|
+
...at,
|
|
745
|
+
"value",
|
|
746
|
+
"env",
|
|
747
|
+
key
|
|
748
|
+
], jobScopeOf(j, "env"));
|
|
749
|
+
const seen = /* @__PURE__ */ new Set();
|
|
750
|
+
listOr$1(j["steps"]).forEach((step, index) => {
|
|
751
|
+
const sat = [
|
|
752
|
+
...at,
|
|
753
|
+
"value",
|
|
754
|
+
"steps",
|
|
755
|
+
index
|
|
756
|
+
];
|
|
757
|
+
if (!isEnvelope(step) || step.type !== "step") {
|
|
758
|
+
error("step-form", sat, "A step is the document { type: \"step\", value: { do } }.");
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
const s = recordOr$1(step.value);
|
|
762
|
+
const scope = jobScopeOf(j, { step: index });
|
|
763
|
+
for (const field of [
|
|
764
|
+
"if",
|
|
765
|
+
"do",
|
|
766
|
+
"env"
|
|
767
|
+
]) if (s[field] !== void 0) checkRefs(s[field], [
|
|
768
|
+
...sat,
|
|
769
|
+
"value",
|
|
770
|
+
field
|
|
771
|
+
], scope);
|
|
772
|
+
const call = s["do"];
|
|
773
|
+
if (isEnvelope(call) && Array.isArray(call.type) && call.type[0] === "procedure") checkCall(call.type.slice(1).join("."), recordOr$1(call.value), [
|
|
774
|
+
...sat,
|
|
775
|
+
"value",
|
|
776
|
+
"do"
|
|
777
|
+
]);
|
|
778
|
+
if (typeof s["id"] === "string") {
|
|
779
|
+
if (!ID.test(s["id"])) error("step-id", [
|
|
780
|
+
...sat,
|
|
781
|
+
"value",
|
|
782
|
+
"id"
|
|
783
|
+
], `A step id has letters, digits, _ and -. ${JSON.stringify(s["id"])} does not.`);
|
|
784
|
+
else if (seen.has(s["id"])) error("step-id-twice", [
|
|
785
|
+
...sat,
|
|
786
|
+
"value",
|
|
787
|
+
"id"
|
|
788
|
+
], `Two steps of the job ${id} have the id ${s["id"]}.`);
|
|
789
|
+
seen.add(s["id"]);
|
|
790
|
+
}
|
|
791
|
+
});
|
|
792
|
+
if (listOr$1(j["steps"]).length === 0) warn("steps-none", [
|
|
793
|
+
...at,
|
|
794
|
+
"value",
|
|
795
|
+
"steps"
|
|
796
|
+
], `The job ${id} has no step.`);
|
|
797
|
+
for (const [key, expr] of Object.entries(recordOr$1(j["outputs"]))) checkRefs(expr, [
|
|
798
|
+
...at,
|
|
799
|
+
"value",
|
|
800
|
+
"outputs",
|
|
801
|
+
key
|
|
802
|
+
], jobScopeOf(j, "outputs"));
|
|
803
|
+
}
|
|
804
|
+
function checkCall(path, inputsOfCall, at) {
|
|
805
|
+
const info = options.procedures === void 0 ? void 0 : options.procedures[path];
|
|
806
|
+
if (options.procedures !== void 0 && info === void 0) {
|
|
807
|
+
warn("procedure-unknown", at, `No procedure has the path ${path}.`);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
for (const name of info?.required ?? []) if (!(name in inputsOfCall)) error("input-missing", [...at, "value"], `The procedure ${path} needs the input ${name}.`);
|
|
811
|
+
if (options.access === void 0) return;
|
|
812
|
+
for (const access of ["read", "write"]) for (const name of info?.workspaces?.[access] ?? []) {
|
|
813
|
+
const target = inputsOfCall[name];
|
|
814
|
+
if (typeof target !== "string") {
|
|
815
|
+
warn("grant-at-run", [
|
|
816
|
+
...at,
|
|
817
|
+
"value",
|
|
818
|
+
name
|
|
819
|
+
], `The input ${name} is computed, thus the runner checks the grant at the run.`);
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
const answer = options.access(workspace, access, target);
|
|
823
|
+
if (!answer.ok) error("grant", [
|
|
824
|
+
...at,
|
|
825
|
+
"value",
|
|
826
|
+
name
|
|
827
|
+
], `The workspace ${workspace} cannot ${access} ${target}: ${answer.why}`);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
for (const cycle of cycles(graph)) if (cycle.certain) error("needs-cycle", [
|
|
831
|
+
...root,
|
|
832
|
+
"jobs",
|
|
833
|
+
cycle.nodes[0],
|
|
834
|
+
"value",
|
|
835
|
+
"needs"
|
|
836
|
+
], `The jobs ${cycle.nodes.join(", ")} need each other in a cycle.`);
|
|
837
|
+
const calls = /* @__PURE__ */ new Set(["procedure", ...options.callNamespaces ?? []]);
|
|
838
|
+
for (const secret of secretsIn(value, root, false, calls)) {
|
|
839
|
+
if (options.openSecrets !== true && typeof secret.name === "string" && !secretNames.has(secret.name)) error("secret-unknown", secret.at, `The workflow declares no secret ${secret.name}.`);
|
|
840
|
+
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.");
|
|
841
|
+
}
|
|
842
|
+
return issues;
|
|
843
|
+
}
|
|
844
|
+
function labelOf(value, fallback) {
|
|
845
|
+
return typeof value["name"] === "string" && value["name"] !== "" ? value["name"] : fallback;
|
|
846
|
+
}
|
|
847
|
+
/** This function gives the text of the path of a call, for example `git.clone`, or an empty text. */
|
|
848
|
+
function callPath(doc) {
|
|
849
|
+
return isEnvelope(doc) && Array.isArray(doc.type) && doc.type[0] === "procedure" ? doc.type.slice(1).join(".") : "";
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* This function gives the graph of a workflow from its document. The data edges come from the references of the
|
|
853
|
+
* expressions. Thus the graph shows each value that moves from a step or a job to another.
|
|
854
|
+
*
|
|
855
|
+
* @param doc - The workflow document.
|
|
856
|
+
* @returns The nodes, the edges, and the layers of the jobs.
|
|
857
|
+
*/
|
|
858
|
+
function workflowGraph(doc) {
|
|
859
|
+
const nodes = [];
|
|
860
|
+
const edges = [];
|
|
861
|
+
if (!isEnvelope(doc) || !isRecord(doc.value)) return {
|
|
862
|
+
nodes,
|
|
863
|
+
edges,
|
|
864
|
+
layers: []
|
|
865
|
+
};
|
|
866
|
+
const value = doc.value;
|
|
867
|
+
const jobs = recordOr$1(value["jobs"]);
|
|
868
|
+
const jobGraph = /* @__PURE__ */ new Map();
|
|
869
|
+
listOr$1(value["triggers"]).forEach((trigger, index) => {
|
|
870
|
+
const kind = isEnvelope(trigger) && Array.isArray(trigger.type) ? String(trigger.type[1]) : "trigger";
|
|
871
|
+
const data = isEnvelope(trigger) ? recordOr$1(trigger.value) : {};
|
|
872
|
+
const detail = typeof data["event"] === "string" ? data["event"] : typeof data["cron"] === "string" ? data["cron"] : typeof data["from"] === "string" ? data["from"] : "";
|
|
873
|
+
nodes.push({
|
|
874
|
+
id: `trigger:${index}`,
|
|
875
|
+
kind: "trigger",
|
|
876
|
+
label: detail === "" ? kind : `${kind}: ${detail}`,
|
|
877
|
+
at: [
|
|
878
|
+
"value",
|
|
879
|
+
"triggers",
|
|
880
|
+
index
|
|
881
|
+
]
|
|
882
|
+
});
|
|
883
|
+
});
|
|
884
|
+
for (const [id, job] of Object.entries(jobs)) {
|
|
885
|
+
const j = isEnvelope(job) ? recordOr$1(job.value) : {};
|
|
886
|
+
const needs = listOr$1(j["needs"]).filter((need) => typeof need === "string" && need in jobs);
|
|
887
|
+
jobGraph.set(id, { edges: needs.map((to) => ({
|
|
888
|
+
to,
|
|
889
|
+
lazy: false
|
|
890
|
+
})) });
|
|
891
|
+
nodes.push({
|
|
892
|
+
id: `job:${id}`,
|
|
893
|
+
kind: "job",
|
|
894
|
+
label: labelOf(j, id),
|
|
895
|
+
at: [
|
|
896
|
+
"value",
|
|
897
|
+
"jobs",
|
|
898
|
+
id
|
|
899
|
+
]
|
|
900
|
+
});
|
|
901
|
+
for (const need of needs) edges.push({
|
|
902
|
+
from: `job:${need}`,
|
|
903
|
+
to: `job:${id}`,
|
|
904
|
+
kind: "needs"
|
|
905
|
+
});
|
|
906
|
+
if (needs.length === 0) listOr$1(value["triggers"]).forEach((_, index) => edges.push({
|
|
907
|
+
from: `trigger:${index}`,
|
|
908
|
+
to: `job:${id}`,
|
|
909
|
+
kind: "start"
|
|
910
|
+
}));
|
|
911
|
+
const stepIds = /* @__PURE__ */ new Map();
|
|
912
|
+
let previous;
|
|
913
|
+
listOr$1(j["steps"]).forEach((step, index) => {
|
|
914
|
+
const s = isEnvelope(step) ? recordOr$1(step.value) : {};
|
|
915
|
+
const node = `step:${id}:${index}`;
|
|
916
|
+
const path = callPath(s["do"]);
|
|
917
|
+
nodes.push({
|
|
918
|
+
id: node,
|
|
919
|
+
kind: "step",
|
|
920
|
+
job: id,
|
|
921
|
+
label: labelOf(s, typeof s["id"] === "string" ? s["id"] : path || `step ${index + 1}`),
|
|
922
|
+
at: [
|
|
923
|
+
"value",
|
|
924
|
+
"jobs",
|
|
925
|
+
id,
|
|
926
|
+
"value",
|
|
927
|
+
"steps",
|
|
928
|
+
index
|
|
929
|
+
]
|
|
930
|
+
});
|
|
931
|
+
edges.push({
|
|
932
|
+
from: previous ?? `job:${id}`,
|
|
933
|
+
to: node,
|
|
934
|
+
kind: "next"
|
|
935
|
+
});
|
|
936
|
+
previous = node;
|
|
937
|
+
const seen = /* @__PURE__ */ new Set();
|
|
938
|
+
for (const ref of refsIn(step, [])) {
|
|
939
|
+
const [first, name, key] = ref.path;
|
|
940
|
+
if (first !== "names" || typeof key !== "string") continue;
|
|
941
|
+
const from = name === "steps" ? stepIds.get(key) : name === "needs" && key in jobs ? `job:${key}` : void 0;
|
|
942
|
+
if (from !== void 0 && !seen.has(from)) {
|
|
943
|
+
seen.add(from);
|
|
944
|
+
edges.push({
|
|
945
|
+
from,
|
|
946
|
+
to: node,
|
|
947
|
+
kind: "data"
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
if (typeof s["id"] === "string") stepIds.set(s["id"], node);
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
return {
|
|
955
|
+
nodes,
|
|
956
|
+
edges,
|
|
957
|
+
layers: nodeLayers(jobGraph)
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
// src/sample.ts
|
|
961
|
+
/**
|
|
962
|
+
* A sample workflow: a build on two versions of a runtime, then a release with a secret. The tests and the site use it.
|
|
963
|
+
*/
|
|
964
|
+
const SAMPLE_WORKFLOW = {
|
|
965
|
+
type: "workflow",
|
|
966
|
+
value: {
|
|
967
|
+
name: "Build and release",
|
|
968
|
+
summary: "It builds the package on two versions, and it releases it when a person asks.",
|
|
969
|
+
workspace: "technical",
|
|
970
|
+
triggers: [{
|
|
971
|
+
type: ["trigger", "event"],
|
|
972
|
+
value: { event: "push" }
|
|
973
|
+
}, {
|
|
974
|
+
type: ["trigger", "manual"],
|
|
975
|
+
value: { summary: "Release now" }
|
|
976
|
+
}],
|
|
977
|
+
inputs: { channel: {
|
|
978
|
+
type: "choice",
|
|
979
|
+
options: ["latest", "next"],
|
|
980
|
+
default: "latest",
|
|
981
|
+
description: "The channel of the release."
|
|
982
|
+
} },
|
|
983
|
+
secrets: { REGISTRY_TOKEN: {
|
|
984
|
+
description: "The token of the registry.",
|
|
985
|
+
required: true
|
|
986
|
+
} },
|
|
987
|
+
jobs: {
|
|
988
|
+
build: {
|
|
989
|
+
type: "job",
|
|
990
|
+
value: {
|
|
991
|
+
name: "Build",
|
|
992
|
+
matrix: {
|
|
993
|
+
type: "matrix",
|
|
994
|
+
value: { axes: { version: {
|
|
995
|
+
type: "list",
|
|
996
|
+
value: [20, 22]
|
|
997
|
+
} } }
|
|
998
|
+
},
|
|
999
|
+
steps: [{
|
|
1000
|
+
type: "step",
|
|
1001
|
+
value: {
|
|
1002
|
+
id: "source",
|
|
1003
|
+
name: "Get the source",
|
|
1004
|
+
do: {
|
|
1005
|
+
type: [
|
|
1006
|
+
"procedure",
|
|
1007
|
+
"git",
|
|
1008
|
+
"clone"
|
|
1009
|
+
],
|
|
1010
|
+
value: { branch: "main" }
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}, {
|
|
1014
|
+
type: "step",
|
|
1015
|
+
value: {
|
|
1016
|
+
id: "test",
|
|
1017
|
+
name: "Test",
|
|
1018
|
+
do: {
|
|
1019
|
+
type: [
|
|
1020
|
+
"procedure",
|
|
1021
|
+
"pnpm",
|
|
1022
|
+
"test"
|
|
1023
|
+
],
|
|
1024
|
+
value: {
|
|
1025
|
+
cwd: {
|
|
1026
|
+
type: "ref",
|
|
1027
|
+
value: [
|
|
1028
|
+
"names",
|
|
1029
|
+
"steps",
|
|
1030
|
+
"source",
|
|
1031
|
+
"folder"
|
|
1032
|
+
]
|
|
1033
|
+
},
|
|
1034
|
+
version: {
|
|
1035
|
+
type: "ref",
|
|
1036
|
+
value: [
|
|
1037
|
+
"names",
|
|
1038
|
+
"matrix",
|
|
1039
|
+
"version"
|
|
1040
|
+
]
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
}],
|
|
1046
|
+
outputs: { folder: {
|
|
1047
|
+
type: "ref",
|
|
1048
|
+
value: [
|
|
1049
|
+
"names",
|
|
1050
|
+
"steps",
|
|
1051
|
+
"source",
|
|
1052
|
+
"folder"
|
|
1053
|
+
]
|
|
1054
|
+
} }
|
|
1055
|
+
}
|
|
1056
|
+
},
|
|
1057
|
+
release: {
|
|
1058
|
+
type: "job",
|
|
1059
|
+
value: {
|
|
1060
|
+
name: "Release",
|
|
1061
|
+
needs: ["build"],
|
|
1062
|
+
if: {
|
|
1063
|
+
type: ["ops", "eq"],
|
|
1064
|
+
value: [{
|
|
1065
|
+
type: "ref",
|
|
1066
|
+
value: ["event", "name"]
|
|
1067
|
+
}, "manual"]
|
|
1068
|
+
},
|
|
1069
|
+
steps: [{
|
|
1070
|
+
type: "step",
|
|
1071
|
+
value: {
|
|
1072
|
+
id: "publish",
|
|
1073
|
+
name: "Publish",
|
|
1074
|
+
do: {
|
|
1075
|
+
type: [
|
|
1076
|
+
"procedure",
|
|
1077
|
+
"npm",
|
|
1078
|
+
"publish"
|
|
1079
|
+
],
|
|
1080
|
+
value: {
|
|
1081
|
+
cwd: {
|
|
1082
|
+
type: "ref",
|
|
1083
|
+
value: [
|
|
1084
|
+
"names",
|
|
1085
|
+
"needs",
|
|
1086
|
+
"build",
|
|
1087
|
+
"outputs",
|
|
1088
|
+
"folder"
|
|
1089
|
+
]
|
|
1090
|
+
},
|
|
1091
|
+
tag: {
|
|
1092
|
+
type: "ref",
|
|
1093
|
+
value: [
|
|
1094
|
+
"names",
|
|
1095
|
+
"inputs",
|
|
1096
|
+
"channel"
|
|
1097
|
+
]
|
|
1098
|
+
},
|
|
1099
|
+
token: {
|
|
1100
|
+
type: "secret",
|
|
1101
|
+
value: "REGISTRY_TOKEN"
|
|
1102
|
+
},
|
|
1103
|
+
note: {
|
|
1104
|
+
type: "template",
|
|
1105
|
+
value: ["Release on the channel ", {
|
|
1106
|
+
type: "ref",
|
|
1107
|
+
value: [
|
|
1108
|
+
"names",
|
|
1109
|
+
"inputs",
|
|
1110
|
+
"channel"
|
|
1111
|
+
]
|
|
1112
|
+
}]
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
}]
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
};
|
|
1123
|
+
// src/edit.ts
|
|
1124
|
+
const recordOr = (value) => isRecord(value) ? value : {};
|
|
1125
|
+
const listOr = (value) => Array.isArray(value) ? value : [];
|
|
1126
|
+
/**
|
|
1127
|
+
* This function reads the value at a JSON path. The path goes through records and lists, also through the `value` of
|
|
1128
|
+
* an envelope, as the paths of the problems of `checkWorkflow` do.
|
|
1129
|
+
*
|
|
1130
|
+
* @param doc - The document.
|
|
1131
|
+
* @param at - The JSON path.
|
|
1132
|
+
* @returns The value, or `undefined`.
|
|
1133
|
+
*/
|
|
1134
|
+
function getIn(doc, at) {
|
|
1135
|
+
let here = doc;
|
|
1136
|
+
for (const key of at) if (Array.isArray(here) && typeof key === "number") here = here[key];
|
|
1137
|
+
else if (isRecord(here) && typeof key === "string") here = here[key];
|
|
1138
|
+
else return void 0;
|
|
1139
|
+
return here;
|
|
1140
|
+
}
|
|
1141
|
+
/**
|
|
1142
|
+
* This function gives a copy of a document with a new value at a JSON path. The value `undefined` removes the key, or
|
|
1143
|
+
* the item of a list. A missing record on the path becomes an empty record.
|
|
1144
|
+
*
|
|
1145
|
+
* @param doc - The document.
|
|
1146
|
+
* @param at - The JSON path.
|
|
1147
|
+
* @param value - The new value.
|
|
1148
|
+
* @returns The new document.
|
|
1149
|
+
*/
|
|
1150
|
+
function setIn(doc, at, value) {
|
|
1151
|
+
if (at.length === 0) return value;
|
|
1152
|
+
const [key, ...rest] = at;
|
|
1153
|
+
if (Array.isArray(doc) && typeof key === "number") {
|
|
1154
|
+
const copy = [...doc];
|
|
1155
|
+
if (rest.length === 0 && value === void 0) copy.splice(key, 1);
|
|
1156
|
+
else copy[key] = setIn(doc[key], rest, value);
|
|
1157
|
+
return copy;
|
|
1158
|
+
}
|
|
1159
|
+
const record = recordOr(doc);
|
|
1160
|
+
const copy = { ...record };
|
|
1161
|
+
const next = setIn(record[String(key)], rest, value);
|
|
1162
|
+
if (next === void 0) delete copy[String(key)];
|
|
1163
|
+
else copy[String(key)] = next;
|
|
1164
|
+
return copy;
|
|
1165
|
+
}
|
|
1166
|
+
const jobsOf = (doc) => recordOr(getIn(doc, ["value", "jobs"]));
|
|
1167
|
+
const needsOf = (job) => listOr(getIn(job, ["value", "needs"])).filter((need) => typeof need === "string");
|
|
1168
|
+
/** This function gives a new id from a base that no key of a record has: `base`, `base-2`, `base-3` and on. */
|
|
1169
|
+
function freeId(taken, base) {
|
|
1170
|
+
const used = new Set(taken);
|
|
1171
|
+
if (!used.has(base)) return base;
|
|
1172
|
+
for (let n = 2;; n++) if (!used.has(`${base}-${String(n)}`)) return `${base}-${String(n)}`;
|
|
1173
|
+
}
|
|
1174
|
+
/**
|
|
1175
|
+
* This function adds a job with the given steps.
|
|
1176
|
+
*
|
|
1177
|
+
* @param doc - The workflow document.
|
|
1178
|
+
* @param base - The base of the id of the job.
|
|
1179
|
+
* @param steps - The step documents of the job.
|
|
1180
|
+
* @returns The new document and the id of the job.
|
|
1181
|
+
*/
|
|
1182
|
+
function addJob(doc, base, steps) {
|
|
1183
|
+
const id = freeId(Object.keys(jobsOf(doc)), base);
|
|
1184
|
+
return {
|
|
1185
|
+
doc: setIn(doc, [
|
|
1186
|
+
"value",
|
|
1187
|
+
"jobs",
|
|
1188
|
+
id
|
|
1189
|
+
], {
|
|
1190
|
+
type: "job",
|
|
1191
|
+
value: { steps: [...steps] }
|
|
1192
|
+
}),
|
|
1193
|
+
id
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
/** This function changes each reference to a job in a document: `needs.<from>` and `jobs.<from>`. */
|
|
1197
|
+
function renameRefs(value, from, to) {
|
|
1198
|
+
if (Array.isArray(value)) return value.map((item) => renameRefs(item, from, to));
|
|
1199
|
+
if (!isRecord(value)) return value;
|
|
1200
|
+
const record = value;
|
|
1201
|
+
if (record["type"] === "ref" && Array.isArray(record["value"])) {
|
|
1202
|
+
const path = record["value"];
|
|
1203
|
+
if (path[0] === "names" && (path[1] === "needs" || path[1] === "jobs") && path[2] === from && to !== void 0) return {
|
|
1204
|
+
type: "ref",
|
|
1205
|
+
value: [
|
|
1206
|
+
path[0],
|
|
1207
|
+
path[1],
|
|
1208
|
+
to,
|
|
1209
|
+
...path.slice(3)
|
|
1210
|
+
]
|
|
1211
|
+
};
|
|
1212
|
+
return record;
|
|
1213
|
+
}
|
|
1214
|
+
return Object.fromEntries(Object.entries(record).map(([key, item]) => [key, renameRefs(item, from, to)]));
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* This function removes a job, and removes it from the needs of the other jobs. A reference to its outputs stays, and
|
|
1218
|
+
* the check finds it.
|
|
1219
|
+
*
|
|
1220
|
+
* @param doc - The workflow document.
|
|
1221
|
+
* @param id - The id of the job.
|
|
1222
|
+
* @returns The new document.
|
|
1223
|
+
*/
|
|
1224
|
+
function removeJob(doc, id) {
|
|
1225
|
+
let out = setIn(doc, [
|
|
1226
|
+
"value",
|
|
1227
|
+
"jobs",
|
|
1228
|
+
id
|
|
1229
|
+
], void 0);
|
|
1230
|
+
for (const [other, job] of Object.entries(jobsOf(out))) {
|
|
1231
|
+
const needs = needsOf(job);
|
|
1232
|
+
if (needs.includes(id)) out = setIn(out, [
|
|
1233
|
+
"value",
|
|
1234
|
+
"jobs",
|
|
1235
|
+
other,
|
|
1236
|
+
"value",
|
|
1237
|
+
"needs"
|
|
1238
|
+
], needs.length === 1 ? void 0 : needs.filter((need) => need !== id));
|
|
1239
|
+
}
|
|
1240
|
+
return out;
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* This function gives a job a new id. The needs of the other jobs and each reference to the job change with it. The
|
|
1244
|
+
* order of the jobs stays.
|
|
1245
|
+
*
|
|
1246
|
+
* @param doc - The workflow document.
|
|
1247
|
+
* @param from - The id of the job.
|
|
1248
|
+
* @param to - The new id.
|
|
1249
|
+
* @returns The new document, or the reason of a refusal.
|
|
1250
|
+
*/
|
|
1251
|
+
function renameJob(doc, from, to) {
|
|
1252
|
+
const jobs = jobsOf(doc);
|
|
1253
|
+
if (!(from in jobs)) return {
|
|
1254
|
+
ok: false,
|
|
1255
|
+
why: `The workflow has no job ${from}.`
|
|
1256
|
+
};
|
|
1257
|
+
if (from === to) return {
|
|
1258
|
+
ok: true,
|
|
1259
|
+
doc
|
|
1260
|
+
};
|
|
1261
|
+
if (to in jobs) return {
|
|
1262
|
+
ok: false,
|
|
1263
|
+
why: `The workflow has a job ${to} already.`
|
|
1264
|
+
};
|
|
1265
|
+
if (!/^[A-Za-z_][A-Za-z0-9_-]*$/.test(to)) return {
|
|
1266
|
+
ok: false,
|
|
1267
|
+
why: "A job id has letters, digits, _ and -, and it starts with a letter or _."
|
|
1268
|
+
};
|
|
1269
|
+
const renamed = {};
|
|
1270
|
+
for (const [id, job] of Object.entries(jobs)) {
|
|
1271
|
+
const needs = needsOf(job);
|
|
1272
|
+
const next = needs.includes(from) ? setIn(job, ["value", "needs"], needs.map((need) => need === from ? to : need)) : job;
|
|
1273
|
+
renamed[id === from ? to : id] = next;
|
|
1274
|
+
}
|
|
1275
|
+
return {
|
|
1276
|
+
ok: true,
|
|
1277
|
+
doc: renameRefs(setIn(doc, ["value", "jobs"], renamed), from, to)
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
/** This function tells if a job reaches another job through the needs. */
|
|
1281
|
+
function reaches(jobs, from, to) {
|
|
1282
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1283
|
+
const stack = [from];
|
|
1284
|
+
while (stack.length > 0) {
|
|
1285
|
+
const id = stack.pop();
|
|
1286
|
+
if (id === to) return true;
|
|
1287
|
+
if (seen.has(id)) continue;
|
|
1288
|
+
seen.add(id);
|
|
1289
|
+
stack.push(...needsOf(jobs[id]));
|
|
1290
|
+
}
|
|
1291
|
+
return false;
|
|
1292
|
+
}
|
|
1293
|
+
/**
|
|
1294
|
+
* This function adds a need to a job, or removes it. A need that makes a cycle is refused.
|
|
1295
|
+
*
|
|
1296
|
+
* @param doc - The workflow document.
|
|
1297
|
+
* @param job - The id of the job.
|
|
1298
|
+
* @param need - The id of the job that it needs.
|
|
1299
|
+
* @param on - `true` to add the need, `false` to remove it.
|
|
1300
|
+
* @returns The new document, or the reason of a refusal.
|
|
1301
|
+
*/
|
|
1302
|
+
function setNeed(doc, job, need, on) {
|
|
1303
|
+
const jobs = jobsOf(doc);
|
|
1304
|
+
if (!(job in jobs) || !(need in jobs)) return {
|
|
1305
|
+
ok: false,
|
|
1306
|
+
why: "The workflow has no such job."
|
|
1307
|
+
};
|
|
1308
|
+
const needs = needsOf(jobs[job]);
|
|
1309
|
+
if (!on) return {
|
|
1310
|
+
ok: true,
|
|
1311
|
+
doc: setIn(doc, [
|
|
1312
|
+
"value",
|
|
1313
|
+
"jobs",
|
|
1314
|
+
job,
|
|
1315
|
+
"value",
|
|
1316
|
+
"needs"
|
|
1317
|
+
], needs.length <= 1 && needs.includes(need) ? void 0 : needs.filter((id) => id !== need))
|
|
1318
|
+
};
|
|
1319
|
+
if (needs.includes(need)) return {
|
|
1320
|
+
ok: true,
|
|
1321
|
+
doc
|
|
1322
|
+
};
|
|
1323
|
+
if (need === job || reaches(jobs, need, job)) return {
|
|
1324
|
+
ok: false,
|
|
1325
|
+
why: `The job ${need} already runs after ${job}. This need makes a cycle.`
|
|
1326
|
+
};
|
|
1327
|
+
return {
|
|
1328
|
+
ok: true,
|
|
1329
|
+
doc: setIn(doc, [
|
|
1330
|
+
"value",
|
|
1331
|
+
"jobs",
|
|
1332
|
+
job,
|
|
1333
|
+
"value",
|
|
1334
|
+
"needs"
|
|
1335
|
+
], [...needs, need])
|
|
1336
|
+
};
|
|
1337
|
+
}
|
|
1338
|
+
/**
|
|
1339
|
+
* This function adds a step to a job.
|
|
1340
|
+
*
|
|
1341
|
+
* @param doc - The workflow document.
|
|
1342
|
+
* @param job - The id of the job.
|
|
1343
|
+
* @param step - The step document.
|
|
1344
|
+
* @param index - The place of the new step. The default is the end.
|
|
1345
|
+
* @returns The new document.
|
|
1346
|
+
*/
|
|
1347
|
+
function addStep(doc, job, step, index) {
|
|
1348
|
+
const steps = [...listOr(getIn(doc, [
|
|
1349
|
+
"value",
|
|
1350
|
+
"jobs",
|
|
1351
|
+
job,
|
|
1352
|
+
"value",
|
|
1353
|
+
"steps"
|
|
1354
|
+
]))];
|
|
1355
|
+
steps.splice(index ?? steps.length, 0, step);
|
|
1356
|
+
return setIn(doc, [
|
|
1357
|
+
"value",
|
|
1358
|
+
"jobs",
|
|
1359
|
+
job,
|
|
1360
|
+
"value",
|
|
1361
|
+
"steps"
|
|
1362
|
+
], steps);
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* This function moves a step of a job to another place in the job.
|
|
1366
|
+
*
|
|
1367
|
+
* @param doc - The workflow document.
|
|
1368
|
+
* @param job - The id of the job.
|
|
1369
|
+
* @param from - The place of the step.
|
|
1370
|
+
* @param to - The new place.
|
|
1371
|
+
* @returns The new document.
|
|
1372
|
+
*/
|
|
1373
|
+
function moveStep(doc, job, from, to) {
|
|
1374
|
+
const steps = [...listOr(getIn(doc, [
|
|
1375
|
+
"value",
|
|
1376
|
+
"jobs",
|
|
1377
|
+
job,
|
|
1378
|
+
"value",
|
|
1379
|
+
"steps"
|
|
1380
|
+
]))];
|
|
1381
|
+
if (from < 0 || from >= steps.length || to < 0 || to >= steps.length) return doc;
|
|
1382
|
+
const [step] = steps.splice(from, 1);
|
|
1383
|
+
steps.splice(to, 0, step);
|
|
1384
|
+
return setIn(doc, [
|
|
1385
|
+
"value",
|
|
1386
|
+
"jobs",
|
|
1387
|
+
job,
|
|
1388
|
+
"value",
|
|
1389
|
+
"steps"
|
|
1390
|
+
], steps);
|
|
1391
|
+
}
|
|
1392
|
+
const nameRef = (...path) => ({
|
|
1393
|
+
type: "ref",
|
|
1394
|
+
value: ["names", ...path]
|
|
1395
|
+
});
|
|
1396
|
+
const choice = (group, ...path) => ({
|
|
1397
|
+
group,
|
|
1398
|
+
label: path.join("."),
|
|
1399
|
+
doc: nameRef(...path)
|
|
1400
|
+
});
|
|
1401
|
+
/**
|
|
1402
|
+
* This function gives the paths into the result of each step that the references of the document read, for example
|
|
1403
|
+
* `folder` of `steps.source.folder`. The result of a step has no declared shape, thus the known reads are the offer.
|
|
1404
|
+
*/
|
|
1405
|
+
function knownStepReads(doc, out = /* @__PURE__ */ new Map()) {
|
|
1406
|
+
if (Array.isArray(doc)) for (const item of doc) knownStepReads(item, out);
|
|
1407
|
+
else if (isRecord(doc)) {
|
|
1408
|
+
const record = doc;
|
|
1409
|
+
const path = record["value"];
|
|
1410
|
+
if (record["type"] === "ref" && Array.isArray(path) && path[1] === "steps" && typeof path[2] === "string" && path.length > 3 && path.slice(3).every((key) => typeof key === "string")) {
|
|
1411
|
+
const reads = out.get(path[2]) ?? /* @__PURE__ */ new Set();
|
|
1412
|
+
reads.add(path.slice(3).join("."));
|
|
1413
|
+
out.set(path[2], reads);
|
|
1414
|
+
} else for (const item of Object.values(record)) knownStepReads(item, out);
|
|
1415
|
+
}
|
|
1416
|
+
return out;
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* This function gives the values that an expression at a JSON path can read. These are the inputs, the env
|
|
1420
|
+
* values, the matrix axes, the outputs of the needed jobs and the known reads of the earlier steps. The list follows
|
|
1421
|
+
* the scope of `scopeAt`, thus each choice passes the check.
|
|
1422
|
+
*
|
|
1423
|
+
* @param doc - The workflow document.
|
|
1424
|
+
* @param at - The JSON path of the expression.
|
|
1425
|
+
* @param host - More choices of the host, for example its contexts.
|
|
1426
|
+
* @returns The choices, in groups.
|
|
1427
|
+
*/
|
|
1428
|
+
function refChoices(doc, at, host = []) {
|
|
1429
|
+
const scope = scopeAt(doc, at);
|
|
1430
|
+
if (scope === void 0) return [...host];
|
|
1431
|
+
const out = [];
|
|
1432
|
+
const value = recordOr(getIn(doc, ["value"]));
|
|
1433
|
+
const jobs = recordOr(value["jobs"]);
|
|
1434
|
+
if (scope.names.has("inputs")) for (const name of Object.keys(recordOr(value["inputs"]))) out.push(choice("inputs", "inputs", name));
|
|
1435
|
+
if (scope.names.has("env")) {
|
|
1436
|
+
const job = at[1] === "jobs" && typeof at[2] === "string" ? jobs[at[2]] : void 0;
|
|
1437
|
+
const names = /* @__PURE__ */ new Set([...Object.keys(recordOr(value["env"])), ...Object.keys(recordOr(getIn(job, ["value", "env"])))]);
|
|
1438
|
+
for (const name of names) out.push(choice("env", "env", name));
|
|
1439
|
+
}
|
|
1440
|
+
if (scope.names.has("matrix") && scope.matrix !== void 0 && scope.matrix !== "any") for (const axis of scope.matrix) out.push(choice("matrix", "matrix", axis));
|
|
1441
|
+
if (scope.names.has("steps") && scope.steps !== void 0) {
|
|
1442
|
+
const known = knownStepReads(doc);
|
|
1443
|
+
for (const id of scope.steps) for (const read of known.get(id) ?? []) out.push(choice("steps", "steps", id, ...read.split(".")));
|
|
1444
|
+
}
|
|
1445
|
+
if (scope.names.has("needs") && scope.needs !== void 0) for (const id of scope.needs) for (const name of Object.keys(recordOr(getIn(jobs[id], ["value", "outputs"])))) out.push(choice("needs", "needs", id, "outputs", name));
|
|
1446
|
+
if (scope.names.has("jobs")) for (const [id, job] of Object.entries(jobs)) for (const name of Object.keys(recordOr(getIn(job, ["value", "outputs"])))) out.push(choice("jobs", "jobs", id, "outputs", name));
|
|
1447
|
+
return [...out, ...host];
|
|
1448
|
+
}
|
|
1449
|
+
export { SAMPLE_WORKFLOW, WORKFLOW_FORMAT, WORKFLOW_LANGUAGE, WORKFLOW_OPS, WORKFLOW_VERSION, WORKFLOW_WORLD, addJob, addStep, callPath, checkWorkflow, expandMatrix, freeId, getIn, isPresenceTest, isSecretRef, moveStep, refChoices, removeJob, renameJob, scopeAt, setIn, setNeed, workflowGraph, workflowMachine, workflowMachineWith };
|
|
1450
|
+
|
|
1451
|
+
//# sourceMappingURL=index.js.map
|