|
1 |
| -const { Probot } = require('probot') |
2 |
| -const { resolve } = require('probot/lib/helpers/resolve-app-function') |
3 |
| -const { findPrivateKey } = require('probot/lib/helpers/get-private-key') |
4 |
| -const { template } = require('./views/probot') |
| 1 | +const { Probot } = require("probot"); |
| 2 | +const { resolve } = require("probot/lib/helpers/resolve-app-function"); |
| 3 | +const { findPrivateKey } = require("probot/lib/helpers/get-private-key"); |
| 4 | +const { template } = require("./views/probot"); |
5 | 5 |
|
6 |
| -let probot |
| 6 | +let probot; |
7 | 7 |
|
8 | 8 | const loadProbot = (appFn) => {
|
9 | 9 | probot =
|
10 | 10 | probot ||
|
11 | 11 | new Probot({
|
12 | 12 | id: process.env.APP_ID,
|
13 | 13 | secret: process.env.WEBHOOK_SECRET,
|
14 |
| - privateKey: findPrivateKey() |
15 |
| - }) |
| 14 | + privateKey: findPrivateKey(), |
| 15 | + }); |
16 | 16 |
|
17 |
| - if (typeof appFn === 'string') { |
18 |
| - appFn = resolve(appFn) |
| 17 | + if (typeof appFn === "string") { |
| 18 | + appFn = resolve(appFn); |
19 | 19 | }
|
20 | 20 |
|
21 |
| - probot.load(appFn) |
| 21 | + probot.load(appFn); |
22 | 22 |
|
23 |
| - return probot |
24 |
| -} |
| 23 | + return probot; |
| 24 | +}; |
25 | 25 |
|
26 | 26 | const lowerCaseKeys = (obj = {}) =>
|
27 | 27 | Object.keys(obj).reduce(
|
28 | 28 | (accumulator, key) =>
|
29 | 29 | Object.assign(accumulator, { [key.toLocaleLowerCase()]: obj[key] }),
|
30 | 30 | {}
|
31 |
| - ) |
| 31 | + ); |
32 | 32 |
|
33 | 33 | module.exports.serverless = (appFn) => {
|
34 | 34 | return async (event, context) => {
|
35 | 35 | // 🤖 A friendly homepage if there isn't a payload
|
36 |
| - if (event.httpMethod === 'GET' && event.path === '/probot') { |
| 36 | + if (event.httpMethod === "GET" && event.path === "/probot") { |
37 | 37 | const res = {
|
38 | 38 | statusCode: 200,
|
39 | 39 | headers: {
|
40 |
| - 'Content-Type': 'text/html' |
| 40 | + "Content-Type": "text/html", |
41 | 41 | },
|
42 |
| - body: template |
43 |
| - } |
44 |
| - return res |
| 42 | + body: template, |
| 43 | + }; |
| 44 | + return res; |
45 | 45 | }
|
46 | 46 |
|
47 | 47 | // Otherwise let's listen handle the payload
|
48 |
| - probot = probot || loadProbot(appFn) |
| 48 | + probot = probot || loadProbot(appFn); |
49 | 49 |
|
50 | 50 | // Ends function immediately after callback
|
51 |
| - context.callbackWaitsForEmptyEventLoop = false |
| 51 | + context.callbackWaitsForEmptyEventLoop = false; |
52 | 52 |
|
53 | 53 | // Determine incoming webhook event type
|
54 |
| - const headers = lowerCaseKeys(event.headers) |
55 |
| - const e = headers['x-github-event'] |
| 54 | + const headers = lowerCaseKeys(event.headers); |
| 55 | + const e = headers["x-github-event"]; |
56 | 56 | if (!e) {
|
57 | 57 | return {
|
58 | 58 | statusCode: 400,
|
59 |
| - body: 'X-Github-Event header is missing' |
60 |
| - } |
| 59 | + body: "X-Github-Event header is missing", |
| 60 | + }; |
61 | 61 | }
|
62 | 62 |
|
63 | 63 | // If body is expected to be base64 encoded, decode it and continue
|
64 | 64 | if (event.isBase64Encoded) {
|
65 |
| - event.body = Buffer.from(event.body, 'base64').toString('utf8') |
| 65 | + event.body = Buffer.from(event.body, "base64").toString("utf8"); |
66 | 66 | }
|
67 | 67 |
|
68 | 68 | // Convert the payload to an Object if API Gateway stringifies it
|
69 | 69 | event.body =
|
70 |
| - typeof event.body === 'string' ? JSON.parse(event.body) : event.body |
| 70 | + typeof event.body === "string" ? JSON.parse(event.body) : event.body; |
71 | 71 |
|
72 | 72 | // Bail for null body
|
73 | 73 | if (!event.body) {
|
74 | 74 | return {
|
75 | 75 | statusCode: 400,
|
76 |
| - body: 'Event body is null.' |
77 |
| - } |
| 76 | + body: "Event body is null.", |
| 77 | + }; |
78 | 78 | }
|
79 | 79 |
|
80 | 80 | // Do the thing
|
81 | 81 | console.log(
|
82 |
| - `Received event ${e}${event.body.action ? '.' + event.body.action : ''}` |
83 |
| - ) |
| 82 | + `Received event ${e}${event.body.action ? "." + event.body.action : ""}` |
| 83 | + ); |
84 | 84 | if (event) {
|
85 | 85 | try {
|
86 | 86 | await probot.receive({
|
87 | 87 | name: e,
|
88 |
| - payload: event.body |
89 |
| - }) |
| 88 | + payload: event.body, |
| 89 | + }); |
90 | 90 | return {
|
91 | 91 | statusCode: 200,
|
92 | 92 | body: JSON.stringify({
|
93 |
| - message: `Received ${e}.${event.body.action}` |
94 |
| - }) |
95 |
| - } |
| 93 | + message: `Received ${e}.${event.body.action}`, |
| 94 | + }), |
| 95 | + }; |
96 | 96 | } catch (err) {
|
97 |
| - console.error(err) |
| 97 | + console.error(err); |
98 | 98 | return {
|
99 | 99 | statusCode: 500,
|
100 |
| - body: JSON.stringify(err) |
101 |
| - } |
| 100 | + body: JSON.stringify(err), |
| 101 | + }; |
102 | 102 | }
|
103 | 103 | } else {
|
104 |
| - console.error({ event, context }) |
105 |
| - throw new Error('unknown error') |
| 104 | + console.error({ event, context }); |
| 105 | + throw new Error("unknown error"); |
106 | 106 | }
|
107 |
| - } |
108 |
| -} |
| 107 | + }; |
| 108 | +}; |
0 commit comments