-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathgithub-webhook.ts
More file actions
70 lines (60 loc) · 2.17 KB
/
github-webhook.ts
File metadata and controls
70 lines (60 loc) · 2.17 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
61
62
63
64
65
66
67
68
69
70
import * as path from 'path';
import { SingletonFunction, Runtime, Code } from '@aws-cdk/aws-lambda';
import { Construct, Duration, CustomResource } from '@aws-cdk/core';
import { SecretKey } from '@cloudcomponents/cdk-secret-key';
export interface GithubWebhookProps {
/**
* The OAuth access token
*/
readonly githubApiToken: string | SecretKey;
/**
* The Github repo url
*/
readonly githubRepoUrl: string;
/**
* The URL to which the payloads will be delivered.
*/
readonly payloadUrl: string;
/**
* Determines what events the hook is triggered for.
* @see https://developer.github.com/v3/activity/events/types/
*/
readonly events: string[];
/**
* The webhook secret that GitHub uses to create a
* hash signature with each payload
* @see https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks#validating-payloads-from-github
*/
readonly webhookSecret?: string
readonly logLevel?: 'debug' | 'info' | 'warning' | 'error';
}
export class GithubWebhook extends Construct {
constructor(scope: Construct, id: string, props: GithubWebhookProps) {
super(scope, id);
const githubApiToken = typeof props.githubApiToken === 'string' ? SecretKey.fromPlainText(props.githubApiToken) : props.githubApiToken;
const handler = new SingletonFunction(this, 'CustomResourceHandler', {
uuid: '83CBF3EB-7B62-44F2-8C67-8441E4C1232E',
runtime: Runtime.NODEJS_12_X,
code: Code.fromAsset(path.join(__dirname, 'lambdas', 'github-webhook')),
handler: 'index.handler',
lambdaPurpose: 'Custom::GithubWebhook',
timeout: Duration.minutes(15),
});
if (githubApiToken.grantRead) {
githubApiToken.grantRead(handler);
}
new CustomResource(this, 'CustomResource', {
serviceToken: handler.functionArn,
resourceType: 'Custom::GithubWebhook',
pascalCaseProperties: true,
properties: {
githubApiTokenString: githubApiToken.serialize(),
githubRepoUrl: props.githubRepoUrl,
payloadUrl: props.payloadUrl,
events: props.events,
logLevel: props.logLevel,
webhookSecret: props.webhookSecret
},
});
}
}