forked from npm/npm-hook-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (87 loc) · 3.38 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
98
99
100
101
102
103
104
105
106
107
108
109
var
assert = require('assert'),
bole = require('bole'),
logstring = require('common-log-string'),
makeReceiver = require('npm-hook-receiver'),
slack = require('@slack/client')
;
var logger = bole(process.env.SERVICE_NAME || 'hooks-bot');
bole.output({ level: 'info', stream: process.stdout });
var token = process.env.SLACK_API_TOKEN || '';
assert(token, 'you must supply a slack api token in process.env.SLACK_API_TOKEN');
var channelID = process.env.SLACK_CHANNEL;
assert(channelID, 'you must supply a slack channel ID or name in process.env.SLACK_CHANNEL');
var port = process.env.PORT || '6666';
// This is how we post to slack.
var web = new slack.WebClient(token);
// Make a webhooks receiver and have it act on interesting events.
// The receiver is a restify server!
var opts = {
name: process.env.SERVICE_NAME || 'hooks-bot',
secret: process.env.SHARED_SECRET,
mount: process.env.MOUNT_POINT || '/incoming',
};
var server = makeReceiver(opts);
// All hook events, with special handling for some.
server.on('hook', function onIncomingHook(hook)
{
var pkg = hook.name.replace('/', '%2F');
var type = hook.type;
var change = hook.event.replace(type + ':', '');
var message;
console.log(hook.change);
var user = hook.change ? hook.change.user : '';
switch (hook.event)
{
case 'package:star':
message = `★ \<https://www.npmjs.com/~${user}|${user}\> starred :package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\>`;
break;
case 'package:unstar':
message = `✩ \<https://www.npmjs.com/~${user}|${user}\> unstarred :package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\>`;
break;
case 'package:publish':
message = `:package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\>@${hook.change.version} published!`;
break;
case 'package:unpublish':
message = `:package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\>@${hook.change.version} unpublished`;
break;
case 'package:dist-tag':
message = `:package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\>@${hook.change.version} new dist-tag: \`${hook.change['dist-tag']}\``;
break;
case 'package:dist-tag-rm':
message = `:package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\>@${hook.change.version} dist-tag removed: \`${hook.change['dist-tag']}\``;
break;
case 'package:owner':
message = `:package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\> owner added: \`${hook.change.user}\``;
break;
case 'package:owner-rm':
message = `:package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\> owner removed: \`${hook.change.user}\``;
break;
default:
message = [
`:package: \<https://www.npmjs.com/package/${pkg}|${hook.name}\>`,
'*event*: ' + change,
'*type*: ' + type,
].join('\n');
}
web.chat.postMessage(channelID, message, {"as_user": true});
});
server.on('hook:error', function(message)
{
web.chat.postMessage(channelID, '*error handling web hook:* ' + message, {"as_user": true});
});
// now make it ready for production
server.on('after', function logEachRequest(request, response, route, error)
{
logger.info(logstring(request, response));
});
server.get('/ping', function handlePing(request, response, next)
{
response.send(200, 'pong');
next();
});
server.listen(port, function()
{
logger.info('listening on ' + port);
web.chat.postMessage(channelID, 'npm hooks slackbot coming on line beep boop', {"as_user": true});
});