From 97c0f7ff395d13f50e5c743a87447a1b32790814 Mon Sep 17 00:00:00 2001 From: branberry Date: Fri, 26 Jan 2024 10:51:13 -0600 Subject: [PATCH] [DOP-4171]: Check if snooty.toml is changed --- api/controllers/v2/cache.ts | 25 +++++++++++++++++-- .../cache-updater-api-construct.ts | 3 +++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/api/controllers/v2/cache.ts b/api/controllers/v2/cache.ts index fd6aff721..a8797afb8 100644 --- a/api/controllers/v2/cache.ts +++ b/api/controllers/v2/cache.ts @@ -69,7 +69,9 @@ async function runCacheRebuildJob(repos: RepoInfo[]) { /** * Handles requests from individual doc sites and when the docs-worker-pool repository has a release with an updated Snooty Parser version. * In the latter case, we should receive an event to build all doc site caches. - * @param {APIGatewayEvent} event + * @param {APIGatewayEvent} event An event object that comes from either a webhook payload or from the custom GitHub Action for the docs-worker pool. + * + * In either scenario, the body should contain an array of RepoInfo objects. * @returns {Promise} */ export async function rebuildCacheHandler(event: APIGatewayEvent): Promise { @@ -108,7 +110,13 @@ export async function rebuildCacheHandler(event: APIGatewayEvent): Promise} + */ +export async function rebuildCacheGithubWebhookHandler(event: APIGatewayEvent): Promise { if (!event.body) { const errorMessage = 'Error! No body found in event payload.'; console.error(errorMessage); @@ -133,7 +141,20 @@ export async function rebuildCacheGithubWebhookHandler(event: APIGatewayEvent) { const repoOwner = body.repository.owner.login; const repoName = body.repository.name; + // Checks the commits to see if there have been changes made to the snooty.toml file. + const snootyTomlChanged = body.commits.some( + (commit) => + commit.added.some((fileName) => fileName === 'snooty.toml') || + commit.removed.some((fileName) => fileName === 'snooty.toml') || + commit.modified.some((fileName) => fileName === 'snooty.toml') + ); + + if (!snootyTomlChanged) { + return { statusCode: 202, body: 'snooty.toml has not changed, no need to rebuild cache' }; + } + const ref = body.ref; + // For webhook requests, this should only run on the primary branch, and if the repository belongs to the 10gen or mongodb orgs. if ((ref !== 'refs/head/master' && ref !== 'refs/head/main') || (repoOwner !== '10gen' && repoOwner !== 'mongodb')) { return { statusCode: 403, diff --git a/cdk-infra/lib/constructs/cache-updater/cache-updater-api-construct.ts b/cdk-infra/lib/constructs/cache-updater/cache-updater-api-construct.ts index 92c8c6f0c..fc73bfef4 100644 --- a/cdk-infra/lib/constructs/cache-updater/cache-updater-api-construct.ts +++ b/cdk-infra/lib/constructs/cache-updater/cache-updater-api-construct.ts @@ -24,6 +24,9 @@ interface CacheUpdaterApiConstructProps { const HANDLERS_PATH = path.join(__dirname, '/../../../../api/controllers/v2'); +/** + * This stack creates the resources for the Snooty Parser cache updater. + */ export class CacheUpdaterApiConstruct extends Construct { constructor( scope: Construct,