From f95ceb273751456e2d2f8e59272d689929f13dec Mon Sep 17 00:00:00 2001 From: Alex Crome Date: Thu, 8 Sep 2022 17:36:28 +0100 Subject: [PATCH] PR Feedback Signed-off-by: Alex Crome --- .changeset/sweet-grapes-explain.md | 2 +- .../SingleInstanceGithubCredentialsProvider.ts | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.changeset/sweet-grapes-explain.md b/.changeset/sweet-grapes-explain.md index 3b75b8849d..014fbb3d41 100644 --- a/.changeset/sweet-grapes-explain.md +++ b/.changeset/sweet-grapes-explain.md @@ -1,5 +1,5 @@ --- -'@backstage/integration': minor +'@backstage/integration': patch --- Improved caching around github app tokens. diff --git a/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts b/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts index aeaa0f5e04..02f050f69f 100644 --- a/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts +++ b/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts @@ -44,22 +44,26 @@ class Cache { repo: string | undefined, supplier: () => Promise, ): Promise<{ accessToken: string }> { - let ownerData = this.tokenCache.get(owner); + let existingInstallationData = this.tokenCache.get(owner); - if (!ownerData || this.isExpired(ownerData.expiresAt)) { - ownerData = await supplier(); + if ( + !existingInstallationData || + this.isExpired(existingInstallationData.expiresAt) + ) { + existingInstallationData = await supplier(); // Allow 10 minutes grace to account for clock skew - ownerData.expiresAt = ownerData.expiresAt.minus({ minutes: 10 }); - this.tokenCache.set(owner, ownerData); + existingInstallationData.expiresAt = + existingInstallationData.expiresAt.minus({ minutes: 10 }); + this.tokenCache.set(owner, existingInstallationData); } - if (!this.appliesToRepo(ownerData, repo)) { + if (!this.appliesToRepo(existingInstallationData, repo)) { throw new Error( `The Backstage GitHub application used in the ${owner} organization does not have access to a repository with the name ${repo}`, ); } - return { accessToken: ownerData.token }; + return { accessToken: existingInstallationData.token }; } private isExpired = (date: DateTime) => DateTime.local() > date;