@backstage/integration 0.6.10 → 0.7.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
@@ -37,7 +37,7 @@ function basicIntegrations(integrations, getHost) {
37
37
  };
38
38
  }
39
39
  function defaultScmResolveUrl(options) {
40
- const {url, base, lineNumber} = options;
40
+ const { url, base, lineNumber } = options;
41
41
  try {
42
42
  new URL(url);
43
43
  return url;
@@ -45,7 +45,7 @@ function defaultScmResolveUrl(options) {
45
45
  }
46
46
  let updated;
47
47
  if (url.startsWith("/")) {
48
- const {filepath} = parseGitUrl(base);
48
+ const { filepath } = parseGitUrl(base);
49
49
  updated = new URL(base);
50
50
  const repoRootPath = trimEnd(updated.pathname.substring(0, updated.pathname.length - filepath.length), "/");
51
51
  updated.pathname = `${repoRootPath}${url}`;
@@ -67,6 +67,11 @@ var __privateGet = (obj, member, getter) => {
67
67
  __accessCheck(obj, member, "read from private field");
68
68
  return getter ? getter.call(obj) : member.get(obj);
69
69
  };
70
+ var __privateAdd = (obj, member, value) => {
71
+ if (member.has(obj))
72
+ throw TypeError("Cannot add the same private member more than once");
73
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
74
+ };
70
75
  var __privateSet = (obj, member, value, setter) => {
71
76
  __accessCheck(obj, member, "write to private field");
72
77
  setter ? setter.call(obj, value) : member.set(obj, value);
@@ -76,13 +81,13 @@ var _origin, _owner, _project, _repo, _path, _ref, _baseUrl;
76
81
  const VERSION_PREFIX_GIT_BRANCH = "GB";
77
82
  const _AzureUrl = class {
78
83
  constructor(origin, owner, project, repo, path, ref) {
79
- _origin.set(this, void 0);
80
- _owner.set(this, void 0);
81
- _project.set(this, void 0);
82
- _repo.set(this, void 0);
83
- _path.set(this, void 0);
84
- _ref.set(this, void 0);
85
- _baseUrl.set(this, (...parts) => {
84
+ __privateAdd(this, _origin, void 0);
85
+ __privateAdd(this, _owner, void 0);
86
+ __privateAdd(this, _project, void 0);
87
+ __privateAdd(this, _repo, void 0);
88
+ __privateAdd(this, _path, void 0);
89
+ __privateAdd(this, _ref, void 0);
90
+ __privateAdd(this, _baseUrl, (...parts) => {
86
91
  const url = new URL(__privateGet(this, _origin));
87
92
  url.pathname = parts.map((part) => encodeURIComponent(part)).join("/");
88
93
  return url;
@@ -205,12 +210,12 @@ function readAzureIntegrationConfig(config) {
205
210
  if (!isValidHost(host)) {
206
211
  throw new Error(`Invalid Azure integration config, '${host}' is not a valid host`);
207
212
  }
208
- return {host, token};
213
+ return { host, token };
209
214
  }
210
215
  function readAzureIntegrationConfigs(configs) {
211
216
  const result = configs.map(readAzureIntegrationConfig);
212
217
  if (!result.some((c) => c.host === AZURE_HOST)) {
213
- result.push({host: AZURE_HOST});
218
+ result.push({ host: AZURE_HOST });
214
219
  }
215
220
  return result;
216
221
  }
@@ -230,7 +235,7 @@ const _AzureIntegration = class {
230
235
  }
231
236
  resolveUrl(options) {
232
237
  var _a;
233
- const {url, base} = options;
238
+ const { url, base } = options;
234
239
  if (isValidUrl(url)) {
235
240
  return url;
236
241
  }
@@ -256,7 +261,7 @@ const _AzureIntegration = class {
256
261
  }
257
262
  };
258
263
  let AzureIntegration = _AzureIntegration;
259
- AzureIntegration.factory = ({config}) => {
264
+ AzureIntegration.factory = ({ config }) => {
260
265
  var _a;
261
266
  const configs = readAzureIntegrationConfigs((_a = config.getOptionalConfigArray("integrations.azure")) != null ? _a : []);
262
267
  return basicIntegrations(configs.map((c) => new _AzureIntegration(c)), (i) => i.config.host);
@@ -272,12 +277,12 @@ function getAzureCommitsUrl(url) {
272
277
  return AzureUrl.fromRepoUrl(url).toCommitsUrl();
273
278
  }
274
279
  function getAzureRequestOptions(config, additionalHeaders) {
275
- const headers = additionalHeaders ? {...additionalHeaders} : {};
280
+ const headers = additionalHeaders ? { ...additionalHeaders } : {};
276
281
  if (config.token) {
277
282
  const buffer = Buffer.from(`:${config.token}`, "utf8");
278
283
  headers.Authorization = `Basic ${buffer.toString("base64")}`;
279
284
  }
280
- return {headers};
285
+ return { headers };
281
286
  }
282
287
 
283
288
  const BITBUCKET_HOST = "bitbucket.org";
@@ -358,7 +363,7 @@ BitbucketIntegration.factory = ({
358
363
  };
359
364
 
360
365
  async function getBitbucketDefaultBranch(url, config) {
361
- const {name: repoName, owner: project, resource} = parseGitUrl(url);
366
+ const { name: repoName, owner: project, resource } = parseGitUrl(url);
362
367
  const isHosted = resource === "bitbucket.org";
363
368
  let branchUrl = isHosted ? `${config.apiBaseUrl}/repositories/${project}/${repoName}` : `${config.apiBaseUrl}/projects/${project}/repos/${repoName}/default-branch`;
364
369
  let response = await fetch(branchUrl, getBitbucketRequestOptions(config));
@@ -375,7 +380,7 @@ async function getBitbucketDefaultBranch(url, config) {
375
380
  const repoInfo = await response.json();
376
381
  defaultBranch = repoInfo.mainbranch.name;
377
382
  } else {
378
- const {displayId} = await response.json();
383
+ const { displayId } = await response.json();
379
384
  defaultBranch = displayId;
380
385
  }
381
386
  if (!defaultBranch) {
@@ -403,7 +408,7 @@ async function getBitbucketDownloadUrl(url, config) {
403
408
  }
404
409
  function getBitbucketFileFetchUrl(url, config) {
405
410
  try {
406
- const {owner, name, ref, filepathtype, filepath} = parseGitUrl(url);
411
+ const { owner, name, ref, filepathtype, filepath } = parseGitUrl(url);
407
412
  if (!owner || !name || filepathtype !== "browse" && filepathtype !== "raw" && filepathtype !== "src") {
408
413
  throw new Error("Invalid Bitbucket URL or file path");
409
414
  }
@@ -462,7 +467,7 @@ function readGitHubIntegrationConfig(config) {
462
467
  } else if (host === GITHUB_HOST) {
463
468
  rawBaseUrl = GITHUB_RAW_BASE_URL;
464
469
  }
465
- return {host, apiBaseUrl, rawBaseUrl, token, apps};
470
+ return { host, apiBaseUrl, rawBaseUrl, token, apps };
466
471
  }
467
472
  function readGitHubIntegrationConfigs(configs) {
468
473
  const result = configs.map(readGitHubIntegrationConfig);
@@ -478,7 +483,7 @@ function readGitHubIntegrationConfigs(configs) {
478
483
 
479
484
  function getGitHubFileFetchUrl(url, config, credentials) {
480
485
  try {
481
- const {owner, name, ref, filepathtype, filepath} = parseGitUrl(url);
486
+ const { owner, name, ref, filepathtype, filepath } = parseGitUrl(url);
482
487
  if (!owner || !name || !ref || filepathtype !== "blob" && filepathtype !== "raw" && filepathtype !== "tree") {
483
488
  throw new Error("Invalid GitHub URL or file path");
484
489
  }
@@ -499,7 +504,7 @@ function getGitHubRequestOptions(config, credentials) {
499
504
  if (credentials.token) {
500
505
  headers.Authorization = `token ${credentials.token}`;
501
506
  }
502
- return {headers};
507
+ return { headers };
503
508
  }
504
509
  function chooseEndpoint(config, credentials) {
505
510
  if (config.apiBaseUrl && (credentials.token || !config.rawBaseUrl)) {
@@ -510,17 +515,17 @@ function chooseEndpoint(config, credentials) {
510
515
 
511
516
  class Cache {
512
517
  constructor() {
513
- this.tokenCache = new Map();
518
+ this.tokenCache = /* @__PURE__ */ new Map();
514
519
  this.isNotExpired = (date) => date.diff(DateTime.local(), "minutes").minutes > 50;
515
520
  }
516
521
  async getOrCreateToken(key, supplier) {
517
522
  const item = this.tokenCache.get(key);
518
523
  if (item && this.isNotExpired(item.expiresAt)) {
519
- return {accessToken: item.token};
524
+ return { accessToken: item.token };
520
525
  }
521
526
  const result = await supplier();
522
527
  this.tokenCache.set(key, result);
523
- return {accessToken: result.token};
528
+ return { accessToken: result.token };
524
529
  }
525
530
  }
526
531
  const HEADERS = {
@@ -544,10 +549,10 @@ class GithubAppManager {
544
549
  }
545
550
  async getInstallationCredentials(owner, repo) {
546
551
  var _a;
547
- const {installationId, suspended} = await this.getInstallationData(owner);
552
+ const { installationId, suspended } = await this.getInstallationData(owner);
548
553
  if (this.allowedInstallationOwners) {
549
554
  if (!((_a = this.allowedInstallationOwners) == null ? void 0 : _a.includes(owner))) {
550
- throw new Error(`The GitHub application for ${owner} is not included in the allowed installation list (${installationId}).`);
555
+ return { accessToken: void 0 };
551
556
  }
552
557
  }
553
558
  if (suspended) {
@@ -614,7 +619,7 @@ class GithubAppCredentialsMux {
614
619
  if (this.apps.length === 0) {
615
620
  return void 0;
616
621
  }
617
- const results = await Promise.all(this.apps.map((app) => app.getInstallationCredentials(owner, repo).then((credentials) => ({credentials, error: void 0}), (error) => ({credentials: void 0, error}))));
622
+ const results = await Promise.all(this.apps.map((app) => app.getInstallationCredentials(owner, repo).then((credentials) => ({ credentials, error: void 0 }), (error) => ({ credentials: void 0, error }))));
618
623
  const result = results.find((resultItem) => resultItem.credentials);
619
624
  if (result) {
620
625
  return result.credentials.accessToken;
@@ -627,14 +632,11 @@ class GithubAppCredentialsMux {
627
632
  return void 0;
628
633
  }
629
634
  }
630
- class GithubCredentialsProvider {
635
+ const _SingleInstanceGithubCredentialsProvider = class {
631
636
  constructor(githubAppCredentialsMux, token) {
632
637
  this.githubAppCredentialsMux = githubAppCredentialsMux;
633
638
  this.token = token;
634
639
  }
635
- static create(config) {
636
- return new GithubCredentialsProvider(new GithubAppCredentialsMux(config), config.token);
637
- }
638
640
  async getCredentials(opts) {
639
641
  const parsed = parseGitUrl(opts.url);
640
642
  const owner = parsed.owner || parsed.name;
@@ -646,11 +648,37 @@ class GithubCredentialsProvider {
646
648
  token = this.token;
647
649
  }
648
650
  return {
649
- headers: token ? {Authorization: `Bearer ${token}`} : void 0,
651
+ headers: token ? { Authorization: `Bearer ${token}` } : void 0,
650
652
  token,
651
653
  type
652
654
  };
653
655
  }
656
+ };
657
+ let SingleInstanceGithubCredentialsProvider = _SingleInstanceGithubCredentialsProvider;
658
+ SingleInstanceGithubCredentialsProvider.create = (config) => {
659
+ return new _SingleInstanceGithubCredentialsProvider(new GithubAppCredentialsMux(config), config.token);
660
+ };
661
+
662
+ class DefaultGithubCredentialsProvider {
663
+ constructor(providers) {
664
+ this.providers = providers;
665
+ }
666
+ static fromIntegrations(integrations) {
667
+ const credentialsProviders = /* @__PURE__ */ new Map();
668
+ integrations.github.list().forEach((integration) => {
669
+ const credentialsProvider = SingleInstanceGithubCredentialsProvider.create(integration.config);
670
+ credentialsProviders.set(integration.config.host, credentialsProvider);
671
+ });
672
+ return new DefaultGithubCredentialsProvider(credentialsProviders);
673
+ }
674
+ async getCredentials(opts) {
675
+ const parsed = new URL(opts.url);
676
+ const provider = this.providers.get(parsed.host);
677
+ if (!provider) {
678
+ throw new Error(`There is no GitHub integration that matches ${opts.url}. Please add a configuration for an integration.`);
679
+ }
680
+ return provider.getCredentials(opts);
681
+ }
654
682
  }
655
683
 
656
684
  const _GitHubIntegration = class {
@@ -674,7 +702,7 @@ const _GitHubIntegration = class {
674
702
  }
675
703
  };
676
704
  let GitHubIntegration = _GitHubIntegration;
677
- GitHubIntegration.factory = ({config}) => {
705
+ GitHubIntegration.factory = ({ config }) => {
678
706
  var _a;
679
707
  const configs = readGitHubIntegrationConfigs((_a = config.getOptionalConfigArray("integrations.github")) != null ? _a : []);
680
708
  return basicIntegrations(configs.map((c) => new _GitHubIntegration(c)), (i) => i.config.host);
@@ -711,7 +739,7 @@ function readGitLabIntegrationConfig(config) {
711
739
  } else if (!isValidUrl(baseUrl)) {
712
740
  throw new Error(`Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl`);
713
741
  }
714
- return {host, token, apiBaseUrl, baseUrl};
742
+ return { host, token, apiBaseUrl, baseUrl };
715
743
  }
716
744
  function readGitLabIntegrationConfigs(configs) {
717
745
  const result = configs.map(readGitLabIntegrationConfig);
@@ -733,7 +761,7 @@ async function getGitLabFileFetchUrl(url, config) {
733
761
  return buildRawUrl(url).toString();
734
762
  }
735
763
  function getGitLabRequestOptions(config) {
736
- const {token = ""} = config;
764
+ const { token = "" } = config;
737
765
  return {
738
766
  headers: {
739
767
  "PRIVATE-TOKEN": token
@@ -811,7 +839,7 @@ const _GitLabIntegration = class {
811
839
  }
812
840
  };
813
841
  let GitLabIntegration = _GitLabIntegration;
814
- GitLabIntegration.factory = ({config}) => {
842
+ GitLabIntegration.factory = ({ config }) => {
815
843
  var _a;
816
844
  const configs = readGitLabIntegrationConfigs((_a = config.getOptionalConfigArray("integrations.gitlab")) != null ? _a : []);
817
845
  return basicIntegrations(configs.map((c) => new _GitLabIntegration(c)), (i) => i.config.host);
@@ -829,20 +857,41 @@ function readGoogleGcsIntegrationConfig(config) {
829
857
  }
830
858
  const privateKey = config.getString("privateKey").split("\\n").join("\n");
831
859
  const clientEmail = config.getString("clientEmail");
832
- return {clientEmail, privateKey};
860
+ return { clientEmail, privateKey };
833
861
  }
834
862
 
835
863
  const AMAZON_AWS_HOST = "amazonaws.com";
836
864
  function readAwsS3IntegrationConfig(config) {
837
865
  var _a;
838
- const host = (_a = config.getOptionalString("host")) != null ? _a : AMAZON_AWS_HOST;
866
+ const endpoint = config.getOptionalString("endpoint");
867
+ const s3ForcePathStyle = (_a = config.getOptionalBoolean("s3ForcePathStyle")) != null ? _a : false;
868
+ let host;
869
+ let pathname;
870
+ if (endpoint) {
871
+ try {
872
+ const url = new URL(endpoint);
873
+ host = url.host;
874
+ pathname = url.pathname;
875
+ } catch {
876
+ throw new Error(`invalid awsS3 integration config, endpoint '${endpoint}' is not a valid URL`);
877
+ }
878
+ if (pathname !== "/") {
879
+ throw new Error(`invalid awsS3 integration config, endpoints cannot contain path, got '${endpoint}'`);
880
+ }
881
+ } else {
882
+ host = AMAZON_AWS_HOST;
883
+ }
839
884
  const accessKeyId = config.getOptionalString("accessKeyId");
840
885
  const secretAccessKey = config.getOptionalString("secretAccessKey");
841
886
  const roleArn = config.getOptionalString("roleArn");
842
- if (!isValidHost(host)) {
843
- throw new Error(`Invalid awsS3 integration config, '${host}' is not a valid host`);
844
- }
845
- return {host, accessKeyId, secretAccessKey, roleArn};
887
+ return {
888
+ host,
889
+ endpoint,
890
+ s3ForcePathStyle,
891
+ accessKeyId,
892
+ secretAccessKey,
893
+ roleArn
894
+ };
846
895
  }
847
896
  function readAwsS3IntegrationConfigs(configs) {
848
897
  const result = configs.map(readAwsS3IntegrationConfig);
@@ -876,7 +925,7 @@ const _AwsS3Integration = class {
876
925
  }
877
926
  };
878
927
  let AwsS3Integration = _AwsS3Integration;
879
- AwsS3Integration.factory = ({config}) => {
928
+ AwsS3Integration.factory = ({ config }) => {
880
929
  var _a;
881
930
  const configs = readAwsS3IntegrationConfigs((_a = config.getOptionalConfigArray("integrations.awsS3")) != null ? _a : []);
882
931
  return basicIntegrations(configs.map((c) => new _AwsS3Integration(c)), (i) => i.config.host);
@@ -885,11 +934,11 @@ AwsS3Integration.factory = ({config}) => {
885
934
  class ScmIntegrations {
886
935
  static fromConfig(config) {
887
936
  return new ScmIntegrations({
888
- awsS3: AwsS3Integration.factory({config}),
889
- azure: AzureIntegration.factory({config}),
890
- bitbucket: BitbucketIntegration.factory({config}),
891
- github: GitHubIntegration.factory({config}),
892
- gitlab: GitLabIntegration.factory({config})
937
+ awsS3: AwsS3Integration.factory({ config }),
938
+ azure: AzureIntegration.factory({ config }),
939
+ bitbucket: BitbucketIntegration.factory({ config }),
940
+ github: GitHubIntegration.factory({ config }),
941
+ gitlab: GitLabIntegration.factory({ config })
893
942
  });
894
943
  }
895
944
  constructor(integrationsByType) {
@@ -935,5 +984,5 @@ class ScmIntegrations {
935
984
  }
936
985
  }
937
986
 
938
- export { AwsS3Integration, AzureIntegration, BitbucketIntegration, GitHubIntegration, GitLabIntegration, GithubAppCredentialsMux, GithubCredentialsProvider, ScmIntegrations, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabRequestOptions, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType };
987
+ export { AwsS3Integration, AzureIntegration, BitbucketIntegration, DefaultGithubCredentialsProvider, GitHubIntegration, GitLabIntegration, GithubAppCredentialsMux, ScmIntegrations, SingleInstanceGithubCredentialsProvider, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabRequestOptions, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType };
939
988
  //# sourceMappingURL=index.esm.js.map