Last active
September 16, 2018 14:30
-
-
Save kob-to-wni/6990d45bfe4352d327d76ce69bd9a77a to your computer and use it in GitHub Desktop.
Node.js Backlog WebHook server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Http = require('http'); | |
const Request = require('request'); | |
const LISTEN_ADDRESS = '127.0.0.1'; | |
const LISTEN_PORT = 9090; | |
const REQUEST_TIMEOUT = 1000; | |
const URL_PREFIX = '/webhook'; | |
const PROXY = ''; | |
const SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/*********/*********/***********************'; | |
const SLACK_CHANNEL = 'general'; | |
const BACKLOG_HOST = '**********.backlog.jp' | |
const MODULES = { | |
'backlog': require('./backlog.js') | |
}; | |
new Http.Server().on('request', (req, res) => { | |
if (req.method !== 'POST') { | |
res.writeHeader(403); | |
res.end(); | |
return; | |
} | |
var d = ''; | |
var timeout = setTimeout(() => { | |
req.end(); | |
}, REQUEST_TIMEOUT); | |
req.on('data', (c) => { | |
d += c; | |
}).on('end', () => { | |
clearTimeout(timeout); | |
console.log('WebHook called: ' + req.url); | |
try { | |
process(req.url, d); | |
res.writeHeader(200); | |
} catch (e) { | |
console.error(e); | |
res.writeHeader(503); | |
} | |
res.end(); | |
}); | |
}).on('close', () => { | |
console.log('WebHook server closed.'); | |
}).listen(LISTEN_PORT, LISTEN_ADDRESS, () => { | |
console.log('WebHook server started.'); | |
}); | |
function process(url, data) { | |
var d = JSON.parse(data); | |
var matched = false; | |
for (var m in MODULES) { | |
} | |
switch(url) { | |
case '/webhook/backlog': processBacklog(d); break; | |
default: | |
throw new Error('Unknown url: ' + url); | |
} | |
} | |
function processBacklog(data) { | |
switch(data.type) { | |
case 1: // 課題追加 | |
console.log('Backlog issue added.'); | |
postSlack( | |
'Backlog', | |
null, | |
SLACK_CHANNEL, | |
'【新規' + data.content.issueType.name + '】 ' | |
+ '<https://' | |
+ BACKLOG_HOST | |
+ '/view/' | |
+ data.project.projectKey + '-' + data.content.key_id | |
+ '|' | |
+ data.project.projectKey + '-' + data.content.key_id | |
+ '> ' + data.content.summary | |
); | |
break; | |
case 2: // 課題更新 | |
console.log('Backlog issue changed.'); | |
postSlack( | |
'Backlog', | |
null, | |
SLACK_CHANNEL, | |
'【更新】 ' | |
+ '<https://' | |
+ BACKLOG_HOST | |
+ '/view/' | |
+ data.project.projectKey + '-' + data.content.key_id | |
+ '|' | |
+ data.project.projectKey + '-' + data.content.key_id | |
+ '> ' + data.content.summary | |
); | |
break; | |
case 3: // 課題にコメント | |
console.log('Backlog comment posted to issue.'); | |
postSlack( | |
'Backlog', | |
null, | |
SLACK_CHANNEL, | |
'【新規コメント】 ' | |
+ '<https://' | |
+ BACKLOG_HOST | |
+ '/view/' | |
+ data.project.projectKey + '-' + data.content.key_id | |
+ '|' | |
+ data.project.projectKey + '-' + data.content.key_id | |
+ '> ' + data.content.summary | |
+ '\n' | |
+ data.createdUser.name | |
+ ': ' | |
+ data.content.comment.content | |
); | |
break; | |
case 4: // 課題の削除 | |
console.log('Backlog issue deleted.'); | |
break; | |
case 14: // 課題をまとめて更新 | |
console.log('Backlog issues changed.'); | |
postSlack( | |
'Backlog', | |
null, | |
SLACK_CHANNEL, | |
data.content.link.map((d) => { | |
return '【更新】 ' | |
+ '<https://' | |
+ BACKLOG_HOST | |
+ '/view/' | |
+ data.project.projectKey + '-' + d.key_id | |
+ '|' | |
+ data.project.projectKey + '-' + d.key_id | |
+ '> ' + d.title | |
}).join('\n') | |
); | |
break; | |
} | |
} | |
function postSlack(botname, boticon, channel, message) { | |
var p = { 'text': message }; | |
if (botname) { | |
p.username = botname; | |
} | |
if (/^:[^:]+:$/.test(boticon)) { | |
p.icon_emoji = boticon; | |
} else if (/^https?:\/\//.test(boticon)) { | |
p.icon_url = boticon; | |
} | |
if (channel) { | |
p.channel = '#' + channel; | |
} | |
var o = { | |
method: 'POST', | |
uri: SLACK_WEBHOOK_URL, | |
body: p, | |
json: true | |
}; | |
if (PROXY) { | |
o.proxy = PROXY; | |
} | |
console.log('Sending message to Slack...'); | |
Request(o, (e, r, b) => { | |
if (e) { | |
console.error('Error occured while posting message to Slack'); | |
console.error(e); | |
} else { | |
console.log('Sending message to Slack completed successfly.'); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment