-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathIssueBodyChecker.js
More file actions
79 lines (65 loc) · 1.61 KB
/
IssueBodyChecker.js
File metadata and controls
79 lines (65 loc) · 1.61 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
function fromBase64 (content) {
return Buffer.from(content, 'base64').toString()
}
async function getIssueTemplateBody (context, path) {
try {
const singleTemplate = await context.github.repos.getContents(
context.repo({ path })
)
return fromBase64(singleTemplate.data.content)
} catch (err) {
return null
}
}
async function getIssueTemplatePaths (context) {
try {
const templatesDirectory = await context.github.repos.getContents(
context.repo({ path: '.github/ISSUE_TEMPLATE/' })
)
return templatesDirectory.data.map(f => f.path)
} catch (err) {
return null
}
}
async function getIssueTemplates (context) {
const defaultTemplate = await getIssueTemplateBody(
context,
'.github/ISSUE_TEMPLATE.md'
)
if (defaultTemplate !== null) {
return [defaultTemplate]
}
const paths = await getIssueTemplatePaths(context)
if (paths !== null) {
const templates = []
for (const path of paths) {
const template = await getIssueTemplateBody(context, path)
if (template != null) {
templates.push(template)
}
}
return templates
}
return []
}
async function isBodyEqualToTemplate (body, context) {
const issueTemplates = await getIssueTemplates(context)
for (const template of issueTemplates) {
if (body.includes(template)) {
return true
}
}
return false
}
class IssueBodyChecker {
static async isBodyValid (body, context) {
if (!body) {
return false
}
if (await isBodyEqualToTemplate(body, context)) {
return false
}
return true
}
}
module.exports = IssueBodyChecker