-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.ts
271 lines (246 loc) · 7.26 KB
/
parser.ts
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
import { parse as parseYaml } from 'yaml';
type File = {
name: string;
path: string;
sha: string;
size: number;
url: string;
html_url: string;
git_url: string;
download_url: string;
type: string;
raw: string;
};
type BodyBase<TAttributes> = {
id?: string;
attributes: TAttributes;
validations?: {
required?: boolean;
};
};
type CheckboxBody = {
type: 'checkboxes';
} & BodyBase<{
label: string;
description?: string;
options: {
label: string;
required?: boolean;
}[];
}>;
type DropdownBody = {
type: 'dropdown';
} & BodyBase<{
label: string;
description?: string;
multiple?: boolean;
options: string[];
}>;
type InputBody = {
type: 'input';
} & BodyBase<{
label: string;
description?: string;
placeholder?: string;
value?: string;
}>;
type TextAreaBody = {
type: 'textarea';
} & BodyBase<{
label: string;
description?: string;
placeholder?: string;
value?: string;
render?: string;
}>;
type MarkdownBody = {
type: 'markdown';
} & BodyBase<{
label?: string;
value: string;
}>;
export type Body =
| CheckboxBody
| DropdownBody
| InputBody
| TextAreaBody
| MarkdownBody;
type Template = {
name: string;
description: string;
title?: string;
body: Body[];
assignees?: Array<string> | string;
labels?: Array<string> | string;
};
type OneOfBody = Extract<Body, { type: Body['type'] }>;
/**
* Match individual value on issue
* @param item Current issue template body entry that is being parsed.
* @param originalText Issue text
* @param nextItem Next issue template body entry or undefined if the `item` was last
* @returns An array containing remaining text to parse on the issue on index 0 and a
* map of key: value pair matched from the issue body.
*/
export const match = (
item: OneOfBody,
originalText: string,
nextItem: OneOfBody | undefined
): [string, { [key: string]: string | string[] }] => {
const key =
item.id || item.attributes.label?.replace(/\s+/g, '-').toLowerCase();
if (!key || !item.attributes.label) {
throw new Error('Key not found in template');
}
const text = originalText.replaceAll('\n', '\\n');
// If there's a next item, match until it's label is found. In case there's no next item
// match until the end.
const regex = new RegExp(
nextItem
? `^### ${item.attributes.label}\\\\n\\\\n(?<text>([^])*?)(\\\\n)*(?=### ${nextItem.attributes.label})`
: `^### ${item.attributes.label}\\\\n\\\\n(?<text>([^])*)$`
);
const matches = regex.exec(text);
if (
!matches ||
!matches.length ||
!matches?.groups?.text ||
(['dropdown', 'input'].includes(item.type) &&
matches.groups.text.includes('\\n'))
) {
throw new Error(`No match found for ${item.attributes.label}`);
}
// Remove the match from the end of the text
const nextText = text.slice(matches[0].length).replaceAll('\\n', '\n');
const value = matches.groups.text.replaceAll('\\n', '\n');
// "_No response" is left by dropdown, textarea or input if no value is set.
// If nothing is set, "checkboxes" outputs all options with not selected checkboxes
// This has to be handled when parsing values
if (value === '_No response_') {
switch (item.type) {
case 'dropdown':
return [nextText, { [key]: [] }];
default:
return [nextText, { [key]: '' }];
}
}
switch (item.type) {
case 'checkboxes':
// Filter selected values only
return [
nextText,
{
[key]: value.split('\n').reduce((acc, i) => {
if (i.startsWith('- [X] ')) {
acc.push(i.replace(/^- \[X\] /, ''));
}
return acc;
}, [] as string[]),
},
];
case 'dropdown':
// Coma is used to separate individual values (if `multiple` option is truthy),
// however the same letter can be present in each option as well. We need to properly
// check for option presence.
return [
nextText,
{ [key]: item.attributes.options.filter((o) => value.includes(o)) },
];
case 'textarea':
// if render == true, value is wrapped in "```true" on the first line and
// "```" on last, omit those lines
if (item.attributes.render) {
const lines = value.split('\n');
return [
nextText,
{ [key]: lines.slice(1, lines.length - 1).join('\n') },
];
} else {
return [nextText, { [key]: value }];
}
default:
return [nextText, { [key]: value }];
}
};
/**
* Fetch all YAML issue templates for given repo
* @param context Probot context on `issue.created` event
* @returns All YAML issue templates found in given git repo.
*/
export const fetchTemplates = async (context: any): Promise<File[]> => {
const owner = context.payload.repository.owner.login;
const repo = context.payload.repository.name;
const isYamlFile = (f: File) => f.name.match(/.*\.ya?ml/);
const fetchFileContent = async (f: File) => ({
...f,
raw: await context.octokit
.request(`GET ${f.download_url}`)
.then((r: Record<string, unknown>) => r.data)
.catch(() => ''),
});
return context.octokit.repos
.getContent({
owner,
repo,
path: '.github/ISSUE_TEMPLATE',
})
.then(async (r: Record<string, unknown>) =>
Promise.all(
(r.data as Array<File>).filter(isYamlFile).map(fetchFileContent)
)
)
.catch(() => []);
};
/**
* Parse issue template into a javascript object
* @param context Probot context for `issue.created` event
* @returns Promise that resolves to a map where keys are IDs (or labels) from
* issue template fields and values are data from the issue
*/
export const parse = async (
context: any
): Promise<Record<string, string | string[]>> => {
const body =
(context.payload.issue.body as string).replaceAll('\r\n', '\n') || '';
const templates = (await fetchTemplates(context))
.map((f) => ({
file: f,
template: parseYaml(f.raw) as Template,
}))
.sort((a, b) => b.template.body.length - a.template.body.length);
let parsed = {};
for (const idx in templates) {
try {
let iterBody = body;
parsed = templates[idx].template.body
// "markdown" type can be ignored
.filter((item) => item.type !== 'markdown')
// Collect each item from parser
.reduce((acc, item, idx, src) => {
const nextItem = (idx < src.length - 1 && src[idx + 1]) || undefined;
const [nextBody, map] = match(item, iterBody, nextItem);
iterBody = nextBody;
return {
...acc,
...map,
};
}, {});
} catch (e: any) {
// match() function throws exception in case there's no label for given field in the issue.
// This means the issue was not created via this template and we should continue iterating through templates.
context.log.debug("This template doesn't match", e, {
template: templates[idx].file.name,
});
parsed = {};
continue;
}
// No exception was risen, that means we found a template which matches all the fields found in a template
break;
}
if (!parsed) {
throw new Error(
'No template matches this issue body. Issue was probably created using something else than Issue Forms'
);
}
return parsed;
};