-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetadata.ts
More file actions
103 lines (94 loc) · 2.74 KB
/
Copy pathmetadata.ts
File metadata and controls
103 lines (94 loc) · 2.74 KB
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
import { jobKindSchema, runModeSchema } from "../config/schema.ts";
import type { IssueMeta, JobKind, RunMode } from "../model/types.ts";
const BODY_BLOCK = /<!--\s*beflow\s*([\s\S]*?)-->/i;
function asRunMode(value: string): RunMode | undefined {
const r = runModeSchema.safeParse(value);
return r.success ? r.data : undefined;
}
function asJobKind(value: string): JobKind | undefined {
const r = jobKindSchema.safeParse(value);
return r.success ? r.data : undefined;
}
function parseBodyBlock(body: string): IssueMeta {
const match = BODY_BLOCK.exec(body);
if (!match || match[1] === undefined) {
return {};
}
const meta: IssueMeta = {};
for (const line of match[1].split("\n")) {
const sep = line.indexOf(":");
if (sep === -1) {
continue;
}
const key = line.slice(0, sep).trim();
const value = line.slice(sep + 1).trim();
if (value === "") {
continue;
}
switch (key) {
case "agent":
meta.agent = value;
break;
case "repo":
meta.repo = value;
break;
case "runMode": {
const rm = asRunMode(value);
if (rm) {
meta.runMode = rm;
}
break;
}
case "jobKind": {
const jk = asJobKind(value);
if (jk) {
meta.jobKind = jk;
}
break;
}
}
}
return meta;
}
function parseLabels(labels: string[]): IssueMeta {
const meta: IssueMeta = {};
for (const label of labels) {
const sep = label.indexOf(":");
if (sep === -1) {
continue;
}
const key = label.slice(0, sep).trim();
const value = label.slice(sep + 1).trim();
if (value === "") {
continue;
}
switch (key) {
case "agent":
meta.agent = value;
break;
case "repo":
meta.repo = value;
break;
case "run": {
const rm = asRunMode(value);
if (rm) {
meta.runMode = rm;
}
break;
}
case "jobkind": {
const jk = asJobKind(value);
if (jk) {
meta.jobKind = jk;
}
break;
}
}
}
return meta;
}
export function parseIssueMeta(body: string, labels: string[]): IssueMeta {
const fromLabels = parseLabels(labels);
const fromBody = parseBodyBlock(body);
return { ...fromLabels, ...fromBody };
}