-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.js
103 lines (93 loc) · 3.18 KB
/
server.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
102
103
// Basic express server to listen for Bitbucket webhooks.
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
// Used to push message to chat services.
var request = require('request');
// See config.js for server ports and chat webhook configuration.
var config = require('./config');
// Create Discord-formatted message and send it to the Discord webhook.
const discordPost = (message) => {
let newMessage = {
"username": config.username,
"content": 'New push to ' + message.repo + ' by ' + message.username + '.',
"embeds": [{
title: message.hash,
description: message.commit,
url: message.link
}]
}
request({
url: config.discordEndpoint,
method: 'POST',
json: true,
body: newMessage
}, function(error, response, body) {
console.log('Discord message sent.');
//console.log(response);
});
}
// Create Slack-formatted message and send it to the Slack webhook.
const slackPost = (message) => {
let newMessage = {
'username': config.username,
'icon_emoji': ':card_file_box:',
'text': 'New push to ' + message.repo + ' by ' + message.username + '.',
'attachments': [{
"title": message.hash,
"title_link": message.link,
"text": message.commit
}]
}
request({
url: config.slackEndpoint,
method: 'POST',
json: true,
body: newMessage
}, function(error, response, body) {
console.log('Slack message sent.');
//console.log(response);
});
}
// Figure out which endpoints are configured.
const post = (message) => {
console.log('Posting message...');
if(config.discordEndpoint) {
discordPost(message);
}
if(config.slackEndpoint) {
slackPost(message);
}
}
// Debug to see if your server's config'd correctly.
/*app.get('/', function (req, res) {
res.send('You\'re not supposed to be GET-ing here.');
});*/
// Listen for HTTP POST requests in whatever folder you run this app in.
app.post('/', function(req,res) {
console.log('Bitbucket webhook recieved!');
res.json({message: 'Message recieved by Bitbot.'});
// console.log(req.body);
// Turn the response into something easier to work with.
let message = {
'username': req.body.actor.username,
'display_name': req.body.actor.display_name,
'repo': req.body.repository.name,
'hash': req.body.push.changes[0].commits[0].hash,
'commit': req.body.push.changes[0].commits[0].message,
'link': req.body.push.changes[0].links.html.href
};
console.log(message);
post(message);
});
// Start listening on the configured port.
app.listen(config.port, function () {
console.log(config.name + ' running on port ' + config.port + '.');
if(config.discordEndpoint && config.slackEndpoint) {
console.log('Running in Discord and Slack mode.');
}
else if(config.discordEndpoint) { console.log('Running in Discord mode.') }
else if(config.slackEndpoint) { console.log('Running in Slack mode.') }
else { console.log('Endpoints not configured.') }
});