-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathindex.ts
More file actions
86 lines (68 loc) · 2.53 KB
/
index.ts
File metadata and controls
86 lines (68 loc) · 2.53 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { SecretKey } from '@cloudcomponents/lambda-utils';
import type { CloudFormationCustomResourceEventCommon } from 'aws-lambda';
import {
customResourceHelper,
OnCreateHandler,
OnUpdateHandler,
OnDeleteHandler,
ResourceHandler,
ResourceHandlerReturn,
camelizeKeys,
} from 'custom-resource-helper';
import { createWebhook, updateWebhook, deleteWebhook } from './webhook-api';
export interface WebhookProps {
githubApiTokenString: string;
githubRepoUrl: string;
payloadUrl: string;
events: string[];
webhookSecret?: string;
}
const handleCreate: OnCreateHandler = async (event): Promise<ResourceHandlerReturn> => {
const { githubApiTokenString, githubRepoUrl, payloadUrl, events, webhookSecret } = camelizeKeys<
WebhookProps,
CloudFormationCustomResourceEventCommon['ResourceProperties']
>(event.ResourceProperties);
const secretKey = new SecretKey(githubApiTokenString);
const githubApiToken = await secretKey.getValue();
const { data } = await createWebhook(githubApiToken, githubRepoUrl, payloadUrl, events, webhookSecret);
const physicalResourceId = data.id.toString();
return {
physicalResourceId,
responseData: {
...data,
},
};
};
const handleUpdate: OnUpdateHandler = async (event): Promise<ResourceHandlerReturn> => {
const { githubApiTokenString, githubRepoUrl, payloadUrl, events, webhookSecret } = camelizeKeys<
WebhookProps,
CloudFormationCustomResourceEventCommon['ResourceProperties']
>(event.ResourceProperties);
const secretKey = new SecretKey(githubApiTokenString);
const githubApiToken = await secretKey.getValue();
const hookId = event.PhysicalResourceId;
const { data } = await updateWebhook(githubApiToken, githubRepoUrl, payloadUrl, events, parseInt(hookId, 10), webhookSecret);
const physicalResourceId = data.id.toString();
return {
physicalResourceId,
responseData: {
...data,
},
};
};
const handleDelete: OnDeleteHandler = async (event): Promise<void> => {
const { githubApiTokenString, githubRepoUrl } = camelizeKeys<WebhookProps, CloudFormationCustomResourceEventCommon['ResourceProperties']>(
event.ResourceProperties,
);
const secretKey = new SecretKey(githubApiTokenString);
const githubApiToken = await secretKey.getValue();
const hookId = event.PhysicalResourceId;
await deleteWebhook(githubApiToken, githubRepoUrl, parseInt(hookId, 10));
};
export const handler = customResourceHelper(
(): ResourceHandler => ({
onCreate: handleCreate,
onUpdate: handleUpdate,
onDelete: handleDelete,
}),
);