@backstage/integration 2.0.1 → 2.0.2-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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @backstage/integration
2
2
 
3
+ ## 2.0.2-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 6b112d3: Fixed two issues in the GitLab integration's fetch behavior:
8
+
9
+ - The internal fetch wrapper was passing `mode: 'same-origin'` on every request. This had no practical effect server-side, but would have caused cross-origin requests to be rejected when the integration is used from a browser. Requests now use the default fetch mode and work correctly in both browser and Node environments.
10
+ - When retries are configured, transient network errors (such as dropped connections or DNS hiccups) are now retried using the same `maxRetries` and exponential delay as retryable HTTP status codes. Previously, a thrown fetch error would propagate immediately on the first failure regardless of the retry configuration. Caller-initiated aborts continue to surface immediately without being retried.
11
+
12
+ ## 2.0.2-next.0
13
+
14
+ ### Patch Changes
15
+
16
+ - b62781f: Moved `registerMswTestHooks` to test files.
17
+ - Updated dependencies
18
+ - @backstage/errors@1.3.1-next.0
19
+ - @backstage/config@1.3.8-next.0
20
+
3
21
  ## 2.0.1
4
22
 
5
23
  ### Patch Changes
@@ -54,5 +54,4 @@ function replaceCodeCommitUrlType(url, repositoryName, type) {
54
54
  }
55
55
 
56
56
  exports.AwsCodeCommitIntegration = AwsCodeCommitIntegration;
57
- exports.replaceCodeCommitUrlType = replaceCodeCommitUrlType;
58
57
  //# sourceMappingURL=AwsCodeCommitIntegration.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"AwsCodeCommitIntegration.cjs.js","sources":["../../src/awsCodeCommit/AwsCodeCommitIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n AwsCodeCommitIntegrationConfig,\n readAwsCodeCommitIntegrationConfigs,\n} from './config';\n\n/**\n * Integrates with AWS CodeCommit.\n *\n * @public\n */\nexport class AwsCodeCommitIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<AwsCodeCommitIntegration> = ({\n config,\n }) => {\n const configs = readAwsCodeCommitIntegrationConfigs(\n config.getOptionalConfigArray('integrations.awsCodeCommit') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new AwsCodeCommitIntegration(c)),\n i => i.config.host,\n );\n };\n\n get type(): string {\n return 'awsCodeCommit';\n }\n\n get config(): AwsCodeCommitIntegrationConfig {\n return this.integrationConfig;\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n constructor(\n private readonly integrationConfig: AwsCodeCommitIntegrationConfig,\n ) {}\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number | undefined;\n }): string {\n const resolved = defaultScmResolveUrl(options);\n return resolved;\n }\n resolveEditUrl(url: string): string {\n const parsedUrl = new URL(url);\n const pathMatch = parsedUrl.pathname.match(\n /^\\/codesuite\\/codecommit\\/repositories\\/([^\\/]+)\\//,\n );\n if (!pathMatch) {\n throw new Error(``);\n }\n const [, repositoryName] = pathMatch;\n return replaceCodeCommitUrlType(url, repositoryName, 'edit');\n }\n}\n\n/**\n * Takes a CodeCommit URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'edit'\n * @public\n */\nexport function replaceCodeCommitUrlType(\n url: string,\n repositoryName: string,\n type: 'browse' | 'edit',\n): string {\n const newString = type === 'edit' ? `files/edit` : type;\n return url.replace(\n new RegExp(\n `\\/codesuite\\/codecommit\\/repositories\\/${repositoryName}\\/(browse|files\\/edit)\\/`,\n ),\n `/codesuite/codecommit/repositories/${repositoryName}/${newString}/`,\n );\n}\n"],"names":["config","readAwsCodeCommitIntegrationConfigs","basicIntegrations","defaultScmResolveUrl"],"mappings":";;;;;AA4BO,MAAM,wBAAA,CAAmD;AAAA,EAyB9D,YACmB,iBAAA,EACjB;AADiB,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAAA,EAChB;AAAA,EA1BH,OAAO,UAA4D,CAAC;AAAA,YAClEA;AAAA,GACF,KAAM;AACJ,IAAA,MAAM,OAAA,GAAUC,0CAAA;AAAA,MACdD,QAAA,CAAO,sBAAA,CAAuB,4BAA4B,CAAA,IAAK;AAAC,KAClE;AACA,IAAA,OAAOE,yBAAA;AAAA,MACL,QAAQ,GAAA,CAAI,CAAA,CAAA,KAAK,IAAI,wBAAA,CAAyB,CAAC,CAAC,CAAA;AAAA,MAChD,CAAA,CAAA,KAAK,EAAE,MAAA,CAAO;AAAA,KAChB;AAAA,EACF,CAAA;AAAA,EAEA,IAAI,IAAA,GAAe;AACjB,IAAA,OAAO,eAAA;AAAA,EACT;AAAA,EAEA,IAAI,MAAA,GAAyC;AAC3C,IAAA,OAAO,IAAA,CAAK,iBAAA;AAAA,EACd;AAAA,EAEA,IAAI,KAAA,GAAgB;AAClB,IAAA,OAAO,KAAK,iBAAA,CAAkB,IAAA;AAAA,EAChC;AAAA,EAMA,WAAW,OAAA,EAIA;AACT,IAAA,MAAM,QAAA,GAAWC,6BAAqB,OAAO,CAAA;AAC7C,IAAA,OAAO,QAAA;AAAA,EACT;AAAA,EACA,eAAe,GAAA,EAAqB;AAClC,IAAA,MAAM,SAAA,GAAY,IAAI,GAAA,CAAI,GAAG,CAAA;AAC7B,IAAA,MAAM,SAAA,GAAY,UAAU,QAAA,CAAS,KAAA;AAAA,MACnC;AAAA,KACF;AACA,IAAA,IAAI,CAAC,SAAA,EAAW;AACd,MAAA,MAAM,IAAI,MAAM,CAAA,CAAE,CAAA;AAAA,IACpB;AACA,IAAA,MAAM,GAAG,cAAc,CAAA,GAAI,SAAA;AAC3B,IAAA,OAAO,wBAAA,CAAyB,GAAA,EAAK,cAAsB,CAAA;AAAA,EAC7D;AACF;AASO,SAAS,wBAAA,CACd,GAAA,EACA,cAAA,EACA,IAAA,EACQ;AACR,EAAA,MAAM,SAAA,GAA8B,CAAA,UAAA,CAAA,CAAe;AACnD,EAAA,OAAO,GAAA,CAAI,OAAA;AAAA,IACT,IAAI,MAAA;AAAA,MACF,sCAA0C,cAAc,CAAA,qBAAA;AAAA,KAC1D;AAAA,IACA,CAAA,mCAAA,EAAsC,cAAc,CAAA,CAAA,EAAI,SAAS,CAAA,CAAA;AAAA,GACnE;AACF;;;;;"}
1
+ {"version":3,"file":"AwsCodeCommitIntegration.cjs.js","sources":["../../src/awsCodeCommit/AwsCodeCommitIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n AwsCodeCommitIntegrationConfig,\n readAwsCodeCommitIntegrationConfigs,\n} from './config';\n\n/**\n * Integrates with AWS CodeCommit.\n *\n * @public\n */\nexport class AwsCodeCommitIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<AwsCodeCommitIntegration> = ({\n config,\n }) => {\n const configs = readAwsCodeCommitIntegrationConfigs(\n config.getOptionalConfigArray('integrations.awsCodeCommit') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new AwsCodeCommitIntegration(c)),\n i => i.config.host,\n );\n };\n\n get type(): string {\n return 'awsCodeCommit';\n }\n\n get config(): AwsCodeCommitIntegrationConfig {\n return this.integrationConfig;\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n constructor(\n private readonly integrationConfig: AwsCodeCommitIntegrationConfig,\n ) {}\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number | undefined;\n }): string {\n const resolved = defaultScmResolveUrl(options);\n return resolved;\n }\n resolveEditUrl(url: string): string {\n const parsedUrl = new URL(url);\n const pathMatch = parsedUrl.pathname.match(\n /^\\/codesuite\\/codecommit\\/repositories\\/([^\\/]+)\\//,\n );\n if (!pathMatch) {\n throw new Error(``);\n }\n const [, repositoryName] = pathMatch;\n return replaceCodeCommitUrlType(url, repositoryName, 'edit');\n }\n}\n\n/**\n * Takes a CodeCommit URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'edit'\n */\nfunction replaceCodeCommitUrlType(\n url: string,\n repositoryName: string,\n type: 'browse' | 'edit',\n): string {\n const newString = type === 'edit' ? `files/edit` : type;\n return url.replace(\n new RegExp(\n `\\/codesuite\\/codecommit\\/repositories\\/${repositoryName}\\/(browse|files\\/edit)\\/`,\n ),\n `/codesuite/codecommit/repositories/${repositoryName}/${newString}/`,\n );\n}\n"],"names":["config","readAwsCodeCommitIntegrationConfigs","basicIntegrations","defaultScmResolveUrl"],"mappings":";;;;;AA4BO,MAAM,wBAAA,CAAmD;AAAA,EAyB9D,YACmB,iBAAA,EACjB;AADiB,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAAA,EAChB;AAAA,EA1BH,OAAO,UAA4D,CAAC;AAAA,YAClEA;AAAA,GACF,KAAM;AACJ,IAAA,MAAM,OAAA,GAAUC,0CAAA;AAAA,MACdD,QAAA,CAAO,sBAAA,CAAuB,4BAA4B,CAAA,IAAK;AAAC,KAClE;AACA,IAAA,OAAOE,yBAAA;AAAA,MACL,QAAQ,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itUrlType(url, repositoryName, type) {
51
51
  );
52
52
  }
53
53
 
54
- export { AwsCodeCommitIntegration, replaceCodeCommitUrlType };
54
+ export { AwsCodeCommitIntegration };
55
55
  //# sourceMappingURL=AwsCodeCommitIntegration.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"AwsCodeCommitIntegration.esm.js","sources":["../../src/awsCodeCommit/AwsCodeCommitIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n AwsCodeCommitIntegrationConfig,\n readAwsCodeCommitIntegrationConfigs,\n} from './config';\n\n/**\n * Integrates with AWS CodeCommit.\n *\n * @public\n */\nexport class AwsCodeCommitIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<AwsCodeCommitIntegration> = ({\n config,\n }) => {\n const configs = readAwsCodeCommitIntegrationConfigs(\n config.getOptionalConfigArray('integrations.awsCodeCommit') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new AwsCodeCommitIntegration(c)),\n i => i.config.host,\n );\n };\n\n get type(): string {\n return 'awsCodeCommit';\n }\n\n get config(): AwsCodeCommitIntegrationConfig {\n return this.integrationConfig;\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n constructor(\n private readonly integrationConfig: AwsCodeCommitIntegrationConfig,\n ) {}\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number | undefined;\n }): string {\n const resolved = defaultScmResolveUrl(options);\n return resolved;\n }\n resolveEditUrl(url: string): string {\n const parsedUrl = new URL(url);\n const pathMatch = parsedUrl.pathname.match(\n /^\\/codesuite\\/codecommit\\/repositories\\/([^\\/]+)\\//,\n );\n if (!pathMatch) {\n throw new Error(``);\n }\n const [, repositoryName] = pathMatch;\n return replaceCodeCommitUrlType(url, repositoryName, 'edit');\n }\n}\n\n/**\n * Takes a CodeCommit URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'edit'\n * @public\n */\nexport function replaceCodeCommitUrlType(\n url: string,\n repositoryName: string,\n type: 'browse' | 'edit',\n): string {\n const newString = type === 'edit' ? `files/edit` : type;\n return url.replace(\n new RegExp(\n `\\/codesuite\\/codecommit\\/repositories\\/${repositoryName}\\/(browse|files\\/edit)\\/`,\n ),\n `/codesuite/codecommit/repositories/${repositoryName}/${newString}/`,\n );\n}\n"],"names":[],"mappings":";;;AA4BO,MAAM,wBAAA,CAAmD;AAAA,EAyB9D,YACmB,iBAAA,EACjB;AADiB,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAAA,EAChB;AAAA,EA1BH,OAAO,UAA4D,CAAC;AAAA,IAClE;AAAA,GACF,KAAM;AACJ,IAAA,MAAM,OAAA,GAAU,mCAAA;AAAA,MACd,MAAA,CAAO,sBAAA,CAAuB,4BAA4B,CAAA,IAAK;AAAC,KAClE;AACA,IAAA,OAAO,iBAAA;AAAA,MACL,QAAQ,GAAA,CAAI,CAAA,CAAA,KAAK,IAAI,wBAAA,CAAyB,CAAC,CAAC,CAAA;AAAA,MAChD,CAAA,CAAA,KAAK,EAAE,MAAA,CAAO;AAAA,KAChB;AAAA,EACF,CAAA;AAAA,EAEA,IAAI,IAAA,GAAe;AACjB,IAAA,OAAO,eAAA;AAAA,EACT;AAAA,EAEA,IAAI,MAAA,GAAyC;AAC3C,IAAA,OAAO,IAAA,CAAK,iBAAA;AAAA,EACd;AAAA,EAEA,IAAI,KAAA,GAAgB;AAClB,IAAA,OAAO,KAAK,iBAAA,CAAkB,IAAA;AAAA,EAChC;AAAA,EAMA,WAAW,OAAA,EAIA;AACT,IAAA,MAAM,QAAA,GAAW,qBAAqB,OAAO,CAAA;AAC7C,IAAA,OAAO,QAAA;AAAA,EACT;AAAA,EACA,eAAe,GAAA,EAAqB;AAClC,IAAA,MAAM,SAAA,GAAY,IAAI,GAAA,CAAI,GAAG,CAAA;AAC7B,IAAA,MAAM,SAAA,GAAY,UAAU,QAAA,CAAS,KAAA;AAAA,MACnC;AAAA,KACF;AACA,IAAA,IAAI,CAAC,SAAA,EAAW;AACd,MAAA,MAAM,IAAI,MAAM,CAAA,CAAE,CAAA;AAAA,IACpB;AACA,IAAA,MAAM,GAAG,cAAc,CAAA,GAAI,SAAA;AAC3B,IAAA,OAAO,wBAAA,CAAyB,GAAA,EAAK,cAAsB,CAAA;AAAA,EAC7D;AACF;AASO,SAAS,wBAAA,CACd,GAAA,EACA,cAAA,EACA,IAAA,EACQ;AACR,EAAA,MAAM,SAAA,GAA8B,CAAA,UAAA,CAAA,CAAe;AACnD,EAAA,OAAO,GAAA,CAAI,OAAA;AAAA,IACT,IAAI,MAAA;AAAA,MACF,sCAA0C,cAAc,CAAA,qBAAA;AAAA,KAC1D;AAAA,IACA,CAAA,mCAAA,EAAsC,cAAc,CAAA,CAAA,EAAI,SAAS,CAAA,CAAA;AAAA,GACnE;AACF;;;;"}
