-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (33 loc) · 1.04 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
class Command {
constructor (name, callback) {
this.name = name
this.callback = callback
}
get matcher () {
return /^\/([\w-]+)\b *(.*)?$/m
}
listener (context) {
const { comment, issue, pull_request: pr } = context.payload
const command = (comment || issue || pr).body.match(this.matcher)
if (command && this.name === command[1]) {
return this.callback(context, { name: command[1], arguments: command[2] })
}
}
}
/**
* Probot extension to abstract pattern for receiving slash commands in comments.
*
* @example
*
* // Type `/label foo, bar` in a comment box to add labels
* commands(robot, 'label', (context, command) => {
* const labels = command.arguments.split(/, *\/);
* context.github.issues.addLabels(context.issue({labels}));
* });
*/
module.exports = (robot, name, callback) => {
const command = new Command(name, callback)
const events = ['issue_comment.created', 'issues.opened', 'pull_request.opened']
robot.on(events, command.listener.bind(command))
}
module.exports.Command = Command