|
1 |
| -const pubsub = require('@google-cloud/pubsub')(functions.config().firebase); |
2 |
| -const githubTopic = functions.config().github.topic; |
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const functions = require('firebase-functions'); |
3 | 19 | const rp = require('request-promise');
|
| 20 | +const crypto = require('crypto'); |
4 | 21 |
|
5 |
| -exports.githubWebhook = functions.https.onRequest((request, response) => { |
| 22 | +/** |
| 23 | + * Webhook that will be called each time there is a new GitHub commit and will post a message to |
| 24 | + * Slack. |
| 25 | + */ |
| 26 | +exports.githubWebhook = functions.https.onRequest((req, res) => { |
6 | 27 | const cipher = 'sha1';
|
7 |
| - const signature = request.headers['x-hub-signature']; |
8 |
| - const hmac = require('crypto') |
9 |
| - .createHmac(cipher, functions.config().github.secret) |
10 |
| - .update(JSON.stringify(request.body, null, 2)) |
11 |
| - .digest('hex'); |
| 28 | + const signature = req.headers['x-hub-signature']; |
| 29 | + |
| 30 | + // TODO: Configure the `github.secret` Google Cloud environment variables. |
| 31 | + const hmac = crypto.createHmac(cipher, functions.config().github.secret) |
| 32 | + // The JSON body is automatically parsed by Cloud Functions so we re-stringify it. |
| 33 | + .update(JSON.stringify(req.body, null, 0)) |
| 34 | + .digest('hex'); |
12 | 35 | const expectedSignature = `${cipher}=${hmac}`;
|
13 | 36 |
|
14 |
| - // Skipping verification, since JSON.stringify fails hmac, need the raw post |
15 |
| - if (true || signature === expectedSignature) { |
16 |
| - pubsub.topic(githubTopic).publish( |
17 |
| - request.body, |
18 |
| - (err, messageIds, apiResponse) => { |
19 |
| - if (err) { |
20 |
| - console.error(err); |
21 |
| - response.status(500).send('Something went wrong.'); |
22 |
| - } else { |
23 |
| - response.status(200).send(''); |
24 |
| - } |
25 |
| - } |
26 |
| - ); |
| 37 | + // Check that the body of the request has been signed with the GitHub Secret. |
| 38 | + if (signature === expectedSignature) { |
| 39 | + postToSlack(req.body.compare, req.body.commits.length, req.body.repository).then(() => { |
| 40 | + res.end(); |
| 41 | + }).catch(error => { |
| 42 | + console.error(error); |
| 43 | + res.status(500).send('Something went wrong while posting the message to Slack.'); |
| 44 | + }); |
27 | 45 | } else {
|
28 |
| - console.error(`x-hub-signature ${signature} did not match ${expectedSignature}`); |
29 |
| - response.status(403).send('Your x-hub-signature\'s bad and you should feel bad!'); |
| 46 | + console.error('x-hub-signature', signature, 'did not match', expectedSignature); |
| 47 | + res.status(403).send('Your x-hub-signature\'s bad and you should feel bad!'); |
30 | 48 | }
|
31 | 49 | });
|
32 | 50 |
|
33 |
| - |
34 |
| -exports.githubAnnouncer = functions.pubsub.topic(githubTopic).onPublish(event => { |
35 |
| - const payload = JSON.parse(event.data.json.payload); |
36 |
| - if (payload.ref === null) { return null; } |
37 |
| - const commits = payload.commits.length; |
38 |
| - const repo = payload.repository; |
39 |
| - const url = payload.compare; |
| 51 | +/** |
| 52 | + * Post a message to Slack about the new GitHub commit. |
| 53 | + */ |
| 54 | +function postToSlack(url, commits, repo) { |
40 | 55 | return rp({
|
41 | 56 | method: 'POST',
|
42 |
| - uri: functions.config().slack.webhookurl, |
| 57 | + // TODO: Configure the `slack.webhook_url` Google Cloud environment variables. |
| 58 | + uri: functions.config().slack.webhook_url, |
43 | 59 | body: {
|
44 |
| - text: `<${url}|${commits} new commit${commits > 1 ? 's' : ''}> pushed to <${repo.url}|${repo.name}>.` |
| 60 | + text: `<${url}|${commits} new commit${commits > 1 ? 's' : ''}> pushed to <${repo.url}|${repo.full_name}>.` |
45 | 61 | },
|
46 | 62 | json: true
|
47 | 63 | });
|
48 |
| -}); |
| 64 | +} |
0 commit comments