-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira.js
58 lines (48 loc) · 1.85 KB
/
jira.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
var config = require('./config');
var HipChatClient = require('node-hipchat');
function messageHipchat(req){
var hipchat = new HipChatClient(config.hipchatApiKey);
var roomId = 379365; //default of System Announcements room
config.teams.forEach(function (team){
if (req.body.issue.key.toLowerCase().substring(0,team.key.length) === team.key)
roomId = team.roomId;
})
var htmlMessage = parseJiraPostBodyToHtmlMessage(req.body);
hipchat.postMessage(
{
room: roomId,
from: "JustSayin/JR",
message: htmlMessage
}
, function (resp, err) {
console.log(resp, err);
if (resp) {
if (resp.status === 'sent') {
console.log("Message sent from JIRA to Hipchat: " + htmlMessage)
}
}
});
}
function parseJiraPostBodyToHtmlMessage(jira) {
var message = '';
if (jira.webhookEvent == 'jira:issue_updated') {
var username = jira.user.displayName;
if (jira.changelog != 'undefined') {
jira.changelog.items.forEach(function (change) {
if (change.field == 'status') {
message = createStatusUpdateMessage(username, change, jira.issue.key);
}
})
}
}
return message;
}
function createStatusUpdateMessage(username,change, issueKey){
var html =
"<strong>" + username + "</strong> changed the <strong>status</strong> of " +
"<a href=\"https://justgiving.atlassian.net/browse/" + issueKey + "\">" + issueKey + "</a> from " +
"<strong>" + change.fromString + "</strong> to <strong>" + change.toString + "</strong>";
return html;
}
module.exports.messageHipchat = messageHipchat;
module.exports.parseJiraPostBodyToHtmlMessage = parseJiraPostBodyToHtmlMessage;