1
+ {"version":3,"file":"AwsCodeCommitIntegration.esm.js","sources":["../../src/awsCodeCommit/AwsCodeCommitIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n AwsCodeCommitIntegrationConfig,\n readAwsCodeCommitIntegrationConfigs,\n} from './config';\n\n/**\n * Integrates with AWS CodeCommit.\n *\n * @public\n */\nexport class AwsCodeCommitIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<AwsCodeCommitIntegration> = ({\n config,\n }) => {\n const configs = readAwsCodeCommitIntegrationConfigs(\n config.getOptionalConfigArray('integrations.awsCodeCommit') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new AwsCodeCommitIntegration(c)),\n i => i.config.host,\n );\n };\n\n get type(): string {\n return 'awsCodeCommit';\n }\n\n get config(): AwsCodeCommitIntegrationConfig {\n return this.integrationConfig;\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n constructor(\n private readonly integrationConfig: AwsCodeCommitIntegrationConfig,\n ) {}\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number | undefined;\n }): string {\n const resolved = defaultScmResolveUrl(options);\n return resolved;\n }\n resolveEditUrl(url: string): string {\n const parsedUrl = new URL(url);\n const pathMatch = parsedUrl.pathname.match(\n /^\\/codesuite\\/codecommit\\/repositories\\/([^\\/]+)\\//,\n );\n if (!pathMatch) {\n throw new Error(``);\n }\n const [, repositoryName] = pathMatch;\n return replaceCodeCommitUrlType(url, repositoryName, 'edit');\n }\n}\n\n/**\n * Takes a CodeCommit URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'edit'\n */\nfunction replaceCodeCommitUrlType(\n url: string,\n repositoryName: string,\n type: 'browse' | 'edit',\n): string {\n const newString = type === 'edit' ? `files/edit` : type;\n return url.replace(\n new RegExp(\n `\\/codesuite\\/codecommit\\/repositories\\/${repositoryName}\\/(browse|files\\/edit)\\/`,\n ),\n `/codesuite/codecommit/repositories/${repositoryName}/${newString}/`,\n );\n}\n"],"names":[],"mappings":";;;AA4B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A,EACA,cAAA,EACA,IAAA,EACQ;AACR,EAAA,MAAM,SAAA,GAA8B,CAAA,UAAA,CAAA,CAAe;AACnD,EAAA,OAAO,GAAA,CAAI,OAAA;AAAA,IACT,IAAI,MAAA;AAAA,MACF,sCAA0C,cAAc,CAAA,qBAAA;AAAA,KAC1D;AAAA,IACA,CAAA,mCAAA,EAAsC,cAAc,CAAA,CAAA,EAAI,SAAS,CAAA,CAAA;AAAA,GACnE;AACF;;;;"}
@@ -178,7 +178,6 @@ async function parseGerritJsonResponse(response) {
178
178
 
179
179
  exports.buildGerritEditUrl = buildGerritEditUrl;
180
180
  exports.buildGerritGitilesArchiveUrlFromLocation = buildGerritGitilesArchiveUrlFromLocation;
181
- exports.getAuthenticationPrefix = getAuthenticationPrefix;
182
181
  exports.getGerritBranchApiUrl = getGerritBranchApiUrl;
183
182
  exports.getGerritCloneRepoUrl = getGerritCloneRepoUrl;
184
183
  exports.getGerritFileContentsApiUrl = getGerritFileContentsApiUrl;
@@ -1 +1 @@
1
- {"version":3,"file":"core.cjs.js","sources":["../../src/gerrit/core.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { join, takeWhile, trimEnd, trimStart } from 'lodash';\nimport { GerritIntegrationConfig } from './config';\n\nconst GERRIT_BODY_PREFIX = \")]}'\";\n\n/**\n * Parses Gitiles urls and returns the following:\n *\n * - The project\n * - The type of ref. I.e: branch name, SHA, HEAD or tag.\n * - The file path from the repo root.\n * - The base path as the path that points to the repo root.\n *\n * Supported types of gitiles urls that point to:\n *\n * - Branches\n * - Tags\n * - A commit SHA\n * - HEAD\n *\n * @param config - A Gerrit provider config.\n * @param url - An url to a file or folder in Gitiles.\n * @public\n */\nexport function parseGitilesUrlRef(\n config: GerritIntegrationConfig,\n url: string,\n): {\n project: string;\n path: string;\n ref: string;\n refType: 'sha' | 'branch' | 'tag' | 'head';\n basePath: string;\n} {\n const baseUrlParse = new URL(config.gitilesBaseUrl!);\n const urlParse = new URL(url);\n // Remove the gerrit authentication prefix '/a/' from the url\n // In case of the gitilesBaseUrl is https://review.gerrit.com/plugins/gitiles\n // and the url provided is https://review.gerrit.com/a/plugins/gitiles/...\n // remove the prefix only if the pathname start with '/a/'\n const urlPath = trimStart(\n urlParse.pathname\n .substring(urlParse.pathname.startsWith('/a/') ? 2 : 0)\n .replace(baseUrlParse.pathname, ''),\n '/',\n );\n\n // Find the project by taking everything up to \"/+/\".\n const parts = urlPath.split('/').filter(p => !!p);\n const projectParts = takeWhile(parts, p => p !== '+');\n if (projectParts.length === 0) {\n throw new Error(`Unable to parse gitiles url: ${url}`);\n }\n // Also remove the \"+\" after the project.\n const rest = parts.slice(projectParts.length + 1);\n const project = join(projectParts, '/');\n\n // match <project>/+/HEAD/<path>\n if (rest.length > 0 && rest[0] === 'HEAD') {\n const ref = rest.shift()!;\n const path = join(rest, '/');\n return {\n project,\n ref,\n refType: 'head' as const,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n // match <project>/+/<sha>/<path>\n if (rest.length > 0 && rest[0].length === 40) {\n const ref = rest.shift()!;\n const path = join(rest, '/');\n return {\n project,\n ref,\n refType: 'sha' as const,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n const remainingPath = join(rest, '/');\n // Regexp for matching \"refs/tags/<tag>\" or \"refs/heads/<branch>/\"\n const refsRegexp = /^refs\\/(?<refsReference>heads|tags)\\/(?<ref>.*?)(\\/|$)/;\n const result = refsRegexp.exec(remainingPath);\n if (result) {\n const matchString = result[0];\n let refType;\n const { refsReference, ref } = result.groups || {};\n const path = remainingPath.replace(matchString, '');\n switch (refsReference) {\n case 'heads':\n refType = 'branch' as const;\n break;\n case 'tags':\n refType = 'tag' as const;\n break;\n default:\n throw new Error(`Unable to parse gitiles url: ${url}`);\n }\n return {\n project,\n ref,\n refType,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n throw new Error(`Unable to parse gitiles : ${url}`);\n}\n\n/**\n * Build a Gerrit Gitiles url that targets a specific path.\n *\n * @param config - A Gerrit provider config.\n * @param project - The name of the git project\n * @param branch - The branch we will target.\n * @param filePath - The absolute file path.\n * @public\n */\nexport function buildGerritGitilesUrl(\n config: GerritIntegrationConfig,\n project: string,\n branch: string,\n filePath: string,\n): string {\n return `${\n config.gitilesBaseUrl\n }/${project}/+/refs/heads/${branch}/${trimStart(filePath, '/')}`;\n}\n\n/**\n * Build a Gerrit Gitiles url that targets a specific path.\n *\n * @param config - A Gerrit provider config.\n * @param project - The name of the git project\n * @param branch - The branch we will target.\n * @param filePath - The absolute file path.\n * @public\n */\nexport function buildGerritEditUrl(\n config: GerritIntegrationConfig,\n project: string,\n branch: string,\n filePath: string,\n): string {\n return `${\n config.baseUrl\n }/admin/repos/edit/repo/${project}/branch/refs/heads/${branch}/file/${trimStart(\n filePath,\n '/',\n )}`;\n}\n\n/**\n * Build a Gerrit Gitiles archive url from a Gitiles url.\n *\n * @param config - A Gerrit provider config.\n * @param url - The gitiles url\n * @public\n */\nexport function buildGerritGitilesArchiveUrlFromLocation(\n config: GerritIntegrationConfig,\n url: string,\n): string {\n const {\n path: filePath,\n ref,\n project,\n refType,\n } = parseGitilesUrlRef(config, url);\n const archiveName =\n filePath === '/' || filePath === '' ? '.tar.gz' : `/${filePath}.tar.gz`;\n if (refType === 'branch') {\n return `${getGitilesAuthenticationUrl(\n config,\n )}/${project}/+archive/refs/heads/${ref}${archiveName}`;\n }\n if (refType === 'sha') {\n return `${getGitilesAuthenticationUrl(\n config,\n )}/${project}/+archive/${ref}${archiveName}`;\n }\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n}\n\n/**\n * Return the authentication prefix.\n *\n * @remarks\n *\n * To authenticate with a password the API url must be prefixed with \"/a/\".\n * If no password is set anonymous access (without the prefix) will\n * be used.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getAuthenticationPrefix(\n config: GerritIntegrationConfig,\n): string {\n return config.password ? '/a/' : '/';\n}\n\n/**\n * Return the authentication gitiles url.\n *\n * @remarks\n *\n * To authenticate with a password the API url must be prefixed with \"/a/\".\n * If no password is set anonymous access (without the prefix) will\n * be used.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getGitilesAuthenticationUrl(\n config: GerritIntegrationConfig,\n): string {\n if (!config.baseUrl || !config.gitilesBaseUrl) {\n throw new Error(\n 'Unexpected Gerrit config values. baseUrl or gitilesBaseUrl not set.',\n );\n }\n if (config.gitilesBaseUrl.startsWith(config.baseUrl)) {\n return config.gitilesBaseUrl.replace(\n config.baseUrl.concat('/'),\n config.baseUrl.concat(getAuthenticationPrefix(config)),\n );\n }\n if (config.password) {\n throw new Error(\n 'Since the baseUrl (Gerrit) is not part of the gitilesBaseUrl, an authentication URL could not be constructed.',\n );\n }\n return config.gitilesBaseUrl!;\n}\n\n/**\n * Return the url to get branch info from the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritBranchApiUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { ref, refType, project } = parseGitilesUrlRef(config, url);\n\n if (refType !== 'branch') {\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n }\n\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(project)}/branches/${ref}`;\n}\n\n/**\n * Return the url to clone the repo that is referenced by the url.\n *\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritCloneRepoUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { project } = parseGitilesUrlRef(config, url);\n\n return `${config.cloneUrl}${getAuthenticationPrefix(config)}${project}`;\n}\n\n/**\n * Return the url to fetch the contents of a file using the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritFileContentsApiUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { ref, refType, path, project } = parseGitilesUrlRef(config, url);\n\n // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content\n if (refType === 'branch') {\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(\n project,\n )}/branches/${ref}/files/${encodeURIComponent(path)}/content`;\n }\n // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content-from-commit\n if (refType === 'sha') {\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(\n project,\n )}/commits/${ref}/files/${encodeURIComponent(path)}/content`;\n }\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n}\n\n/**\n * Return the url to query available projects using the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getGerritProjectsApiUrl(config: GerritIntegrationConfig) {\n return `${config.baseUrl}${getAuthenticationPrefix(config)}projects/`;\n}\n\n/**\n * Return request headers for a Gerrit provider.\n *\n * @param config - A Gerrit provider config\n * @public\n */\nexport function getGerritRequestOptions(config: GerritIntegrationConfig): {\n headers?: Record<string, string>;\n} {\n const headers: Record<string, string> = {};\n\n if (!config.password) {\n return headers;\n }\n const buffer = Buffer.from(`${config.username}:${config.password}`, 'utf8');\n headers.Authorization = `Basic ${buffer.toString('base64')}`;\n return {\n headers,\n };\n}\n\n/**\n * Parse the json response from Gerrit and strip the magic prefix.\n *\n * @remarks\n *\n * To prevent against XSSI attacks the JSON response body from Gerrit starts\n * with a magic prefix that must be stripped before it can be fed to a JSON\n * parser.\n *\n * @param response - An API response.\n * @public\n */\nexport async function parseGerritJsonResponse(\n response: Response,\n): Promise<unknown> {\n const responseBody = await response.text();\n if (responseBody.startsWith(GERRIT_BODY_PREFIX)) {\n try {\n return JSON.parse(responseBody.slice(GERRIT_BODY_PREFIX.length));\n } catch (ex) {\n throw new Error(\n `Invalid response from Gerrit: ${responseBody.slice(0, 10)} - ${ex}`,\n );\n }\n }\n throw new Error(\n `Gerrit JSON body prefix missing. Found: ${responseBody.slice(0, 10)}`,\n );\n}\n"],"names":["trimStart","takeWhile","join","trimEnd"],"mappings":";;;;AAkBA,MAAM,kBAAA,GAAqB,MAAA;AAqBpB,SAAS,kBAAA,CACd,QACA,GAAA,EAOA;AACA,EAAA,MAAM,YAAA,GAAe,IAAI,GAAA,CAAI,MAAA,CAAO,cAAe,CAAA;AACnD,EAAA,MAAM,QAAA,GAAW,IAAI,GAAA,CAAI,GAAG,CAAA;AAK5B,EAAA,MAAM,OAAA,GAAUA,gBAAA;AAAA,IACd,QAAA,CAAS,QAAA,CACN,SAAA,CAAU,QAAA,CAAS,SAAS,UAAA,CAAW,KAAK,CAAA,GAAI,CAAA,GAAI,CAAC,CAAA,CACrD,OAAA,CAAQ,YAAA,CAAa,UAAU,EAAE,CAAA;AAAA,IACpC;AAAA,GACF;AAGA,EAAA,MAAM,KAAA,GAAQ,QAAQ,KAAA,CAAM,GAAG,EAAE,MAAA,CAAO,CAAA,CAAA,KAAK,CAAC,CAAC,CAAC,CAAA;AAChD,EAAA,MAAM,YAAA,GAAeC,gBAAA,CAAU,KAAA,EAAO,CAAA,CAAA,KAAK,MAAM,GAAG,CAAA;AACpD,EAAA,IAAI,YAAA,CAAa,WAAW,CAAA,EAAG;AAC7B,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,6BAAA,EAAgC,GAAG,CAAA,CAAE,CAAA;AAAA,EACvD;AAEA,EAAA,MAAM,IAAA,GAAO,KAAA,CAAM,KAAA,CAAM,YAAA,CAAa,SAAS,CAAC,CAAA;AAChD,EAAA,MAAM,OAAA,GAAUC,WAAA,CAAK,YAAA,EAAc,GAAG,CAAA;AAGtC,EAAA,I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n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { join, takeWhile, trimEnd, trimStart } from 'lodash';\nimport { GerritIntegrationConfig } from './config';\n\nconst GERRIT_BODY_PREFIX = \")]}'\";\n\n/**\n * Parses Gitiles urls and returns the following:\n *\n * - The project\n * - The type of ref. I.e: branch name, SHA, HEAD or tag.\n * - The file path from the repo root.\n * - The base path as the path that points to the repo root.\n *\n * Supported types of gitiles urls that point to:\n *\n * - Branches\n * - Tags\n * - A commit SHA\n * - HEAD\n *\n * @param config - A Gerrit provider config.\n * @param url - An url to a file or folder in Gitiles.\n * @public\n */\nexport function parseGitilesUrlRef(\n config: GerritIntegrationConfig,\n url: string,\n): {\n project: string;\n path: string;\n ref: string;\n refType: 'sha' | 'branch' | 'tag' | 'head';\n basePath: string;\n} {\n const baseUrlParse = new URL(config.gitilesBaseUrl!);\n const urlParse = new URL(url);\n // Remove the gerrit authentication prefix '/a/' from the url\n // In case of the gitilesBaseUrl is https://review.gerrit.com/plugins/gitiles\n // and the url provided is https://review.gerrit.com/a/plugins/gitiles/...\n // remove the prefix only if the pathname start with '/a/'\n const urlPath = trimStart(\n urlParse.pathname\n .substring(urlParse.pathname.startsWith('/a/') ? 2 : 0)\n .replace(baseUrlParse.pathname, ''),\n '/',\n );\n\n // Find the project by taking everything up to \"/+/\".\n const parts = urlPath.split('/').filter(p => !!p);\n const projectParts = takeWhile(parts, p => p !== '+');\n if (projectParts.length === 0) {\n throw new Error(`Unable to parse gitiles url: ${url}`);\n }\n // Also remove the \"+\" after the project.\n const rest = parts.slice(projectParts.length + 1);\n const project = join(projectParts, '/');\n\n // match <project>/+/HEAD/<path>\n if (rest.length > 0 && rest[0] === 'HEAD') {\n const ref = rest.shift()!;\n const path = join(rest, '/');\n return {\n project,\n ref,\n refType: 'head' as const,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n // match <project>/+/<sha>/<path>\n if (rest.length > 0 && rest[0].length === 40) {\n const ref = rest.shift()!;\n const path = join(rest, '/');\n return {\n project,\n ref,\n refType: 'sha' as const,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n const remainingPath = join(rest, '/');\n // Regexp for matching \"refs/tags/<tag>\" or \"refs/heads/<branch>/\"\n const refsRegexp = /^refs\\/(?<refsReference>heads|tags)\\/(?<ref>.*?)(\\/|$)/;\n const result = refsRegexp.exec(remainingPath);\n if (result) {\n const matchString = result[0];\n let refType;\n const { refsReference, ref } = result.groups || {};\n const path = remainingPath.replace(matchString, '');\n switch (refsReference) {\n case 'heads':\n refType = 'branch' as const;\n break;\n case 'tags':\n refType = 'tag' as const;\n break;\n default:\n throw new Error(`Unable to parse gitiles url: ${url}`);\n }\n return {\n project,\n ref,\n refType,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n throw new Error(`Unable to parse gitiles : ${url}`);\n}\n\n/**\n * Build a Gerrit Gitiles url that targets a specific path.\n *\n * @param config - A Gerrit provider config.\n * @param project - The name of the git project\n * @param branch - The branch we will target.\n * @param filePath - The absolute file path.\n * @public\n */\nexport function buildGerritGitilesUrl(\n config: GerritIntegrationConfig,\n project: string,\n branch: string,\n filePath: string,\n): string {\n return `${\n config.gitilesBaseUrl\n }/${project}/+/refs/heads/${branch}/${trimStart(filePath, '/')}`;\n}\n\n/**\n * Build a Gerrit Gitiles url that targets a specific path.\n *\n * @param config - A Gerrit provider config.\n * @param project - The name of the git project\n * @param branch - The branch we will target.\n * @param filePath - The absolute file path.\n * @public\n */\nexport function buildGerritEditUrl(\n config: GerritIntegrationConfig,\n project: string,\n branch: string,\n filePath: string,\n): string {\n return `${\n config.baseUrl\n }/admin/repos/edit/repo/${project}/branch/refs/heads/${branch}/file/${trimStart(\n filePath,\n '/',\n )}`;\n}\n\n/**\n * Build a Gerrit Gitiles archive url from a Gitiles url.\n *\n * @param config - A Gerrit provider config.\n * @param url - The gitiles url\n * @public\n */\nexport function buildGerritGitilesArchiveUrlFromLocation(\n config: GerritIntegrationConfig,\n url: string,\n): string {\n const {\n path: filePath,\n ref,\n project,\n refType,\n } = parseGitilesUrlRef(config, url);\n const archiveName =\n filePath === '/' || filePath === '' ? '.tar.gz' : `/${filePath}.tar.gz`;\n if (refType === 'branch') {\n return `${getGitilesAuthenticationUrl(\n config,\n )}/${project}/+archive/refs/heads/${ref}${archiveName}`;\n }\n if (refType === 'sha') {\n return `${getGitilesAuthenticationUrl(\n config,\n )}/${project}/+archive/${ref}${archiveName}`;\n }\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n}\n\n/**\n * Return the authentication prefix.\n *\n * @remarks\n *\n * To authenticate with a password the API url must be prefixed with \"/a/\".\n * If no password is set anonymous access (without the prefix) will\n * be used.\n *\n * @param config - A Gerrit provider config.\n */\nfunction getAuthenticationPrefix(config: GerritIntegrationConfig): string {\n return config.password ? '/a/' : '/';\n}\n\n/**\n * Return the authentication gitiles url.\n *\n * @remarks\n *\n * To authenticate with a password the API url must be prefixed with \"/a/\".\n * If no password is set anonymous access (without the prefix) will\n * be used.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getGitilesAuthenticationUrl(\n config: GerritIntegrationConfig,\n): string {\n if (!config.baseUrl || !config.gitilesBaseUrl) {\n throw new Error(\n 'Unexpected Gerrit config values. baseUrl or gitilesBaseUrl not set.',\n );\n }\n if (config.gitilesBaseUrl.startsWith(config.baseUrl)) {\n return config.gitilesBaseUrl.replace(\n config.baseUrl.concat('/'),\n config.baseUrl.concat(getAuthenticationPrefix(config)),\n );\n }\n if (config.password) {\n throw new Error(\n 'Since the baseUrl (Gerrit) is not part of the gitilesBaseUrl, an authentication URL could not be constructed.',\n );\n }\n return config.gitilesBaseUrl!;\n}\n\n/**\n * Return the url to get branch info from the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritBranchApiUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { ref, refType, project } = parseGitilesUrlRef(config, url);\n\n if (refType !== 'branch') {\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n }\n\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(project)}/branches/${ref}`;\n}\n\n/**\n * Return the url to clone the repo that is referenced by the url.\n *\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritCloneRepoUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { project } = parseGitilesUrlRef(config, url);\n\n return `${config.cloneUrl}${getAuthenticationPrefix(config)}${project}`;\n}\n\n/**\n * Return the url to fetch the contents of a file using the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritFileContentsApiUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { ref, refType, path, project } = parseGitilesUrlRef(config, url);\n\n // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content\n if (refType === 'branch') {\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(\n project,\n )}/branches/${ref}/files/${encodeURIComponent(path)}/content`;\n }\n // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content-from-commit\n if (refType === 'sha') {\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(\n project,\n )}/commits/${ref}/files/${encodeURIComponent(path)}/content`;\n }\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n}\n\n/**\n * Return the url to query available projects using the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getGerritProjectsApiUrl(config: GerritIntegrationConfig) {\n return `${config.baseUrl}${getAuthenticationPrefix(config)}projects/`;\n}\n\n/**\n * Return request headers for a Gerrit provider.\n *\n * @param config - A Gerrit provider config\n * @public\n */\nexport function getGerritRequestOptions(config: GerritIntegrationConfig): {\n headers?: Record<string, string>;\n} {\n const headers: Record<string, string> = {};\n\n if (!config.password) {\n return headers;\n }\n const buffer = Buffer.from(`${config.username}:${config.password}`, 'utf8');\n headers.Authorization = `Basic ${buffer.toString('base64')}`;\n return {\n headers,\n };\n}\n\n/**\n * Parse the json response from Gerrit and strip the magic prefix.\n *\n * @remarks\n *\n * To prevent against XSSI attacks the JSON response body from Gerrit starts\n * with a magic prefix that must be stripped before it can be fed to a JSON\n * parser.\n *\n * @param response - An API response.\n * @public\n */\nexport async function parseGerritJsonResponse(\n response: Response,\n): Promise<unknown> {\n const responseBody = await response.text();\n if (responseBody.startsWith(GERRIT_BODY_PREFIX)) {\n try {\n return JSON.parse(responseBody.slice(GERRIT_BODY_PREFIX.length));\n } catch (ex) {\n throw new Error(\n `Invalid response from Gerrit: ${responseBody.slice(0, 10)} - ${ex}`,\n );\n }\n }\n throw new Error(\n `Gerrit JSON body prefix missing. Found: ${responseBody.slice(0, 10)}`,\n );\n}\n"],"names":["trimStart","takeWhile","join","trimEnd"],"mappings":";;;;AAkBA,MAAM,kBAAA,GAAqB,MAAA;AAqBpB,SAAS,kBAAA,CACd,QACA,GAAA,EAOA;AACA,EAAA,MAAM,YAAA,GAAe,IAAI,GAAA,CAAI,MAAA,CAAO,cAAe,CAAA;AACnD,EAAA,MAAM,QAAA,GAAW,IAAI,GAAA,CAAI,GAAG,CAAA;AAK5B,EAAA,MAAM,OAAA,GAAUA,gBAAA;AAAA,IACd,QAAA,CAAS,QAAA,CACN,SAAA,CAAU,QAAA,CAAS,SAAS,UAAA,CAAW,KAAK,CAAA,GAAI,CAAA,GAAI,CAAC,CAAA,CACrD,OAAA,CAAQ,YAAA,CAAa,UAAU,EAAE,CAAA;AAAA,IACpC;AAAA,GACF;AAGA,EAAA,M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AAI;AACF,MAAA,OAAO,KAAK,KAAA,CAAM,YAAA,CAAa,KAAA,CAAM,kBAAA,CAAmB,MAAM,CAAC,CAAA;AAAA,IACjE,SAAS,EAAA,EAAI;AACX,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,iCAAiC,YAAA,CAAa,KAAA,CAAM,GAAG,EAAE,CAAC,MAAM,EAAE,CAAA;AAAA,OACpE;AAAA,IACF;AAAA,EACF;AACA,EAAA,MAAM,IAAI,KAAA;AAAA,IACR,CAAA,wCAAA,EAA2C,YAAA,CAAa,KAAA,CAAM,CAAA,EAAG,EAAE,CAAC,CAAA;AAAA,GACtE;AACF;;;;;;;;;;;;;"}
@@ -174,5 +174,5 @@ async function parseGerritJsonResponse(response) {
174
174
  );
