1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
import { parseWorkflow, type ParsedWorkflow } from "./workflow-parser";
export type MatrixSpec = {
axes: Record<string, unknown[]>;
include?: Record<string, unknown>[];
exclude?: Record<string, unknown>[];
failFast?: boolean;
maxParallel?: number;
};
export type ExtendedStep = {
name?: string;
id?: string;
if?: string;
run?: string;
uses?: string;
with?: Record<string, unknown>;
env?: Record<string, string>;
parallel?: boolean;
continueOnError?: boolean;
};
export type ExtendedJob = {
name: string;
runsOn: string;
if?: string;
needs?: string[];
strategy?: { matrix?: MatrixSpec };
env?: Record<string, string>;
outputs?: Record<string, string>;
steps: ExtendedStep[];
};
export type DispatchInput = {
type: "string" | "boolean" | "choice" | "number";
required?: boolean;
default?: string | boolean | number;
options?: string[];
description?: string;
};
export type ExtendedWorkflow = {
name: string;
on: string[];
dispatchInputs?: Record<string, DispatchInput>;
env?: Record<string, string>;
jobs: ExtendedJob[];
warnings: string[];
};
export type ExtendedParseResult =
| { ok: true; workflow: ExtendedWorkflow }
| { ok: false; error: string };
type Line = { indent: number; text: string; raw: string };
function lexLines(source: string): Line[] {
const out: Line[] = [];
const raw = source.replace(/\r\n?/g, "\n").split("\n");
for (const r of raw) {
let indent = 0;
while (indent < r.length && (r[indent] === " " || r[indent] === "\t")) indent++;
const body = r.slice(indent);
if (body.length === 0 || body.startsWith("#")) continue;
out.push({ indent, text: body, raw: r });
}
return out;
}
function parseScalar(raw: string): string | number | boolean | null {
const s = raw.trim();
if (s.length === 0) return null;
if (s === "true") return true;
if (s === "false") return false;
if (s === "null" || s === "~") return null;
if (/^-?\d+$/.test(s)) return parseInt(s, 10);
if (/^-?\d+\.\d+$/.test(s)) return parseFloat(s);
if (
(s.startsWith('"') && s.endsWith('"')) ||
(s.startsWith("'") && s.endsWith("'"))
) {
return s.slice(1, -1);
}
return s;
}
function parseFlowArray(raw: string): unknown[] {
const trimmed = raw.trim();
if (!trimmed.startsWith("[") || !trimmed.endsWith("]")) return [];
const body = trimmed.slice(1, -1).trim();
if (body.length === 0) return [];
const items: string[] = [];
let depth = 0;
let cur = "";
let inQuote: string | null = null;
for (const ch of body) {
if (inQuote) {
cur += ch;
if (ch === inQuote) inQuote = null;
continue;
}
if (ch === '"' || ch === "'") {
inQuote = ch;
cur += ch;
continue;
}
if (ch === "[" || ch === "{") depth++;
else if (ch === "]" || ch === "}") depth--;
if (ch === "," && depth === 0) {
items.push(cur);
cur = "";
} else {
cur += ch;
}
}
if (cur.trim().length > 0) items.push(cur);
return items.map((s) => parseScalar(s));
}
function collectBlock(
lines: Line[],
startIdx: number
): { children: Line[]; nextIdx: number } {
const startIndent = lines[startIdx].indent;
const children: Line[] = [];
let i = startIdx + 1;
while (i < lines.length && lines[i].indent > startIndent) {
children.push(lines[i]);
i++;
}
return { children, nextIdx: i };
}
function splitKV(text: string): [string, string] | null {
let inQuote: string | null = null;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (inQuote) {
if (ch === inQuote) inQuote = null;
continue;
}
if (ch === '"' || ch === "'") inQuote = ch;
else if (ch === ":" && (i + 1 === text.length || /\s|$/.test(text[i + 1] ?? ""))) {
return [text.slice(0, i).trim(), text.slice(i + 1).trim()];
}
}
return null;
}
function parseBlockMap(children: Line[]): Record<string, string> {
const out: Record<string, string> = {};
if (children.length === 0) return out;
const baseIndent = children[0].indent;
for (const ln of children) {
if (ln.indent !== baseIndent) continue;
const kv = splitKV(ln.text);
if (!kv) continue;
const scalar = parseScalar(kv[1]);
out[kv[0]] = String(scalar ?? "");
}
return out;
}
function parseNeeds(valueAfterColon: string, children: Line[]): string[] | undefined {
const v = valueAfterColon.trim();
if (v.length > 0) {
if (v.startsWith("[")) {
return parseFlowArray(v).map(String);
}
return [String(parseScalar(v))];
}
const items: string[] = [];
if (children.length === 0) return undefined;
const baseIndent = children[0].indent;
for (const ln of children) {
if (ln.indent !== baseIndent) continue;
if (ln.text.startsWith("- ")) {
items.push(String(parseScalar(ln.text.slice(2))));
}
}
return items.length > 0 ? items : undefined;
}
function parseMatrix(children: Line[], warnings: string[]): MatrixSpec | undefined {
if (children.length === 0) return undefined;
const baseIndent = children[0].indent;
const spec: MatrixSpec = { axes: {} };
let i = 0;
while (i < children.length) {
const ln = children[i];
if (ln.indent !== baseIndent) {
i++;
continue;
}
const kv = splitKV(ln.text);
if (!kv) {
i++;
continue;
}
const [key, val] = kv;
if (key === "include" || key === "exclude") {
const slice: Record<string, unknown>[] = [];
let j = i + 1;
let current: Record<string, unknown> | null = null;
while (j < children.length && children[j].indent > baseIndent) {
const c = children[j];
if (c.text.startsWith("- ")) {
if (current) slice.push(current);
current = {};
const inner = c.text.slice(2);
const innerKv = splitKV(inner);
if (innerKv) current[innerKv[0]] = parseScalar(innerKv[1]);
} else if (current) {
const innerKv = splitKV(c.text);
if (innerKv) current[innerKv[0]] = parseScalar(innerKv[1]);
}
j++;
}
if (current) slice.push(current);
if (key === "include") spec.include = slice;
else spec.exclude = slice;
i = j;
continue;
}
if (key === "fail-fast") {
spec.failFast = val.trim() === "true";
i++;
continue;
}
if (key === "max-parallel") {
const n = parseInt(val.trim(), 10);
if (Number.isFinite(n)) spec.maxParallel = n;
i++;
continue;
}
if (val.trim().length > 0 && val.trim().startsWith("[")) {
spec.axes[key] = parseFlowArray(val);
i++;
} else if (val.trim().length === 0) {
const items: unknown[] = [];
let j = i + 1;
while (j < children.length && children[j].indent > baseIndent) {
const c = children[j];
if (c.text.startsWith("- ")) items.push(parseScalar(c.text.slice(2)));
j++;
}
spec.axes[key] = items;
i = j;
} else {
warnings.push(`matrix axis '${key}' has unsupported shape`);
i++;
}
}
return spec;
}
function extractDispatchInputs(
lines: Line[],
warnings: string[]
): { onArray: string[]; dispatchInputs?: Record<string, DispatchInput> } {
const onArray: string[] = [];
let dispatchInputs: Record<string, DispatchInput> | undefined;
for (let i = 0; i < lines.length; i++) {
const ln = lines[i];
if (ln.indent !== 0) continue;
const kv = splitKV(ln.text);
if (!kv || kv[0] !== "on") continue;
const val = kv[1].trim();
if (val.length > 0) {
if (val.startsWith("[")) {
onArray.push(...parseFlowArray(val).map(String));
} else {
onArray.push(String(parseScalar(val)));
}
break;
}
const { children } = collectBlock(lines, i);
if (children.length === 0) break;
const baseIndent = children[0].indent;
for (let j = 0; j < children.length; j++) {
const c = children[j];
if (c.indent !== baseIndent) continue;
if (c.text.startsWith("- ")) {
onArray.push(String(parseScalar(c.text.slice(2))));
continue;
}
const eventKv = splitKV(c.text);
if (!eventKv) continue;
const event = eventKv[0];
onArray.push(event);
if (event === "workflow_dispatch" && eventKv[1].trim().length === 0) {
let k = j + 1;
while (k < children.length && children[k].indent > baseIndent) {
const inputsLine = children[k];
const inputsKv = splitKV(inputsLine.text);
if (inputsKv && inputsKv[0] === "inputs") {
dispatchInputs = {};
let m = k + 1;
const inputBaseIndent = inputsLine.indent + 2;
while (m < children.length && children[m].indent >= inputBaseIndent) {
const nameLine = children[m];
if (nameLine.indent === inputBaseIndent) {
const nameKv = splitKV(nameLine.text);
if (nameKv) {
const inp: DispatchInput = { type: "string" };
let n = m + 1;
while (n < children.length && children[n].indent > nameLine.indent) {
const fieldKv = splitKV(children[n].text);
if (fieldKv) {
const [fk, fv] = fieldKv;
if (fk === "type") {
const t = String(parseScalar(fv));
if (t === "string" || t === "boolean" || t === "choice" || t === "number") {
inp.type = t;
}
} else if (fk === "required") {
inp.required = parseScalar(fv) === true;
} else if (fk === "default") {
const s = parseScalar(fv);
if (s !== null) inp.default = s as string | boolean | number;
} else if (fk === "description") {
inp.description = String(parseScalar(fv));
} else if (fk === "options" && fv.trim().startsWith("[")) {
inp.options = parseFlowArray(fv).map(String);
}
}
n++;
}
dispatchInputs[nameKv[0]] = inp;
m = n;
continue;
}
}
m++;
}
k = m;
} else {
k++;
}
}
}
}
break;
}
return { onArray, dispatchInputs };
}
function extractJobExtensions(
lines: Line[],
warnings: string[]
): {
workflowEnv?: Record<string, string>;
jobExts: Map<string, Partial<ExtendedJob>>;
stepExts: Map<string, ExtendedStep[]>;
} {
const jobExts = new Map<string, Partial<ExtendedJob>>();
const stepExts = new Map<string, ExtendedStep[]>();
let workflowEnv: Record<string, string> | undefined;
for (let i = 0; i < lines.length; i++) {
const ln = lines[i];
if (ln.indent !== 0) continue;
const kv = splitKV(ln.text);
if (!kv) continue;
if (kv[0] === "env" && kv[1].trim().length === 0) {
const { children, nextIdx } = collectBlock(lines, i);
workflowEnv = parseBlockMap(children);
i = nextIdx - 1;
continue;
}
if (kv[0] !== "jobs" || kv[1].trim().length > 0) continue;
const { children: jobChildren } = collectBlock(lines, i);
if (jobChildren.length === 0) continue;
const jobIndent = jobChildren[0].indent;
let j = 0;
while (j < jobChildren.length) {
const jline = jobChildren[j];
if (jline.indent !== jobIndent) {
j++;
continue;
}
const jkv = splitKV(jline.text);
if (!jkv) {
j++;
continue;
}
const jobName = jkv[0];
const absIdx = lines.indexOf(jline);
const { children: jobBody, nextIdx: jobNextIdx } = collectBlock(lines, absIdx);
const ext: Partial<ExtendedJob> = {};
const steps: ExtendedStep[] = [];
const jobBodyIndent = jobBody.length > 0 ? jobBody[0].indent : 0;
let k = 0;
while (k < jobBody.length) {
const bline = jobBody[k];
if (bline.indent !== jobBodyIndent) {
k++;
continue;
}
const bkv = splitKV(bline.text);
if (!bkv) {
k++;
continue;
}
const [bkey, bval] = bkv;
const bodyAbsIdx = lines.indexOf(bline);
const { children: bodyChildren, nextIdx: bodyNextIdx } = collectBlock(
lines,
bodyAbsIdx
);
if (bkey === "if") {
ext.if = bval.trim().length > 0 ? bval.trim() : undefined;
k++;
} else if (bkey === "needs") {
ext.needs = parseNeeds(bval, bodyChildren);
k = bodyNextIdx - bodyAbsIdx - 1 + k + 1;
} else if (bkey === "env" && bval.trim().length === 0) {
ext.env = parseBlockMap(bodyChildren);
k += bodyChildren.length + 1;
} else if (bkey === "outputs" && bval.trim().length === 0) {
ext.outputs = parseBlockMap(bodyChildren);
k += bodyChildren.length + 1;
} else if (bkey === "strategy" && bval.trim().length === 0) {
for (let s = 0; s < bodyChildren.length; s++) {
const sline = bodyChildren[s];
if (sline.indent !== bodyChildren[0].indent) continue;
const skv = splitKV(sline.text);
if (skv && skv[0] === "matrix" && skv[1].trim().length === 0) {
const sAbsIdx = lines.indexOf(sline);
const { children: matrixChildren } = collectBlock(lines, sAbsIdx);
const matrix = parseMatrix(matrixChildren, warnings);
if (matrix) ext.strategy = { matrix };
}
}
k += bodyChildren.length + 1;
} else if (bkey === "steps" && bval.trim().length === 0) {
let stepIdx = 0;
let s = 0;
while (s < bodyChildren.length) {
const sline = bodyChildren[s];
if (sline.text.startsWith("- ")) {
const stepBody: Line[] = [];
const stepIndent = sline.indent;
const stepFirst: Line = {
indent: stepIndent + 2,
text: sline.text.slice(2),
raw: sline.raw,
};
stepBody.push(stepFirst);
let t = s + 1;
while (t < bodyChildren.length && bodyChildren[t].indent > stepIndent) {
stepBody.push(bodyChildren[t]);
t++;
}
const stepExt: ExtendedStep = {};
for (let u = 0; u < stepBody.length; u++) {
const stepLine = stepBody[u];
const sKv = splitKV(stepLine.text);
if (!sKv) continue;
const [sk, sv] = sKv;
if (sk === "id") stepExt.id = String(parseScalar(sv));
else if (sk === "name") stepExt.name = String(parseScalar(sv));
else if (sk === "if") stepExt.if = sv.trim();
else if (sk === "run") stepExt.run = String(parseScalar(sv));
else if (sk === "uses") stepExt.uses = String(parseScalar(sv));
else if (sk === "parallel") stepExt.parallel = parseScalar(sv) === true;
else if (sk === "continue-on-error")
stepExt.continueOnError = parseScalar(sv) === true;
else if (sk === "with" && sv.trim().length === 0) {
const withBody: Line[] = [];
let v = u + 1;
while (v < stepBody.length && stepBody[v].indent > stepLine.indent) {
withBody.push(stepBody[v]);
v++;
}
stepExt.with = parseBlockMap(withBody);
u = v - 1;
} else if (sk === "env" && sv.trim().length === 0) {
const envBody: Line[] = [];
let v = u + 1;
while (v < stepBody.length && stepBody[v].indent > stepLine.indent) {
envBody.push(stepBody[v]);
v++;
}
stepExt.env = parseBlockMap(envBody);
u = v - 1;
}
}
steps.push(stepExt);
stepIdx++;
s = t;
} else {
s++;
}
}
k += bodyChildren.length + 1;
} else {
k++;
}
}
if (Object.keys(ext).length > 0) jobExts.set(jobName, ext);
if (steps.length > 0) stepExts.set(jobName, steps);
j = jobNextIdx - absIdx - 1 + j + 1;
}
break;
}
return { workflowEnv, jobExts, stepExts };
}
function preprocessForBaseParser(yaml: string): string {
const lines = yaml.replace(/\r\n?/g, "\n").split("\n");
const out: string[] = [];
let inStepsBlock = false;
let stepsIndent = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const trimmed = line.replace(/^\s+/, "");
const indent = line.length - trimmed.length;
if (/^steps\s*:\s*$/.test(trimmed)) {
inStepsBlock = true;
stepsIndent = indent;
out.push(line);
continue;
}
if (inStepsBlock && trimmed.length > 0 && indent <= stepsIndent && !trimmed.startsWith("-")) {
inStepsBlock = false;
}
out.push(line);
if (inStepsBlock && /^-\s+uses\s*:/.test(trimmed)) {
const stepItemIndent = indent;
let hasRun = false;
for (let j = i + 1; j < lines.length; j++) {
const jline = lines[j];
const jtrim = jline.replace(/^\s+/, "");
const jindent = jline.length - jtrim.length;
if (jtrim.length === 0) continue;
if (jtrim.startsWith("-") && jindent === stepItemIndent) break;
if (jindent <= stepItemIndent) break;
if (/^run\s*:/.test(jtrim)) {
hasRun = true;
break;
}
}
if (!hasRun) {
out.push(`${" ".repeat(indent + 2)}run: ':'`);
}
}
}
return out.join("\n");
}
export function parseExtended(yaml: string): ExtendedParseResult {
const base = parseWorkflow(preprocessForBaseParser(yaml));
if (!base.ok) return base;
const warnings: string[] = [];
let extended: ExtendedWorkflow;
try {
const lines = lexLines(yaml);
const { onArray, dispatchInputs } = extractDispatchInputs(lines, warnings);
const { workflowEnv, jobExts, stepExts } = extractJobExtensions(lines, warnings);
const mergedOn = onArray.length > 0 ? Array.from(new Set(onArray)) : base.workflow.on;
const jobs: ExtendedJob[] = base.workflow.jobs.map((baseJob) => {
const ext = jobExts.get(baseJob.name) ?? {};
const extendedStepsForJob = stepExts.get(baseJob.name) ?? [];
const steps: ExtendedStep[] = baseJob.steps.map((bs, idx) => {
const ext = extendedStepsForJob[idx] ?? {};
return { name: bs.name, run: bs.run, ...ext };
});
return {
name: baseJob.name,
runsOn: baseJob.runsOn,
steps,
if: ext.if,
needs: ext.needs,
strategy: ext.strategy,
env: ext.env,
outputs: ext.outputs,
};
});
extended = {
name: base.workflow.name,
on: mergedOn,
dispatchInputs,
env: workflowEnv,
jobs,
warnings,
};
} catch (err) {
warnings.push(
`extension-pass error: ${err instanceof Error ? err.message : String(err)}`
);
extended = {
name: base.workflow.name,
on: base.workflow.on,
jobs: base.workflow.jobs.map((j) => ({
name: j.name,
runsOn: j.runsOn,
steps: j.steps.map((s) => ({ name: s.name, run: s.run })),
})),
warnings,
};
}
return { ok: true, workflow: extended };
}
|