forked from ChrisBye/bubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubo_bot.js
101 lines (88 loc) · 3.53 KB
/
bubo_bot.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
try {
var config = require('./config');
console.log(' -=- > Loading configuration from config.js');
}
catch (e) {
console.log(' -=- > Loading configuration from environment variables');
}
var https = require('https');
var jira = require('jira-api');
var wobot = require('wobot');
// Pull this in now to allow either file-based config (checked second) or environment variables (checked first)
var runtimeOptions = {
hipchatUser: process.env.HIPCHAT_USER || config.hipchatUser,
hipchatPassword: process.env.HIPCHAT_PASSWORD || config.hipchatPassword,
jiraBrowseUrl: process.env.JIRA_BROWSE_URL || config.jiraBrowseUrl,
jiraHostname: process.env.JIRA_HOSTNAME || config.jiraHostname,
jiraUsername: process.env.JIRA_USERNAME || config.jiraUsername,
jiraPassword: process.env.JIRA_PASSWORD || config.jiraPassword
};
runtimeOptions.hipchatIgnoredUsers = process.env.HIPCHAT_IGNORED_USERS ? process.env.HIPCHAT_IGNORED_USERS.split(',') : config.hipchatIgnoredUsers;
runtimeOptions.hipchatRoomsToJoin = process.env.HIPCHAT_ROOMS_TO_JOIN ? process.env.HIPCHAT_ROOMS_TO_JOIN.split(',') : config.hipchatRoomsToJoin;
runtimeOptions.jiraProjectRe = process.env.JIRA_PROJECT_RE ? new RegExp(process.env.JIRA_PROJECT_RE, "g") : config.jiraProjectRe;
var b = new wobot.Bot({
jid: runtimeOptions.hipchatUser + "/bot",
password: runtimeOptions.hipchatPassword
});
b.connect();
b.onConnect(function() {
var self = this;
console.log(' -=- > Connect');
runtimeOptions.hipchatRoomsToJoin.forEach(function(room) {
console.log(' -=- > Joining default room ' + room);
self.join(room);
});
});
b.onInvite(function(roomJid, fromJid, reason) {
var self = this;
console.log(' -=- > Invited to and joining ' + roomJid + ' by ' + fromJid + ': ' + reason);
self.join(roomJid);
});
b.onMessage(function(channel, from, message) {
var self = this;
var alreadyProcessed = [];
var matches = message.match(runtimeOptions.jiraProjectRe);
if (!matches) {
return;
}
console.log(' -=- > Looking up JIRA details for ' + message + ' with matches: ' + matches + ' [from ' + from + ']');
matches.forEach(function(jiraKey) {
if (alreadyProcessed.indexOf(jiraKey) < 0 && runtimeOptions.hipchatIgnoredUsers.indexOf(from) < 0) {
alreadyProcessed.push(jiraKey);
var options = {
auth: runtimeOptions.jiraUsername + ':' + runtimeOptions.jiraPassword,
headers: { 'accept': 'application/json' },
hostname: runtimeOptions.jiraHostname,
method: 'GET',
path: '/rest/api/2/issue/' + jiraKey,
port: 443
};
var body = '';
var req = https.request(options, function(res) {
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
try {
var jiraData = JSON.parse(body);
var clarification = runtimeOptions.jiraBrowseUrl
+ jiraData.key + ' “'
+ jiraData.fields.summary + '” ('
+ jiraData.fields.customfield_10995 + ' pts) marked as '
+ jiraData.fields.status.name + ' and assigned to ' + (jiraData.fields.assignee && jiraData.fields.assignee.displayName ? jiraData.fields.assignee.displayName : "Nobody" );
self.message(channel, clarification);
}
catch (e) {
var sorry = '/me could not find ' + jiraKey;
console.error(e);
self.message(channel, sorry);
}
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
}
});
});