@backstage/integration 0.8.0 → 1.1.0-next.1

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.d.ts CHANGED
@@ -296,6 +296,107 @@ declare function getBitbucketRequestOptions(config: BitbucketIntegrationConfig):
296
296
  headers: Record<string, string>;
297
297
  };
298
298
 
299
+ /**
300
+ * The configuration parameters for a single Gerrit API provider.
301
+ *
302
+ * @public
303
+ */
304
+ declare type GerritIntegrationConfig = {
305
+ /**
306
+ * The host of the target that this matches on, e.g. "gerrit-review.com"
307
+ */
308
+ host: string;
309
+ /**
310
+ * The optional base URL of the Gerrit instance. It is assumed that https
311
+ * is used and that the base path is "/" on the host. If that is not the
312
+ * case set the complete base url to the gerrit instance, e.g.
313
+ * "https://gerrit-review.com/gerrit". This is the url that you would open
314
+ * in a browser.
315
+ */
316
+ baseUrl?: string;
317
+ /**
318
+ * Optional base url for Gitiles. This is needed for creating a valid
319
+ * user-friendly url that can be used for browsing the content of the
320
+ * provider. If not set a default value will be created in the same way
321
+ * as the "baseUrl" option.
322
+ */
323
+ gitilesBaseUrl?: string;
324
+ /**
325
+ * The username to use for requests to gerrit.
326
+ */
327
+ username?: string;
328
+ /**
329
+ * The password or http token to use for authentication.
330
+ */
331
+ password?: string;
332
+ };
333
+ /**
334
+ * Reads a single Gerrit integration config.
335
+ *
336
+ * @param config - The config object of a single integration
337
+ *
338
+ * @public
339
+ */
340
+ declare function readGerritIntegrationConfig(config: Config): GerritIntegrationConfig;
341
+ /**
342
+ * Reads a set of Gerrit integration configs.
343
+ *
344
+ * @param configs - All of the integration config objects
345
+ *
346
+ * @public
347
+ */
348
+ declare function readGerritIntegrationConfigs(configs: Config[]): GerritIntegrationConfig[];
349
+
350
+ /**
351
+ * A Gerrit based integration.
352
+ *
353
+ * @public
354
+ */
355
+ declare class GerritIntegration implements ScmIntegration {
356
+ private readonly integrationConfig;
357
+ static factory: ScmIntegrationsFactory<GerritIntegration>;
358
+ constructor(integrationConfig: GerritIntegrationConfig);
359
+ get type(): string;
360
+ get title(): string;
361
+ get config(): GerritIntegrationConfig;
362
+ resolveUrl(options: {
363
+ url: string;
364
+ base: string;
365
+ lineNumber?: number;
366
+ }): string;
367
+ resolveEditUrl(url: string): string;
368
+ }
369
+
370
+ /**
371
+ * Return the url to fetch the contents of a file using the Gerrit API.
372
+ *
373
+ * @param url - An url pointing to a file in git.
374
+ * @public
375
+ */
376
+ declare function getGerritFileContentsApiUrl(config: GerritIntegrationConfig, url: string): string;
377
+ /**
378
+ * Return request headers for a Gerrit provider.
379
+ *
380
+ * @param config - A Gerrit provider config
381
+ * @public
382
+ */
383
+ declare function getGerritRequestOptions(config: GerritIntegrationConfig): {
384
+ headers?: Record<string, string>;
385
+ };
386
+ /**
387
+ * Parse the json response from Gerrit and strip the magic prefix.
388
+ *
389
+ * @remarks
390
+ *
391
+ * To prevent against XSSI attacks the JSON response body from Gerrit starts
392
+ * with a magic prefix that must be stripped before it can be fed to a JSON
393
+ * parser.
394
+ *
395
+ * @param response - An API response.
396
+ * @public
397
+ */
398
+ declare function parseGerritJsonResponse(response: Response): Promise<unknown>;
399
+
299
400
  /**
300
401
  * The configuration parameters for a single GitHub integration.
301
402
  *
@@ -488,6 +589,10 @@ declare type AwsS3IntegrationConfig = {
488
589
  * (Optional) ARN of role to be assumed
489
590
  */
