-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (120 loc) · 3.93 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
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
const { size, extend, isEmpty, template } = require('lodash')
const getConfig = require('probot-config')
const emptyCommit = require('make-empty-github-commit')
const NO_CONFIG = 'Hey, if `.config/probot-empty.yaml` file is not present in the repository, I won\'t be able to do anything'
const ERROR = 'Ouch, there was a problem pushing the empty commit, please contact xavijam@gmail.com'
const botName = 'probot-empty[bot]'
module.exports = app => {
app.log('Probot loaded!')
app.on('issue_comment.created', async context => {
const { github, payload } = context
const isPR = !!payload.issue.pull_request
const comment = payload.comment
const user = comment.user
const commentBody = comment.body && comment.body.toLowerCase()
const userName = user.login
const isBot = user.type === 'Bot'
const issueNumber = payload.issue.number
const prResult = await github.pullRequests.get(
extend(
context.repo(),
{
number: issueNumber
}
)
)
const branch = prResult.data.head.ref
// Let's not make anything if it is our Bot 😬
if (userName === botName && isBot) {
return
}
// Check if it is a PR
if (!isPR) {
return
}
// Check config
const config = await getConfig(context, 'probot-empty.yaml') || {}
const magicWords = config.magicWords
if (isEmpty(config)) {
return context.github.issues.createComment(
context.issue({ body: NO_CONFIG })
)
}
// Check magic words
if (commentBody.search(magicWords) > 0) {
const compiled = template(config.messages.exact)
const params = context.issue({ body: compiled({ magicWords }) })
return context.github.issues.createComment(params)
} else if (commentBody.search(magicWords) < 0) {
return
}
// Check requiredFiles
if (config.requiredFiles) {
let missingFiles = []
for (const path of config.requiredFiles) {
await github.repos.getContent(
context.repo({ path })
).catch(e => {
missingFiles.push(path)
})
}
if (size(missingFiles) > 0) {
const compiled = template(config.messages.fileNotPresent)
const params = context.issue({ body: compiled({ files: missingFiles.join(', ') }) })
return context.github.issues.createComment(params)
}
}
// Push empty commit
const result = await emptyCommit(
extend(
context.repo(),
{
github,
branch,
message: config.messages.commit
}
)
).catch(e => {
return context.github.issues.createComment(
context.issue({ body: ERROR })
)
})
if (!result) {
return
}
// Comment
const params = context.issue({ body: config.messages.trigger })
return context.github.issues.createComment(params)
})
app.on('pull_request.opened', async context => {
const { github, payload } = context
// Check config
const config = await getConfig(context, 'probot-empty.yaml') || {}
const magicWords = config.magicWords
if (isEmpty(config)) {
return context.github.issues.createComment(
context.issue({ body: NO_CONFIG })
)
}
let compiled = template(config.messages.appDisabled)
let params
// Check requiredFiles
if (config.requiredFiles) {
let missingFiles = []
for (const path of config.requiredFiles) {
await github.repos.getContent(
context.repo({ path })
).catch(e => {
missingFiles.push(path)
})
}
if (size(missingFiles) > 0) {
params = context.issue({ body: compiled({ files: missingFiles.join(', ') }) })
return context.github.issues.createComment(params)
}
}
compiled = template(config.messages.appEnabled)
params = context.issue({ body: compiled({ magicWords: config.magicWords }) })
return context.github.issues.createComment(params)
})
}