-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
111 lines (108 loc) · 3.14 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const {
fetchJSON,
getOwner,
getRepo,
getComment,
isBodyFollowingTemplate,
getFinalComment,
parseBody,
ISSUE_TEMPLATE_FAILURE_MESSAGE,
ISSUE_TEMPLATE_SUCCESS_MESSAGE,
PULL_REQUEST_TEMPLATE_SUCCESS_MESSAGE,
PULL_REQUEST_TEMPLATE_FAILURE_MESSAGE
} = require('./util')
/**
* This is the entry point for your Probot App.
* @param {import('probot').Application} app - Probot's Application class.
*/
module.exports = app => {
app.log('Yay, the app was loaded!')
app.on('issues.opened', async context => {
const issueBody = context.payload.issue.body
const issueCommentOnSuccess = getComment(
context,
ISSUE_TEMPLATE_SUCCESS_MESSAGE
)
const issueCommentOnFailure = getComment(
context,
ISSUE_TEMPLATE_FAILURE_MESSAGE
)
const owner = getOwner(context)
const repo = getRepo(context)
/**
* Fetch contents of ISSUE_TEMPLATE.md containing
* content in base64 format
*/
const templateBody = await fetchJSON(
`https://api.github.com/repos/${owner}/${repo}/contents/ISSUE_TEMPLATE.md`
)
/**
* Parse the fetched JSON issue template and
* extract all fields from it for comparasion
* against the issue body
*/
let templateFields = []
try {
templateFields = parseBody(templateBody)
} catch (err) {
console.error(err)
}
/**
* Checks for occurance of every template field in the
* issue body and returns an appropriate comment based
* on whether the template is abided by
*/
return getFinalComment(
context,
isBodyFollowingTemplate(templateFields, false, issueBody),
issueCommentOnSuccess,
issueCommentOnFailure
)
})
app.on('pull_request.opened', async context => {
const pullRequestBody = context.payload.pull_request.body
const pullRequestCommentOnSuccess = getComment(
context,
PULL_REQUEST_TEMPLATE_SUCCESS_MESSAGE
)
const pullRequestCommentOnFailure = getComment(
context,
PULL_REQUEST_TEMPLATE_FAILURE_MESSAGE
)
const owner = getOwner(context)
const repo = getRepo(context)
/**
* Fetch contents of PULL_REQUEST_TEMPLATE.md containing
* content in base64 format
*/
const templateBody = await fetchJSON(
`https://api.github.com/repos/${owner}/${repo}/contents/PULL_REQUEST_TEMPLATE.md`
)
/**
* Parse the fetched JSON pull request template and
* extract all fields from it for comparasion
* against the PR body
*/
let templateFields = []
try {
templateFields = parseBody(templateBody)
} catch (err) {
console.error(err)
}
/**
* Checks for occurance of every template field in the
* PR body and returns an appropriate comment based
* on whether the template is abided by
*/
return getFinalComment(
context,
isBodyFollowingTemplate(templateFields, false, pullRequestBody),
pullRequestCommentOnSuccess,
pullRequestCommentOnFailure
)
})
// For more information on building apps:
// https://probot.github.io/docs/
// To get your app running against GitHub, see:
// https://probot.github.io/docs/development/
}