490
591
  roleArn?: string;
592
+ /**
593
+ * (Optional) External ID to use when assuming role
594
+ */
595
+ externalId?: string;
491
596
  };
492
597
  /**
493
598
  * Reads a single Aws S3 integration config.
@@ -629,6 +734,7 @@ interface ScmIntegrationRegistry extends ScmIntegrationsGroup<ScmIntegration> {
629
734
  awsS3: ScmIntegrationsGroup<AwsS3Integration>;
630
735
  azure: ScmIntegrationsGroup<AzureIntegration>;
631
736
  bitbucket: ScmIntegrationsGroup<BitbucketIntegration>;
737
+ gerrit: ScmIntegrationsGroup<GerritIntegration>;
632
738
  github: ScmIntegrationsGroup<GitHubIntegration>;
633
739
  gitlab: ScmIntegrationsGroup<GitLabIntegration>;
634
740
  /**
@@ -833,6 +939,7 @@ interface IntegrationsByType {
833
939
  awsS3: ScmIntegrationsGroup<AwsS3Integration>;
834
940
  azure: ScmIntegrationsGroup<AzureIntegration>;
835
941
  bitbucket: ScmIntegrationsGroup<BitbucketIntegration>;
942
+ gerrit: ScmIntegrationsGroup<GerritIntegration>;
836
943
  github: ScmIntegrationsGroup<GitHubIntegration>;
837
944
  gitlab: ScmIntegrationsGroup<GitLabIntegration>;
838
945
  }
@@ -848,6 +955,7 @@ declare class ScmIntegrations implements ScmIntegrationRegistry {
848
955
  get awsS3(): ScmIntegrationsGroup<AwsS3Integration>;
849
956
  get azure(): ScmIntegrationsGroup<AzureIntegration>;
850
957
  get bitbucket(): ScmIntegrationsGroup<BitbucketIntegration>;
958
+ get gerrit(): ScmIntegrationsGroup<GerritIntegration>;
851
959
  get github(): ScmIntegrationsGroup<GitHubIntegration>;
852
960
  get gitlab(): ScmIntegrationsGroup<GitLabIntegration>;
853
961
  list(): ScmIntegration[];
@@ -861,4 +969,4 @@ declare class ScmIntegrations implements ScmIntegrationRegistry {
861
969
  resolveEditUrl(url: string): string;
862
970
  }
863
971
 
864
- export { AwsS3Integration, AwsS3IntegrationConfig, AzureIntegration, AzureIntegrationConfig, BitbucketIntegration, BitbucketIntegrationConfig, DefaultGithubCredentialsProvider, GitHubIntegration, GitHubIntegrationConfig, GitLabIntegration, GitLabIntegrationConfig, GithubAppConfig, GithubAppCredentialsMux, GithubCredentialType, GithubCredentials, GithubCredentialsProvider, GoogleGcsIntegrationConfig, IntegrationsByType, ScmIntegration, ScmIntegrationRegistry, ScmIntegrations, ScmIntegrationsFactory, ScmIntegrationsGroup, 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 };
972
+ export { AwsS3Integration, AwsS3IntegrationConfig, AzureIntegration, AzureIntegrationConfig, BitbucketIntegration, BitbucketIntegrationConfig, DefaultGithubCredentialsProvider, GerritIntegration, GerritIntegrationConfig, GitHubIntegration, GitHubIntegrationConfig, GitLabIntegration, GitLabIntegrationConfig, GithubAppConfig, GithubAppCredentialsMux, GithubCredentialType, GithubCredentials, GithubCredentialsProvider, GoogleGcsIntegrationConfig, IntegrationsByType, ScmIntegration, ScmIntegrationRegistry, ScmIntegrations, ScmIntegrationsFactory, ScmIntegrationsGroup, SingleInstanceGithubCredentialsProvider, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getGerritFileContentsApiUrl, getGerritRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabRequestOptions, parseGerritJsonResponse, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readGerritIntegrationConfig, readGerritIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType };
package/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import parseGitUrl from 'git-url-parse';
2
- import { trimEnd } from 'lodash';
2
+ import { trimEnd, trimStart } from 'lodash';
3
3
  import fetch from 'cross-fetch';
4
4
  import { createAppAuth } from '@octokit/auth-app';
5
5
  import { Octokit } from '@octokit/rest';
@@ -442,6 +442,129 @@ function getBitbucketRequestOptions(config) {
442
442
  };
443
443
  }
444
444
 
445
+ function readGerritIntegrationConfig(config) {
446
+ const host = config.getString("host");
447
+ let baseUrl = config.getOptionalString("baseUrl");
448
+ let gitilesBaseUrl = config.getOptionalString("gitilesBaseUrl");
449
+ const username = config.getOptionalString("username");
450
+ const password = config.getOptionalString("password");
451
+ if (!isValidHost(host)) {
452
+ throw new Error(`Invalid Gerrit integration config, '${host}' is not a valid host`);
453
+ } else if (baseUrl && !isValidUrl(baseUrl)) {
454
+ throw new Error(`Invalid Gerrit integration config, '${baseUrl}' is not a valid baseUrl`);
455
+ } else if (gitilesBaseUrl && !isValidUrl(gitilesBaseUrl)) {
456
+ throw new Error(`Invalid Gerrit integration config, '${gitilesBaseUrl}' is not a valid gitilesBaseUrl`);
457
+ }
458
+ if (baseUrl) {
459
+ baseUrl = trimEnd(baseUrl, "/");
460
+ } else {
461
+ baseUrl = `https://${host}`;
462
+ }
463
+ if (gitilesBaseUrl) {
464
+ gitilesBaseUrl = trimEnd(gitilesBaseUrl, "/");
465
+ } else {
466
+ gitilesBaseUrl = `https://${host}`;
467
+ }
468
+ return {
469
+ host,
470
+ baseUrl,
471
+ gitilesBaseUrl,
472
+ username,
473
+ password
474
+ };
475
+ }
476
+ function readGerritIntegrationConfigs(configs) {
477
+ return configs.map(readGerritIntegrationConfig);
478
+ }
479
+
480
+ const _GerritIntegration = class {
481
+ constructor(integrationConfig) {
482
+ this.integrationConfig = integrationConfig;
483
+ }
484
+ get type() {
485
+ return "gerrit";
486
+ }
487
+ get title() {
488
+ return this.integrationConfig.host;
489
+ }
490
+ get config() {
491
+ return this.integrationConfig;
492
+ }
493
+ resolveUrl(options) {
494
+ const { url, base, lineNumber } = options;
495
+ let updated;
496
+ if (url) {
497
+ updated = new URL(url, base);
498
+ } else {
499
+ updated = new URL(base);
500
+ }
501
+ if (lineNumber) {
502
+ updated.hash = lineNumber.toString();
503
+ }
504
+ return updated.toString();
505
+ }
506
+ resolveEditUrl(url) {
507
+ return url;
508
+ }
509
+ };
510
+ let GerritIntegration = _GerritIntegration;
511
+ GerritIntegration.factory = ({ config }) => {
512
+ var _a;
513
+ const configs = readGerritIntegrationConfigs((_a = config.getOptionalConfigArray("integrations.gerrit")) != null ? _a : []);
514
+ return basicIntegrations(configs.map((c) => new _GerritIntegration(c)), (i) => i.config.host);
515
+ };
516
+
517
+ const GERRIT_BODY_PREFIX = ")]}'";
518
+ function parseGitilesUrl(config, url) {
519
+ const urlPath = url.replace(config.gitilesBaseUrl, "");
520
+ const parts = urlPath.split("/").filter((p) => !!p);
521
+ const projectEndIndex = parts.indexOf("+");
522
+ if (projectEndIndex <= 0) {
523
+ throw new Error(`Unable to parse project from url: ${url}`);
524
+ }
525
+ const project = trimStart(parts.slice(0, projectEndIndex).join("/"), "/");
526
+ const branchIndex = parts.indexOf("heads");
527
+ if (branchIndex <= 0) {
528
+ throw new Error(`Unable to parse branch from url: ${url}`);
529
+ }
530
+ const branch = parts[branchIndex + 1];
531
+ const filePath = parts.slice(branchIndex + 2).join("/");
532
+ return {
533
+ branch,
534
+ filePath: filePath === "" ? "/" : filePath,
535
+ project
536
+ };
537
+ }
538
+ function getAuthenticationPrefix(config) {
539
+ return config.password ? "/a/" : "/";
540
+ }
541
+ function getGerritFileContentsApiUrl(config, url) {
542
+ const { branch, filePath, project } = parseGitilesUrl(config, url);
543
+ return `${config.baseUrl}${getAuthenticationPrefix(config)}projects/${encodeURIComponent(project)}/branches/${branch}/files/${encodeURIComponent(filePath)}/content`;
544
+ }
545
+ function getGerritRequestOptions(config) {
546
+ const headers = {};
547
+ if (!config.password) {
548
+ return headers;
549
+ }
550
+ const buffer = Buffer.from(`${config.username}:${config.password}`, "utf8");
551
+ headers.Authorization = `Basic ${buffer.toString("base64")}`;
552
+ return {
553
+ headers
554
+ };
555
+ }
556
+ async function parseGerritJsonResponse(response) {
557
+ const responseBody = await response.text();
558
+ if (responseBody.startsWith(GERRIT_BODY_PREFIX)) {
559
+ try {
560
+ return JSON.parse(responseBody.slice(GERRIT_BODY_PREFIX.length));
561
+ } catch (ex) {
562
+ throw new Error(`Invalid response from Gerrit: ${responseBody.slice(0, 10)} - ${ex}`);
563
+ }
564
+ }
565
+ throw new Error(`Gerrit JSON body prefix missing. Found: ${responseBody.slice(0, 10)}`);
566
+ }
567
+
445
568
  const GITHUB_HOST = "github.com";
446
569
  const GITHUB_API_BASE_URL = "https://api.github.com";
447
570
  const GITHUB_RAW_BASE_URL = "https://raw.githubusercontent.com";
@@ -887,13 +1010,15 @@ function readAwsS3IntegrationConfig(config) {
887
1010
  const accessKeyId = config.getOptionalString("accessKeyId");
888
1011
  const secretAccessKey = config.getOptionalString("secretAccessKey");
889
1012
  const roleArn = config.getOptionalString("roleArn");
1013
+ const externalId = config.getOptionalString("externalId");
890
1014
  return {
891
1015
  host,
892
1016
  endpoint,
893
1017
  s3ForcePathStyle,
894
1018
  accessKeyId,
895
1019
  secretAccessKey,
896
- roleArn
1020
+ roleArn,
1021
+ externalId
897
1022
  };
898
1023
  }
899
1024
  function readAwsS3IntegrationConfigs(configs) {
@@ -940,6 +1065,7 @@ class ScmIntegrations {
940
1065
  awsS3: AwsS3Integration.factory({ config }),
941
1066
  azure: AzureIntegration.factory({ config }),
942
1067
  bitbucket: BitbucketIntegration.factory({ config }),
1068
+ gerrit: GerritIntegration.factory({ config }),
943
1069
  github: GitHubIntegration.factory({ config }),
944
1070
  gitlab: GitLabIntegration.factory({ config })
945
1071
  });
@@ -956,6 +1082,9 @@ class ScmIntegrations {
956
1082
  get bitbucket() {
957
1083
  return this.byType.bitbucket;
958
1084
  }
1085
+ get gerrit() {
1086
+ return this.byType.gerrit;
1087
+ }
959
1088
  get github() {
960
1089
  return this.byType.github;
961
1090
  }
@@ -987,5 +1116,5 @@ class ScmIntegrations {
987
1116
  }
988
1117
  }
989
1118
 
990
- 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 };
1119
+ export { AwsS3Integration, AzureIntegration, BitbucketIntegration, DefaultGithubCredentialsProvider, GerritIntegration, GitHubIntegration, GitLabIntegration, GithubAppCredentialsMux, ScmIntegrations, SingleInstanceGithubCredentialsProvider, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getGerritFileContentsApiUrl, getGerritRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabRequestOptions, parseGerritJsonResponse, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readGerritIntegrationConfig, readGerritIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType };
991
1120
  //# sourceMappingURL=index.esm.js.map