-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.js
62 lines (54 loc) · 2.07 KB
/
github.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
var config = require('./config');
var HipChatClient = require('node-hipchat');
function messageHipchat(request){
var hipchat = new HipChatClient(config.hipchatApiKey);
var payload = request.body;
var branchName = payload.ref.replace('refs/heads/','');
var repoName = payload.repository.name;
var roomId = 379365; //default of System Announcements room
config.teams.forEach(function (team){
if (branchName.toLowerCase().substring(0,team.key.length) === team.key)
if (team.key == 'master')
{
if (team.repo == repoName){
roomId = team.roomId;
}
}
else
{
roomId = team.roomId;
}
})
var messageHtml = parseGithubJsonIntoHipChatMessageHtml(payload);
hipchat.postMessage(
{
room: roomId,
from: "JustSayin/GH",
message: messageHtml
}
,function(resp, err){
console.log(resp, err);
if (resp) {
if (resp.status === 'sent'){
console.log("Message sent from Github to Hipchat:" + messageHtml)
}
}
});
}
function parseGithubJsonIntoHipChatMessageHtml(payload){
var github = "https://github.com/";
var pusherUsername = payload.pusher.name;
var branch = payload.ref.replace('refs/heads/','');
var repoUrl = payload.repository.url;
var repo = payload.repository.name;
var html =
"<a href=\"" + github + pusherUsername + "\">" + pusherUsername + "</a> pushed to " +
"<a href=\"" + repoUrl + "/tree/" + branch + "\">" + branch + "</a> branch of repo " +
"<a href=\"" + repoUrl + "\">" + repo + "</a><br>";
payload.commits.forEach(function(commit){
html = html + "- " + commit.message + " (<a href=\"" + commit.url + "\">" + commit.id.slice(0,7) + "</a>)<br>";
});
return html;
}
module.exports.messageHipchat = messageHipchat;
module.exports.parseGithubJsonIntoHipChatMessageHtml = parseGithubJsonIntoHipChatMessageHtml;