-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (59 loc) · 2.39 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
var botName = require('./package.json').name
const configFile = 'claimsy.yml'
const defaultConfig = {
selfAssignCommands: ['assign me'],
assignCommands: ['assign USERNAME'],
enableUniversalAssign: false,
enableDefaultLabels: false,
defaultLabels: []
}
module.exports = app => {
app.on('issue_comment.created', async context => {
// Ignore if the comment was made by the bot
if (context.payload.comment.user.login === botName + '[bot]') { return }
const config = await context.config(configFile, defaultConfig)
let assignToUser = {
assign: false,
user: context.payload.comment.user.login
}
// Extract comment body
const commentBody = context.payload.comment.body.toString().toLowerCase()
// Check for self-assignments
config.selfAssignCommands.forEach((command) => {
if (commentBody.includes(command.toLowerCase())) {
assignToUser = {
assign: true,
user: context.payload.comment.user.login
}
}
})
// Check for mentions only if not self-assigned
if (config.enableUniversalAssign && assignToUser.assign !== true) {
// Check for mentioned user assignments
config.assignCommands.forEach((command) => {
command = command.toLowerCase()
if (commentBody.includes(command.replace('username', '@$').split('$')[0])) {
if (assignToUser.assign !== true) {
assignToUser = {
assign: true,
user: commentBody.split(command.split('username')[0])[1].split(' ')[0].replace('@', '')
}
}
}
})
}
// Check for duplicate assignment
context.payload.issue.assignees.forEach((user) => {
if (assignToUser.user === user.login) {
assignToUser.assign = false
}
})
// Do nothing for non-assignment comments
if (assignToUser.assign === false) { return }
// Choose photo
const img = (Math.random() * 10 % 2 === 0 ? 'https://media1.tenor.com/images/494ba2ef2a2826323f5bf82e9e357283/tenor.gif?itemid=16349021' : 'https://media1.tenor.com/images/72a9ce1fe0e4b94d46684ad710b27c13/tenor.gif')
const newAssignees = context.issue({ assignees: assignToUser.user })
await context.github.issues.addAssignees(newAssignees)
return context.github.issues.createComment(context.issue({ body: '<h3 align="center">Done! 🎉 </h3> \n\n ![image](' + img + ')' }))
})
}