PR Feedback

Signed-off-by: Alex Crome <afscrome@users.noreply.github.com>
This commit is contained in:
Alex Crome
2022-09-08 17:36:28 +01:00
parent 13ec891dd3
commit f95ceb2737
2 changed files with 12 additions and 8 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
---
'@backstage/integration': minor
'@backstage/integration': patch
---
Improved caching around github app tokens.
@@ -44,22 +44,26 @@ class Cache {
repo: string | undefined,
supplier: () => Promise<InstallationTokenData>,
): 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;