-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.33 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
'use strict'
/*jshint camelcase: false */
var app = require('express')()
var bodyParser = require('body-parser')
var server = require('http').Server(app)
if (!process.env.SLACK_WEBHOOK_URL || !process.env.TITO_ACCESS_KEY) {
console.log('no SLACK_WEBHOOK_URL or TITO_ACCESS_KEY set')
process.exit(1)
}
var slack = require('slack-notify')(process.env.SLACK_WEBHOOK_URL)
slack.onError = function (err) {
console.log('slack error:', err)
}
var ticketSlack = slack.extend({
channel: process.env.SLACK_CHANNEL || '#general',
icon_emoji: ':ticket:',
username: 'Ticket'
})
app.disable('x-powered-by')
app.use(bodyParser.json())
app.post('/tito/:key', function (req, res) {
console.log('new tito request')
var whname = req.headers['x-webhook-name']
if (req.params.key && req.params.key === process.env.TITO_ACCESS_KEY && whname) {
if (req.body.state_name === 'complete') {
var msg = '"' + req.body.name + '" bought/updated a new ticket. (' + req.body.release + ')'
console.log('send to slack:', msg)
ticketSlack(msg)
} else {
console.log('skipped webhook:', whname, '-', req.body.state_name)
}
res.send('ok')
} else res.send('error')
})
app.get('/ping', function (req, res) {
res.send('ok')
})
server.listen(process.env.PORT, function () {
console.log('listening on port %d', process.env.PORT)
})