forked from ehues/slack-workitems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtc.js
More file actions
192 lines (152 loc) · 5.45 KB
/
rtc.js
File metadata and controls
192 lines (152 loc) · 5.45 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
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
var html2text = require('html-to-text');
var WorkItems = require('./wi.js');
var relDate = require('relative-date');
var format = require('string-template');
var pkg = require('./package');
function load_env(name, desc) {
var value = process.env[name];
if ("" == value || undefined === value) {
console.log("Environment variable " + name + " must be set. " + desc);
process.exit(1);
}
return value;
}
// Consume our configuration
var REPO = load_env("RTC_REPO", "It is the URI of the RTC server.");
var USER = load_env("RTC_USER", "It is the username of the RTC user that the bot will login as.");
var PASS = load_env("RTC_PASS", "It is the password of the RTC user that the bot logs in as. ");
var TOKEN = load_env("RTC_BOT_TOKEN", "It is the Slack bot token to use.");
var WEBHOOK = load_env("RTC_WEBHOOK", "It is the Slack webhook to use for rich attachments.");
var RTC_URI_OVERRIDE = process.env.RTC_URI_OVERRIDE;
var wiFetcher = new WorkItems(REPO, USER, PASS);
var ERR_CHANNEL = process.env.RTC_ERROR_CHANNEL;
if (ERR_CHANNEL[0] != '#') {
ERR_CHANNEL = '#' + ERR_CHANNEL;
}
var Session = require('slackr-bot');
var sess = new Session({
token: TOKEN,
webhookClient: {
webhookUrl: WEBHOOK
}
});
// Handling for rewriting the link on a work item. This allows us to
// make work items viewable outside a firewall.
function wiLinkPassThrough(wi) {
return wi['rdf:resource'];
}
function wiLinkRewrite(wi) {
return format(RTC_URI_OVERRIDE, wi['dc:identifier']);
}
var rewriteLink = wiLinkPassThrough;
if (RTC_URI_OVERRIDE) {
console.log('rewriting');
rewriteLink = wiLinkRewrite;
}
// The handler for matches on the channel
function handleWorkItemMatch(message, match) {
console.log("got " + match[1]);
message.typing();
wiFetcher.fetchOSLC(match[1]).then(function(wi) {
// console.log(wi);
var titleText = "Bug " + wi["dc:identifier"] + ": " + html2text.fromString(wi["dc:title"]);
console.log(titleText);
var statusLine = '';
if (wi["rtc_cm:ownedBy"] && wi["rtc_cm:ownedBy"]["rdf:resource"]) {
var ownerUri = wi["rtc_cm:ownedBy"]["rdf:resource"];
var lastSlash = ownerUri.lastIndexOf('/');
if (-1 != lastSlash) {
var userName = ownerUri.substring(lastSlash + 1);
statusLine += '_Owned by *' + userName + '*_. ';
}
}
if (wi["dc:creator"] && wi["dc:creator"]["rdf:resource"]) {
var ownerUri = wi["dc:creator"]["rdf:resource"];
var lastSlash = ownerUri.lastIndexOf('/');
if (-1 != lastSlash) {
var userName = ownerUri.substring(lastSlash + 1);
statusLine += '_Created by *' + userName + '*_. ';
}
}
if (wi["rtc_cm:subscribers"]) {
var subCount = wi["rtc_cm:subscribers"].length;
if (subCount == 1) {
statusLine += '_One subscriber_. ';
}
else {
statusLine += '_' + subCount + " subscribers_. ";
}
}
if (wi["rtc_cm:comments"]) {
var ct = wi["rtc_cm:comments"].length;
if (ct == 1) {
statusLine += '_One comment_. ';
}
else {
statusLine += '_' + ct + ' comments_. ';
}
}
if (wi["dc:created"]) {
var created = Date.parse(wi["dc:created"]);
statusLine += '_Created ' + relDate(created) + '_. ';
}
if (wi["dc:modified"]) {
var mod = Date.parse(wi["dc:modified"]);
statusLine += '_Last modified ' + relDate(mod) + '_. ';
}
message.reply({
text: '',
attachments: [{
fallback: titleText,
title: titleText,
title_link: rewriteLink(wi),
text: statusLine + "\n" + html2text.fromString(wi['dc:description']),
color: "#7CD197",
mrkdwn_in: ["pretext", "text"]
}]
});
}, function(err) {
console.log("Error");
console.log(err);
if (ERR_CHANNEL) {
var errMsg = "Failed fetching work item " + match[1];
var chan = sess.channelData(message.data.channel);
if (chan) {
errMsg += " (referenced in #" + chan.name + ")";
}
sess.sendMessage({
channel: ERR_CHANNEL,
text: '',
attachments: [{
color: 'danger',
fallback: errMsg,
title: errMsg,
text: JSON.stringify(err, null, 2),
mrkdwn_in: ["pretext", "text", "title"]
}]
});
}
else {
message.reply({
text: err.toString()
});
}
});
}
sess.on(/bug\s*(\d+)/i, handleWorkItemMatch);
sess.on(/\b(\d+): \S*/i, handleWorkItemMatch);
sess.on(/http\S*action=com.ibm.team.workitem.viewWorkItem\S*id=(\d+)/i, handleWorkItemMatch);
require("./interactions/bark")(sess);
require("./interactions/karma")(sess);
sess.connected
.then(function (success) {
if (ERR_CHANNEL) {
sess.sendMessage({
channel: ERR_CHANNEL,
text: pkg.name + " (" + pkg.version + ") started."
});
}
})
.fail(function (err) {
console.log(err);
});