@backstage/integration 1.0.1-next.0 → 1.1.0

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
@@ -314,6 +314,13 @@ declare type GerritIntegrationConfig = {
314
314
  * in a browser.
315
315
  */
316
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;
317
324
  /**
318
325
  * The username to use for requests to gerrit.
319
326
  */
@@ -360,6 +367,36 @@ declare class GerritIntegration implements ScmIntegration {
360
367
  resolveEditUrl(url: string): string;
361
368
  }
362
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
+
363
400
  /**
364
401
  * The configuration parameters for a single GitHub integration.
365
402
  *
@@ -687,6 +724,14 @@ declare class GitLabIntegration implements ScmIntegration {
687
724
  }): string;
688
725
  resolveEditUrl(url: string): string;
689
726
  }
727
+ /**
728
+ * Takes a GitLab URL and replaces the type part (blob, tree etc).
729
+ *
730
+ * @param url - The original URL
731
+ * @param type - The desired type, e.g. 'blob', 'tree', 'edit'
732
+ * @public
733
+ */
734
+ declare function replaceGitLabUrlType(url: string, type: 'blob' | 'tree' | 'edit'): string;
690
735
 
691
736
  /**
692
737
  * Holds all registered SCM integrations, of all types.
@@ -932,4 +977,4 @@ declare class ScmIntegrations implements ScmIntegrationRegistry {
932
977
  resolveEditUrl(url: string): string;
933
978
  }
934
979
 
935
- 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, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabRequestOptions, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readGerritIntegrationConfig, readGerritIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType };
980
+ 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, replaceGitLabUrlType };
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';
@@ -445,21 +445,30 @@ function getBitbucketRequestOptions(config) {
445
445
  function readGerritIntegrationConfig(config) {
446
446
  const host = config.getString("host");
447
447
  let baseUrl = config.getOptionalString("baseUrl");
448
+ let gitilesBaseUrl = config.getOptionalString("gitilesBaseUrl");
448
449
  const username = config.getOptionalString("username");
449
450
  const password = config.getOptionalString("password");
450
451
  if (!isValidHost(host)) {
451
452
  throw new Error(`Invalid Gerrit integration config, '${host}' is not a valid host`);
452
453
  } else if (baseUrl && !isValidUrl(baseUrl)) {
453
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`);
454
457
  }
455
458
  if (baseUrl) {
456
459
  baseUrl = trimEnd(baseUrl, "/");
457
460
  } else {
458
461
  baseUrl = `https://${host}`;
459
462
  }
463
+ if (gitilesBaseUrl) {
464
+ gitilesBaseUrl = trimEnd(gitilesBaseUrl, "/");
465
+ } else {
466
+ gitilesBaseUrl = `https://${host}`;
467
+ }
460
468
  return {
461
469
  host,
462
470
  baseUrl,
471
+ gitilesBaseUrl,
463
472
  username,
464
473
  password
465
474
  };
@@ -505,6 +514,57 @@ GerritIntegration.factory = ({ config }) => {
505
514
  return basicIntegrations(configs.map((c) => new _GerritIntegration(c)), (i) => i.config.host);
506
515
  };
507
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
+
508
568
  const GITHUB_HOST = "github.com";
509
569
  const GITHUB_API_BASE_URL = "https://api.github.com";
510
570
  const GITHUB_RAW_BASE_URL = "https://raw.githubusercontent.com";
@@ -901,7 +961,7 @@ const _GitLabIntegration = class {
901
961
  return defaultScmResolveUrl(options);
902
962
  }
903
963
  resolveEditUrl(url) {
904
- return replaceUrlType(url, "edit");
964
+ return replaceGitLabUrlType(url, "edit");
905
965
  }
906
966
  };
907
967
  let GitLabIntegration = _GitLabIntegration;
@@ -910,7 +970,7 @@ GitLabIntegration.factory = ({ config }) => {
910
970
  const configs = readGitLabIntegrationConfigs((_a = config.getOptionalConfigArray("integrations.gitlab")) != null ? _a : []);
911
971
  return basicIntegrations(configs.map((c) => new _GitLabIntegration(c)), (i) => i.config.host);
912
972
  };
913
- function replaceUrlType(url, type) {
973
+ function replaceGitLabUrlType(url, type) {
914
974
  return url.replace(/\/\-\/(blob|tree|edit)\//, `/-/${type}/`);
915
975
  }
916
976
 
@@ -1056,5 +1116,5 @@ class ScmIntegrations {
1056
1116
  }
1057
1117
  }
1058
1118
 
1059
- export { AwsS3Integration, AzureIntegration, BitbucketIntegration, DefaultGithubCredentialsProvider, GerritIntegration, GitHubIntegration, GitLabIntegration, GithubAppCredentialsMux, ScmIntegrations, SingleInstanceGithubCredentialsProvider, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabRequestOptions, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readGerritIntegrationConfig, readGerritIntegrationConfigs, 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, replaceGitLabUrlType };
1060
1120
  //# sourceMappingURL=index.esm.js.map