175
175
  }
176
176
 
177
- export { buildGerritEditUrl, buildGerritGitilesArchiveUrlFromLocation, getAuthenticationPrefix, getGerritBranchApiUrl, getGerritCloneRepoUrl, getGerritFileContentsApiUrl, getGerritProjectsApiUrl, getGerritRequestOptions, getGitilesAuthenticationUrl, parseGerritJsonResponse, parseGitilesUrlRef };
177
+ export { buildGerritEditUrl, buildGerritGitilesArchiveUrlFromLocation, getGerritBranchApiUrl, getGerritCloneRepoUrl, getGerritFileContentsApiUrl, getGerritProjectsApiUrl, getGerritRequestOptions, getGitilesAuthenticationUrl, parseGerritJsonResponse, parseGitilesUrlRef };
178
178
  //# sourceMappingURL=core.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"core.esm.js","sources":["../../src/gerrit/core.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { join, takeWhile, trimEnd, trimStart } from 'lodash';\nimport { GerritIntegrationConfig } from './config';\n\nconst GERRIT_BODY_PREFIX = \")]}'\";\n\n/**\n * Parses Gitiles urls and returns the following:\n *\n * - The project\n * - The type of ref. I.e: branch name, SHA, HEAD or tag.\n * - The file path from the repo root.\n * - The base path as the path that points to the repo root.\n *\n * Supported types of gitiles urls that point to:\n *\n * - Branches\n * - Tags\n * - A commit SHA\n * - HEAD\n *\n * @param config - A Gerrit provider config.\n * @param url - An url to a file or folder in Gitiles.\n * @public\n */\nexport function parseGitilesUrlRef(\n config: GerritIntegrationConfig,\n url: string,\n): {\n project: string;\n path: string;\n ref: string;\n refType: 'sha' | 'branch' | 'tag' | 'head';\n basePath: string;\n} {\n const baseUrlParse = new URL(config.gitilesBaseUrl!);\n const urlParse = new URL(url);\n // Remove the gerrit authentication prefix '/a/' from the url\n // In case of the gitilesBaseUrl is https://review.gerrit.com/plugins/gitiles\n // and the url provided is https://review.gerrit.com/a/plugins/gitiles/...\n // remove the prefix only if the pathname start with '/a/'\n const urlPath = trimStart(\n urlParse.pathname\n .substring(urlParse.pathname.startsWith('/a/') ? 2 : 0)\n .replace(baseUrlParse.pathname, ''),\n '/',\n );\n\n // Find the project by taking everything up to \"/+/\".\n const parts = urlPath.split('/').filter(p => !!p);\n const projectParts = takeWhile(parts, p => p !== '+');\n if (projectParts.length === 0) {\n throw new Error(`Unable to parse gitiles url: ${url}`);\n }\n // Also remove the \"+\" after the project.\n const rest = parts.slice(projectParts.length + 1);\n const project = join(projectParts, '/');\n\n // match <project>/+/HEAD/<path>\n if (rest.length > 0 && rest[0] === 'HEAD') {\n const ref = rest.shift()!;\n const path = join(rest, '/');\n return {\n project,\n ref,\n refType: 'head' as const,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n // match <project>/+/<sha>/<path>\n if (rest.length > 0 && rest[0].length === 40) {\n const ref = rest.shift()!;\n const path = join(rest, '/');\n return {\n project,\n ref,\n refType: 'sha' as const,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n const remainingPath = join(rest, '/');\n // Regexp for matching \"refs/tags/<tag>\" or \"refs/heads/<branch>/\"\n const refsRegexp = /^refs\\/(?<refsReference>heads|tags)\\/(?<ref>.*?)(\\/|$)/;\n const result = refsRegexp.exec(remainingPath);\n if (result) {\n const matchString = result[0];\n let refType;\n const { refsReference, ref } = result.groups || {};\n const path = remainingPath.replace(matchString, '');\n switch (refsReference) {\n case 'heads':\n refType = 'branch' as const;\n break;\n case 'tags':\n refType = 'tag' as const;\n break;\n default:\n throw new Error(`Unable to parse gitiles url: ${url}`);\n }\n return {\n project,\n ref,\n refType,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n throw new Error(`Unable to parse gitiles : ${url}`);\n}\n\n/**\n * Build a Gerrit Gitiles url that targets a specific path.\n *\n * @param config - A Gerrit provider config.\n * @param project - The name of the git project\n * @param branch - The branch we will target.\n * @param filePath - The absolute file path.\n * @public\n */\nexport function buildGerritGitilesUrl(\n config: GerritIntegrationConfig,\n project: string,\n branch: string,\n filePath: string,\n): string {\n return `${\n config.gitilesBaseUrl\n }/${project}/+/refs/heads/${branch}/${trimStart(filePath, '/')}`;\n}\n\n/**\n * Build a Gerrit Gitiles url that targets a specific path.\n *\n * @param config - A Gerrit provider config.\n * @param project - The name of the git project\n * @param branch - The branch we will target.\n * @param filePath - The absolute file path.\n * @public\n */\nexport function buildGerritEditUrl(\n config: GerritIntegrationConfig,\n project: string,\n branch: string,\n filePath: string,\n): string {\n return `${\n config.baseUrl\n }/admin/repos/edit/repo/${project}/branch/refs/heads/${branch}/file/${trimStart(\n filePath,\n '/',\n )}`;\n}\n\n/**\n * Build a Gerrit Gitiles archive url from a Gitiles url.\n *\n * @param config - A Gerrit provider config.\n * @param url - The gitiles url\n * @public\n */\nexport function buildGerritGitilesArchiveUrlFromLocation(\n config: GerritIntegrationConfig,\n url: string,\n): string {\n const {\n path: filePath,\n ref,\n project,\n refType,\n } = parseGitilesUrlRef(config, url);\n const archiveName =\n filePath === '/' || filePath === '' ? '.tar.gz' : `/${filePath}.tar.gz`;\n if (refType === 'branch') {\n return `${getGitilesAuthenticationUrl(\n config,\n )}/${project}/+archive/refs/heads/${ref}${archiveName}`;\n }\n if (refType === 'sha') {\n return `${getGitilesAuthenticationUrl(\n config,\n )}/${project}/+archive/${ref}${archiveName}`;\n }\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n}\n\n/**\n * Return the authentication prefix.\n *\n * @remarks\n *\n * To authenticate with a password the API url must be prefixed with \"/a/\".\n * If no password is set anonymous access (without the prefix) will\n * be used.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getAuthenticationPrefix(\n config: GerritIntegrationConfig,\n): string {\n return config.password ? '/a/' : '/';\n}\n\n/**\n * Return the authentication gitiles url.\n *\n * @remarks\n *\n * To authenticate with a password the API url must be prefixed with \"/a/\".\n * If no password is set anonymous access (without the prefix) will\n * be used.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getGitilesAuthenticationUrl(\n config: GerritIntegrationConfig,\n): string {\n if (!config.baseUrl || !config.gitilesBaseUrl) {\n throw new Error(\n 'Unexpected Gerrit config values. baseUrl or gitilesBaseUrl not set.',\n );\n }\n if (config.gitilesBaseUrl.startsWith(config.baseUrl)) {\n return config.gitilesBaseUrl.replace(\n config.baseUrl.concat('/'),\n config.baseUrl.concat(getAuthenticationPrefix(config)),\n );\n }\n if (config.password) {\n throw new Error(\n 'Since the baseUrl (Gerrit) is not part of the gitilesBaseUrl, an authentication URL could not be constructed.',\n );\n }\n return config.gitilesBaseUrl!;\n}\n\n/**\n * Return the url to get branch info from the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritBranchApiUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { ref, refType, project } = parseGitilesUrlRef(config, url);\n\n if (refType !== 'branch') {\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n }\n\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(project)}/branches/${ref}`;\n}\n\n/**\n * Return the url to clone the repo that is referenced by the url.\n *\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritCloneRepoUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { project } = parseGitilesUrlRef(config, url);\n\n return `${config.cloneUrl}${getAuthenticationPrefix(config)}${project}`;\n}\n\n/**\n * Return the url to fetch the contents of a file using the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritFileContentsApiUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { ref, refType, path, project } = parseGitilesUrlRef(config, url);\n\n // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content\n if (refType === 'branch') {\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(\n project,\n )}/branches/${ref}/files/${encodeURIComponent(path)}/content`;\n }\n // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content-from-commit\n if (refType === 'sha') {\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(\n project,\n )}/commits/${ref}/files/${encodeURIComponent(path)}/content`;\n }\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n}\n\n/**\n * Return the url to query available projects using the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getGerritProjectsApiUrl(config: GerritIntegrationConfig) {\n return `${config.baseUrl}${getAuthenticationPrefix(config)}projects/`;\n}\n\n/**\n * Return request headers for a Gerrit provider.\n *\n * @param config - A Gerrit provider config\n * @public\n */\nexport function getGerritRequestOptions(config: GerritIntegrationConfig): {\n headers?: Record<string, string>;\n} {\n const headers: Record<string, string> = {};\n\n if (!config.password) {\n return headers;\n }\n const buffer = Buffer.from(`${config.username}:${config.password}`, 'utf8');\n headers.Authorization = `Basic ${buffer.toString('base64')}`;\n return {\n headers,\n };\n}\n\n/**\n * Parse the json response from Gerrit and strip the magic prefix.\n *\n * @remarks\n *\n * To prevent against XSSI attacks the JSON response body from Gerrit starts\n * with a magic prefix that must be stripped before it can be fed to a JSON\n * parser.\n *\n * @param response - An API response.\n * @public\n */\nexport async function parseGerritJsonResponse(\n response: Response,\n): Promise<unknown> {\n const responseBody = await response.text();\n if (responseBody.startsWith(GERRIT_BODY_PREFIX)) {\n try {\n return JSON.parse(responseBody.slice(GERRIT_BODY_PREFIX.length));\n } catch (ex) {\n throw new Error(\n `Invalid response from Gerrit: ${responseBody.slice(0, 10)} - ${ex}`,\n );\n }\n }\n throw new Error(\n `Gerrit JSON body prefix missing. Found: ${responseBody.slice(0, 10)}`,\n );\n}\n"],"names":[],"mappings":";;AAkBA,MAAM,kBAAA,GAAqB,MAAA;AAqBpB,SAAS,kBAAA,CACd,QACA,GAAA,EAOA;AACA,EAAA,MAAM,YAAA,GAAe,IAAI,GAAA,CAAI,MAAA,CAAO,cAAe,CAAA;AACnD,EAAA,MAAM,QAAA,GAAW,IAAI,GAAA,CAAI,GAAG,CAAA;AAK5B,EAAA,MAAM,OAAA,GAAU,SAAA;AAAA,IACd,QAAA,CAAS,QAAA,CACN,SAAA,CAAU,QAAA,CAAS,SAAS,UAAA,CAAW,KAAK,CAAA,GAAI,CAAA,GAAI,CAAC,CAAA,CACrD,OAAA,CAAQ,YAAA,CAAa,UAAU,EAAE,CAAA;AAAA,IACpC;AAAA,GACF;AAGA,EAAA,MAAM,KAAA,GAAQ,QAAQ,KAAA,CAAM,GAAG,EAAE,MAAA,CAAO,CAAA,CAAA,KAAK,CAAC,CAAC,CAAC,CAAA;AAChD,EAAA,MAAM,YAAA,GAAe,SAAA,CAAU,KAAA,EAAO,CAAA,CAAA,KAAK,MAAM,GAAG,CAAA;AACpD,EAAA,IAAI,YAAA,CAAa,WAAW,CAAA,EAAG;AAC7B,IAAA,MAAM,IAAI,K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IAAI,KAAA;AAAA,IACR,CAAA,wCAAA,EAA2C,YAAA,CAAa,KAAA,CAAM,CAAA,EAAG,EAAE,CAAC,CAAA;AAAA,GACtE;AACF;;;;"}
1
+ {"version":3,"file":"core.esm.js","sources":["../../src/gerrit/core.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { join, takeWhile, trimEnd, trimStart } from 'lodash';\nimport { GerritIntegrationConfig } from './config';\n\nconst GERRIT_BODY_PREFIX = \")]}'\";\n\n/**\n * Parses Gitiles urls and returns the following:\n *\n * - The project\n * - The type of ref. I.e: branch name, SHA, HEAD or tag.\n * - The file path from the repo root.\n * - The base path as the path that points to the repo root.\n *\n * Supported types of gitiles urls that point to:\n *\n * - Branches\n * - Tags\n * - A commit SHA\n * - HEAD\n *\n * @param config - A Gerrit provider config.\n * @param url - An url to a file or folder in Gitiles.\n * @public\n */\nexport function parseGitilesUrlRef(\n config: GerritIntegrationConfig,\n url: string,\n): {\n project: string;\n path: string;\n ref: string;\n refType: 'sha' | 'branch' | 'tag' | 'head';\n basePath: string;\n} {\n const baseUrlParse = new URL(config.gitilesBaseUrl!);\n const urlParse = new URL(url);\n // Remove the gerrit authentication prefix '/a/' from the url\n // In case of the gitilesBaseUrl is https://review.gerrit.com/plugins/gitiles\n // and the url provided is https://review.gerrit.com/a/plugins/gitiles/...\n // remove the prefix only if the pathname start with '/a/'\n const urlPath = trimStart(\n urlParse.pathname\n .substring(urlParse.pathname.startsWith('/a/') ? 2 : 0)\n .replace(baseUrlParse.pathname, ''),\n '/',\n );\n\n // Find the project by taking everything up to \"/+/\".\n const parts = urlPath.split('/').filter(p => !!p);\n const projectParts = takeWhile(parts, p => p !== '+');\n if (projectParts.length === 0) {\n throw new Error(`Unable to parse gitiles url: ${url}`);\n }\n // Also remove the \"+\" after the project.\n const rest = parts.slice(projectParts.length + 1);\n const project = join(projectParts, '/');\n\n // match <project>/+/HEAD/<path>\n if (rest.length > 0 && rest[0] === 'HEAD') {\n const ref = rest.shift()!;\n const path = join(rest, '/');\n return {\n project,\n ref,\n refType: 'head' as const,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n // match <project>/+/<sha>/<path>\n if (rest.length > 0 && rest[0].length === 40) {\n const ref = rest.shift()!;\n const path = join(rest, '/');\n return {\n project,\n ref,\n refType: 'sha' as const,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n const remainingPath = join(rest, '/');\n // Regexp for matching \"refs/tags/<tag>\" or \"refs/heads/<branch>/\"\n const refsRegexp = /^refs\\/(?<refsReference>heads|tags)\\/(?<ref>.*?)(\\/|$)/;\n const result = refsRegexp.exec(remainingPath);\n if (result) {\n const matchString = result[0];\n let refType;\n const { refsReference, ref } = result.groups || {};\n const path = remainingPath.replace(matchString, '');\n switch (refsReference) {\n case 'heads':\n refType = 'branch' as const;\n break;\n case 'tags':\n refType = 'tag' as const;\n break;\n default:\n throw new Error(`Unable to parse gitiles url: ${url}`);\n }\n return {\n project,\n ref,\n refType,\n path: path || '/',\n basePath: trimEnd(url.replace(path, ''), '/'),\n };\n }\n throw new Error(`Unable to parse gitiles : ${url}`);\n}\n\n/**\n * Build a Gerrit Gitiles url that targets a specific path.\n *\n * @param config - A Gerrit provider config.\n * @param project - The name of the git project\n * @param branch - The branch we will target.\n * @param filePath - The absolute file path.\n * @public\n */\nexport function buildGerritGitilesUrl(\n config: GerritIntegrationConfig,\n project: string,\n branch: string,\n filePath: string,\n): string {\n return `${\n config.gitilesBaseUrl\n }/${project}/+/refs/heads/${branch}/${trimStart(filePath, '/')}`;\n}\n\n/**\n * Build a Gerrit Gitiles url that targets a specific path.\n *\n * @param config - A Gerrit provider config.\n * @param project - The name of the git project\n * @param branch - The branch we will target.\n * @param filePath - The absolute file path.\n * @public\n */\nexport function buildGerritEditUrl(\n config: GerritIntegrationConfig,\n project: string,\n branch: string,\n filePath: string,\n): string {\n return `${\n config.baseUrl\n }/admin/repos/edit/repo/${project}/branch/refs/heads/${branch}/file/${trimStart(\n filePath,\n '/',\n )}`;\n}\n\n/**\n * Build a Gerrit Gitiles archive url from a Gitiles url.\n *\n * @param config - A Gerrit provider config.\n * @param url - The gitiles url\n * @public\n */\nexport function buildGerritGitilesArchiveUrlFromLocation(\n config: GerritIntegrationConfig,\n url: string,\n): string {\n const {\n path: filePath,\n ref,\n project,\n refType,\n } = parseGitilesUrlRef(config, url);\n const archiveName =\n filePath === '/' || filePath === '' ? '.tar.gz' : `/${filePath}.tar.gz`;\n if (refType === 'branch') {\n return `${getGitilesAuthenticationUrl(\n config,\n )}/${project}/+archive/refs/heads/${ref}${archiveName}`;\n }\n if (refType === 'sha') {\n return `${getGitilesAuthenticationUrl(\n config,\n )}/${project}/+archive/${ref}${archiveName}`;\n }\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n}\n\n/**\n * Return the authentication prefix.\n *\n * @remarks\n *\n * To authenticate with a password the API url must be prefixed with \"/a/\".\n * If no password is set anonymous access (without the prefix) will\n * be used.\n *\n * @param config - A Gerrit provider config.\n */\nfunction getAuthenticationPrefix(config: GerritIntegrationConfig): string {\n return config.password ? '/a/' : '/';\n}\n\n/**\n * Return the authentication gitiles url.\n *\n * @remarks\n *\n * To authenticate with a password the API url must be prefixed with \"/a/\".\n * If no password is set anonymous access (without the prefix) will\n * be used.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getGitilesAuthenticationUrl(\n config: GerritIntegrationConfig,\n): string {\n if (!config.baseUrl || !config.gitilesBaseUrl) {\n throw new Error(\n 'Unexpected Gerrit config values. baseUrl or gitilesBaseUrl not set.',\n );\n }\n if (config.gitilesBaseUrl.startsWith(config.baseUrl)) {\n return config.gitilesBaseUrl.replace(\n config.baseUrl.concat('/'),\n config.baseUrl.concat(getAuthenticationPrefix(config)),\n );\n }\n if (config.password) {\n throw new Error(\n 'Since the baseUrl (Gerrit) is not part of the gitilesBaseUrl, an authentication URL could not be constructed.',\n );\n }\n return config.gitilesBaseUrl!;\n}\n\n/**\n * Return the url to get branch info from the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritBranchApiUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { ref, refType, project } = parseGitilesUrlRef(config, url);\n\n if (refType !== 'branch') {\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n }\n\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(project)}/branches/${ref}`;\n}\n\n/**\n * Return the url to clone the repo that is referenced by the url.\n *\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritCloneRepoUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { project } = parseGitilesUrlRef(config, url);\n\n return `${config.cloneUrl}${getAuthenticationPrefix(config)}${project}`;\n}\n\n/**\n * Return the url to fetch the contents of a file using the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @param url - An url pointing to a file in git.\n * @public\n */\nexport function getGerritFileContentsApiUrl(\n config: GerritIntegrationConfig,\n url: string,\n) {\n const { ref, refType, path, project } = parseGitilesUrlRef(config, url);\n\n // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content\n if (refType === 'branch') {\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(\n project,\n )}/branches/${ref}/files/${encodeURIComponent(path)}/content`;\n }\n // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-content-from-commit\n if (refType === 'sha') {\n return `${config.baseUrl}${getAuthenticationPrefix(\n config,\n )}projects/${encodeURIComponent(\n project,\n )}/commits/${ref}/files/${encodeURIComponent(path)}/content`;\n }\n throw new Error(`Unsupported gitiles ref type: ${refType}`);\n}\n\n/**\n * Return the url to query available projects using the Gerrit API.\n *\n * @param config - A Gerrit provider config.\n * @public\n */\nexport function getGerritProjectsApiUrl(config: GerritIntegrationConfig) {\n return `${config.baseUrl}${getAuthenticationPrefix(config)}projects/`;\n}\n\n/**\n * Return request headers for a Gerrit provider.\n *\n * @param config - A Gerrit provider config\n * @public\n */\nexport function getGerritRequestOptions(config: GerritIntegrationConfig): {\n headers?: Record<string, string>;\n} {\n const headers: Record<string, string> = {};\n\n if (!config.password) {\n return headers;\n }\n const buffer = Buffer.from(`${config.username}:${config.password}`, 'utf8');\n headers.Authorization = `Basic ${buffer.toString('base64')}`;\n return {\n headers,\n };\n}\n\n/**\n * Parse the json response from Gerrit and strip the magic prefix.\n *\n * @remarks\n *\n * To prevent against XSSI attacks the JSON response body from Gerrit starts\n * with a magic prefix that must be stripped before it can be fed to a JSON\n * parser.\n *\n * @param response - An API response.\n * @public\n */\nexport async function parseGerritJsonResponse(\n response: Response,\n): Promise<unknown> {\n const responseBody = await response.text();\n if (responseBody.startsWith(GERRIT_BODY_PREFIX)) {\n try {\n return JSON.parse(responseBody.slice(GERRIT_BODY_PREFIX.length));\n } catch (ex) {\n throw new Error(\n `Invalid response from Gerrit: ${responseBody.slice(0, 10)} - ${ex}`,\n );\n }\n }\n throw new Error(\n `Gerrit JSON body prefix missing. Found: ${responseBody.slice(0, 10)}`,\n );\n}\n"],"names":[],"mappings":";;AAkBA,MAAM,kBAAA,GAAqB,MAAA;AAqBpB,SAAS,kBAAA,CACd,QACA,GAAA,EAOA;AACA,EAAA,MAAM,YAAA,GAAe,IAAI,GAAA,CAAI,MAAA,CAAO,cAAe,CAAA;AACnD,EAAA,MAAM,QAAA,GAAW,IAAI,GAAA,CAAI,GAAG,CAAA;AAK5B,EAAA,MAAM,OAAA,GAAU,SAAA;AAAA,IACd,QAAA,CAA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;AAcA,eAAsB,wBACpB,QAAA,EACkB;AAClB,EAAA,MAAM,YAAA,GAAe,MAAM,QAAA,CAAS,IAAA,EAAK;AACzC,EAAA,IAAI,YAAA,CAAa,UAAA,CAAW,kBAAkB,CAAA,EAAG;AAC/C,IAAA,IAAI;AACF,MAAA,OAAO,KAAK,KAAA,CAAM,YAAA,CAAa,KAAA,CAAM,kBAAA,CAAmB,MAAM,CAAC,CAAA;AAAA,IACjE,SAAS,EAAA,EAAI;AACX,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,iCAAiC,YAAA,CAAa,KAAA,CAAM,GAAG,EAAE,CAAC,MAAM,EAAE,CAAA;AAAA,OACpE;AAAA,IACF;AAAA,EACF;AACA,EAAA,MAAM,IAAI,KAAA;AAAA,IACR,CAAA,wCAAA,EAA2C,YAAA,CAAa,KAAA,CAAM,CAAA,EAAG,EAAE,CAAC,CAAA;AAAA,GACtE;AACF;;;;"}
@@ -26,6 +26,5 @@ function chooseEndpoint(config, credentials) {
26
26
  return "raw";
27
27
  }
