-
Notifications
You must be signed in to change notification settings - Fork 317
/
template.js
92 lines (85 loc) · 2.44 KB
/
template.js
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
const { log } = require('./log')
const regexParser = require('regex-parser')
const regexEscape = require('escape-string-regexp')
/**
* replaces all uppercase dollar templates with their string representation from object
* if replacement is undefined in object the dollar template string is left untouched
*/
const template = (string, object, customReplacers) => {
let input = string.replace(/(\$[A-Z_]+)/g, (_, k) => {
let result
if (object[k] === undefined || object[k] === null) {
result = k
} else if (typeof object[k] === 'object') {
result = template(object[k].template, object[k])
} else {
result = `${object[k]}`
}
return result
})
if (customReplacers) {
for (const { search, replace } of customReplacers) {
input = input.replace(search, replace)
}
}
return input
}
function toRegex(search) {
return /^\/.+\/[AJUXgimsux]*$/.test(search)
? regexParser(search)
: new RegExp(regexEscape(search), 'g')
}
function validateReplacers({ context, replacers }) {
return replacers
.map((replacer) => {
try {
return { ...replacer, search: toRegex(replacer.search) }
} catch {
log({
context,
message: `Bad replacer regex: '${replacer.search}'`,
})
return false
}
})
.filter(Boolean)
}
function validateAutolabeler({ context, autolabeler }) {
return autolabeler
.map((autolabel) => {
try {
return {
...autolabel,
branch: autolabel.branch.map((reg) => {
return toRegex(reg)
}),
title: autolabel.title.map((reg) => {
return toRegex(reg)
}),
body: autolabel.body.map((reg) => {
return toRegex(reg)
}),
}
} catch {
log({
context,
message: `Bad autolabeler regex: '${autolabel.branch}', '${autolabel.title}' or '${autolabel.body}'`,
})
return false
}
})
.filter(Boolean)
}
function validateCategories({ categories }) {
if (
categories.filter((category) => category.labels.length === 0).length > 1
) {
throw new Error(
'Multiple categories detected with no labels.\nOnly one category with no labels is supported for uncategorized pull requests.'
)
}
}
exports.template = template
exports.validateReplacers = validateReplacers
exports.validateAutolabeler = validateAutolabeler
exports.validateCategories = validateCategories