Repository navigation
Expand file tree
/
Copy pathlambda-function.js
More file actions
60 lines (54 loc) · 1.74 KB
/
Copy pathlambda-function.js
File metadata and controls
60 lines (54 loc) · 1.74 KB
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
import lowercaseKeys from "lowercase-keys";
import { template } from "./views/probot.js";
export default async function lambdaFunction(probot, event, context) {
if (event.httpMethod === "GET" && event.path === "/probot") {
const res = {
statusCode: 200,
headers: {
"Content-Type": "text/html",
},
body: template,
};
return res;
}
// lowercase all headers to respect headers insensitivity (RFC 7230 $3.2 'Header Fields', see issue #62)
const headersLowerCase = lowercaseKeys(event.headers ?? {});
const missingHeaders = [];
if (!headersLowerCase["x-github-delivery"]) {
missingHeaders.push("X-GitHub-Delivery");
}
if (!headersLowerCase["x-github-event"]) {
missingHeaders.push("X-GitHub-Event");
}
if (
!headersLowerCase["x-hub-signature-256"] &&
!headersLowerCase["x-hub-signature"]
) {
missingHeaders.push("X-Hub-Signature-256 or X-Hub-Signature");
}
if (missingHeaders.length > 0) {
return {
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
error: `Missing required headers: ${missingHeaders.join(", ")}`,
documentation_url:
"https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html",
}),
};
}
// this will be simpler once we ship `verifyAndParse()`
// see https://github.com/octokit/webhooks.js/issues/379
await probot.webhooks.verifyAndReceive({
id: headersLowerCase["x-github-delivery"],
name: headersLowerCase["x-github-event"],
signature:
headersLowerCase["x-hub-signature-256"] ||
headersLowerCase["x-hub-signature"],
payload: event.body,
});
return {
statusCode: 200,
body: JSON.stringify({ ok: true }),
};
}