28
28
 
29
- exports.chooseEndpoint = chooseEndpoint;
30
29
  exports.getGithubFileFetchUrl = getGithubFileFetchUrl;
31
30
  //# sourceMappingURL=core.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"core.cjs.js","sources":["../../src/github/core.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { GithubIntegrationConfig } from './config';\nimport { parseGitUrlSafe } from '../helpers';\nimport { GithubCredentials } from './types';\n\n/**\n * Given a URL pointing to a file on a provider, returns a URL that is suitable\n * for fetching the contents of the data.\n *\n * @remarks\n *\n * Converts\n * from: https://github.com/a/b/blob/branchname/path/to/c.yaml\n * to: https://api.github.com/repos/a/b/contents/path/to/c.yaml?ref=branchname\n * or: https://raw.githubusercontent.com/a/b/branchname/c.yaml\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @public\n */\nexport function getGithubFileFetchUrl(\n url: string,\n config: GithubIntegrationConfig,\n credentials: GithubCredentials,\n): string {\n try {\n const { owner, name, ref, filepathtype, filepath } = parseGitUrlSafe(url);\n if (\n !owner ||\n !name ||\n !ref ||\n // GitHub is automatically redirecting tree urls to blob urls so it's\n // fine to pass a tree url.\n (filepathtype !== 'blob' &&\n filepathtype !== 'raw' &&\n filepathtype !== 'tree')\n ) {\n throw new Error('Invalid GitHub URL or file path');\n }\n\n const pathWithoutSlash = filepath.replace(/^\\//, '');\n if (chooseEndpoint(config, credentials) === 'api') {\n return `${config.apiBaseUrl}/repos/${owner}/${name}/contents/${pathWithoutSlash}?ref=${ref}`;\n }\n return `${config.rawBaseUrl}/${owner}/${name}/${ref}/${pathWithoutSlash}`;\n } catch (e) {\n throw new Error(`Incorrect URL: ${url}, ${e}`);\n }\n}\n\nexport function chooseEndpoint(\n config: GithubIntegrationConfig,\n credentials: GithubCredentials,\n): 'api' | 'raw' {\n if (config.apiBaseUrl && (credentials.token || !config.rawBaseUrl)) {\n return 'api';\n }\n return 'raw';\n}\n"],"names":["parseGitUrlSafe"],"mappings":";;;;AAmCO,SAAS,qBAAA,CACd,GAAA,EACA,MAAA,EACA,WAAA,EACQ;AACR,EAAA,IAAI;AACF,IAAA,MAAM,EAAE,OAAO,IAAA,EAAM,GAAA,EAAK,cAAc,QAAA,EAAS,GAAIA,wBAAgB,GAAG,CAAA;AACxE,IAAA,IACE,CAAC,KAAA,IACD,CAAC,IAAA,IACD,CAAC,GAAA;AAAA;AAAA,IAGA,YAAA,KAAiB,MAAA,IAChB,YAAA,KAAiB,KAAA,IACjB,iBAAiB,MAAA,EACnB;AACA,MAAA,MAAM,IAAI,MAAM,iCAAiC,CAAA;AAAA,IACnD;AAEA,IAAA,MAAM,gBAAA,GAAmB,QAAA,CAAS,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AACnD,IAAA,IAAI,cAAA,CAAe,MAAA,EAAQ,WAAW,CAAA,KAAM,KAAA,EAAO;AACjD,MAAA,OAAO,CAAA,EAAG,MAAA,CAAO,UAAU,CAAA,OAAA,EAAU,KAAK,IAAI,IAAI,CAAA,UAAA,EAAa,gBAAgB,CAAA,KAAA,EAAQ,GAAG,CAAA,CAAA;AAAA,IAC5F;AACA,IAAA,OAAO,CAAA,EAAG,MAAA,CAAO,UAAU,CAAA,CAAA,EAAI,KAAK,IAAI,IAAI,CAAA,CAAA,EAAI,GAAG,CAAA,CAAA,EAAI,gBAAgB,CAAA,CAAA;AAAA,EACzE,SAAS,CAAA,EAAG;AACV,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,eAAA,EAAkB,GAAG,CAAA,EAAA,EAAK,CAAC,CAAA,CAAE,CAAA;AAAA,EAC/C;AACF;AAEO,SAAS,cAAA,CACd,QACA,WAAA,EACe;AACf,EAAA,IAAI,OAAO,UAAA,KAAe,WAAA,CAAY,KAAA,IAAS,CAAC,OAAO,UAAA,CAAA,EAAa;AAClE,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,KAAA;AACT;;;;;"}
1
+ {"version":3,"file":"core.cjs.js","sources":["../../src/github/core.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { GithubIntegrationConfig } from './config';\nimport { parseGitUrlSafe } from '../helpers';\nimport { GithubCredentials } from './types';\n\n/**\n * Given a URL pointing to a file on a provider, returns a URL that is suitable\n * for fetching the contents of the data.\n *\n * @remarks\n *\n * Converts\n * from: https://github.com/a/b/blob/branchname/path/to/c.yaml\n * to: https://api.github.com/repos/a/b/contents/path/to/c.yaml?ref=branchname\n * or: https://raw.githubusercontent.com/a/b/branchname/c.yaml\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @public\n */\nexport function getGithubFileFetchUrl(\n url: string,\n config: GithubIntegrationConfig,\n credentials: GithubCredentials,\n): string {\n try {\n const { owner, name, ref, filepathtype, filepath } = parseGitUrlSafe(url);\n if (\n !owner ||\n !name ||\n !ref ||\n // GitHub is automatically redirecting tree urls to blob urls so it's\n // fine to pass a tree url.\n (filepathtype !== 'blob' &&\n filepathtype !== 'raw' &&\n filepathtype !== 'tree')\n ) {\n throw new Error('Invalid GitHub URL or file path');\n }\n\n const pathWithoutSlash = filepath.replace(/^\\//, '');\n if (chooseEndpoint(config, credentials) === 'api') {\n return `${config.apiBaseUrl}/repos/${owner}/${name}/contents/${pathWithoutSlash}?ref=${ref}`;\n }\n return `${config.rawBaseUrl}/${owner}/${name}/${ref}/${pathWithoutSlash}`;\n } catch (e) {\n throw new Error(`Incorrect URL: ${url}, ${e}`);\n }\n}\n\nfunction chooseEndpoint(\n config: GithubIntegrationConfig,\n credentials: GithubCredentials,\n): 'api' | 'raw' {\n if (config.apiBaseUrl && (credentials.token || !config.rawBaseUrl)) {\n return 'api';\n }\n return 'raw';\n}\n"],"names":["parseGitUrlSafe"],"mappings":";;;;AAmCO,SAAS,qBAAA,CACd,GAAA,EACA,MAAA,EACA,WAAA,EACQ;AACR,EAAA,IAAI;AACF,IAAA,MAAM,EAAE,OAAO,IAAA,EAAM,GAAA,EAAK,cAAc,QAAA,EAAS,GAAIA,wBAAgB,GAAG,CAAA;AACxE,IAAA,IACE,CAAC,KAAA,IACD,CAAC,IAAA,IACD,CAAC,GAAA;AAAA;AAAA,IAGA,YAAA,KAAiB,MAAA,IAChB,YAAA,KAAiB,KAAA,IACjB,iBAAiB,MAAA,EACnB;AACA,MAAA,MAAM,IAAI,MAAM,iCAAiC,CAAA;AAAA,IACnD;AAEA,IAAA,MAAM,gBAAA,GAAmB,QAAA,CAAS,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AACnD,IAAA,IAAI,cAAA,CAAe,MAAA,EAAQ,WAAW,CAAA,KAAM,KAAA,EAAO;AACjD,MAAA,OAAO,CAAA,EAAG,MAAA,CAAO,UAAU,CAAA,OAAA,EAAU,KAAK,IAAI,IAAI,CAAA,UAAA,EAAa,gBAAgB,CAAA,KAAA,EAAQ,GAAG,CAAA,CAAA;AAAA,IAC5F;AACA,IAAA,OAAO,CAAA,EAAG,MAAA,CAAO,UAAU,CAAA,CAAA,EAAI,KAAK,IAAI,IAAI,CAAA,CAAA,EAAI,GAAG,CAAA,CAAA,EAAI,gBAAgB,CAAA,CAAA;AAAA,EACzE,SAAS,CAAA,EAAG;AACV,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,eAAA,EAAkB,GAAG,CAAA,EAAA,EAAK,CAAC,CAAA,CAAE,CAAA;AAAA,EAC/C;AACF;AAEA,SAAS,cAAA,CACP,QACA,WAAA,EACe;AACf,EAAA,IAAI,OAAO,UAAA,KAAe,WAAA,CAAY,KAAA,IAAS,CAAC,OAAO,UAAA,CAAA,EAAa;AAClE,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,KAAA;AACT;;;;"}
@@ -24,5 +24,5 @@ function chooseEndpoint(config, credentials) {
24
24
  return "raw";
25
25
  }
26
26
 
27
- export { chooseEndpoint, getGithubFileFetchUrl };
27
+ export { getGithubFileFetchUrl };
28
28
  //# sourceMappingURL=core.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"core.esm.js","sources":["../../src/github/core.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { GithubIntegrationConfig } from './config';\nimport { parseGitUrlSafe } from '../helpers';\nimport { GithubCredentials } from './types';\n\n/**\n * Given a URL pointing to a file on a provider, returns a URL that is suitable\n * for fetching the contents of the data.\n *\n * @remarks\n *\n * Converts\n * from: https://github.com/a/b/blob/branchname/path/to/c.yaml\n * to: https://api.github.com/repos/a/b/contents/path/to/c.yaml?ref=branchname\n * or: https://raw.githubusercontent.com/a/b/branchname/c.yaml\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @public\n */\nexport function getGithubFileFetchUrl(\n url: string,\n config: GithubIntegrationConfig,\n credentials: GithubCredentials,\n): string {\n try {\n const { owner, name, ref, filepathtype, filepath } = parseGitUrlSafe(url);\n if (\n !owner ||\n !name ||\n !ref ||\n // GitHub is automatically redirecting tree urls to blob urls so it's\n // fine to pass a tree url.\n (filepathtype !== 'blob' &&\n filepathtype !== 'raw' &&\n filepathtype !== 'tree')\n ) {\n throw new Error('Invalid GitHub URL or file path');\n }\n\n const pathWithoutSlash = filepath.replace(/^\\//, '');\n if (chooseEndpoint(config, credentials) === 'api') {\n return `${config.apiBaseUrl}/repos/${owner}/${name}/contents/${pathWithoutSlash}?ref=${ref}`;\n }\n return `${config.rawBaseUrl}/${owner}/${name}/${ref}/${pathWithoutSlash}`;\n } catch (e) {\n throw new Error(`Incorrect URL: ${url}, ${e}`);\n }\n}\n\nexport function chooseEndpoint(\n config: GithubIntegrationConfig,\n credentials: GithubCredentials,\n): 'api' | 'raw' {\n if (config.apiBaseUrl && (credentials.token || !config.rawBaseUrl)) {\n return 'api';\n }\n return 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":"core.esm.js","sources":["../../src/github/core.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { GithubIntegrationConfig } from './config';\nimport { parseGitUrlSafe } from '../helpers';\nimport { GithubCredentials } from './types';\n\n/**\n * Given a URL pointing to a file on a provider, returns a URL that is suitable\n * for fetching the contents of the data.\n *\n * @remarks\n *\n * Converts\n * from: https://github.com/a/b/blob/branchname/path/to/c.yaml\n * to: https://api.github.com/repos/a/b/contents/path/to/c.yaml?ref=branchname\n * or: https://raw.githubusercontent.com/a/b/branchname/c.yaml\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @public\n */\nexport function getGithubFileFetchUrl(\n url: string,\n config: GithubIntegrationConfig,\n credentials: GithubCredentials,\n): string {\n try {\n const { owner, name, ref, filepathtype, filepath } = parseGitUrlSafe(url);\n if (\n !owner ||\n !name ||\n !ref ||\n // GitHub is automatically redirecting tree urls to blob urls so it's\n // fine to pass a tree url.\n (filepathtype !== 'blob' &&\n filepathtype !== 'raw' &&\n filepathtype !== 'tree')\n ) {\n throw new Error('Invalid GitHub URL or file path');\n }\n\n const pathWithoutSlash = filepath.replace(/^\\//, '');\n if (chooseEndpoint(config, credentials) === 'api') {\n return `${config.apiBaseUrl}/repos/${owner}/${name}/contents/${pathWithoutSlash}?ref=${ref}`;\n }\n return `${config.rawBaseUrl}/${owner}/${name}/${ref}/${pathWithoutSlash}`;\n } catch (e) {\n throw new Error(`Incorrect URL: ${url}, ${e}`);\n }\n}\n\nfunction chooseEndpoint(\n config: GithubIntegrationConfig,\n credentials: GithubC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ClE,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,KAAA;AACT;;;;"}
@@ -42,9 +42,7 @@ class GitLabIntegration {
42
42
  return this.fetchImpl(input, init);
43
43
  }
