@backstage/integration 1.3.1-next.1 → 1.3.1-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1072,16 +1072,30 @@ function chooseEndpoint(config, credentials) {
1072
1072
  class Cache {
1073
1073
  constructor() {
1074
1074
  this.tokenCache = /* @__PURE__ */ new Map();
1075
- this.isNotExpired = (date) => date.diff(DateTime.local(), "minutes").minutes > 50;
1075
+ this.isExpired = (date) => DateTime.local() > date;
1076
+ }
1077
+ async getOrCreateToken(owner, repo, supplier) {
1078
+ let existingInstallationData = this.tokenCache.get(owner);
1079
+ if (!existingInstallationData || this.isExpired(existingInstallationData.expiresAt)) {
1080
+ existingInstallationData = await supplier();
1081
+ existingInstallationData.expiresAt = existingInstallationData.expiresAt.minus({ minutes: 10 });
1082
+ this.tokenCache.set(owner, existingInstallationData);
1083
+ }
1084
+ if (!this.appliesToRepo(existingInstallationData, repo)) {
1085
+ throw new Error(
1086
+ `The Backstage GitHub application used in the ${owner} organization does not have access to a repository with the name ${repo}`
1087
+ );
1088
+ }
1089
+ return { accessToken: existingInstallationData.token };
1076
1090
  }
1077
- async getOrCreateToken(key, supplier) {
1078
- const item = this.tokenCache.get(key);
1079
- if (item && this.isNotExpired(item.expiresAt)) {
1080
- return { accessToken: item.token };
1091
+ appliesToRepo(tokenData, repo) {
1092
+ if (repo === void 0) {
1093
+ return true;
1094
+ }
1095
+ if (tokenData.repositories !== void 0) {
1096
+ return tokenData.repositories.includes(repo);
1081
1097
  }
1082
- const result = await supplier();
1083
- this.tokenCache.set(key, result);
1084
- return { accessToken: result.token };
1098
+ return true;
1085
1099
  }
1086
1100
  }
1087
1101
  const HEADERS = {
@@ -1105,23 +1119,25 @@ class GithubAppManager {
1105
1119
  }
1106
1120
  async getInstallationCredentials(owner, repo) {
1107
1121
  var _a;
1108
- const { installationId, suspended } = await this.getInstallationData(owner);
1109
1122
  if (this.allowedInstallationOwners) {
1110
1123
  if (!((_a = this.allowedInstallationOwners) == null ? void 0 : _a.includes(owner))) {
1111
1124
  return { accessToken: void 0 };
1112
1125
  }
1113
1126
  }
1114
- if (suspended) {
1115
- throw new Error(`The GitHub application for ${owner} is suspended`);
1116
- }
1117
- const cacheKey = repo ? `${owner}/${repo}` : owner;
1118
- return this.cache.getOrCreateToken(cacheKey, async () => {
1127
+ return this.cache.getOrCreateToken(owner, repo, async () => {
1119
1128
  var _a2;
1129
+ const { installationId, suspended } = await this.getInstallationData(
1130
+ owner
1131
+ );
1132
+ if (suspended) {
1133
+ throw new Error(`The GitHub application for ${owner} is suspended`);
1134
+ }
1120
1135
  const result = await this.appClient.apps.createInstallationAccessToken({
1121
1136
  installation_id: installationId,
1122
1137
  headers: HEADERS
1123
1138
  });
1124
- if (repo && result.data.repository_selection === "selected") {
1139
+ let repositoryNames;
1140
+ if (result.data.repository_selection === "selected") {
1125
1141
  const installationClient = new Octokit({
1126
1142
  baseUrl: this.baseUrl,
1127
1143
  auth: result.data.token
@@ -1130,18 +1146,12 @@ class GithubAppManager {
1130
1146
  installationClient.apps.listReposAccessibleToInstallation
1131
1147
  );
1132
1148
  const repositories = (_a2 = repos.repositories) != null ? _a2 : repos;
1133
- const hasRepo = repositories.some((repository) => {
1134
- return repository.name === repo;
1135
- });
1136
- if (!hasRepo) {
1137
- throw new Error(
1138
- `The Backstage GitHub application used in the ${owner} organization does not have access to a repository with the name ${repo}`
1139
- );
1140
- }
1149
+ repositoryNames = repositories.map((repository) => repository.name);
1141
1150
  }
1142
1151
  return {
1143
1152
  token: result.data.token,
1144
- expiresAt: DateTime.fromISO(result.data.expires_at)
1153
+ expiresAt: DateTime.fromISO(result.data.expires_at),
1154
+ repositories: repositoryNames
1145
1155
  };
1146
1156
  });
1147
1157
  }