-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (43 loc) · 1.24 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
const express = require('express');
const bodyParser = require('body-parser');
const _ = require('lodash');
const WebhooksApi = require('@octokit/webhooks');
const { handleLabelUpdate } = require('./src/webhooks/github/pull_request');
// Env vars
const { SLACK_TOKEN, GITHUB_SECRET } = process.env;
const PORT = process.env.PORT || 3000;
// Create instance of express
const app = express();
// Create instance of webhook handler
const webhooks = WebhooksApi({
secret: GITHUB_SECRET,
path: '/webhooks/github',
});
// Setup middlewares
app.use(bodyParser.json());
app.use(webhooks.middleware);
// Start listening
app.get('/', (req, res) => {
if (!SLACK_TOKEN) {
res.status(400).send('No Slack Token provided');
}
res.status(200).send('Clank is up and running');
});
// Github hook
webhooks.on('pull_request', async ({ payload }) => {
// TODO Move this out into handlePullRequestEvent.js
const { action } = payload;
if (action === 'labeled') {
return await handleLabelUpdate(payload);
}
return;
});
webhooks.on('error', error => {
console.log('ERROR:', error);
res
.status(500)
.send('Something wrong with the webhook! Check logs for error');
});
app.listen(PORT, () => {
console.log('Clank listening on port', PORT);
});