@backstage/integration 1.3.1-next.0 → 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
@@ -807,8 +807,7 @@ function getBitbucketServerRequestOptions(config) {
807
807
  const headers = {};
808
808
  if (config.token) {
809
809
  headers.Authorization = `Bearer ${config.token}`;
810
- }
811
- if (config.username && config.password) {
810
+ } else if (config.username && config.password) {
812
811
  const buffer = Buffer.from(`${config.username}:${config.password}`, "utf8");
813
812
  headers.Authorization = `Basic ${buffer.toString("base64")}`;
814
813
  }
@@ -1073,16 +1072,30 @@ function chooseEndpoint(config, credentials) {
1073
1072
  class Cache {
1074
1073
  constructor() {
1075
1074
  this.tokenCache = /* @__PURE__ */ new Map();
1076
- 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 };
1077
1090
  }
1078
- async getOrCreateToken(key, supplier) {
1079
- const item = this.tokenCache.get(key);
1080
- if (item && this.isNotExpired(item.expiresAt)) {
1081
- 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);
1082
1097
  }
1083
- const result = await supplier();
1084
- this.tokenCache.set(key, result);
1085
- return { accessToken: result.token };
1098
+ return true;
1086
1099
  }
1087
1100
  }
1088
1101
  const HEADERS = {
@@ -1106,23 +1119,25 @@ class GithubAppManager {
1106
1119
  }
1107
1120
  async getInstallationCredentials(owner, repo) {
1108
1121
  var _a;
1109
- const { installationId, suspended } = await this.getInstallationData(owner);
1110
1122
  if (this.allowedInstallationOwners) {
1111
1123
  if (!((_a = this.allowedInstallationOwners) == null ? void 0 : _a.includes(owner))) {
1112
1124
  return { accessToken: void 0 };
1113
1125
  }
1114
1126
  }
1115
- if (suspended) {
1116
- throw new Error(`The GitHub application for ${owner} is suspended`);
1117
- }
1118
- const cacheKey = repo ? `${owner}/${repo}` : owner;
1119
- return this.cache.getOrCreateToken(cacheKey, async () => {
1127
+ return this.cache.getOrCreateToken(owner, repo, async () => {
1120
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
+ }
1121
1135
  const result = await this.appClient.apps.createInstallationAccessToken({
1122
1136
  installation_id: installationId,
1123
1137
  headers: HEADERS
1124
1138
  });
1125
- if (repo && result.data.repository_selection === "selected") {
1139
+ let repositoryNames;
1140
+ if (result.data.repository_selection === "selected") {
1126
1141
  const installationClient = new Octokit({
1127
1142
  baseUrl: this.baseUrl,
1128
1143
  auth: result.data.token
@@ -1131,18 +1146,12 @@ class GithubAppManager {
1131
1146
  installationClient.apps.listReposAccessibleToInstallation
1132
1147
  );
1133
1148
  const repositories = (_a2 = repos.repositories) != null ? _a2 : repos;
1134
- const hasRepo = repositories.some((repository) => {
1135
- return repository.name === repo;
1136
- });
1137
- if (!hasRepo) {
1138
- throw new Error(
1139
- `The Backstage GitHub application used in the ${owner} organization does not have access to a repository with the name ${repo}`
1140
- );
1141
- }
1149
+ repositoryNames = repositories.map((repository) => repository.name);
1142
1150
  }
1143
1151
  return {
1144
1152
  token: result.data.token,
1145
- expiresAt: DateTime.fromISO(result.data.expires_at)
1153
+ expiresAt: DateTime.fromISO(result.data.expires_at),
1154
+ repositories: repositoryNames
1146
1155
  };
1147
1156
  });
1148
1157
  }