From f8d0c432bb9b003c352c7139aff9c564d5e4edbd Mon Sep 17 00:00:00 2001 From: "Zee V. (Philip)" Date: Fri, 3 Jun 2022 10:09:38 +0200 Subject: [PATCH] fix: ensure we do not have a race condition We achieve this by setting the start time prior to making the request. Signed-off-by: Zee V. (Philip) --- .../src/GitLabDiscoveryProcessor.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.ts b/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.ts index 7f81683408..a23d1f2803 100644 --- a/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.ts +++ b/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.ts @@ -84,6 +84,7 @@ export class GitLabDiscoveryProcessor implements CatalogProcessor { return false; } + const startTime = new Date(); const { group, host, branch, catalogPath } = parseUrl(location.target); const integration = this.integrations.gitlab.byUrl(`https://${host}`); @@ -97,7 +98,6 @@ export class GitLabDiscoveryProcessor implements CatalogProcessor { config: integration.config, logger: this.logger, }); - const startTimestamp = Date.now(); this.logger.debug(`Reading GitLab projects from ${location.target}`); const opts = { @@ -110,6 +110,8 @@ export class GitLabDiscoveryProcessor implements CatalogProcessor { }; const lastActivity = (await this.cache.get(this.getCacheKey())) as string; + // We check for the existence of lastActivity and only set it if it's present to ensure + // that the options doesn't include the key so that the API doesn't receive an empty query parameter. if (lastActivity) { opts.last_activity_after = lastActivity; } @@ -166,9 +168,10 @@ export class GitLabDiscoveryProcessor implements CatalogProcessor { ); } - await this.cache.set(this.getCacheKey(), new Date().toISOString()); + // Save an ISO formatted string in the cache as that's what GitLab expects in the API request. + await this.cache.set(this.getCacheKey(), startTime.toISOString()); - const duration = ((Date.now() - startTimestamp) / 1000).toFixed(1); + const duration = ((Date.now() - startTime.getTime()) / 1000).toFixed(1); this.logger.debug( `Read ${res.scanned} GitLab repositories in ${duration} seconds`, );