44
44
  createFetchStrategy() {
45
- let fetchFn = async (url, options) => {
46
- return fetch(url, { ...options, mode: "same-origin" });
47
- };
45
+ let fetchFn = (url, options) => fetch(url, options);
48
46
  const retryConfig = this.integrationConfig.retry;
49
47
  if (retryConfig) {
50
48
  fetchFn = this.withRetry(fetchFn, retryConfig);
@@ -63,26 +61,55 @@ class GitLabIntegration {
63
61
  if (maxRetries <= 0 || retryStatusCodes.length === 0) {
64
62
  return fetchFn;
65
63
  }
64
+ const backoffDelay = (a) => Math.min(100 * Math.pow(2, a - 1), 1e4);
66
65
  return async (url, options) => {
67
66
  const abortSignal = options?.signal;
68
- let response;
69
67
  let attempt = 0;
70
68
  for (; ; ) {
71
- response = await fetchFn(url, options);
69
+ let response;
70
+ try {
71
+ response = await fetchFn(url, options);
72
+ } catch (e) {
73
+ if (abortSignal?.aborted) throw e;
74
+ if (attempt++ >= maxRetries) throw e;
75
+ await sleep(backoffDelay(attempt), abortSignal);
76
+ if (abortSignal?.aborted) throw e;
77
+ continue;
78
+ }
72
79
  if (!retryStatusCodes.includes(response.status)) {
73
- break;
80
+ return response;
74
81
  }
75
82
  if (attempt++ >= maxRetries) {
76
- break;
83
+ return response;
77
84
  }
78
- const retryAfter = response.headers.get("Retry-After");
79
- const delay = retryAfter ? parseInt(retryAfter, 10) * 1e3 : Math.min(100 * Math.pow(2, attempt - 1), 1e4);
85
+ const delay = parseRetryAfterMs(
86
+ response.headers.get("Retry-After"),
87
+ backoffDelay(attempt)
88
+ );
89
+ await response.body?.cancel().catch(() => {
90
+ });
80
91
  await sleep(delay, abortSignal);
92
+ if (abortSignal?.aborted) return response;
81
93
  }
82
- return response;
83
94
  };
84
95
  }
85
96
  }
97
+ function parseRetryAfterMs(headerValue, fallbackMs) {
98
+ if (!headerValue) {
99
+ return fallbackMs;
100
+ }
101
+ if (/^\d+$/.test(headerValue)) {
102
+ return Number(headerValue) * 1e3;
103
+ }
104
+ if (headerValue.includes(",")) {
105
+ const dateMs = Date.parse(headerValue);
106
+ if (Number.isFinite(dateMs)) {
107
+ const deltaMs = dateMs - Date.now();
108
+ return deltaMs > 0 ? deltaMs : 0;
109
+ }
110
+ }
111
+ return fallbackMs;
112
+ }
86
113
  async function sleep(durationMs, abortSignal) {
87
114
  if (abortSignal?.aborted) {
88
115
  return;
@@ -105,6 +132,7 @@ function replaceGitLabUrlType(url, type) {
105
132
  }
106
133
 
107
134
  exports.GitLabIntegration = GitLabIntegration;
135
+ exports.parseRetryAfterMs = parseRetryAfterMs;
108
136
  exports.replaceGitLabUrlType = replaceGitLabUrlType;
109
137
  exports.sleep = sleep;
110
138
  //# sourceMappingURL=GitLabIntegration.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"GitLabIntegration.cjs.js","sources":["../../src/gitlab/GitLabIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\nimport pThrottle from 'p-throttle';\n\ntype FetchFunction = typeof fetch;\n\n/**\n * A GitLab based integration.\n *\n * @public\n */\nexport class GitLabIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<GitLabIntegration> = ({ config }) => {\n const configs = readGitLabIntegrationConfigs(\n config.getOptionalConfigArray('integrations.gitlab') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new GitLabIntegration(c)),\n i => i.config.host,\n );\n };\n\n private readonly fetchImpl: FetchFunction;\n\n constructor(private readonly integrationConfig: GitLabIntegrationConfig) {\n // Configure fetch strategy based on configuration\n this.fetchImpl = this.createFetchStrategy();\n }\n\n get type(): string {\n return 'gitlab';\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n get config(): GitLabIntegrationConfig {\n return this.integrationConfig;\n }\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n }): string {\n return defaultScmResolveUrl(options);\n }\n\n resolveEditUrl(url: string): string {\n return replaceGitLabUrlType(url, 'edit');\n }\n\n fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {\n return this.fetchImpl(input, init);\n }\n\n private createFetchStrategy(): FetchFunction {\n let fetchFn: FetchFunction = async (url, options) => {\n return fetch(url, { ...options, mode: 'same-origin' });\n };\n\n const retryConfig = this.integrationConfig.retry;\n if (retryConfig) {\n // Apply retry wrapper if configured\n fetchFn = this.withRetry(fetchFn, retryConfig);\n\n // Apply throttling wrapper if configured\n if (\n retryConfig.maxApiRequestsPerMinute &&\n retryConfig.maxApiRequestsPerMinute > 0\n ) {\n fetchFn = pThrottle({\n limit: retryConfig.maxApiRequestsPerMinute,\n interval: 60_000,\n })(fetchFn);\n }\n }\n\n return fetchFn;\n }\n\n private withRetry(\n fetchFn: FetchFunction,\n retryConfig: { maxRetries?: number; retryStatusCodes?: number[] },\n ): FetchFunction {\n const maxRetries = retryConfig?.maxRetries ?? 0;\n const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];\n if (maxRetries <= 0 || retryStatusCodes.length === 0) {\n return fetchFn;\n }\n\n return async (url, options) => {\n const abortSignal = options?.signal;\n let response: Response;\n let attempt = 0;\n for (;;) {\n response = await fetchFn(url, options);\n // If response is not retryable, return immediately\n if (!retryStatusCodes.includes(response.status)) {\n break;\n }\n\n // If this was the last allowed attempt, return response\n if (attempt++ >= maxRetries) {\n break;\n }\n // Determine delay from Retry-After header if present, otherwise exponential backoff\n const retryAfter = response.headers.get('Retry-After');\n const delay = retryAfter\n ? parseInt(retryAfter, 10) * 1000\n : Math.min(100 * Math.pow(2, attempt - 1), 10000); // Exponential backoff, cap at 10 seconds\n\n await sleep(delay, abortSignal);\n }\n\n return response;\n };\n }\n}\n\nexport async function sleep(\n durationMs: number,\n abortSignal: AbortSignal | null | undefined,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, durationMs);\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Takes a GitLab URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'tree', 'edit'\n * @public\n */\nexport function replaceGitLabUrlType(\n url: string,\n type: 'blob' | 'tree' | 'edit',\n): string {\n return url.replace(/\\/\\-\\/(blob|tree|edit)\\//, `/-/${type}/`);\n}\n"],"names":["config","readGitLabIntegrationConfigs","basicIntegrations","defaultScmResolveUrl","pThrottle"],"mappings":";;;;;;;;;;AA8BO,MAAM,iBAAA,CAA4C;AAAA,EAavD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAE3B,IAAA,IAAA,CAAK,SAAA,GAAY,KAAK,mBAAA,EAAoB;AAAA,EAC5C;AAAA,EAfA,OAAO,OAAA,GAAqD,CAAC,UAAEA,UAAO,KAAM;AAC1E,IAAA,MAAM,OAAA,GAAUC,mCAAA;AAAA,MACdD,QAAA,CAAO,sBAAA,CAAuB,qBAAqB,CAAA,IAAK;AAAC,KAC3D;AACA,IAAA,OAAOE,yBAAA;AAAA,MACL,QAAQ,GAAA,CAAI,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AA,MAAM,IAAI,QAAc,CAAA,OAAA,KAAW;AACjC,IAAA,IAAI,aAAA,GAA4C,MAAA;AAEhD,IAAA,MAAM,OAAO,MAAM;AACjB,MAAA,IAAI,aAAA,EAAe;AACjB,QAAA,YAAA,CAAa,aAAa,CAAA;AAAA,MAC5B;AACA,MAAA,WAAA,EAAa,mBAAA,CAAoB,SAAS,IAAI,CAAA;AAC9C,MAAA,OAAA,EAAQ;AAAA,IACV,CAAA;AAEA,IAAA,aAAA,GAAgB,UAAA,CAAW,MAAM,UAAU,CAAA;AAC3C,IAAA,WAAA,EAAa,gBAAA,CAAiB,SAAS,IAAI,CAAA;AAAA,EAC7C,CAAC,CAAA;AACH;AASO,SAAS,oBAAA,CACd,KACA,IAAA,EACQ;AACR,EAAA,OAAO,GAAA,CAAI,OAAA,CAAQ,0BAAA,EAA4B,CAAA,GAAA,EAAM,IAAI,CAAA,CAAA,CAAG,CAAA;AAC9D;;;;;;"}
1
+ {"version":3,"file":"GitLabIntegration.cjs.js","sources":["../../src/gitlab/GitLabIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\nimport pThrottle from 'p-throttle';\n\ntype FetchFunction = typeof fetch;\n\n/**\n * A GitLab based integration.\n *\n * @public\n */\nexport class GitLabIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<GitLabIntegration> = ({ config }) => {\n const configs = readGitLabIntegrationConfigs(\n config.getOptionalConfigArray('integrations.gitlab') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new GitLabIntegration(c)),\n i => i.config.host,\n );\n };\n\n private readonly fetchImpl: FetchFunction;\n\n constructor(private readonly integrationConfig: GitLabIntegrationConfig) {\n // Configure fetch strategy based on configuration\n this.fetchImpl = this.createFetchStrategy();\n }\n\n get type(): string {\n return 'gitlab';\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n get config(): GitLabIntegrationConfig {\n return this.integrationConfig;\n }\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n }): string {\n return defaultScmResolveUrl(options);\n }\n\n resolveEditUrl(url: string): string {\n return replaceGitLabUrlType(url, 'edit');\n }\n\n fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {\n return this.fetchImpl(input, init);\n }\n\n private createFetchStrategy(): FetchFunction {\n let fetchFn: FetchFunction = (url, options) => fetch(url, options);\n\n const retryConfig = this.integrationConfig.retry;\n if (retryConfig) {\n // Apply retry wrapper if configured\n fetchFn = this.withRetry(fetchFn, retryConfig);\n\n // Apply throttling wrapper if configured\n if (\n retryConfig.maxApiRequestsPerMinute &&\n retryConfig.maxApiRequestsPerMinute > 0\n ) {\n fetchFn = pThrottle({\n limit: retryConfig.maxApiRequestsPerMinute,\n interval: 60_000,\n })(fetchFn);\n }\n }\n\n return fetchFn;\n }\n\n private withRetry(\n fetchFn: FetchFunction,\n retryConfig: { maxRetries?: number; retryStatusCodes?: number[] },\n ): FetchFunction {\n const maxRetries = retryConfig?.maxRetries ?? 0;\n const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];\n if (maxRetries <= 0 || retryStatusCodes.length === 0) {\n return fetchFn;\n }\n\n // Exponential backoff, cap at 10 seconds\n const backoffDelay = (a: number) =>\n Math.min(100 * Math.pow(2, a - 1), 10000);\n\n return async (url, options) => {\n const abortSignal = options?.signal;\n let attempt = 0;\n for (;;) {\n let response: Response;\n try {\n response = await fetchFn(url, options);\n } catch (e) {\n // The caller aborted — surface that immediately rather than retrying.\n if (abortSignal?.aborted) throw e;\n // No more attempts left — propagate the network error.\n if (attempt++ >= maxRetries) throw e;\n await sleep(backoffDelay(attempt), abortSignal);\n if (abortSignal?.aborted) throw e;\n continue;\n }\n\n // Successful, non-retryable response: return immediately\n if (!retryStatusCodes.includes(response.status)) {\n return response;\n }\n\n // No more attempts left — return the last (retryable) response.\n if (attempt++ >= maxRetries) {\n return response;\n }\n\n // Retry-After is either delay-seconds or an HTTP-date (RFC 9110 §10.2.3).\n const delay = parseRetryAfterMs(\n response.headers.get('Retry-After'),\n backoffDelay(attempt),\n );\n\n // Release the underlying connection so it can be reused, since we're\n // about to discard this response in favor of a retry.\n await response.body?.cancel().catch(() => {});\n\n await sleep(delay, abortSignal);\n if (abortSignal?.aborted) return response;\n }\n };\n }\n}\n\n/** @internal */\nexport function parseRetryAfterMs(\n headerValue: string | null,\n fallbackMs: number,\n): number {\n if (!headerValue) {\n return fallbackMs;\n }\n\n // delay-seconds per RFC 9110 is 1*DIGIT\n if (/^\\d+$/.test(headerValue)) {\n return Number(headerValue) * 1000;\n }\n\n // HTTP-dates (IMF-fixdate) always contain a comma, e.g.\n // \"Sun, 06 Nov 1994 08:49:37 GMT\" — use that as a prerequisite\n // to avoid Date.parse interpreting random strings as dates.\n if (headerValue.includes(',')) {\n const dateMs = Date.parse(headerValue);\n if (Number.isFinite(dateMs)) {\n const deltaMs = dateMs - Date.now();\n return deltaMs > 0 ? deltaMs : 0;\n }\n }\n\n return fallbackMs;\n}\n\n/** @internal */\nexport async function sleep(\n durationMs: number,\n abortSignal: AbortSignal | null | undefined,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, durationMs);\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Takes a GitLab URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'tree', 'edit'\n * @public\n */\nexport function replaceGitLabUrlType(\n url: string,\n type: 'blob' | 'tree' | 'edit',\n): string {\n return url.replace(/\\/\\-\\/(blob|tree|edit)\\//, `/-/${type}/`);\n}\n"],"names":["config","readGitLabIntegrationConfigs","basicIntegrations","defaultScmResolveUrl","pThrottle"],"mappings":";;;;;;;;;;AA8BO,MAAM,iBAAA,CAA4C;AAAA,EAavD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAE3B,IAAA,IAAA,CAAK,SAAA,GAAY,KAAK,mBAAA,EAAoB;AAAA,EAC5C;AAAA,EAfA,OAAO,OAAA,GAAqD,CAAC,UAAEA,UAAO,KAAM;AAC1E,IAAA,MAAM,OAAA,GAAUC,mCAAA;AAAA,MACdD,QAAA,CAAO,sBAAA,CAAuB,qBAAqB,CAAA,IAAK;AAAC,KAC3D;AACA,IAAA,OAAOE,yBAAA;AAAA,MACL,QAAQ,GAAA,CAAI,CAAA,CAAA,KAAK,IAAI,iBAAA,CAAkB,CAAC,CAAC,CAAA;AAAA,MACzC,CAAA,CAAA,KAAK,EAAE,MAAA,CAAO;AAAA,KAChB;AAAA,EACF,CAAA;AAAA,EAEiB,SAAA;AAAA,EAOjB,IAAI,IAAA,GAAe;AACjB,IAAA,OAAO,QAAA;AAAA,EACT;AAAA,EAEA,IAAI,KAAA,GAAgB;AAClB,IAAA,OAAO,KAAK,iBAAA,CAAkB,IAAA;AAAA,EAChC;AAAA,EAEA,IAAI,MAAA,GAAkC;AACpC,IAAA,OAAO,IAAA,CAAK,iBAAA;AAAA,EACd;AAAA,EAEA,WAAW,OAAA,EAIA;AACT,IAAA,OAAOC,6BAAqB,OAAO,CAAA;AAAA,EACrC;AAAA,EAEA,eAA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AAA,CAAO,WAAW,CAAA,GAAI,GAAA;AAAA,EAC/B;AAKA,EAAA,IAAI,WAAA,CAAY,QAAA,CAAS,GAAG,CAAA,EAAG;AAC7B,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,KAAA,CAAM,WAAW,CAAA;AACrC,IAAA,IAAI,MAAA,CAAO,QAAA,CAAS,MAAM,CAAA,EAAG;AAC3B,MAAA,MAAM,OAAA,GAAU,MAAA,GAAS,IAAA,CAAK,GAAA,EAAI;AAClC,MAAA,OAAO,OAAA,GAAU,IAAI,OAAA,GAAU,CAAA;AAAA,IACjC;AAAA,EACF;AAEA,EAAA,OAAO,UAAA;AACT;AAGA,eAAsB,KAAA,CACpB,YACA,WAAA,EACe;AACf,EAAA,IAAI,aAAa,OAAA,EAAS;AACxB,IAAA;AAAA,EACF;AAEA,EAAA,MAAM,IAAI,QAAc,CAAA,OAAA,KAAW;AACjC,IAAA,IAAI,aAAA,GAA4C,MAAA;AAEhD,IAAA,MAAM,OAAO,MAAM;AACjB,MAAA,IAAI,aAAA,EAAe;AACjB,QAAA,YAAA,CAAa,aAAa,CAAA;AAAA,MAC5B;AACA,MAAA,WAAA,EAAa,mBAAA,CAAoB,SAAS,IAAI,CAAA;AAC9C,MAAA,OAAA,EAAQ;AAAA,IACV,CAAA;AAEA,IAAA,aAAA,GAAgB,UAAA,CAAW,MAAM,UAAU,CAAA;AAC3C,IAAA,WAAA,EAAa,gBAAA,CAAiB,SAAS,IAAI,CAAA;AAAA,EAC7C,CAAC,CAAA;AACH;AASO,SAAS,oBAAA,CACd,KACA,IAAA,EACQ;AACR,EAAA,OAAO,GAAA,CAAI,OAAA,CAAQ,0BAAA,EAA4B,CAAA,GAAA,EAAM,IAAI,CAAA,CAAA,CAAG,CAAA;AAC9D;;;;;;;"}
@@ -36,9 +36,7 @@ class GitLabIntegration {
36
36
  return this.fetchImpl(input, init);
37
37
  }
38
38
  createFetchStrategy() {
39
- let fetchFn = async (url, options) => {
40
- return fetch(url, { ...options, mode: "same-origin" });
41
- };
39
+ let fetchFn = (url, options) => fetch(url, options);
42
40
  const retryConfig = this.integrationConfig.retry;
43
41
  if (retryConfig) {
44
42
  fetchFn = this.withRetry(fetchFn, retryConfig);
@@ -57,26 +55,55 @@ class GitLabIntegration {
57
55
  if (maxRetries <= 0 || retryStatusCodes.length === 0) {
58
56
  return fetchFn;
59
57
  }
58
+ const backoffDelay = (a) => Math.min(100 * Math.pow(2, a - 1), 1e4);
60
59
  return async (url, options) => {
61
60
  const abortSignal = options?.signal;
62
- let response;
63
61
  let attempt = 0;
64
62
  for (; ; ) {
65
- response = await fetchFn(url, options);
63
+ let response;
64
+ try {
65
+ response = await fetchFn(url, options);
66
+ } catch (e) {
67
+ if (abortSignal?.aborted) throw e;
68
+ if (attempt++ >= maxRetries) throw e;
69
+ await sleep(backoffDelay(attempt), abortSignal);
70
+ if (abortSignal?.aborted) throw e;
71
+ continue;
72
+ }
66
73
  if (!retryStatusCodes.includes(response.status)) {
67
- break;
74
+ return response;
68
75
  }
69
76
  if (attempt++ >= maxRetries) {
70
- break;
77
+ return response;
71
78
  }
72
- const retryAfter = response.headers.get("Retry-After");
73
- const delay = retryAfter ? parseInt(retryAfter, 10) * 1e3 : Math.min(100 * Math.pow(2, attempt - 1), 1e4);
79
+ const delay = parseRetryAfterMs(
80
+ response.headers.get("Retry-After"),
81
+ backoffDelay(attempt)
82
+ );
83
+ await response.body?.cancel().catch(() => {
84
+ });
74
85
  await sleep(delay, abortSignal);
86
+ if (abortSignal?.aborted) return response;
75
87
  }
76
- return response;
77
88
  };
78
89
  }
79
90
  }
91
+ function parseRetryAfterMs(headerValue, fallbackMs) {
92
+ if (!headerValue) {
93
+ return fallbackMs;
94
+ }
95
+ if (/^\d+$/.test(headerValue)) {
96
+ return Number(headerValue) * 1e3;
97
+ }
98
+ if (headerValue.includes(",")) {
99
+ const dateMs = Date.parse(headerValue);
100
+ if (Number.isFinite(dateMs)) {
101
+ const deltaMs = dateMs - Date.now();
102
+ return deltaMs > 0 ? deltaMs : 0;
103
+ }
104
+ }
105
+ return fallbackMs;
106
+ }
80
107
  async function sleep(durationMs, abortSignal) {
81
108
  if (abortSignal?.aborted) {
82
109
  return;
@@ -98,5 +125,5 @@ function replaceGitLabUrlType(url, type) {
98
125
  return url.replace(/\/\-\/(blob|tree|edit)\//, `/-/${type}/`);
99
126
  }
100
127
 
101
- export { GitLabIntegration, replaceGitLabUrlType, sleep };
128
+ export { GitLabIntegration, parseRetryAfterMs, replaceGitLabUrlType, sleep };
102
129
  //# sourceMappingURL=GitLabIntegration.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"GitLabIntegration.esm.js","sources":["../../src/gitlab/GitLabIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\nimport pThrottle from 'p-throttle';\n\ntype FetchFunction = typeof fetch;\n\n/**\n * A GitLab based integration.\n *\n * @public\n */\nexport class GitLabIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<GitLabIntegration> = ({ config }) => {\n const configs = readGitLabIntegrationConfigs(\n config.getOptionalConfigArray('integrations.gitlab') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new GitLabIntegration(c)),\n i => i.config.host,\n );\n };\n\n private readonly fetchImpl: FetchFunction;\n\n constructor(private readonly integrationConfig: GitLabIntegrationConfig) {\n // Configure fetch strategy based on configuration\n this.fetchImpl = this.createFetchStrategy();\n }\n\n get type(): string {\n return 'gitlab';\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n get config(): GitLabIntegrationConfig {\n return this.integrationConfig;\n }\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n }): string {\n return defaultScmResolveUrl(options);\n }\n\n resolveEditUrl(url: string): string {\n return replaceGitLabUrlType(url, 'edit');\n }\n\n fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {\n return this.fetchImpl(input, init);\n }\n\n private createFetchStrategy(): FetchFunction {\n let fetchFn: FetchFunction = async (url, options) => {\n return fetch(url, { ...options, mode: 'same-origin' });\n };\n\n const retryConfig = this.integrationConfig.retry;\n if (retryConfig) {\n // Apply retry wrapper if configured\n fetchFn = this.withRetry(fetchFn, retryConfig);\n\n // Apply throttling wrapper if configured\n if (\n retryConfig.maxApiRequestsPerMinute &&\n retryConfig.maxApiRequestsPerMinute > 0\n ) {\n fetchFn = pThrottle({\n limit: retryConfig.maxApiRequestsPerMinute,\n interval: 60_000,\n })(fetchFn);\n }\n }\n\n return fetchFn;\n }\n\n private withRetry(\n fetchFn: FetchFunction,\n retryConfig: { maxRetries?: number; retryStatusCodes?: number[] },\n ): FetchFunction {\n const maxRetries = retryConfig?.maxRetries ?? 0;\n const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];\n if (maxRetries <= 0 || retryStatusCodes.length === 0) {\n return fetchFn;\n }\n\n return async (url, options) => {\n const abortSignal = options?.signal;\n let response: Response;\n let attempt = 0;\n for (;;) {\n response = await fetchFn(url, options);\n // If response is not retryable, return immediately\n if (!retryStatusCodes.includes(response.status)) {\n break;\n }\n\n // If this was the last allowed attempt, return response\n if (attempt++ >= maxRetries) {\n break;\n }\n // Determine delay from Retry-After header if present, otherwise exponential backoff\n const retryAfter = response.headers.get('Retry-After');\n const delay = retryAfter\n ? parseInt(retryAfter, 10) * 1000\n : Math.min(100 * Math.pow(2, attempt - 1), 10000); // Exponential backoff, cap at 10 seconds\n\n await sleep(delay, abortSignal);\n }\n\n return response;\n };\n }\n}\n\nexport async function sleep(\n durationMs: number,\n abortSignal: AbortSignal | null | undefined,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, durationMs);\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Takes a GitLab URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'tree', 'edit'\n * @public\n */\nexport function replaceGitLabUrlType(\n url: string,\n type: 'blob' | 'tree' | 'edit',\n): string {\n return url.replace(/\\/\\-\\/(blob|tree|edit)\\//, `/-/${type}/`);\n}\n"],"names":[],"mappings":";;;;AA8BO,MAAM,iBAAA,CAA4C;AAAA,EAavD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAE3B,IAAA,IAAA,CAAK,SAAA,GAAY,KAAK,mBAAA,EAAoB;AAAA,EAC5C;AAAA,EAfA,OAAO,OAAA,GAAqD,CAAC,EAAE,QAAO,KAAM;AAC1E,IAAA,MAAM,OAAA,GAAU,4BAAA;AAAA,MACd,MAAA,CAAO,sBAAA,CAAuB,qBAAqB,CAAA,IAAK;AAAC,KAC3D;AACA,IAAA,OAAO,iBAAA;AAAA,MACL,QAAQ,GAAA,CAAI,CAAA,CAAA,KAAK,IAAI,iBAAA,CAAkB,CAAC,CAAC,CAAA;AAAA,MACzC,CAAA,CAAA,KAAK,EAAE,MAAA,CAAO;AAAA,KAChB;AAAA,EACF,CAAA;AAAA,EAEiB,SAAA;AAAA,EAOjB,IAAI,IAAA,GAAe;AACjB,IAAA,OAAO,QAAA;AAAA,EACT;AAAA,EAEA,IAAI,KAAA,GAA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MAAA,OAAA,EAAQ;AAAA,IACV,CAAA;AAEA,IAAA,aAAA,GAAgB,UAAA,CAAW,MAAM,UAAU,CAAA;AAC3C,IAAA,WAAA,EAAa,gBAAA,CAAiB,SAAS,IAAI,CAAA;AAAA,EAC7C,CAAC,CAAA;AACH;AASO,SAAS,oBAAA,CACd,KACA,IAAA,EACQ;AACR,EAAA,OAAO,GAAA,CAAI,OAAA,CAAQ,0BAAA,EAA4B,CAAA,GAAA,EAAM,IAAI,CAAA,CAAA,CAAG,CAAA;AAC9D;;;;"}
1
+ {"version":3,"file":"GitLabIntegration.esm.js","sources":["../../src/gitlab/GitLabIntegration.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\nimport pThrottle from 'p-throttle';\n\ntype FetchFunction = typeof fetch;\n\n/**\n * A GitLab based integration.\n *\n * @public\n */\nexport class GitLabIntegration implements ScmIntegration {\n static factory: ScmIntegrationsFactory<GitLabIntegration> = ({ config }) => {\n const configs = readGitLabIntegrationConfigs(\n config.getOptionalConfigArray('integrations.gitlab') ?? [],\n );\n return basicIntegrations(\n configs.map(c => new GitLabIntegration(c)),\n i => i.config.host,\n );\n };\n\n private readonly fetchImpl: FetchFunction;\n\n constructor(private readonly integrationConfig: GitLabIntegrationConfig) {\n // Configure fetch strategy based on configuration\n this.fetchImpl = this.createFetchStrategy();\n }\n\n get type(): string {\n return 'gitlab';\n }\n\n get title(): string {\n return this.integrationConfig.host;\n }\n\n get config(): GitLabIntegrationConfig {\n return this.integrationConfig;\n }\n\n resolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n }): string {\n return defaultScmResolveUrl(options);\n }\n\n resolveEditUrl(url: string): string {\n return replaceGitLabUrlType(url, 'edit');\n }\n\n fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {\n return this.fetchImpl(input, init);\n }\n\n private createFetchStrategy(): FetchFunction {\n let fetchFn: FetchFunction = (url, options) => fetch(url, options);\n\n const retryConfig = this.integrationConfig.retry;\n if (retryConfig) {\n // Apply retry wrapper if configured\n fetchFn = this.withRetry(fetchFn, retryConfig);\n\n // Apply throttling wrapper if configured\n if (\n retryConfig.maxApiRequestsPerMinute &&\n retryConfig.maxApiRequestsPerMinute > 0\n ) {\n fetchFn = pThrottle({\n limit: retryConfig.maxApiRequestsPerMinute,\n interval: 60_000,\n })(fetchFn);\n }\n }\n\n return fetchFn;\n }\n\n private withRetry(\n fetchFn: FetchFunction,\n retryConfig: { maxRetries?: number; retryStatusCodes?: number[] },\n ): FetchFunction {\n const maxRetries = retryConfig?.maxRetries ?? 0;\n const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];\n if (maxRetries <= 0 || retryStatusCodes.length === 0) {\n return fetchFn;\n }\n\n // Exponential backoff, cap at 10 seconds\n const backoffDelay = (a: number) =>\n Math.min(100 * Math.pow(2, a - 1), 10000);\n\n return async (url, options) => {\n const abortSignal = options?.signal;\n let attempt = 0;\n for (;;) {\n let response: Response;\n try {\n response = await fetchFn(url, options);\n } catch (e) {\n // The caller aborted — surface that immediately rather than retrying.\n if (abortSignal?.aborted) throw e;\n // No more attempts left — propagate the network error.\n if (attempt++ >= maxRetries) throw e;\n await sleep(backoffDelay(attempt), abortSignal);\n if (abortSignal?.aborted) throw e;\n continue;\n }\n\n // Successful, non-retryable response: return immediately\n if (!retryStatusCodes.includes(response.status)) {\n return response;\n }\n\n // No more attempts left — return the last (retryable) response.\n if (attempt++ >= maxRetries) {\n return response;\n }\n\n // Retry-After is either delay-seconds or an HTTP-date (RFC 9110 §10.2.3).\n const delay = parseRetryAfterMs(\n response.headers.get('Retry-After'),\n backoffDelay(attempt),\n );\n\n // Release the underlying connection so it can be reused, since we're\n // about to discard this response in favor of a retry.\n await response.body?.cancel().catch(() => {});\n\n await sleep(delay, abortSignal);\n if (abortSignal?.aborted) return response;\n }\n };\n }\n}\n\n/** @internal */\nexport function parseRetryAfterMs(\n headerValue: string | null,\n fallbackMs: number,\n): number {\n if (!headerValue) {\n return fallbackMs;\n }\n\n // delay-seconds per RFC 9110 is 1*DIGIT\n if (/^\\d+$/.test(headerValue)) {\n return Number(headerValue) * 1000;\n }\n\n // HTTP-dates (IMF-fixdate) always contain a comma, e.g.\n // \"Sun, 06 Nov 1994 08:49:37 GMT\" — use that as a prerequisite\n // to avoid Date.parse interpreting random strings as dates.\n if (headerValue.includes(',')) {\n const dateMs = Date.parse(headerValue);\n if (Number.isFinite(dateMs)) {\n const deltaMs = dateMs - Date.now();\n return deltaMs > 0 ? deltaMs : 0;\n }\n }\n\n return fallbackMs;\n}\n\n/** @internal */\nexport async function sleep(\n durationMs: number,\n abortSignal: AbortSignal | null | undefined,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, durationMs);\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Takes a GitLab URL and replaces the type part (blob, tree etc).\n *\n * @param url - The original URL\n * @param type - The desired type, e.g. 'blob', 'tree', 'edit'\n * @public\n */\nexport function replaceGitLabUrlType(\n url: string,\n type: 'blob' | 'tree' | 'edit',\n): string {\n return url.replace(/\\/\\-\\/(blob|tree|edit)\\//, `/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OAAA,GAAU,IAAI,OAAA,GAAU,CAAA;AAAA,IACjC;AAAA,EACF;AAEA,EAAA,OAAO,UAAA;AACT;AAGA,eAAsB,KAAA,CACpB,YACA,WAAA,EACe;AACf,EAAA,IAAI,aAAa,OAAA,EAAS;AACxB,IAAA;AAAA,EACF;AAEA,EAAA,MAAM,IAAI,QAAc,CAAA,OAAA,KAAW;AACjC,IAAA,IAAI,aAAA,GAA4C,MAAA;AAEhD,IAAA,MAAM,OAAO,MAAM;AACjB,MAAA,IAAI,aAAA,EAAe;AACjB,QAAA,YAAA,CAAa,aAAa,CAAA;AAAA,MAC5B;AACA,MAAA,WAAA,EAAa,mBAAA,CAAoB,SAAS,IAAI,CAAA;AAC9C,MAAA,OAAA,EAAQ;AAAA,IACV,CAAA;AAEA,IAAA,aAAA,GAAgB,UAAA,CAAW,MAAM,UAAU,CAAA;AAC3C,IAAA,WAAA,EAAa,gBAAA,CAAiB,SAAS,IAAI,CAAA;AAAA,EAC7C,CAAC,CAAA;AACH;AASO,SAAS,oBAAA,CACd,KACA,IAAA,EACQ;AACR,EAAA,OAAO,GAAA,CAAI,OAAA,CAAQ,0BAAA,EAA4B,CAAA,GAAA,EAAM,IAAI,CAAA,CAAA,CAAG,CAAA;AAC9D;;;;"}
@@ -50,7 +50,6 @@ function extractProjectPath(target, config$1) {
50
50
  return repo.replace(/^\//, "");
51
51
  }
52
52
 
53
- exports.buildProjectUrl = buildProjectUrl;
54
53
  exports.extractProjectPath = extractProjectPath;
55
54
  exports.getGitLabFileFetchUrl = getGitLabFileFetchUrl;
56
55
  exports.getGitLabRequestOptions = getGitLabRequestOptions;
@@ -1 +1 @@
1
- {"version":3,"file":"core.cjs.js","sources":["../../src/gitlab/core.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n getGitLabIntegrationRelativePath,\n GitLabIntegrationConfig,\n} from './config';\n\n/**\n * Given a URL pointing to a file on a provider, returns a URL that is suitable\n * for fetching the contents of the data.\n *\n * @remarks\n *\n * Converts\n * from: https://gitlab.example.com/a/b/blob/master/c.yaml\n * to: https://gitlab.com/api/v4/projects/a%2Fb/repository/files/c.yaml/raw?ref=master\n * -or-\n * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n * to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @param token - An optional auth token (not used in path extraction, kept for compatibility)\n * @public\n */\nexport function getGitLabFileFetchUrl(\n url: string,\n config: GitLabIntegrationConfig,\n _token?: string,\n): Promise<string> {\n // Use project path directly instead of making an API call to get project ID\n // Note: _token parameter kept for backward compatibility but not used for path extraction\n const projectPath = extractProjectPath(url, config);\n return Promise.resolve(buildProjectUrl(url, projectPath, config).toString());\n}\n\n/**\n * Gets the request options necessary to make requests to a given provider.\n *\n * @param config - The relevant provider config\n * @param token - An optional auth token to use for communicating with GitLab. By default uses the integration token\n * @public\n */\nexport function getGitLabRequestOptions(\n config: GitLabIntegrationConfig,\n token?: string,\n): { headers: Record<string, string> } {\n const headers: Record<string, string> = {};\n\n const accessToken = token || config.token;\n if (accessToken) {\n // OAuth, Personal, Project, and Group access tokens can all be passed via\n // a bearer authorization header\n // https://docs.gitlab.com/api/rest/authentication/#personalprojectgroup-access-tokens\n headers.Authorization = `Bearer ${accessToken}`;\n }\n\n return { headers };\n}\n\n// Converts\n// from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n// to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch\nexport function buildProjectUrl(\n target: string,\n projectPathOrID: string | Number,\n config: GitLabIntegrationConfig,\n): URL {\n try {\n const url = new URL(target);\n\n const branchAndFilePath = url.pathname\n .split('/blob/')\n .slice(1)\n .join('/blob/');\n const [branch, ...filePath] = branchAndFilePath.split('/');\n const relativePath = getGitLabIntegrationRelativePath(config);\n\n const projectIdentifier = encodeURIComponent(String(projectPathOrID));\n\n url.pathname = [\n ...(relativePath ? [relativePath] : []),\n 'api/v4/projects',\n projectIdentifier,\n 'repository/files',\n encodeURIComponent(decodeURIComponent(filePath.join('/'))),\n 'raw',\n ].join('/');\n\n url.search = `?ref=${branch}`;\n\n return url;\n } catch (e) {\n throw new Error(`Incorrect url: ${target}, ${e}`);\n }\n}\n\n/**\n * Extracts the project path from a GitLab URL\n * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n * to: groupA/teams/teamA/subgroupA/repoA\n */\nexport function extractProjectPath(\n target: string,\n config: GitLabIntegrationConfig,\n): string {\n const url = new URL(target);\n\n if (!url.pathname.includes('/blob/')) {\n throw new Error(\n `Failed extracting project path from ${url.pathname}. Url path must include /blob/.`,\n );\n }\n\n let repo = url.pathname.split('/-/blob/')[0].split('/blob/')[0];\n\n // Get gitlab relative path\n const relativePath = getGitLabIntegrationRelativePath(config);\n\n // Check relative path exist and replace it if it's the case.\n if (relativePath) {\n repo = repo.replace(relativePath, '');\n }\n\n // Remove leading slash\n return repo.replace(/^\\//, '');\n}\n"],"names":["config","getGitLabIntegrationRelativePath"],"mappings":";;;;AAuCO,SAAS,qBAAA,CACd,GAAA,EACA,MAAA,EACA,MAAA,EACiB;AAGjB,EAAA,MAAM,WAAA,GAAc,kBAAA,CAAmB,GAAA,EAAK,MAAM,CAAA;AAClD,EAAA,OAAO,OAAA,CAAQ,QAAQ,eAAA,CAAgB,GAAA,EAAK,aAAa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M,CAAA,eAAA,EAAkB,MAAM,CAAA,EAAA,EAAK,CAAC,CAAA,CAAE,CAAA;AAAA,EAClD;AACF;AAOO,SAAS,kBAAA,CACd,QACAA,QAAA,EACQ;AACR,EAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,MAAM,CAAA;AAE1B,EAAA,IAAI,CAAC,GAAA,CAAI,QAAA,CAAS,QAAA,CAAS,QAAQ,CAAA,EAAG;AACpC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,oCAAA,EAAuC,IAAI,QAAQ,CAAA,+BAAA;AAAA,KACrD;AAAA,EACF;AAEA,EAAA,IAAI,IAAA,GAAO,GAAA,CAAI,QAAA,CAAS,KAAA,CAAM,UAAU,CAAA,CAAE,CAAC,CAAA,CAAE,KAAA,CAAM,QAAQ,CAAA,CAAE,CAAC,CAAA;AAG9D,EAAA,MAAM,YAAA,GAAeC,wCAAiCD,QAAM,CAAA;AAG5D,EAAA,IAAI,YAAA,EAAc;AAChB,IAAA,IAAA,GAAO,IAAA,CAAK,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAAA,EACtC;AAGA,EAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AAC/B;;;;;;;"}
1
+ {"version":3,"file":"core.cjs.js","sources":["../../src/gitlab/core.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n getGitLabIntegrationRelativePath,\n GitLabIntegrationConfig,\n} from './config';\n\n/**\n * Given a URL pointing to a file on a provider, returns a URL that is suitable\n * for fetching the contents of the data.\n *\n * @remarks\n *\n * Converts\n * from: https://gitlab.example.com/a/b/blob/master/c.yaml\n * to: https://gitlab.com/api/v4/projects/a%2Fb/repository/files/c.yaml/raw?ref=master\n * -or-\n * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n * to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @param token - An optional auth token (not used in path extraction, kept for compatibility)\n * @public\n */\nexport function getGitLabFileFetchUrl(\n url: string,\n config: GitLabIntegrationConfig,\n _token?: string,\n): Promise<string> {\n // Use project path directly instead of making an API call to get project ID\n // Note: _token parameter kept for backward compatibility but not used for path extraction\n const projectPath = extractProjectPath(url, config);\n return Promise.resolve(buildProjectUrl(url, projectPath, config).toString());\n}\n\n/**\n * Gets the request options necessary to make requests to a given provider.\n *\n * @param config - The relevant provider config\n * @param token - An optional auth token to use for communicating with GitLab. By default uses the integration token\n * @public\n */\nexport function getGitLabRequestOptions(\n config: GitLabIntegrationConfig,\n token?: string,\n): { headers: Record<string, string> } {\n const headers: Record<string, string> = {};\n\n const accessToken = token || config.token;\n if (accessToken) {\n // OAuth, Personal, Project, and Group access tokens can all be passed via\n // a bearer authorization header\n // https://docs.gitlab.com/api/rest/authentication/#personalprojectgroup-access-tokens\n headers.Authorization = `Bearer ${accessToken}`;\n }\n\n return { headers };\n}\n\n// Converts\n// from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n// to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch\nfunction buildProjectUrl(\n target: string,\n projectPathOrID: string | Number,\n config: GitLabIntegrationConfig,\n): URL {\n try {\n const url = new URL(target);\n\n const branchAndFilePath = url.pathname\n .split('/blob/')\n .slice(1)\n .join('/blob/');\n const [branch, ...filePath] = branchAndFilePath.split('/');\n const relativePath = getGitLabIntegrationRelativePath(config);\n\n const projectIdentifier = encodeURIComponent(String(projectPathOrID));\n\n url.pathname = [\n ...(relativePath ? [relativePath] : []),\n 'api/v4/projects',\n projectIdentifier,\n 'repository/files',\n encodeURIComponent(decodeURIComponent(filePath.join('/'))),\n 'raw',\n ].join('/');\n\n url.search = `?ref=${branch}`;\n\n return url;\n } catch (e) {\n throw new Error(`Incorrect url: ${target}, ${e}`);\n }\n}\n\n/**\n * Extracts the project path from a GitLab URL\n * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n * to: groupA/teams/teamA/subgroupA/repoA\n */\nexport function extractProjectPath(\n target: string,\n config: GitLabIntegrationConfig,\n): string {\n const url = new URL(target);\n\n if (!url.pathname.includes('/blob/')) {\n throw new Error(\n `Failed extracting project path from ${url.pathname}. Url path must include /blob/.`,\n );\n }\n\n let repo = url.pathname.split('/-/blob/')[0].split('/blob/')[0];\n\n // Get gitlab relative path\n const relativePath = getGitLabIntegrationRelativePath(config);\n\n // Check relative path exist and replace it if it's the case.\n if (relativePath) {\n repo = repo.replace(relativePath, '');\n }\n\n // Remove leading slash\n return repo.replace(/^\\//, '');\n}\n"],"names":["config","getGitLabIntegrationRelativePath"],"mappings":";;;;AAuCO,SAAS,qBAAA,CACd,GAAA,EACA,MAAA,EACA,MAAA,EACiB;AAGjB,EAAA,MAAM,WAAA,GAAc,kBAAA,CAAmB,GAAA,EAAK,MAAM,CAAA;AAClD,EAAA,OAAO,OAAA,CAAQ,QAAQ,eAAA,CAAgB,GAAA,EAAK,aAAa,MAAM,CAAA,CAAE,UAAU,CAAA;AAC7E;AASO,SAAS,uBAAA,CACd,QACA,KAAA,EACqC;AACrC,EAAA,MAAM,UAAkC,EAAC;AAEzC,EAAA,MAAM,WAAA,GAAc,SAAS,MAAA,CAAO,KAAA;AACpC,EAAA,IAAI,WAAA,EAAa;AAIf,IAAA,OAAA,CAAQ,aAAA,GAAgB,UAAU,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AA,CAAS,QAAQ,CAAA,EAAG;AACpC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,oCAAA,EAAuC,IAAI,QAAQ,CAAA,+BAAA;AAAA,KACrD;AAAA,EACF;AAEA,EAAA,IAAI,IAAA,GAAO,GAAA,CAAI,QAAA,CAAS,KAAA,CAAM,UAAU,CAAA,CAAE,CAAC,CAAA,CAAE,KAAA,CAAM,QAAQ,CAAA,CAAE,CAAC,CAAA;AAG9D,EAAA,MAAM,YAAA,GAAeC,wCAAiCD,QAAM,CAAA;AAG5D,EAAA,IAAI,YAAA,EAAc;AAChB,IAAA,IAAA,GAAO,IAAA,CAAK,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAAA,EACtC;AAGA,EAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AAC/B;;;;;;"}
@@ -48,5 +48,5 @@ function extractProjectPath(target, config) {
48
48
  return repo.replace(/^\//, "");
49
49
  }
50
50
 
51
- export { buildProjectUrl, extractProjectPath, getGitLabFileFetchUrl, getGitLabRequestOptions };
51
+ export { extractProjectPath, getGitLabFileFetchUrl, getGitLabRequestOptions };
52
52
  //# sourceMappingURL=core.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"core.esm.js","sources":["../../src/gitlab/core.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n getGitLabIntegrationRelativePath,\n GitLabIntegrationConfig,\n} from './config';\n\n/**\n * Given a URL pointing to a file on a provider, returns a URL that is suitable\n * for fetching the contents of the data.\n *\n * @remarks\n *\n * Converts\n * from: https://gitlab.example.com/a/b/blob/master/c.yaml\n * to: https://gitlab.com/api/v4/projects/a%2Fb/repository/files/c.yaml/raw?ref=master\n * -or-\n * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n * to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @param token - An optional auth token (not used in path extraction, kept for compatibility)\n * @public\n */\nexport function getGitLabFileFetchUrl(\n url: string,\n config: GitLabIntegrationConfig,\n _token?: string,\n): Promise<string> {\n // Use project path directly instead of making an API call to get project ID\n // Note: _token parameter kept for backward compatibility but not used for path extraction\n const projectPath = extractProjectPath(url, config);\n return Promise.resolve(buildProjectUrl(url, projectPath, config).toString());\n}\n\n/**\n * Gets the request options necessary to make requests to a given provider.\n *\n * @param config - The relevant provider config\n * @param token - An optional auth token to use for communicating with GitLab. By default uses the integration token\n * @public\n */\nexport function getGitLabRequestOptions(\n config: GitLabIntegrationConfig,\n token?: string,\n): { headers: Record<string, string> } {\n const headers: Record<string, string> = {};\n\n const accessToken = token || config.token;\n if (accessToken) {\n // OAuth, Personal, Project, and Group access tokens can all be passed via\n // a bearer authorization header\n // https://docs.gitlab.com/api/rest/authentication/#personalprojectgroup-access-tokens\n headers.Authorization = `Bearer ${accessToken}`;\n }\n\n return { headers };\n}\n\n// Converts\n// from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n// to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch\nexport function buildProjectUrl(\n target: string,\n projectPathOrID: string | Number,\n config: GitLabIntegrationConfig,\n): URL {\n try {\n const url = new URL(target);\n\n const branchAndFilePath = url.pathname\n .split('/blob/')\n .slice(1)\n .join('/blob/');\n const [branch, ...filePath] = branchAndFilePath.split('/');\n const relativePath = getGitLabIntegrationRelativePath(config);\n\n const projectIdentifier = encodeURIComponent(String(projectPathOrID));\n\n url.pathname = [\n ...(relativePath ? [relativePath] : []),\n 'api/v4/projects',\n projectIdentifier,\n 'repository/files',\n encodeURIComponent(decodeURIComponent(filePath.join('/'))),\n 'raw',\n ].join('/');\n\n url.search = `?ref=${branch}`;\n\n return url;\n } catch (e) {\n throw new Error(`Incorrect url: ${target}, ${e}`);\n }\n}\n\n/**\n * Extracts the project path from a GitLab URL\n * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n * to: groupA/teams/teamA/subgroupA/repoA\n */\nexport function extractProjectPath(\n target: string,\n config: GitLabIntegrationConfig,\n): string {\n const url = new URL(target);\n\n if (!url.pathname.includes('/blob/')) {\n throw new Error(\n `Failed extracting project path from ${url.pathname}. Url path must include /blob/.`,\n );\n }\n\n let repo = url.pathname.split('/-/blob/')[0].split('/blob/')[0];\n\n // Get gitlab relative path\n const relativePath = getGitLabIntegrationRelativePath(config);\n\n // Check relative path exist and replace it if it's the case.\n if (relativePath) {\n repo = repo.replace(relativePath, '');\n }\n\n // Remove leading slash\n return repo.replace(/^\\//, '');\n}\n"],"names":[],"mappings":";;AAuCO,SAAS,qBAAA,CACd,GAAA,EACA,MAAA,EACA,MAAA,EACiB;AAGjB,EAAA,MAAM,WAAA,GAAc,kBAAA,CAAmB,GAAA,EAAK,MAAM,CAAA;AAClD,EAAA,OAAO,OAAA,CAAQ,QAAQ,eAAA,CAAgB,GAAA,EAAK,aAAa,MAAM,CAAA,CAAE,UAAU,CAAA;AAC7E;AASO,SAAS,uBAAA,CACd,QACA,KAAA,EACqC;AACrC,EAAA,MAAM,UAAkC,EAAC;AAEzC,EAAA,MAAM,WAAA,GAAc,SAAS,MAAA,CAAO,KAAA;AACpC,EAAA,IAAI,WAAA,EAAa;AAIf,IAAA,OAAA,CAAQ,aAAA,GAAgB,UAAU,WAAW,CAAA,CAAA;AAAA,EAC/C;AAEA,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AAM,IAAI,KAAA;AAAA,MACR,CAAA,oCAAA,EAAuC,IAAI,QAAQ,CAAA,+BAAA;AAAA,KACrD;AAAA,EACF;AAEA,EAAA,IAAI,IAAA,GAAO,GAAA,CAAI,QAAA,CAAS,KAAA,CAAM,UAAU,CAAA,CAAE,CAAC,CAAA,CAAE,KAAA,CAAM,QAAQ,CAAA,CAAE,CAAC,CAAA;AAG9D,EAAA,MAAM,YAAA,GAAe,iCAAiC,MAAM,CAAA;AAG5D,EAAA,IAAI,YAAA,EAAc;AAChB,IAAA,IAAA,GAAO,IAAA,CAAK,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAAA,EACtC;AAGA,EAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AAC/B;;;;"}
1
+ {"version":3,"file":"core.esm.js","sources":["../../src/gitlab/core.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n getGitLabIntegrationRelativePath,\n GitLabIntegrationConfig,\n} from './config';\n\n/**\n * Given a URL pointing to a file on a provider, returns a URL that is suitable\n * for fetching the contents of the data.\n *\n * @remarks\n *\n * Converts\n * from: https://gitlab.example.com/a/b/blob/master/c.yaml\n * to: https://gitlab.com/api/v4/projects/a%2Fb/repository/files/c.yaml/raw?ref=master\n * -or-\n * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n * to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @param token - An optional auth token (not used in path extraction, kept for compatibility)\n * @public\n */\nexport function getGitLabFileFetchUrl(\n url: string,\n config: GitLabIntegrationConfig,\n _token?: string,\n): Promise<string> {\n // Use project path directly instead of making an API call to get project ID\n // Note: _token parameter kept for backward compatibility but not used for path extraction\n const projectPath = extractProjectPath(url, config);\n return Promise.resolve(buildProjectUrl(url, projectPath, config).toString());\n}\n\n/**\n * Gets the request options necessary to make requests to a given provider.\n *\n * @param config - The relevant provider config\n * @param token - An optional auth token to use for communicating with GitLab. By default uses the integration token\n * @public\n */\nexport function getGitLabRequestOptions(\n config: GitLabIntegrationConfig,\n token?: string,\n): { headers: Record<string, string> } {\n const headers: Record<string, string> = {};\n\n const accessToken = token || config.token;\n if (accessToken) {\n // OAuth, Personal, Project, and Group access tokens can all be passed via\n // a bearer authorization header\n // https://docs.gitlab.com/api/rest/authentication/#personalprojectgroup-access-tokens\n headers.Authorization = `Bearer ${accessToken}`;\n }\n\n return { headers };\n}\n\n// Converts\n// from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n// to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch\nfunction buildProjectUrl(\n target: string,\n projectPathOrID: string | Number,\n config: GitLabIntegrationConfig,\n): URL {\n try {\n const url = new URL(target);\n\n const branchAndFilePath = url.pathname\n .split('/blob/')\n .slice(1)\n .join('/blob/');\n const [branch, ...filePath] = branchAndFilePath.split('/');\n const relativePath = getGitLabIntegrationRelativePath(config);\n\n const projectIdentifier = encodeURIComponent(String(projectPathOrID));\n\n url.pathname = [\n ...(relativePath ? [relativePath] : []),\n 'api/v4/projects',\n projectIdentifier,\n 'repository/files',\n encodeURIComponent(decodeURIComponent(filePath.join('/'))),\n 'raw',\n ].join('/');\n\n url.search = `?ref=${branch}`;\n\n return url;\n } catch (e) {\n throw new Error(`Incorrect url: ${target}, ${e}`);\n }\n}\n\n/**\n * Extracts the project path from a GitLab URL\n * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n * to: groupA/teams/teamA/subgroupA/repoA\n */\nexport function extractProjectPath(\n target: string,\n config: GitLabIntegrationConfig,\n): string {\n const url = new URL(target);\n\n if (!url.pathname.includes('/blob/')) {\n throw new Error(\n `Failed extracting project path from ${url.pathname}. Url path must include /blob/.`,\n );\n }\n\n let repo = url.pathname.split('/-/blob/')[0].split('/blob/')[0];\n\n // Get gitlab relative path\n const relativePath = getGitLabIntegrationRelativePath(config);\n\n // Check relative path exist and replace it if it's the case.\n if (relativePath) {\n repo = repo.replace(relativePath, '');\n }\n\n // Remove leading slash\n return repo.replace(/^\\//, '');\n}\n"],"names":[],"mappings":";;AAuCO,SAAS,qBAAA,CACd,GAAA,EACA,MAAA,EACA,MAAA,EACiB;AAGjB,EAAA,MAAM,WAAA,GAAc,kBAAA,CAAmB,GAAA,EAAK,MAAM,CAAA;AAClD,EAAA,OAAO,OAAA,CAAQ,QAAQ,eAAA,CAAgB,GAAA,EAAK,aAAa,MAAM,CAAA,CAAE,UAAU,CAAA;AAC7E;AASO,SAAS,uBAAA,CACd,QACA,KAAA,EACqC;AACrC,EAAA,MAAM,UAAkC,EAAC;AAEzC,EAAA,MAAM,WAAA,GAAc,SAAS,MAAA,CAAO,KAAA;AACpC,EAAA,IAAI,WAAA,EAAa;AAIf,IAAA,OAAA,CAAQ,aAAA,GAAgB,UAAU,WAAW,CAAA,CAAA;AAAA,EAC/C;AAEA,EAAA,OAAO,EAAE,OAAA,EAAQ;AACnB;AAKA,SAAS,eAAA,CACP,MAAA,EACA,eAAA,EACA,MAAA,EACK;AACL,EAAA,IAAI;AACF,IAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,MAAM,CAAA;AAE1B,IAAA,MAAM,iBAAA,GAAoB,GAAA,CAAI,QAAA,CAC3B,KAAA,CAAM,QAAQ,EACd,KAAA,CAAM,CAAC,CAAA,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CAAiC,MAAM,CAAA;AAG5D,EAAA,IAAI,YAAA,EAAc;AAChB,IAAA,IAAA,GAAO,IAAA,CAAK,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAAA,EACtC;AAGA,EAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AAC/B;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.cjs.js","sources":["../src/helpers.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport parseGitUrl from 'git-url-parse';\nimport { trimEnd } from 'lodash';\nimport { ScmIntegration, ScmIntegrationsGroup } from './types';\n\n/**\n * Wraps git-url-parse and rejects URLs whose filepath contains path traversal\n * segments. Without this check, a URL like\n * `https://github.com/o/r/blob/main/%2e%2e%2f%2e%2e%2fuser/repos` would be\n * decoded to `../../user/repos` and could escape the expected API path when\n * interpolated into provider API URLs.\n */\nexport function parseGitUrlSafe(url: string) {\n const parsed = parseGitUrl(url);\n if (parsed.filepath) {\n let decoded = parsed.filepath;\n let previous;\n do {\n previous = decoded;\n try {\n decoded = decodeURIComponent(decoded);\n } catch {\n break;\n }\n } while (decoded !== previous);\n\n if (\n decoded.split('/').some(segment => segment === '..' || segment === '.')\n ) {\n throw new Error(\n 'Invalid SCM URL: path traversal is not allowed in the URL',\n );\n }\n }\n return parsed;\n}\n\n/** Checks whether the given argument is a valid URL hostname */\nexport function isValidHost(host: string): boolean {\n const check = new URL('http://example.com');\n check.host = host;\n return check.host === host;\n}\n\n/** Checks whether the given argument is a valid URL */\nexport function isValidUrl(url: string): boolean {\n try {\n // eslint-disable-next-line no-new\n new URL(url);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function basicIntegrations<T extends ScmIntegration>(\n integrations: T[],\n getHost: (integration: T) => string,\n): ScmIntegrationsGroup<T> {\n return {\n list(): T[] {\n return integrations;\n },\n byUrl(url: string | URL): T | undefined {\n try {\n const parsed = typeof url === 'string' ? new URL(url) : url;\n return integrations.find(i => getHost(i) === parsed.host);\n } catch {\n return undefined;\n }\n },\n byHost(host: string): T | undefined {\n return integrations.find(i => getHost(i) === host);\n },\n };\n}\n\n/**\n * Default implementation of {@link ScmIntegration} `resolveUrl`, that only\n * works with URL pathname based providers.\n *\n * @public\n */\nexport function defaultScmResolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n}): string {\n const { url, base, lineNumber } = options;\n\n // If it is a fully qualified URL - then return it verbatim\n try {\n // eslint-disable-next-line no-new\n new URL(url);\n return url;\n } catch {\n // ignore intentionally\n }\n\n let updated: URL;\n\n if (url.startsWith('/')) {\n // If it is an absolute path, move relative to the repo root\n const { href, filepath } = parseGitUrlSafe(base);\n\n updated = new URL(href);\n\n const repoRootPath = trimEnd(\n updated.pathname.substring(0, updated.pathname.length - filepath.length),\n '/',\n );\n updated.pathname = `${repoRootPath}${url}`;\n } else {\n // For relative URLs, just let the default URL constructor handle the\n // resolving. Note that this essentially will treat the last segment of the\n // base as a file - NOT a folder - unless the url ends in a slash.\n updated = new URL(url, base);\n }\n\n updated.search = new URL(base).search;\n if (lineNumber) {\n updated.hash = `L${lineNumber}`;\n }\n return updated.toString();\n}\n\n/**\n * Sets up handlers for request mocking\n *\n * Copied from test-utils, as that is a frontend-only package\n *\n * @param worker - service worker\n */\nexport function registerMswTestHooks(worker: {\n listen: (t: any) => void;\n close: () => void;\n resetHandlers: () => void;\n}) {\n beforeAll(() => worker.listen({ onUnhandledRequest: 'error' }));\n afterAll(() => worker.close());\n afterEach(() => worker.resetHandlers());\n}\n"],"names":["parseGitUrl","trimE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B,EAAA,IAAI,UAAA,EAAY;AACd,IAAA,OAAA,CAAQ,IAAA,GAAO,IAAI,UAAU,CAAA,CAAA;AAAA,EAC/B;AACA,EAAA,OAAO,QAAQ,QAAA,EAAS;AAC1B;;;;;;;;"}
1
+ {"version":3,"file":"helpers.cjs.js","sources":["../src/helpers.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport parseGitUrl from 'git-url-parse';\nimport { trimEnd } from 'lodash';\nimport { ScmIntegration, ScmIntegrationsGroup } from './types';\n\n/**\n * Wraps git-url-parse and rejects URLs whose filepath contains path traversal\n * segments. Without this check, a URL like\n * `https://github.com/o/r/blob/main/%2e%2e%2f%2e%2e%2fuser/repos` would be\n * decoded to `../../user/repos` and could escape the expected API path when\n * interpolated into provider API URLs.\n */\nexport function parseGitUrlSafe(url: string) {\n const parsed = parseGitUrl(url);\n if (parsed.filepath) {\n let decoded = parsed.filepath;\n let previous;\n do {\n previous = decoded;\n try {\n decoded = decodeURIComponent(decoded);\n } catch {\n break;\n }\n } while (decoded !== previous);\n\n if (\n decoded.split('/').some(segment => segment === '..' || segment === '.')\n ) {\n throw new Error(\n 'Invalid SCM URL: path traversal is not allowed in the URL',\n );\n }\n }\n return parsed;\n}\n\n/** Checks whether the given argument is a valid URL hostname */\nexport function isValidHost(host: string): boolean {\n const check = new URL('http://example.com');\n check.host = host;\n return check.host === host;\n}\n\n/** Checks whether the given argument is a valid URL */\nexport function isValidUrl(url: string): boolean {\n try {\n // eslint-disable-next-line no-new\n new URL(url);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function basicIntegrations<T extends ScmIntegration>(\n integrations: T[],\n getHost: (integration: T) => string,\n): ScmIntegrationsGroup<T> {\n return {\n list(): T[] {\n return integrations;\n },\n byUrl(url: string | URL): T | undefined {\n try {\n const parsed = typeof url === 'string' ? new URL(url) : url;\n return integrations.find(i => getHost(i) === parsed.host);\n } catch {\n return undefined;\n }\n },\n byHost(host: string): T | undefined {\n return integrations.find(i => getHost(i) === host);\n },\n };\n}\n\n/**\n * Default implementation of {@link ScmIntegration} `resolveUrl`, that only\n * works with URL pathname based providers.\n *\n * @public\n */\nexport function defaultScmResolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n}): string {\n const { url, base, lineNumber } = options;\n\n // If it is a fully qualified URL - then return it verbatim\n try {\n // eslint-disable-next-line no-new\n new URL(url);\n return url;\n } catch {\n // ignore intentionally\n }\n\n let updated: URL;\n\n if (url.startsWith('/')) {\n // If it is an absolute path, move relative to the repo root\n const { href, filepath } = parseGitUrlSafe(base);\n\n updated = new URL(href);\n\n const repoRootPath = trimEnd(\n updated.pathname.substring(0, updated.pathname.length - filepath.length),\n '/',\n );\n updated.pathname = `${repoRootPath}${url}`;\n } else {\n // For relative URLs, just let the default URL constructor handle the\n // resolving. Note that this essentially will treat the last segment of the\n // base as a file - NOT a folder - unless the url ends in a slash.\n updated = new URL(url, base);\n }\n\n updated.search = new URL(base).search;\n if (lineNumber) {\n updated.hash = `L${lineNumber}`;\n }\n return updated.toString();\n}\n"],"names":["parseGitUrl","trimEnd"],"mappings":";;;;;;;;;AA2BO,SAAS,gBAAgB,GAAA,EAAa;AAC3C,EAAA,MAAM,MAAA,GAASA,6BAAY,GAAG,CAAA;AAC9B,EAAA,IAAI,OAAO,QAAA,EAAU;AACnB,IAAA,IAAI,UAAU,MAAA,CAAO,QAAA;AACrB,IAAA,IAAI,QAAA;AACJ,IAAA,GAAG;AACD,MAAA,QAAA,GAAW,OAAA;AACX,MAAA,IAAI;AACF,QAAA,OAAA,GAAU,mBAAmB,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,CAAC,MAAM,IAAI,CAAA;AAAA,IACnD;AAAA,GACF;AACF;AAQO,SAAS,qBAAqB,OAAA,EAI1B;AACT,EAAA,MAAM,EAAE,GAAA,EAAK,IAAA,EAAM,UAAA,EAAW,GAAI,OAAA;AAGlC,EAAA,IAAI;AAEF,IAAA,IAAI,IAAI,GAAG,CAAA;AACX,IAAA,OAAO,GAAA;AAAA,EACT,CAAA,CAAA,MAAQ;AAAA,EAER;AAEA,EAAA,IAAI,OAAA;AAEJ,EAAA,IAAI,GAAA,CAAI,UAAA,CAAW,GAAG,CAAA,EAAG;AAEvB,IAAA,MAAM,EAAE,IAAA,EAAM,QAAA,EAAS,GAAI,gBAAgB,IAAI,CAAA;AAE/C,IAAA,OAAA,GAAU,IAAI,IAAI,IAAI,CAAA;AAEtB,IAAA,MAAM,YAAA,GAAeC,cAAA;AAAA,MACnB,OAAA,CAAQ,SAAS,SAAA,CAAU,CAAA,EAAG,QAAQ,QAAA,CAAS,MAAA,GAAS,SAAS,MAAM,CAAA;AAAA,MACvE;AAAA,KACF;AACA,IAAA,OAAA,CAAQ,QAAA,GAAW,CAAA,EAAG,YAAY,CAAA,EAAG,GAAG,CAAA,CAAA;AAAA,EAC1C,CAAA,MAAO;AAIL,IAAA,OAAA,GAAU,IAAI,GAAA,CAAI,GAAA,EAAK,IAAI,CAAA;AAAA,EAC7B;AAEA,EAAA,OAAA,CAAQ,MAAA,GAAS,IAAI,GAAA,CAAI,IAAI,CAAA,CAAE,MAAA;AAC/B,EAAA,IAAI,UAAA,EAAY;AACd,IAAA,OAAA,CAAQ,IAAA,GAAO,IAAI,UAAU,CAAA,CAAA;AAAA,EAC/B;AACA,EAAA,OAAO,QAAQ,QAAA,EAAS;AAC1B;;;;;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.esm.js","sources":["../src/helpers.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport parseGitUrl from 'git-url-parse';\nimport { trimEnd } from 'lodash';\nimport { ScmIntegration, ScmIntegrationsGroup } from './types';\n\n/**\n * Wraps git-url-parse and rejects URLs whose filepath contains path traversal\n * segments. Without this check, a URL like\n * `https://github.com/o/r/blob/main/%2e%2e%2f%2e%2e%2fuser/repos` would be\n * decoded to `../../user/repos` and could escape the expected API path when\n * interpolated into provider API URLs.\n */\nexport function parseGitUrlSafe(url: string) {\n const parsed = parseGitUrl(url);\n if (parsed.filepath) {\n let decoded = parsed.filepath;\n let previous;\n do {\n previous = decoded;\n try {\n decoded = decodeURIComponent(decoded);\n } catch {\n break;\n }\n } while (decoded !== previous);\n\n if (\n decoded.split('/').some(segment => segment === '..' || segment === '.')\n ) {\n throw new Error(\n 'Invalid SCM URL: path traversal is not allowed in the URL',\n );\n }\n }\n return parsed;\n}\n\n/** Checks whether the given argument is a valid URL hostname */\nexport function isValidHost(host: string): boolean {\n const check = new URL('http://example.com');\n check.host = host;\n return check.host === host;\n}\n\n/** Checks whether the given argument is a valid URL */\nexport function isValidUrl(url: string): boolean {\n try {\n // eslint-disable-next-line no-new\n new URL(url);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function basicIntegrations<T extends ScmIntegration>(\n integrations: T[],\n getHost: (integration: T) => string,\n): ScmIntegrationsGroup<T> {\n return {\n list(): T[] {\n return integrations;\n },\n byUrl(url: string | URL): T | undefined {\n try {\n const parsed = typeof url === 'string' ? new URL(url) : url;\n return integrations.find(i => getHost(i) === parsed.host);\n } catch {\n return undefined;\n }\n },\n byHost(host: string): T | undefined {\n return integrations.find(i => getHost(i) === host);\n },\n };\n}\n\n/**\n * Default implementation of {@link ScmIntegration} `resolveUrl`, that only\n * works with URL pathname based providers.\n *\n * @public\n */\nexport function defaultScmResolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n}): string {\n const { url, base, lineNumber } = options;\n\n // If it is a fully qualified URL - then return it verbatim\n try {\n // eslint-disable-next-line no-new\n new URL(url);\n return url;\n } catch {\n // ignore intentionally\n }\n\n let updated: URL;\n\n if (url.startsWith('/')) {\n // If it is an absolute path, move relative to the repo root\n const { href, filepath } = parseGitUrlSafe(base);\n\n updated = new URL(href);\n\n const repoRootPath = trimEnd(\n updated.pathname.substring(0, updated.pathname.length - filepath.length),\n '/',\n );\n updated.pathname = `${repoRootPath}${url}`;\n } else {\n // For relative URLs, just let the default URL constructor handle the\n // resolving. Note that this essentially will treat the last segment of the\n // base as a file - NOT a folder - unless the url ends in a slash.\n updated = new URL(url, base);\n }\n\n updated.search = new URL(base).search;\n if (lineNumber) {\n updated.hash = `L${lineNumber}`;\n }\n return updated.toString();\n}\n\n/**\n * Sets up handlers for request mocking\n *\n * Copied from test-utils, as that is a frontend-only package\n *\n * @param worker - service worker\n */\nexport function registerMswTestHooks(worker: {\n listen: (t: any) => void;\n close: () => void;\n resetHandlers: () => void;\n}) {\n beforeAll(() => worker.listen({ onUnhandledRequest: 'error' }));\n afterAll(() => worker.close());\n afterEach(() => worker.resetHandlers());\n}\n"],"names":[],"mappings":";;;AA2BO,SAAS,gBAAgB,GAAA,EAAa;AAC3C,EAAA,MAAM,MAAA,GAAS,YAAY,GAAG,CAAA;AAC9B,EAAA,IAAI,OAAO,QAAA,EAAU;AACnB,IAAA,IAAI,UAAU,MAAA,CAAO,QAAA;AACrB,IAAA,IAAI,QAAA;AACJ,IAAA,GAAG;AACD,MAAA,QAAA,GAAW,OAAA;AACX,MAAA,IAAI;AACF,QAAA,OAAA,GAAU,mBAAmB,OAAO,CAAA;AAAA,MACtC,CAAA,CAAA,MAAQ;AACN,QAAA;AAAA,MACF;AAAA,IACF,SAAS,OAAA,K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ACT,EAAA,MAAM,EAAE,GAAA,EAAK,IAAA,EAAM,UAAA,EAAW,GAAI,OAAA;AAGlC,EAAA,IAAI;AAEF,IAAA,IAAI,IAAI,GAAG,CAAA;AACX,IAAA,OAAO,GAAA;AAAA,EACT,CAAA,CAAA,MAAQ;AAAA,EAER;AAEA,EAAA,IAAI,OAAA;AAEJ,EAAA,IAAI,GAAA,CAAI,UAAA,CAAW,GAAG,CAAA,EAAG;AAEvB,IAAA,MAAM,EAAE,IAAA,EAAM,QAAA,EAAS,GAAI,gBAAgB,IAAI,CAAA;AAE/C,IAAA,OAAA,GAAU,IAAI,IAAI,IAAI,CAAA;AAEtB,IAAA,MAAM,YAAA,GAAe,OAAA;AAAA,MACnB,OAAA,CAAQ,SAAS,SAAA,CAAU,CAAA,EAAG,QAAQ,QAAA,CAAS,MAAA,GAAS,SAAS,MAAM,CAAA;AAAA,MACvE;AAAA,KACF;AACA,IAAA,OAAA,CAAQ,QAAA,GAAW,CAAA,EAAG,YAAY,CAAA,EAAG,GAAG,CAAA,CAAA;AAAA,EAC1C,CAAA,MAAO;AAIL,IAAA,OAAA,GAAU,IAAI,GAAA,CAAI,GAAA,EAAK,IAAI,CAAA;AAAA,EAC7B;AAEA,EAAA,OAAA,CAAQ,MAAA,GAAS,IAAI,GAAA,CAAI,IAAI,CAAA,CAAE,MAAA;AAC/B,EAAA,IAAI,UAAA,EAAY;AACd,IAAA,OAAA,CAAQ,IAAA,GAAO,IAAI,UAAU,CAAA,CAAA;AAAA,EAC/B;AACA,EAAA,OAAO,QAAQ,QAAA,EAAS;AAC1B;;;;"}
1
+ {"version":3,"file":"helpers.esm.js","sources":["../src/helpers.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport parseGitUrl from 'git-url-parse';\nimport { trimEnd } from 'lodash';\nimport { ScmIntegration, ScmIntegrationsGroup } from './types';\n\n/**\n * Wraps git-url-parse and rejects URLs whose filepath contains path traversal\n * segments. Without this check, a URL like\n * `https://github.com/o/r/blob/main/%2e%2e%2f%2e%2e%2fuser/repos` would be\n * decoded to `../../user/repos` and could escape the expected API path when\n * interpolated into provider API URLs.\n */\nexport function parseGitUrlSafe(url: string) {\n const parsed = parseGitUrl(url);\n if (parsed.filepath) {\n let decoded = parsed.filepath;\n let previous;\n do {\n previous = decoded;\n try {\n decoded = decodeURIComponent(decoded);\n } catch {\n break;\n }\n } while (decoded !== previous);\n\n if (\n decoded.split('/').some(segment => segment === '..' || segment === '.')\n ) {\n throw new Error(\n 'Invalid SCM URL: path traversal is not allowed in the URL',\n );\n }\n }\n return parsed;\n}\n\n/** Checks whether the given argument is a valid URL hostname */\nexport function isValidHost(host: string): boolean {\n const check = new URL('http://example.com');\n check.host = host;\n return check.host === host;\n}\n\n/** Checks whether the given argument is a valid URL */\nexport function isValidUrl(url: string): boolean {\n try {\n // eslint-disable-next-line no-new\n new URL(url);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function basicIntegrations<T extends ScmIntegration>(\n integrations: T[],\n getHost: (integration: T) => string,\n): ScmIntegrationsGroup<T> {\n return {\n list(): T[] {\n return integrations;\n },\n byUrl(url: string | URL): T | undefined {\n try {\n const parsed = typeof url === 'string' ? new URL(url) : url;\n return integrations.find(i => getHost(i) === parsed.host);\n } catch {\n return undefined;\n }\n },\n byHost(host: string): T | undefined {\n return integrations.find(i => getHost(i) === host);\n },\n };\n}\n\n/**\n * Default implementation of {@link ScmIntegration} `resolveUrl`, that only\n * works with URL pathname based providers.\n *\n * @public\n */\nexport function defaultScmResolveUrl(options: {\n url: string;\n base: string;\n lineNumber?: number;\n}): string {\n const { url, base, lineNumber } = options;\n\n // If it is a fully qualified URL - then return it verbatim\n try {\n // eslint-disable-next-line no-new\n new URL(url);\n return url;\n } catch {\n // ignore intentionally\n }\n\n let updated: URL;\n\n if (url.startsWith('/')) {\n // If it is an absolute path, move relative to the repo root\n const { href, filepath } = parseGitUrlSafe(base);\n\n updated = new URL(href);\n\n const repoRootPath = trimEnd(\n updated.pathname.substring(0, updated.pathname.length - filepath.length),\n '/',\n );\n updated.pathname = `${repoRootPath}${url}`;\n } else {\n // For relative URLs, just let the default URL constructor handle the\n // resolving. Note that this essentially will treat the last segment of the\n // base as a file - NOT a folder - unless the url ends in a slash.\n updated = new URL(url, base);\n }\n\n updated.search = new URL(base).search;\n if (lineNumber) {\n updated.hash = `L${lineNumber}`;\n }\n return updated.toString();\n}\n"],"names":[],"mappings":";;;AA2BO,SAAS,gBAAgB,GAAA,EAAa;AAC3C,EAAA,MAAM,MAAA,GAAS,YAAY,GAAG,CAAA;AAC9B,EAAA,IAAI,OAAO,QAAA,EAAU;AACnB,IAAA,IAAI,UAAU,MAAA,CAAO,QAAA;AACrB,IAAA,IAAI,QAAA;AACJ,IAAA,GAAG;AACD,MAAA,QAAA,GAAW,OAAA;AACX,MAAA,IAAI;AACF,QAAA,OAAA,GAAU,mBAAmB,OAAO,CAAA;AAAA,MACtC,CAAA,CAAA,MAAQ;AACN,QAAA;AAAA,MACF;AAAA,IACF,SAAS,OAAA,KAAY,QAAA;AAErB,IAAA,IACE,OAAA,CAAQ,KAAA,CAAM,GAAG,CAAA,CAAE,IAAA,CAAK,aAAW,OAAA,KAAY,IAAA,IAAQ,OAAA,KAAY,GAAG,CAAA,EACtE;AACA,MAAA,MAAM,IAAI,KAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AAAA,EACF;AACA,EAAA,OAAO,MAAA;AACT;AAGO,SAAS,YAAY,IAAA,EAAuB;AACjD,EAAA,MAAM,KAAA,GAAQ,IAAI,GAAA,CAAI,oBAAoB,CAA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,IAAI,CAAA;AAE/C,IAAA,OAAA,GAAU,IAAI,IAAI,IAAI,CAAA;AAEtB,IAAA,MAAM,YAAA,GAAe,OAAA;AAAA,MACnB,OAAA,CAAQ,SAAS,SAAA,CAAU,CAAA,EAAG,QAAQ,QAAA,CAAS,MAAA,GAAS,SAAS,MAAM,CAAA;AAAA,MACvE;AAAA,KACF;AACA,IAAA,OAAA,CAAQ,QAAA,GAAW,CAAA,EAAG,YAAY,CAAA,EAAG,GAAG,CAAA,CAAA;AAAA,EAC1C,CAAA,MAAO;AAIL,IAAA,OAAA,GAAU,IAAI,GAAA,CAAI,GAAA,EAAK,IAAI,CAAA;AAAA,EAC7B;AAEA,EAAA,OAAA,CAAQ,MAAA,GAAS,IAAI,GAAA,CAAI,IAAI,CAAA,CAAE,MAAA;AAC/B,EAAA,IAAI,UAAA,EAAY;AACd,IAAA,OAAA,CAAQ,IAAA,GAAO,IAAI,UAAU,CAAA,CAAA;AAAA,EAC/B;AACA,EAAA,OAAO,QAAQ,QAAA,EAAS;AAC1B;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/integration",
3
- "version": "2.0.1",
3
+ "version": "2.0.2-next.1",
4
4
  "description": "Helpers for managing integrations towards external systems",
5
5
  "backstage": {
6
6
  "role": "common-library"
@@ -39,8 +39,8 @@
39
39
  "dependencies": {
40
40
  "@azure/identity": "^4.0.0",
41
41
  "@azure/storage-blob": "^12.5.0",
42
- "@backstage/config": "^1.3.7",
43
- "@backstage/errors": "^1.3.0",
42
+ "@backstage/config": "1.3.8-next.0",
43
+ "@backstage/errors": "1.3.1-next.0",
44
44
  "@octokit/auth-app": "^4.0.0",
45
45
  "@octokit/rest": "^19.0.3",
46
46
  "cross-fetch": "^4.0.0",
@@ -50,8 +50,9 @@
50
50
  "p-throttle": "^4.1.1"
51
51
  },
52
52
  "devDependencies": {
53
- "@backstage/cli": "^0.36.1",
54
- "@backstage/config-loader": "^1.10.10",
53
+ "@backstage/backend-test-utils": "1.11.3-next.1",
54
+ "@backstage/cli": "0.36.2-next.1",
55
+ "@backstage/config-loader": "1.10.11-next.0",
55
56
  "msw": "^1.0.0"
56
57
  },
57
58
  "configSchema": "config.d.ts",