Skip to content

Commit

Permalink
[DOP-4171]: Add webhook for github push
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry committed Jan 25, 2024
1 parent 4e30595 commit c98491d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
13 changes: 13 additions & 0 deletions api/controllers/v2/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@ export async function rebuildCacheHandler(event: APIGatewayEvent): Promise<APIGa
};
}
}

export async function rebuildCacheGithubWebhookHandler(event: APIGatewayEvent) {
// TODO: Add GITHUB_SECRET
if (!validateJsonWebhook(event, '')) {
const errMsg = "X-Hub-Signature incorrect. Github webhook token doesn't match";
return {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
};
}
return rebuildCacheHandler(event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ export class CacheUpdaterApiConstruct extends Construct {
},
});

const cacheGithubWebhookLambda = new NodejsFunction(this, 'cacheUpdaterWebhookLambda', {
entry: `${HANDLERS_PATH}/cache.ts`,
handler: 'rebuildCacheGithubWebhookHandler',
runtime: Runtime.NODEJS_18_X,
timeout: Duration.minutes(2),
memorySize: 1024,
environment: {
CLUSTER: clusterName,
TASK_DEFINITION: taskDefinition.taskDefinitionArn,
CONTAINER_NAME: containerName,
SUBNETS: JSON.stringify(vpc.privateSubnets.map((subnet) => subnet.subnetId)),
},
});

taskDefinition.grantRun(cacheWebhookLambda);

// generic handler for the root endpoint
Expand All @@ -65,11 +79,11 @@ export class CacheUpdaterApiConstruct extends Construct {
},
});

restApi.root
.addResource('webhook', {
defaultCorsPreflightOptions: { allowOrigins: Cors.ALL_ORIGINS },
})
.addMethod('POST', new LambdaIntegration(cacheWebhookLambda), { apiKeyRequired: true });
const webhook = restApi.root.addResource('webhook', {
defaultCorsPreflightOptions: { allowOrigins: Cors.ALL_ORIGINS },
});

webhook.addMethod('POST', new LambdaIntegration(cacheWebhookLambda), { apiKeyRequired: true });

const usagePlan = restApi.addUsagePlan('cacheUpdaterUsagePlan', {
name: 'defaultPlan',
Expand All @@ -84,5 +98,11 @@ export class CacheUpdaterApiConstruct extends Construct {
const apiKey = restApi.addApiKey('cacheUpdaterApiKey');

usagePlan.addApiKey(apiKey);

const githubWebhook = webhook.addResource('github', {
defaultCorsPreflightOptions: { allowOrigins: Cors.ALL_ORIGINS },
});

githubWebhook.addMethod('POST', new LambdaIntegration(cacheGithubWebhookLambda), { apiKeyRequired: false });
}
}

0 comments on commit c98491d

Please sign in to comment.