-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
315 lines (283 loc) · 11.2 KB
/
index.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var RtmClient = require('@slack/client').RtmClient;
var WebClient = require('@slack/client').WebClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var jenkins = require('jenkins');
import {} from 'primary';
import _ = require('underscore');
var bot_token = process.env.SLACK_BOT_TOKEN || '';
var jenkins_url = process.env.JENKINS_URL || '';
var log_level = process.env.LOG_LEVEL || '';
var crumb_issuer = process.env.CRUMB_ISSUER == "true" || false;
var jenkinsClient = jenkins({baseUrl: jenkins_url, crumbIssuer: crumb_issuer});
var opts: any = {};
if (log_level !== "") {
opts["logLevel"] = log_level;
}
var rtm = new RtmClient(bot_token, opts);
var webClient = new WebClient(bot_token);
rtm.start();
if (RegExp.prototype.flags === undefined) {
Object.defineProperty(RegExp.prototype, 'flags', {
configurable: true,
get: function () {
return this.toString().match(/[gimuy]*$/)[0];
}
});
}
function extractRegex(regex: RegExp, data: string) {
let regexClone = new RegExp(regex.source, regex.flags);
let m: RegExpExecArray, matchs: (RegExpExecArray|string)[] = [];
if (regexClone.global) {
while ((m = regexClone.exec(data)) !== null) {
if (m.index === regexClone.lastIndex) {
regexClone.lastIndex++;
}
if (m.length > 2) {
m.shift();
matchs.push(m);
} else {
matchs.push(m[1]);
}
}
} else {
if (m = regexClone.exec(data)) {
if (m.length > 2) {
m.shift();
matchs.push(m);
} else {
matchs.push(m[1]);
}
}
}
return matchs;
}
function sendNotEnoughParametersAndReturnFalse(regexp: (RegExpExecArray|string)[], channel: string) {
if (!regexp.length) {
rtm.sendMessage("Not enough parameters", channel);
return false;
} else {
return true;
}
}
let actions: BotAction[] = [
{
text: "build info",
regex: /build info ([a-z0-9_]+) ([0-9]+)/i,
description: "build info JOB_NAME JOB_BUILD_NUMBER : output of the build",
func: function (slackUser: SlackUser, message: SlackMessage): void {
let resRegexp = extractRegex(this.regex, message.text);
if (sendNotEnoughParametersAndReturnFalse(resRegexp, message.channel)) {
let jobName: string = resRegexp[0][0];
let jobNumber: string = resRegexp[0][1];
let options = {
name: jobName,
number: jobNumber
};
jenkinsClient.build.get(options, function (err: Error, data: string) {
let msg = "";
if (err) {
msg += "You got an error : " + err.message + "\n" + err.stack;
} else {
msg += "Your job " + jobName + " output :\n" + JSON.stringify(data, null, 3);
}
rtm.sendMessage(msg, message.channel);
});
}
}
},
{
text: "build log",
regex: /build log ([a-z0-9_]+) ([0-9]+)/i,
description: "build log JOB_NAME JOB_BUILD_NUMBER : output of the build",
func: function (slackUser: SlackUser, message: SlackMessage): void {
let resRegexp = extractRegex(this.regex, message.text);
if (sendNotEnoughParametersAndReturnFalse(resRegexp, message.channel)) {
let jobName: string = resRegexp[0][0];
let jobNumber: string = resRegexp[0][1];
let options = {
name: jobName,
number: jobNumber
};
jenkinsClient.build.log(options, function (err: Error, data: string) {
let msg = "";
if (err) {
msg += "You got an error : " + err.message + "\n" + err.stack;
} else {
msg += "Your job " + jobName + " output :\n" + data;
}
let obj = {
attachments: [{
fallback: "Job log",
pretext: "Job log of " + jobName + " " + jobNumber,
text: msg
}]
};
webClient.chat.postMessage(message.channel, "", obj, function (err: Error, data: any) {
if (err) {
console.log(err.message)
}
});
});
}
}
},
{
text: "build job",
regex: /build job ([a-z0-9_]+)( with (.*))?/i,
description: "build job JOB_NAME || build job JOB_NAME with PARAMETERS : build a job with JOB_NAME and optional PARAMETERS",
func: function (slackUser: SlackUser, message: SlackMessage): void {
let resRegexp = extractRegex(this.regex, message.text);
if (sendNotEnoughParametersAndReturnFalse(resRegexp, message.channel)) {
let jobName: string = resRegexp[0][0];
let jobParameters: string = resRegexp[0][2];
let options: any = {name: jobName};
if (jobParameters) {
options["parameters"] = jobParameters;
}
jenkinsClient.job.build(options, function (err: Error, queueItemNumber: string) {
let msg = "";
if (err) {
msg += "You got an error, maybe it does (not) have parameters ... : " + err.message;
} else {
msg += "Your job " + jobName + " have been scheduled !";
// jenkinsClient.queue.item(queueItemNumber, function (err: Error, data: any) {
// let msgQueue = "";
// if (err) {
// msgQueue += "Error while getting the queue detail for your job";
// } else {
// msgQueue += "You can see the job log with : " + slackUser.name + " build log " + jobName + " " + data.executable.number;
// }
// rtm.sendMessage(msgQueue, message.channel);
// });
}
rtm.sendMessage(msg, message.channel);
});
}
}
},
{
text: "job info",
regex: /job info ([a-z0-9_]+)?/i,
description: "job info JOB_NAME : a job info with JOB_NAME",
func: function (slackUser: SlackUser, message: SlackMessage): void {
let resRegexp = extractRegex(this.regex, message.text);
if (sendNotEnoughParametersAndReturnFalse(resRegexp, message.channel)) {
let jobName: string = resRegexp[0] as string;
jenkinsClient.job.get(jobName, function (err: Error, data: string) {
let msg = "";
if (err) {
msg += "You got an error : " + err.message + "\n" + err.stack;
} else {
msg += "Job " + jobName + " info :\n" + JSON.stringify(data, null, 3);
}
rtm.sendMessage(msg, message.channel);
});
}
}
},
{
text: "who are you ?",
description: "really ?",
func: function (slackUser: SlackUser, message: SlackMessage): void {
rtm.sendMessage('I am ' + slackUser.real_name + ", you can call me " + slackUser.name, message.channel);
}
},
{
text: "queue",
description: "the list of jenkins queue",
func: function (slackUser: SlackUser, message: SlackMessage): void {
jenkinsClient.queue.list(function (err: any, data: JenkinsQueue[]) {
let msg = "Queue :\n" + JSON.stringify(data);
rtm.sendMessage(msg, message.channel);
});
}
},
{
text: "jobs",
description: "the list of jenkins jobs",
func: function (slackUser: SlackUser, message: SlackMessage): void {
jenkinsClient.job.list(function (err: any, data: JenkinsJob[]) {
let msg = "";
data.forEach(function (job) {
msg += job.name + "\n"
});
let obj = {
attachments: [{
fallback: "Job list",
pretext: "Jobs :",
text: msg
}]
};
webClient.chat.postMessage(message.channel, "", obj, function (err: Error, data: any) {
if (err) {
console.log(err.message)
}
});
});
}
},
{
text: "help",
description: "this help",
func: function (slackUser: SlackUser, message: SlackMessage): void {
let text = "Help :\n";
_.each(actions, function (action: BotAction) {
if (action.text !== "" && !action.notVisibleInHelp) {
text += "\t - " + action.text + " - " + action.description + "\n";
}
});
rtm.sendMessage(text, message.channel);
}
},
{
text: "life is too hard",
description: "reboot myself to forget",
func: function (slackUser: SlackUser, message: SlackMessage): void {
rtm.sendMessage('Rebooting system ....', message.channel);
process.exit();
}
},
{
text: "take this beer",
description: "give a beer to the bot",
func: function (slackUser: SlackUser, message: SlackMessage): void {
rtm.sendMessage('<@' + message.user + '> Thanks mate', message.channel);
}
},
{
text: "take this bear",
description: "give a bear to the bot",
notVisibleInHelp: true,
func: function (slackUser: SlackUser, message: SlackMessage): void {
rtm.sendMessage('Not that kind of BEER !', message.channel);
}
},
{
text: "",
description: "",
func: function (slackUser: SlackUser, message: SlackMessage): void {
rtm.sendMessage("I understand what you'are saying <@" + message.user + ">, AND I DON'T CARE !", message.channel);
}
}
];
function getAction(user: SlackUser, text: string): BotAction {
return _.find(actions, function (action: BotAction) {
let checks = [
user.name + " " + action.text,
"<@" + user.id + "> " + action.text,
user.real_name + " " + action.text
];
return _.some(checks, function (check) {
return text.indexOf(check) === 0;
})
});
}
rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message: SlackMessage) {
let slackUser: SlackUser = rtm.dataStore.getUserById(rtm.activeUserId);
if (message.text) {
let action = getAction(slackUser, message.text);
if (action) {
action.func(slackUser, message);
}
}
});