-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (82 loc) · 2.55 KB
/
index.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
/* eslint-disable camelcase */
const debug = require('debug')('hookhub:hook:slack-webhook-out')
debug('Loading hookhub:hook:slack-webhook-out')
const express = require('express')
const router = express.Router()
const smb = require('slack-message-builder')
const rp = require('request-promise')
var config = null
var configurable = function (newConfig) {
config = newConfig
}
// Functions
function stackRegistration (req, res, next) {
res.locals.hookhub.stack.push('hookhub-slack-webhook-out')
next()
}
function defaultHandler (req, res, next) {
debug('Handling request')
if (!config) {
throw new Error('Missing configuration')
}
if (!res.locals.hookhub.doc) {
throw new Error('Missing hookhub.doc')
}
if (res.locals.hookhub.result.result !== 'OK') {
throw new Error('Broken flow')
}
let post_body = generateMessage(res.locals.hookhub.doc)
var post_options = {
method: 'POST',
uri: config.credentials.url,
body: post_body,
json: true // Automatically stringifies the body to JSON
}
rp(post_options).then(function (data) {
res.locals.hookhub.result = {
result: 'OK',
message: data
}
next()
}).catch(function (err) {
res.locals.hookhub.result = {
result: 'ERROR',
message: err
}
next('error')
})
}
var generateMessage = function (hookhub_doc) {
var slack_message = smb()
.username(config.options.username)
.iconEmoji(config.options.icon_emoji)
.channel(config.options.channel)
switch (hookhub_doc.type) {
case 'push':
hookhub_doc.messages.forEach(function (commit) {
slack_message = slack_message
.text("The following commit(s) got pushed to '" + hookhub_doc.topic + "':\r\r")
.attachment()
.fallback(commit.message)
.color('#0000cc')
.authorName(hookhub_doc.author.name)
.authorLink(hookhub_doc.author.url)
.authorIcon(hookhub_doc.author.avatar)
.title('Commit: ' + commit.title)
.titleLink(commit.url)
.text(commit.message)
.footer('Via: hookhub:hook:slack-webhook-out')
.ts(Math.round(Date.parse(commit.timestamp) / 1000))
.end()
})
break
default:
slack_message = slack_message.text("We received a new '" + hookhub_doc.type + "' notification for '" + hookhub_doc.topic + "', but we didn't know what to do with this event!")
break
}
return slack_message.json()
}
// Routes
router.all('/', stackRegistration, defaultHandler)
module.exports = router
module.exports.configurable = configurable