This repository was archived by the owner on Oct 30, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (119 loc) · 3.1 KB
/
Copy pathindex.js
File metadata and controls
143 lines (119 loc) · 3.1 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
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
143
async function getConfig(context) {
return await context.config('bot-config.yml', {
bot: {
name: '@bot'
},
whitelist: {
users: [],
labels: []
}
});
}
async function isWhiteListedLabel(label, context) {
const config = await getConfig(context);
return !!config.whitelist.labels.find((e) => {
return e.toUpperCase() === label.toUpperCase();
});
}
async function whiteListedUser(context) {
const config = await getConfig(context);
if (context.payload.comment.author_association === 'OWNER') {
return true;
}
if (config.whitelist.users.includes(context.payload.comment.user.login)) {
return true;
}
return false;
}
function parse(command) {
const pattern = /(".*?"|[^"\s]+)(?=\s*|\s*$)/g;
let match;
let parsed = [];
while (match = pattern.exec(command)) {
parsed = [...parsed, match[0].replace(/"/g, '')];
}
return parsed;
}
module.exports = (robot) => {
console.log('Yay, the app was loaded!');
// robot.on('issues.opened', async context => {
// console.log(context.github.issues);
// const config = await getConfig(context);
// context.github.issues.createComment(context.issue({body: 'test'}));
// });
robot.on('issue_comment', async context => {
//do nothing if the comment is deleted
if (context.payload.action === 'deleted') {
return;
}
//do not accept commands from other bots
if (context.payload.comment.user.type === 'Bot') {
return;
}
const config = await getConfig(context);
const issues = context.github.issues;
const tokens = parse(context.payload.comment.body);
const [bot, command, ...args] = tokens;
const errors = {
unknow: () => {
issues.createComment(context.issue({
body: 'Oh, snap! I couldn\'t understand your request.'
}));
},
command: (command) => {
issues.createComment(context.issue({
body: `You don't have permission to execute this command \`${ command }\``
}));
},
label: (label) => {
issues.createComment(context.issue({
body: `You don't have permission to use this label \`${ label }\``
}));
}
};
console.log('command:', bot, command, args);
if (config.bot.name.toLowerCase() !== bot.toLowerCase()) {
//probably a mention
// errors.unknow();
return;
}
if (!await whiteListedUser(context)) {
errors.command(command);
return;
}
switch (command) {
case 'label':
console.log('labeling issue');
const [operation, ...labels] = args;
const [label] = labels;
//change this if multiple labels commands become necessary
if (!await isWhiteListedLabel(label, context)) {
errors.label(label);
} else {
switch (operation) {
case 'add':
issues.addLabels(context.issue({labels:[label]}));
break;
case 'remove':
issues.removeLabel(context.issue({name:label}));
break;
default:
errors.unknow();
}
}
break;
case 'close':
console.log('closing issue');
issues.update(context.issue({
state: 'closed'
}));
break;
case 'open':
console.log('opening issue');
issues.update(context.issue({
state: 'open'
}));
break;
}
});
};