forked from serverless/examples
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
89 lines (80 loc) · 2.85 KB
/
handler.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
const crypto = require('crypto');
const Slack = require('slack-node');
// validate your payload from GitHub
function signRequestBody(key, body) {
return `sha1=${crypto.createHmac('sha1', key).update(body, 'utf-8').digest('hex')}`;
}
// webhook handler function
exports.gongHandler = async (event) => {
// get the GitHub secret from the environment variables
const token = process.env.GITHUB_WEBHOOK_SECRET;
const calculatedSig = signRequestBody(token, event.body);
let errMsg;
// get the remaining variables from the GitHub event
const headers = event.headers;
const sig = headers['X-Hub-Signature'];
const githubEvent = headers['X-GitHub-Event'];
const body = JSON.parse(event.body);
// get repo variables
const { repository, release } = body;
const repo = repository.full_name;
const url = repository.url;
// set variables for a release event
let releaseVersion, releaseUrl, author = null;
if (githubEvent === 'release') {
releaseVersion = release.tag_name;
releaseUrl = release.html_url;
author = release.author.login;
}
// check that a GitHub webhook secret variable exists, if not, return an error
if (typeof token !== 'string') {
errMsg = 'Must provide a \'GITHUB_WEBHOOK_SECRET\' env variable';
return {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
};
}
// check validity of GitHub token
if (sig !== calculatedSig) {
errMsg = 'X-Hub-Signature incorrect. Github webhook token doesn\'t match';
return {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
};
}
// if the event is a 'release' event, gong the Slack channel!
const webhookUri = process.env.SLACK_WEBHOOK_URL;
const slack = new Slack();
slack.setWebhook(webhookUri);
// send slack message
if (githubEvent === 'release') {
slack.webhook({
channel: '#gong-test', // your desired channel here
username: 'gongbot', // be creative!
icon_emoji: ':gong:', // because Slack is for emojis
// customize your message below
text: `It's time to celebrate! ${author} pushed release version ${releaseVersion}. See it here: ${releaseUrl}!\n:gong: https://youtu.be/8nBOF5sJrSE?t=11`,
}, function (err, response) {
console.log(response);
if (err) {
console.log('Something went wrong');
console.log(err);
}
});
}
// (optional) print some messages to the CloudWatch console (for testing)
console.log('---------------------------------');
console.log(`\nGithub-Event: "${githubEvent}" on this repo: "${repo}" at the url: ${url}.`);
console.log(event.body);
console.log('---------------------------------');
// return a 200 response if the GitHub tokens match
const response = {
statusCode: 200,
body: JSON.stringify({
input: event,
}),
};
return response;
};