@backstage/integration 1.20.0-next.2 → 1.21.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # @backstage/integration
2
2
 
3
+ ## 1.21.0-next.0
4
+
5
+ ### Minor Changes
6
+
7
+ - d933f62: Add configurable throttling and retry mechanism for GitLab integration.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @backstage/config@1.3.6
13
+ - @backstage/errors@1.2.7
14
+
15
+ ## 1.20.0
16
+
17
+ ### Minor Changes
18
+
19
+ - 6999f6d: The AzureUrl class in the @backstage/integration package is now able to process BOTH git branches and git tags. Initially this class only processed git branches and threw an error when non-branch Azure URLs were passed in.
20
+
21
+ ### Patch Changes
22
+
23
+ - cc6206e: Added support for `{org}.visualstudio.com` domains used by Azure DevOps
24
+ - 7455dae: Use node prefix on native imports
25
+
3
26
  ## 1.20.0-next.2
4
27
 
5
28
  ### Patch Changes
package/config.d.ts CHANGED
@@ -386,6 +386,28 @@ export interface Config {
386
386
  * @visibility secret
387
387
  */
388
388
  commitSigningKey?: string;
389
+
390
+ /**
391
+ * Retry configuration for requests.
392
+ * @visibility frontend
393
+ */
394
+ retry?: {
395
+ /**
396
+ * Maximum number of retries for failed requests.
397
+ * @visibility frontend
398
+ */
399
+ maxRetries?: number;
400
+ /**
401
+ * HTTP status codes that should trigger a retry.
402
+ * @visibility frontend
403
+ */
404
+ retryStatusCodes?: number[];
405
+ /**
406
+ * Maximum number of API requests allowed per minute. Set to -1 to disable rate limiting.
407
+ * @visibility frontend
408
+ */
409
+ maxApiRequestsPerMinute?: number;
410
+ };
389
411
  }>;
390
412
 
391
413
  /** Integration configuration for Google Cloud Storage */
@@ -2,10 +2,16 @@
2
2
 
3
3
  var helpers = require('../helpers.cjs.js');
4
4
  var config = require('./config.cjs.js');
5
+ var pThrottle = require('p-throttle');
6
+
7
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
8
+
9
+ var pThrottle__default = /*#__PURE__*/_interopDefaultCompat(pThrottle);
5
10
 
6
11
  class GitLabIntegration {
7
12
  constructor(integrationConfig) {
8
13
  this.integrationConfig = integrationConfig;
14
+ this.fetchImpl = this.createFetchStrategy();
9
15
  }
10
16
  static factory = ({ config: config$1 }) => {
11
17
  const configs = config.readGitLabIntegrationConfigs(
@@ -16,6 +22,7 @@ class GitLabIntegration {
16
22
  (i) => i.config.host
17
23
  );
18
24
  };
25
+ fetchImpl;
19
26
  get type() {
20
27
  return "gitlab";
21
28
  }
@@ -31,6 +38,67 @@ class GitLabIntegration {
31
38
  resolveEditUrl(url) {
32
39
  return replaceGitLabUrlType(url, "edit");
33
40
  }
41
+ fetch(input, init) {
42
+ return this.fetchImpl(input, init);
43
+ }
44
+ createFetchStrategy() {
45
+ let fetchFn = async (url, options) => {
46
+ return fetch(url, { ...options, mode: "same-origin" });
47
+ };
48
+ const retryConfig = this.integrationConfig.retry;
49
+ if (retryConfig) {
50
+ fetchFn = this.withRetry(fetchFn, retryConfig);
51
+ if (retryConfig.maxApiRequestsPerMinute && retryConfig.maxApiRequestsPerMinute > 0) {
52
+ fetchFn = pThrottle__default.default({
53
+ limit: retryConfig.maxApiRequestsPerMinute,
54
+ interval: 6e4
55
+ })(fetchFn);
56
+ }
57
+ }
58
+ return fetchFn;
59
+ }
60
+ withRetry(fetchFn, retryConfig) {
61
+ const maxRetries = retryConfig?.maxRetries ?? 0;
62
+ const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];
63
+ if (maxRetries <= 0 || retryStatusCodes.length === 0) {
64
+ return fetchFn;
65
+ }
66
+ return async (url, options) => {
67
+ const abortSignal = options?.signal;
68
+ let response;
69
+ let attempt = 0;
70
+ for (; ; ) {
71
+ response = await fetchFn(url, options);
72
+ if (!retryStatusCodes.includes(response.status)) {
73
+ break;
74
+ }
75
+ if (attempt++ >= maxRetries) {
76
+ break;
77
+ }
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);
80
+ await sleep(delay, abortSignal);
81
+ }
82
+ return response;
83
+ };
84
+ }
85
+ }
86
+ async function sleep(durationMs, abortSignal) {
87
+ if (abortSignal?.aborted) {
88
+ return;
89
+ }
90
+ await new Promise((resolve) => {
91
+ let timeoutHandle = void 0;
92
+ const done = () => {
93
+ if (timeoutHandle) {
94
+ clearTimeout(timeoutHandle);
95
+ }
96
+ abortSignal?.removeEventListener("abort", done);
97
+ resolve();
98
+ };
99
+ timeoutHandle = setTimeout(done, durationMs);
100
+ abortSignal?.addEventListener("abort", done);
101
+ });
34
102
  }
35
103
  function replaceGitLabUrlType(url, type) {
36
104
  return url.replace(/\/\-\/(blob|tree|edit)\//, `/-/${type}/`);
@@ -38,4 +106,5 @@ function replaceGitLabUrlType(url, type) {
38
106
 
39
107
  exports.GitLabIntegration = GitLabIntegration;
40
108
  exports.replaceGitLabUrlType = replaceGitLabUrlType;
109
+ exports.sleep = sleep;
41
110
  //# 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 */\n\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\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 constructor(private readonly integrationConfig: GitLabIntegrationConfig) {}\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\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"],"mappings":";;;;;AA4BO,MAAM,iBAAA,CAA4C;AAAA,EAWvD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAAA,EAA6C;AAAA,EAV1E,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,EAIA,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,eAAe,GAAA,EAAqB;AAClC,IAAA,OAAO,oBAAA,CAAqB,KAAK,MAAM,CAAA;AAAA,EACzC;AACF;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 = 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,K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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;;;;;;"}
@@ -1,9 +1,11 @@
1
1
  import { basicIntegrations, defaultScmResolveUrl } from '../helpers.esm.js';
2
2
  import { readGitLabIntegrationConfigs } from './config.esm.js';
3
+ import pThrottle from 'p-throttle';
3
4
 
4
5
  class GitLabIntegration {
5
6
  constructor(integrationConfig) {
6
7
  this.integrationConfig = integrationConfig;
8
+ this.fetchImpl = this.createFetchStrategy();
7
9
  }
8
10
  static factory = ({ config }) => {
9
11
  const configs = readGitLabIntegrationConfigs(
@@ -14,6 +16,7 @@ class GitLabIntegration {
14
16
  (i) => i.config.host
15
17
  );
16
18
  };
19
+ fetchImpl;
17
20
  get type() {
18
21
  return "gitlab";
19
22
  }
@@ -29,10 +32,71 @@ class GitLabIntegration {
29
32
  resolveEditUrl(url) {
30
33
  return replaceGitLabUrlType(url, "edit");
31
34
  }
35
+ fetch(input, init) {
36
+ return this.fetchImpl(input, init);
37
+ }
38
+ createFetchStrategy() {
39
+ let fetchFn = async (url, options) => {
40
+ return fetch(url, { ...options, mode: "same-origin" });
41
+ };
42
+ const retryConfig = this.integrationConfig.retry;
43
+ if (retryConfig) {
44
+ fetchFn = this.withRetry(fetchFn, retryConfig);
45
+ if (retryConfig.maxApiRequestsPerMinute && retryConfig.maxApiRequestsPerMinute > 0) {
46
+ fetchFn = pThrottle({
47
+ limit: retryConfig.maxApiRequestsPerMinute,
48
+ interval: 6e4
49
+ })(fetchFn);
50
+ }
51
+ }
52
+ return fetchFn;
53
+ }
54
+ withRetry(fetchFn, retryConfig) {
55
+ const maxRetries = retryConfig?.maxRetries ?? 0;
56
+ const retryStatusCodes = retryConfig?.retryStatusCodes ?? [];
57
+ if (maxRetries <= 0 || retryStatusCodes.length === 0) {
58
+ return fetchFn;
59
+ }
60
+ return async (url, options) => {
61
+ const abortSignal = options?.signal;
62
+ let response;
63
+ let attempt = 0;
64
+ for (; ; ) {
65
+ response = await fetchFn(url, options);
66
+ if (!retryStatusCodes.includes(response.status)) {
67
+ break;
68
+ }
69
+ if (attempt++ >= maxRetries) {
70
+ break;
71
+ }
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);
74
+ await sleep(delay, abortSignal);
75
+ }
76
+ return response;
77
+ };
78
+ }
79
+ }
80
+ async function sleep(durationMs, abortSignal) {
81
+ if (abortSignal?.aborted) {
82
+ return;
83
+ }
84
+ await new Promise((resolve) => {
85
+ let timeoutHandle = void 0;
86
+ const done = () => {
87
+ if (timeoutHandle) {
88
+ clearTimeout(timeoutHandle);
89
+ }
90
+ abortSignal?.removeEventListener("abort", done);
91
+ resolve();
92
+ };
93
+ timeoutHandle = setTimeout(done, durationMs);
94
+ abortSignal?.addEventListener("abort", done);
95
+ });
32
96
  }
33
97
  function replaceGitLabUrlType(url, type) {
34
98
  return url.replace(/\/\-\/(blob|tree|edit)\//, `/-/${type}/`);
35
99
  }
36
100
 
37
- export { GitLabIntegration, replaceGitLabUrlType };
101
+ export { GitLabIntegration, replaceGitLabUrlType, sleep };
38
102
  //# 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 */\n\nimport { basicIntegrations, defaultScmResolveUrl } from '../helpers';\nimport { ScmIntegration, ScmIntegrationsFactory } from '../types';\nimport {\n GitLabIntegrationConfig,\n readGitLabIntegrationConfigs,\n} from './config';\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 constructor(private readonly integrationConfig: GitLabIntegrationConfig) {}\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\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":";;;AA4BO,MAAM,iBAAA,CAA4C;AAAA,EAWvD,YAA6B,iBAAA,EAA4C;AAA5C,IAAA,IAAA,CAAA,iBAAA,GAAA,iBAAA;AAAA,EAA6C;AAAA,EAV1E,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,EAIA,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,OAAO,qBAAqB,OAAO,CAAA;AAAA,EACrC;AAAA,EAEA,eAAe,GAAA,EAAqB;AAClC,IAAA,OAAO,oBAAA,CAAqB,KAAK,MAAM,CAAA;AAAA,EACzC;AACF;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 = 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,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,OAAO,qBAAqB,OAAO,CAAA;AAAA,EACrC;AAAA,EAEA,eAAe,GAAA,EAAqB;AAClC,IAAA,OAAO,oBAAA,CAAqB,KAAK,MAAM,CAAA;AAAA,EACzC;AAAA,EAEA,KAAA,CAAM,OAA0B,IAAA,EAAuC;AACrE,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,KAAA,EAAO,IAAI,CAAA;AAAA,EACnC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BASE_URL = "https://gitlab.com/api/v4";
8
+ function readOptionalNumberArray(config, key) {
9
+ const value = config.getOptional(key);
10
+ if (value === void 0) {
11
+ return void 0;
12
+ }
13
+ if (!Array.isArray(value)) {
14
+ throw new Error(
15
+ `Invalid ${key} config: expected an array, got ${typeof value}`
16
+ );
17
+ }
18
+ return value.map((item, index) => {
19
+ if (typeof item !== "number") {
20
+ throw new Error(
21
+ `Invalid ${key} config: all values must be numbers, got ${typeof item} at index ${index}`
22
+ );
23
+ }
24
+ return item;
25
+ });
26
+ }
8
27
  function readGitLabIntegrationConfig(config) {
9
28
  const host = config.getString("host");
10
29
  let apiBaseUrl = config.getOptionalString("apiBaseUrl");
@@ -33,12 +52,19 @@ function readGitLabIntegrationConfig(config) {
33
52
  `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl`
34
53
  );
35
54
  }
55
+ const retryConfig = config.getOptionalConfig("retry");
56
+ const retry = retryConfig ? {
57
+ maxRetries: retryConfig.getOptionalNumber("maxRetries") ?? 0,
58
+ retryStatusCodes: readOptionalNumberArray(retryConfig, "retryStatusCodes") ?? [],
59
+ maxApiRequestsPerMinute: retryConfig.getOptionalNumber("maxApiRequestsPerMinute") ?? -1
60
+ } : void 0;
36
61
  return {
37
62
  host,
38
63
  token,
39
64
  apiBaseUrl,
40
65
  baseUrl,
41
- commitSigningKey: config.getOptionalString("commitSigningKey")
66
+ commitSigningKey: config.getOptionalString("commitSigningKey"),
67
+ retry
42
68
  };
43
69
  }
44
70
  function readGitLabIntegrationConfigs(configs) {
@@ -1 +1 @@
1
- {"version":3,"file":"config.cjs.js","sources":["../../src/gitlab/config.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 { Config } from '@backstage/config';\nimport { trimEnd } from 'lodash';\nimport { isValidHost, isValidUrl } from '../helpers';\n\nconst GITLAB_HOST = 'gitlab.com';\nconst GITLAB_API_BASE_URL = 'https://gitlab.com/api/v4';\n\n/**\n * The configuration parameters for a single GitLab integration.\n *\n * @public\n */\nexport type GitLabIntegrationConfig = {\n /**\n * The host of the target that this matches on, e.g. `gitlab.com`.\n */\n host: string;\n\n /**\n * The base URL of the API of this provider, e.g.\n * `https://gitlab.com/api/v4`, with no trailing slash.\n *\n * May be omitted specifically for public GitLab; then it will be deduced.\n */\n apiBaseUrl: string;\n\n /**\n * The authorization token to use for requests to this provider.\n *\n * If no token is specified, anonymous access is used.\n */\n token?: string;\n\n /**\n * The baseUrl of this provider, e.g. `https://gitlab.com`, which is passed\n * into the GitLab client.\n *\n * If no baseUrl is provided, it will default to `https://${host}`\n */\n baseUrl: string;\n\n /**\n * Signing key to sign commits\n */\n commitSigningKey?: string;\n};\n\n/**\n * Reads a single GitLab integration config.\n *\n * @param config - The config object of a single integration\n * @public\n */\nexport function readGitLabIntegrationConfig(\n config: Config,\n): GitLabIntegrationConfig {\n const host = config.getString('host');\n let apiBaseUrl = config.getOptionalString('apiBaseUrl');\n const token = config.getOptionalString('token')?.trim();\n let baseUrl = config.getOptionalString('baseUrl');\n if (apiBaseUrl) {\n apiBaseUrl = trimEnd(apiBaseUrl, '/');\n } else if (host === GITLAB_HOST) {\n apiBaseUrl = GITLAB_API_BASE_URL;\n }\n\n if (baseUrl) {\n baseUrl = trimEnd(baseUrl, '/');\n } else {\n baseUrl = `https://${host}`;\n }\n\n if (!isValidHost(host)) {\n throw new Error(\n `Invalid GitLab integration config, '${host}' is not a valid host`,\n );\n } else if (!apiBaseUrl || !isValidUrl(apiBaseUrl)) {\n throw new Error(\n `Invalid GitLab integration config, '${apiBaseUrl}' is not a valid apiBaseUrl`,\n );\n } else if (!isValidUrl(baseUrl)) {\n throw new Error(\n `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl`,\n );\n }\n\n return {\n host,\n token,\n apiBaseUrl,\n baseUrl,\n commitSigningKey: config.getOptionalString('commitSigningKey'),\n };\n}\n\n/**\n * Reads a set of GitLab integration configs, and inserts some defaults for\n * public GitLab if not specified.\n *\n * @param configs - All of the integration config objects\n * @public\n */\nexport function readGitLabIntegrationConfigs(\n configs: Config[],\n): GitLabIntegrationConfig[] {\n // First read all the explicit integrations\n const result = configs.map(readGitLabIntegrationConfig);\n\n // As a convenience we always make sure there's at least an unauthenticated\n // reader for public gitlab repos.\n if (!result.some(c => c.host === GITLAB_HOST)) {\n result.push({\n host: GITLAB_HOST,\n apiBaseUrl: GITLAB_API_BASE_URL,\n baseUrl: `https://${GITLAB_HOST}`,\n });\n }\n\n return result;\n}\n\n/**\n * Reads a GitLab integration config, and returns\n * relative path.\n *\n * @param config - GitLabIntegrationConfig object\n * @public\n */\nexport function getGitLabIntegrationRelativePath(\n config: GitLabIntegrationConfig,\n): string {\n let relativePath = '';\n if (config.host !== GITLAB_HOST) {\n relativePath = new URL(config.baseUrl).pathname;\n }\n return trimEnd(relativePath, '/');\n}\n"],"names":["trimEnd","isValidHost","isValidUrl"],"mappings":";;;;;AAoBA,MAAM,WAAA,GAAc,YAAA;AACpB,MAAM,mBAAA,GAAsB,2BAAA;AAgDrB,SAAS,4BACd,MAAA,EACyB;AACzB,EAAA,MAAM,IAAA,GAAO,MAAA,CAAO,SAAA,CAAU,MAAM,CAAA;AACpC,EAAA,IAAI,UAAA,GAAa,MAAA,CAAO,iBAAA,CAAkB,YAAY,CAAA;AACtD,EAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,iBAAA,CAAkB,OAAO,GAAG,IAAA,EAAK;AACtD,EAAA,IAAI,OAAA,GAAU,MAAA,CAAO,iBAAA,CAAkB,SAAS,CAAA;AAChD,EAAA,IAAI,UAAA,EAAY;AACd,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AA,EAAS,WAAW,WAAW,CAAA;AAAA,KAChC,CAAA;AAAA,EACH;AAEA,EAAA,OAAO,MAAA;AACT;AASO,SAAS,iCACd,MAAA,EACQ;AACR,EAAA,IAAI,YAAA,GAAe,EAAA;AACnB,EAAA,IAAI,MAAA,CAAO,SAAS,WAAA,EAAa;AAC/B,IAAA,YAAA,GAAe,IAAI,GAAA,CAAI,MAAA,CAAO,OAAO,CAAA,CAAE,QAAA;AAAA,EACzC;AACA,EAAA,OAAOF,cAAA,CAAQ,cAAc,GAAG,CAAA;AAClC;;;;;;"}
1
+ {"version":3,"file":"config.cjs.js","sources":["../../src/gitlab/config.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 { Config } from '@backstage/config';\nimport { trimEnd } from 'lodash';\nimport { isValidHost, isValidUrl } from '../helpers';\n\nconst GITLAB_HOST = 'gitlab.com';\nconst GITLAB_API_BASE_URL = 'https://gitlab.com/api/v4';\n\n/**\n * Reads an optional number array from config\n */\nfunction readOptionalNumberArray(\n config: Config,\n key: string,\n): number[] | undefined {\n const value = config.getOptional(key);\n if (value === undefined) {\n return undefined;\n }\n if (!Array.isArray(value)) {\n throw new Error(\n `Invalid ${key} config: expected an array, got ${typeof value}`,\n );\n }\n return value.map((item, index) => {\n if (typeof item !== 'number') {\n throw new Error(\n `Invalid ${key} config: all values must be numbers, got ${typeof item} at index ${index}`,\n );\n }\n return item;\n });\n}\n\n/**\n * The configuration parameters for a single GitLab integration.\n *\n * @public\n */\nexport type GitLabIntegrationConfig = {\n /**\n * The host of the target that this matches on, e.g. `gitlab.com`.\n */\n host: string;\n\n /**\n * The base URL of the API of this provider, e.g.\n * `https://gitlab.com/api/v4`, with no trailing slash.\n *\n * May be omitted specifically for public GitLab; then it will be deduced.\n */\n apiBaseUrl: string;\n\n /**\n * The authorization token to use for requests to this provider.\n *\n * If no token is specified, anonymous access is used.\n */\n token?: string;\n\n /**\n * The baseUrl of this provider, e.g. `https://gitlab.com`, which is passed\n * into the GitLab client.\n *\n * If no baseUrl is provided, it will default to `https://${host}`\n */\n baseUrl: string;\n\n /**\n * Signing key to sign commits\n */\n commitSigningKey?: string;\n\n /**\n * Retry configuration for failed requests.\n */\n retry?: {\n /**\n * Maximum number of retries for failed requests\n * @defaultValue 0\n */\n maxRetries?: number;\n\n /**\n * HTTP status codes that should trigger a retry\n * @defaultValue []\n */\n retryStatusCodes?: number[];\n\n /**\n * Rate limit for requests per minute\n * @defaultValue -1\n */\n maxApiRequestsPerMinute?: number;\n };\n};\n\n/**\n * Reads a single GitLab integration config.\n *\n * @param config - The config object of a single integration\n * @public\n */\nexport function readGitLabIntegrationConfig(\n config: Config,\n): GitLabIntegrationConfig {\n const host = config.getString('host');\n let apiBaseUrl = config.getOptionalString('apiBaseUrl');\n const token = config.getOptionalString('token')?.trim();\n let baseUrl = config.getOptionalString('baseUrl');\n if (apiBaseUrl) {\n apiBaseUrl = trimEnd(apiBaseUrl, '/');\n } else if (host === GITLAB_HOST) {\n apiBaseUrl = GITLAB_API_BASE_URL;\n }\n\n if (baseUrl) {\n baseUrl = trimEnd(baseUrl, '/');\n } else {\n baseUrl = `https://${host}`;\n }\n\n if (!isValidHost(host)) {\n throw new Error(\n `Invalid GitLab integration config, '${host}' is not a valid host`,\n );\n } else if (!apiBaseUrl || !isValidUrl(apiBaseUrl)) {\n throw new Error(\n `Invalid GitLab integration config, '${apiBaseUrl}' is not a valid apiBaseUrl`,\n );\n } else if (!isValidUrl(baseUrl)) {\n throw new Error(\n `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl`,\n );\n }\n\n const retryConfig = config.getOptionalConfig('retry');\n\n const retry = retryConfig\n ? {\n maxRetries: retryConfig.getOptionalNumber('maxRetries') ?? 0,\n retryStatusCodes:\n readOptionalNumberArray(retryConfig, 'retryStatusCodes') ?? [],\n maxApiRequestsPerMinute:\n retryConfig.getOptionalNumber('maxApiRequestsPerMinute') ?? -1,\n }\n : undefined;\n\n return {\n host,\n token,\n apiBaseUrl,\n baseUrl,\n commitSigningKey: config.getOptionalString('commitSigningKey'),\n retry,\n };\n}\n\n/**\n * Reads a set of GitLab integration configs, and inserts some defaults for\n * public GitLab if not specified.\n *\n * @param configs - All of the integration config objects\n * @public\n */\nexport function readGitLabIntegrationConfigs(\n configs: Config[],\n): GitLabIntegrationConfig[] {\n // First read all the explicit integrations\n const result = configs.map(readGitLabIntegrationConfig);\n\n // As a convenience we always make sure there's at least an unauthenticated\n // reader for public gitlab repos.\n if (!result.some(c => c.host === GITLAB_HOST)) {\n result.push({\n host: GITLAB_HOST,\n apiBaseUrl: GITLAB_API_BASE_URL,\n baseUrl: `https://${GITLAB_HOST}`,\n });\n }\n\n return result;\n}\n\n/**\n * Reads a GitLab integration config, and returns\n * relative path.\n *\n * @param config - GitLabIntegrationConfig object\n * @public\n */\nexport function getGitLabIntegrationRelativePath(\n config: GitLabIntegrationConfig,\n): string {\n let relativePath = '';\n if (config.host !== GITLAB_HOST) {\n relativePath = new URL(config.baseUrl).pathname;\n }\n return trimEnd(relativePath, '/');\n}\n"],"names":["trimEnd","isValidHost","isValidUrl"],"mappings":";;;;;AAoBA,MAAM,WAAA,GAAc,YAAA;AACpB,MAAM,mBAAA,GAAsB,2BAAA;AAK5B,SAAS,uBAAA,CACP,QACA,GAAA,EACsB;AACtB,EAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,WAAA,CAAY,GAAG,CAAA;AACpC,EAAA,IAAI,UAAU,MAAA,EAAW;AACvB,IAAA,OAAO,MAAA;AAAA,EACT;AACA,EAAA,IAAI,CAAC,KAAA,CAAM,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A,OAAO,MAAA;AACT;AASO,SAAS,iCACd,MAAA,EACQ;AACR,EAAA,IAAI,YAAA,GAAe,EAAA;AACnB,EAAA,IAAI,MAAA,CAAO,SAAS,WAAA,EAAa;AAC/B,IAAA,YAAA,GAAe,IAAI,GAAA,CAAI,MAAA,CAAO,OAAO,CAAA,CAAE,QAAA;AAAA,EACzC;AACA,EAAA,OAAOF,cAAA,CAAQ,cAAc,GAAG,CAAA;AAClC;;;;;;"}
@@ -3,6 +3,25 @@ import { isValidHost, isValidUrl } from '../helpers.esm.js';
3
3
 
4
4
  const GITLAB_HOST = "gitlab.com";
5
5
  const GITLAB_API_BASE_URL = "https://gitlab.com/api/v4";
6
+ function readOptionalNumberArray(config, key) {
7
+ const value = config.getOptional(key);
8
+ if (value === void 0) {
9
+ return void 0;
10
+ }
11
+ if (!Array.isArray(value)) {
12
+ throw new Error(
13
+ `Invalid ${key} config: expected an array, got ${typeof value}`
14
+ );
15
+ }
16
+ return value.map((item, index) => {
17
+ if (typeof item !== "number") {
18
+ throw new Error(
19
+ `Invalid ${key} config: all values must be numbers, got ${typeof item} at index ${index}`
20
+ );
21
+ }
22
+ return item;
23
+ });
24
+ }
6
25
  function readGitLabIntegrationConfig(config) {
7
26
  const host = config.getString("host");
8
27
  let apiBaseUrl = config.getOptionalString("apiBaseUrl");
@@ -31,12 +50,19 @@ function readGitLabIntegrationConfig(config) {
31
50
  `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl`
32
51
  );
33
52
  }
53
+ const retryConfig = config.getOptionalConfig("retry");
54
+ const retry = retryConfig ? {
55
+ maxRetries: retryConfig.getOptionalNumber("maxRetries") ?? 0,
56
+ retryStatusCodes: readOptionalNumberArray(retryConfig, "retryStatusCodes") ?? [],
57
+ maxApiRequestsPerMinute: retryConfig.getOptionalNumber("maxApiRequestsPerMinute") ?? -1
58
+ } : void 0;
34
59
  return {
35
60
  host,
36
61
  token,
37
62
  apiBaseUrl,
38
63
  baseUrl,
39
- commitSigningKey: config.getOptionalString("commitSigningKey")
64
+ commitSigningKey: config.getOptionalString("commitSigningKey"),
65
+ retry
40
66
  };
41
67
  }
42
68
  function readGitLabIntegrationConfigs(configs) {
@@ -1 +1 @@
1
- {"version":3,"file":"config.esm.js","sources":["../../src/gitlab/config.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 { Config } from '@backstage/config';\nimport { trimEnd } from 'lodash';\nimport { isValidHost, isValidUrl } from '../helpers';\n\nconst GITLAB_HOST = 'gitlab.com';\nconst GITLAB_API_BASE_URL = 'https://gitlab.com/api/v4';\n\n/**\n * The configuration parameters for a single GitLab integration.\n *\n * @public\n */\nexport type GitLabIntegrationConfig = {\n /**\n * The host of the target that this matches on, e.g. `gitlab.com`.\n */\n host: string;\n\n /**\n * The base URL of the API of this provider, e.g.\n * `https://gitlab.com/api/v4`, with no trailing slash.\n *\n * May be omitted specifically for public GitLab; then it will be deduced.\n */\n apiBaseUrl: string;\n\n /**\n * The authorization token to use for requests to this provider.\n *\n * If no token is specified, anonymous access is used.\n */\n token?: string;\n\n /**\n * The baseUrl of this provider, e.g. `https://gitlab.com`, which is passed\n * into the GitLab client.\n *\n * If no baseUrl is provided, it will default to `https://${host}`\n */\n baseUrl: string;\n\n /**\n * Signing key to sign commits\n */\n commitSigningKey?: string;\n};\n\n/**\n * Reads a single GitLab integration config.\n *\n * @param config - The config object of a single integration\n * @public\n */\nexport function readGitLabIntegrationConfig(\n config: Config,\n): GitLabIntegrationConfig {\n const host = config.getString('host');\n let apiBaseUrl = config.getOptionalString('apiBaseUrl');\n const token = config.getOptionalString('token')?.trim();\n let baseUrl = config.getOptionalString('baseUrl');\n if (apiBaseUrl) {\n apiBaseUrl = trimEnd(apiBaseUrl, '/');\n } else if (host === GITLAB_HOST) {\n apiBaseUrl = GITLAB_API_BASE_URL;\n }\n\n if (baseUrl) {\n baseUrl = trimEnd(baseUrl, '/');\n } else {\n baseUrl = `https://${host}`;\n }\n\n if (!isValidHost(host)) {\n throw new Error(\n `Invalid GitLab integration config, '${host}' is not a valid host`,\n );\n } else if (!apiBaseUrl || !isValidUrl(apiBaseUrl)) {\n throw new Error(\n `Invalid GitLab integration config, '${apiBaseUrl}' is not a valid apiBaseUrl`,\n );\n } else if (!isValidUrl(baseUrl)) {\n throw new Error(\n `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl`,\n );\n }\n\n return {\n host,\n token,\n apiBaseUrl,\n baseUrl,\n commitSigningKey: config.getOptionalString('commitSigningKey'),\n };\n}\n\n/**\n * Reads a set of GitLab integration configs, and inserts some defaults for\n * public GitLab if not specified.\n *\n * @param configs - All of the integration config objects\n * @public\n */\nexport function readGitLabIntegrationConfigs(\n configs: Config[],\n): GitLabIntegrationConfig[] {\n // First read all the explicit integrations\n const result = configs.map(readGitLabIntegrationConfig);\n\n // As a convenience we always make sure there's at least an unauthenticated\n // reader for public gitlab repos.\n if (!result.some(c => c.host === GITLAB_HOST)) {\n result.push({\n host: GITLAB_HOST,\n apiBaseUrl: GITLAB_API_BASE_URL,\n baseUrl: `https://${GITLAB_HOST}`,\n });\n }\n\n return result;\n}\n\n/**\n * Reads a GitLab integration config, and returns\n * relative path.\n *\n * @param config - GitLabIntegrationConfig object\n * @public\n */\nexport function getGitLabIntegrationRelativePath(\n config: GitLabIntegrationConfig,\n): string {\n let relativePath = '';\n if (config.host !== GITLAB_HOST) {\n relativePath = new URL(config.baseUrl).pathname;\n }\n return trimEnd(relativePath, '/');\n}\n"],"names":[],"mappings":";;;AAoBA,MAAM,WAAA,GAAc,YAAA;AACpB,MAAM,mBAAA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,MAAA,CAAO,iBAAA,CAAkB,kBAAkB;AAAA,GAC/D;AACF;AASO,SAAS,6BACd,OAAA,EAC2B;AAE3B,EAAA,MAAM,MAAA,GAAS,OAAA,CAAQ,GAAA,CAAI,2BAA2B,CAAA;AAItD,EAAA,IAAI,CAAC,MAAA,CAAO,IAAA,CAAK,OAAK,CAAA,CAAE,IAAA,KAAS,WAAW,CAAA,EAAG;AAC7C,IAAA,MAAA,CAAO,IAAA,CAAK;AAAA,MACV,IAAA,EAAM,WAAA;AAAA,MACN,UAAA,EAAY,mBAAA;AAAA,MACZ,OAAA,EAAS,WAAW,WAAW,CAAA;AAAA,KAChC,CAAA;AAAA,EACH;AAEA,EAAA,OAAO,MAAA;AACT;AASO,SAAS,iCACd,MAAA,EACQ;AACR,EAAA,IAAI,YAAA,GAAe,EAAA;AACnB,EAAA,IAAI,MAAA,CAAO,SAAS,WAAA,EAAa;AAC/B,IAAA,YAAA,GAAe,IAAI,GAAA,CAAI,MAAA,CAAO,OAAO,CAAA,CAAE,QAAA;AAAA,EACzC;AACA,EAAA,OAAO,OAAA,CAAQ,cAAc,GAAG,CAAA;AAClC;;;;"}
1
+ {"version":3,"file":"config.esm.js","sources":["../../src/gitlab/config.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 { Config } from '@backstage/config';\nimport { trimEnd } from 'lodash';\nimport { isValidHost, isValidUrl } from '../helpers';\n\nconst GITLAB_HOST = 'gitlab.com';\nconst GITLAB_API_BASE_URL = 'https://gitlab.com/api/v4';\n\n/**\n * Reads an optional number array from config\n */\nfunction readOptionalNumberArray(\n config: Config,\n key: string,\n): number[] | undefined {\n const value = config.getOptional(key);\n if (value === undefined) {\n return undefined;\n }\n if (!Array.isArray(value)) {\n throw new Error(\n `Invalid ${key} config: expected an array, got ${typeof value}`,\n );\n }\n return value.map((item, index) => {\n if (typeof item !== 'number') {\n throw new Error(\n `Invalid ${key} config: all values must be numbers, got ${typeof item} at index ${index}`,\n );\n }\n return item;\n });\n}\n\n/**\n * The configuration parameters for a single GitLab integration.\n *\n * @public\n */\nexport type GitLabIntegrationConfig = {\n /**\n * The host of the target that this matches on, e.g. `gitlab.com`.\n */\n host: string;\n\n /**\n * The base URL of the API of this provider, e.g.\n * `https://gitlab.com/api/v4`, with no trailing slash.\n *\n * May be omitted specifically for public GitLab; then it will be deduced.\n */\n apiBaseUrl: string;\n\n /**\n * The authorization token to use for requests to this provider.\n *\n * If no token is specified, anonymous access is used.\n */\n token?: string;\n\n /**\n * The baseUrl of this provider, e.g. `https://gitlab.com`, which is passed\n * into the GitLab client.\n *\n * If no baseUrl is provided, it will default to `https://${host}`\n */\n baseUrl: string;\n\n /**\n * Signing key to sign commits\n */\n commitSigningKey?: string;\n\n /**\n * Retry configuration for failed requests.\n */\n retry?: {\n /**\n * Maximum number of retries for failed requests\n * @defaultValue 0\n */\n maxRetries?: number;\n\n /**\n * HTTP status codes that should trigger a retry\n * @defaultValue []\n */\n retryStatusCodes?: number[];\n\n /**\n * Rate limit for requests per minute\n * @defaultValue -1\n */\n maxApiRequestsPerMinute?: number;\n };\n};\n\n/**\n * Reads a single GitLab integration config.\n *\n * @param config - The config object of a single integration\n * @public\n */\nexport function readGitLabIntegrationConfig(\n config: Config,\n): GitLabIntegrationConfig {\n const host = config.getString('host');\n let apiBaseUrl = config.getOptionalString('apiBaseUrl');\n const token = config.getOptionalString('token')?.trim();\n let baseUrl = config.getOptionalString('baseUrl');\n if (apiBaseUrl) {\n apiBaseUrl = trimEnd(apiBaseUrl, '/');\n } else if (host === GITLAB_HOST) {\n apiBaseUrl = GITLAB_API_BASE_URL;\n }\n\n if (baseUrl) {\n baseUrl = trimEnd(baseUrl, '/');\n } else {\n baseUrl = `https://${host}`;\n }\n\n if (!isValidHost(host)) {\n throw new Error(\n `Invalid GitLab integration config, '${host}' is not a valid host`,\n );\n } else if (!apiBaseUrl || !isValidUrl(apiBaseUrl)) {\n throw new Error(\n `Invalid GitLab integration config, '${apiBaseUrl}' is not a valid apiBaseUrl`,\n );\n } else if (!isValidUrl(baseUrl)) {\n throw new Error(\n `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl`,\n );\n }\n\n const retryConfig = config.getOptionalConfig('retry');\n\n const retry = retryConfig\n ? {\n maxRetries: retryConfig.getOptionalNumber('maxRetries') ?? 0,\n retryStatusCodes:\n readOptionalNumberArray(retryConfig, 'retryStatusCodes') ?? [],\n maxApiRequestsPerMinute:\n retryConfig.getOptionalNumber('maxApiRequestsPerMinute') ?? -1,\n }\n : undefined;\n\n return {\n host,\n token,\n apiBaseUrl,\n baseUrl,\n commitSigningKey: config.getOptionalString('commitSigningKey'),\n retry,\n };\n}\n\n/**\n * Reads a set of GitLab integration configs, and inserts some defaults for\n * public GitLab if not specified.\n *\n * @param configs - All of the integration config objects\n * @public\n */\nexport function readGitLabIntegrationConfigs(\n configs: Config[],\n): GitLabIntegrationConfig[] {\n // First read all the explicit integrations\n const result = configs.map(readGitLabIntegrationConfig);\n\n // As a convenience we always make sure there's at least an unauthenticated\n // reader for public gitlab repos.\n if (!result.some(c => c.host === GITLAB_HOST)) {\n result.push({\n host: GITLAB_HOST,\n apiBaseUrl: GITLAB_API_BASE_URL,\n baseUrl: `https://${GITLAB_HOST}`,\n });\n }\n\n return result;\n}\n\n/**\n * Reads a GitLab integration config, and returns\n * relative path.\n *\n * @param config - GitLabIntegrationConfig object\n * @public\n */\nexport function getGitLabIntegrationRelativePath(\n config: GitLabIntegrationConfig,\n): string {\n let relativePath = '';\n if (config.host !== GITLAB_HOST) {\n relativePath = new URL(config.baseUrl).p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,MAAA,GAAS,OAAA,CAAQ,GAAA,CAAI,2BAA2B,CAAA;AAItD,EAAA,IAAI,CAAC,MAAA,CAAO,IAAA,CAAK,OAAK,CAAA,CAAE,IAAA,KAAS,WAAW,CAAA,EAAG;AAC7C,IAAA,MAAA,CAAO,IAAA,CAAK;AAAA,MACV,IAAA,EAAM,WAAA;AAAA,MACN,UAAA,EAAY,mBAAA;AAAA,MACZ,OAAA,EAAS,WAAW,WAAW,CAAA;AAAA,KAChC,CAAA;AAAA,EACH;AAEA,EAAA,OAAO,MAAA;AACT;AASO,SAAS,iCACd,MAAA,EACQ;AACR,EAAA,IAAI,YAAA,GAAe,EAAA;AACnB,EAAA,IAAI,MAAA,CAAO,SAAS,WAAA,EAAa;AAC/B,IAAA,YAAA,GAAe,IAAI,GAAA,CAAI,MAAA,CAAO,OAAO,CAAA,CAAE,QAAA;AAAA,EACzC;AACA,EAAA,OAAO,OAAA,CAAQ,cAAc,GAAG,CAAA;AAClC;;;;"}
@@ -1,15 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var fetch = require('cross-fetch');
4
3
  var config = require('./config.cjs.js');
5
4
 
6
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
7
-
8
- var fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
9
-
10
- async function getGitLabFileFetchUrl(url, config, token) {
11
- const projectID = await getProjectId(url, config, token);
12
- return buildProjectUrl(url, projectID, config).toString();
5
+ function getGitLabFileFetchUrl(url, config, _token) {
6
+ const projectPath = extractProjectPath(url, config);
7
+ return Promise.resolve(buildProjectUrl(url, projectPath, config).toString());
13
8
  }
14
9
  function getGitLabRequestOptions(config, token) {
15
10
  const headers = {};
@@ -19,16 +14,17 @@ function getGitLabRequestOptions(config, token) {
19
14
  }
20
15
  return { headers };
21
16
  }
22
- function buildProjectUrl(target, projectID, config$1) {
17
+ function buildProjectUrl(target, projectPathOrID, config$1) {
23
18
  try {
24
19
  const url = new URL(target);
25
20
  const branchAndFilePath = url.pathname.split("/blob/").slice(1).join("/blob/");
26
21
  const [branch, ...filePath] = branchAndFilePath.split("/");
27
22
  const relativePath = config.getGitLabIntegrationRelativePath(config$1);
23
+ const projectIdentifier = encodeURIComponent(String(projectPathOrID));
28
24
  url.pathname = [
29
25
  ...relativePath ? [relativePath] : [],
30
26
  "api/v4/projects",
31
- projectID,
27
+ projectIdentifier,
32
28
  "repository/files",
33
29
  encodeURIComponent(decodeURIComponent(filePath.join("/"))),
34
30
  "raw"
@@ -39,47 +35,23 @@ function buildProjectUrl(target, projectID, config$1) {
39
35
  throw new Error(`Incorrect url: ${target}, ${e}`);
40
36
  }
41
37
  }
42
- async function getProjectId(target, config$1, token) {
38
+ function extractProjectPath(target, config$1) {
43
39
  const url = new URL(target);
44
40
  if (!url.pathname.includes("/blob/")) {
45
41
  throw new Error(
46
- `Failed converting ${url.pathname} to a project id. Url path must include /blob/.`
42
+ `Failed extracting project path from ${url.pathname}. Url path must include /blob/.`
47
43
  );
48
44
  }
49
- try {
50
- let repo = url.pathname.split("/-/blob/")[0].split("/blob/")[0];
51
- const relativePath = config.getGitLabIntegrationRelativePath(config$1);
52
- if (relativePath) {
53
- repo = repo.replace(relativePath, "");
54
- }
55
- const repoIDLookup = new URL(
56
- `${url.origin}${relativePath}/api/v4/projects/${encodeURIComponent(
57
- repo.replace(/^\//, "")
58
- )}`
59
- );
60
- const response = await fetch__default.default(
61
- repoIDLookup.toString(),
62
- getGitLabRequestOptions(config$1, token)
63
- );
64
- const data = await response.json();
65
- if (!response.ok) {
66
- if (response.status === 401) {
67
- throw new Error(
68
- "GitLab Error: 401 - Unauthorized. The access token used is either expired, or does not have permission to read the project"
69
- );
70
- }
71
- throw new Error(
72
- `GitLab Error '${data.error}', ${data.error_description}`
73
- );
74
- }
75
- return Number(data.id);
76
- } catch (e) {
77
- throw new Error(`Could not get GitLab project ID for: ${target}, ${e}`);
45
+ let repo = url.pathname.split("/-/blob/")[0].split("/blob/")[0];
46
+ const relativePath = config.getGitLabIntegrationRelativePath(config$1);
47
+ if (relativePath) {
48
+ repo = repo.replace(relativePath, "");
78
49
  }
50
+ return repo.replace(/^\//, "");
79
51
  }
80
52
 
81
53
  exports.buildProjectUrl = buildProjectUrl;
54
+ exports.extractProjectPath = extractProjectPath;
82
55
  exports.getGitLabFileFetchUrl = getGitLabFileFetchUrl;
83
56
  exports.getGitLabRequestOptions = getGitLabRequestOptions;
84
- exports.getProjectId = getProjectId;
85
57
  //# sourceMappingURL=core.cjs.js.map
@@ -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 fetch from 'cross-fetch';\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/projectId/repository/c.yaml?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/projectId/repository/files/filepath?ref=branch\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @public\n */\nexport async function getGitLabFileFetchUrl(\n url: string,\n config: GitLabIntegrationConfig,\n token?: string,\n): Promise<string> {\n const projectID = await getProjectId(url, config, token);\n return buildProjectUrl(url, projectID, 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/projectId/repository/files/filepath?ref=branch\nexport function buildProjectUrl(\n target: string,\n projectID: 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 url.pathname = [\n ...(relativePath ? [relativePath] : []),\n 'api/v4/projects',\n projectID,\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// Convert\n// from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n// to: The project ID that corresponds to the URL\nexport async function getProjectId(\n target: string,\n config: GitLabIntegrationConfig,\n token?: string,\n): Promise<number> {\n const url = new URL(target);\n\n if (!url.pathname.includes('/blob/')) {\n throw new Error(\n `Failed converting ${url.pathname} to a project id. Url path must include /blob/.`,\n );\n }\n\n try {\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 // Convert\n // to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FsubgroupA%2FteamA%2Frepo\n const repoIDLookup = new URL(\n `${url.origin}${relativePath}/api/v4/projects/${encodeURIComponent(\n repo.replace(/^\\//, ''),\n )}`,\n );\n\n const response = await fetch(\n repoIDLookup.toString(),\n getGitLabRequestOptions(config, token),\n );\n\n const data = await response.json();\n\n if (!response.ok) {\n if (response.status === 401) {\n throw new Error(\n 'GitLab Error: 401 - Unauthorized. The access token used is either expired, or does not have permission to read the project',\n );\n }\n\n throw new Error(\n `GitLab Error '${data.error}', ${data.error_description}`,\n );\n }\n\n return Number(data.id);\n } catch (e) {\n throw new Error(`Could not get GitLab project ID for: ${target}, ${e}`);\n }\n}\n"],"names":["config","getGitLabIntegrationRelativePath","fetch"],"mappings":";;;;;;;;;AAuCA,eAAsB,qBAAA,CACpB,GAAA,EACA,MAAA,EACA,KAAA,EACiB;AACjB,EAAA,MAAM,SAAA,GAAY,MAAM,YAAA,CAAa,GAAA,EAAK,QAAQ,KAAK,CAAA;AACvD,EAAA,OAAO,eAAA,CAAgB,GAAA,EAAK,SAAA,EAAW,MAAM,EAAE,QAAA,EAAS;AAC1D;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;AAKO,SAAS,eAAA,CACd,MAAA,EACA,SAAA,EACAA,QAAA,EACK;AACL,EAAA,IAAI;AACF,IAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,MAAM,CAAA;AAE1B,IAAA,MAAM,iBAAA,GAAoB,GAAA,CAAI,QAAA,CAC3B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A,GAAeC,wCAAiCD,QAAM,CAAA;AAG5D,IAAA,IAAI,YAAA,EAAc;AAChB,MAAA,IAAA,GAAO,IAAA,CAAK,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAAA,IACtC;AAIA,IAAA,MAAM,eAAe,IAAI,GAAA;AAAA,MACvB,CAAA,EAAG,GAAA,CAAI,MAAM,CAAA,EAAG,YAAY,CAAA,iBAAA,EAAoB,kBAAA;AAAA,QAC9C,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,EAAE;AAAA,OACvB,CAAA;AAAA,KACH;AAEA,IAAA,MAAM,WAAW,MAAME,sBAAA;AAAA,MACrB,aAAa,QAAA,EAAS;AAAA,MACtB,uBAAA,CAAwBF,UAAQ,KAAK;AAAA,KACvC;AAEA,IAAA,MAAM,IAAA,GAAO,MAAM,QAAA,CAAS,IAAA,EAAK;AAEjC,IAAA,IAAI,CAAC,SAAS,EAAA,EAAI;AAChB,MAAA,IAAI,QAAA,CAAS,WAAW,GAAA,EAAK;AAC3B,QAAA,MAAM,IAAI,KAAA;AAAA,UACR;AAAA,SACF;AAAA,MACF;AAEA,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,CAAA,cAAA,EAAiB,IAAA,CAAK,KAAK,CAAA,GAAA,EAAM,KAAK,iBAAiB,CAAA;AAAA,OACzD;AAAA,IACF;AAEA,IAAA,OAAO,MAAA,CAAO,KAAK,EAAE,CAAA;AAAA,EACvB,SAAS,CAAA,EAAG;AACV,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,qCAAA,EAAwC,MAAM,CAAA,EAAA,EAAK,CAAC,CAAA,CAAE,CAAA;AAAA,EACxE;AACF;;;;;;;"}
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,CA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MAAM,IAAI,KAAA,CAAM,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,9 +1,8 @@
1
- import fetch from 'cross-fetch';
2
1
  import { getGitLabIntegrationRelativePath } from './config.esm.js';
3
2
 
4
- async function getGitLabFileFetchUrl(url, config, token) {
5
- const projectID = await getProjectId(url, config, token);
6
- return buildProjectUrl(url, projectID, config).toString();
3
+ function getGitLabFileFetchUrl(url, config, _token) {
4
+ const projectPath = extractProjectPath(url, config);
5
+ return Promise.resolve(buildProjectUrl(url, projectPath, config).toString());
7
6
  }
8
7
  function getGitLabRequestOptions(config, token) {
9
8
  const headers = {};
@@ -13,16 +12,17 @@ function getGitLabRequestOptions(config, token) {
13
12
  }
14
13
  return { headers };
15
14
  }
16
- function buildProjectUrl(target, projectID, config) {
15
+ function buildProjectUrl(target, projectPathOrID, config) {
17
16
  try {
18
17
  const url = new URL(target);
19
18
  const branchAndFilePath = url.pathname.split("/blob/").slice(1).join("/blob/");
20
19
  const [branch, ...filePath] = branchAndFilePath.split("/");
21
20
  const relativePath = getGitLabIntegrationRelativePath(config);
21
+ const projectIdentifier = encodeURIComponent(String(projectPathOrID));
22
22
  url.pathname = [
23
23
  ...relativePath ? [relativePath] : [],
24
24
  "api/v4/projects",
25
- projectID,
25
+ projectIdentifier,
26
26
  "repository/files",
27
27
  encodeURIComponent(decodeURIComponent(filePath.join("/"))),
28
28
  "raw"
@@ -33,44 +33,20 @@ function buildProjectUrl(target, projectID, config) {
33
33
  throw new Error(`Incorrect url: ${target}, ${e}`);
34
34
  }
35
35
  }
36
- async function getProjectId(target, config, token) {
36
+ function extractProjectPath(target, config) {
37
37
  const url = new URL(target);
38
38
  if (!url.pathname.includes("/blob/")) {
39
39
  throw new Error(
40
- `Failed converting ${url.pathname} to a project id. Url path must include /blob/.`
40
+ `Failed extracting project path from ${url.pathname}. Url path must include /blob/.`
41
41
  );
42
42
  }
43
- try {
44
- let repo = url.pathname.split("/-/blob/")[0].split("/blob/")[0];
45
- const relativePath = getGitLabIntegrationRelativePath(config);
46
- if (relativePath) {
47
- repo = repo.replace(relativePath, "");
48
- }
49
- const repoIDLookup = new URL(
50
- `${url.origin}${relativePath}/api/v4/projects/${encodeURIComponent(
51
- repo.replace(/^\//, "")
52
- )}`
53
- );
54
- const response = await fetch(
55
- repoIDLookup.toString(),
56
- getGitLabRequestOptions(config, token)
57
- );
58
- const data = await response.json();
59
- if (!response.ok) {
60
- if (response.status === 401) {
61
- throw new Error(
62
- "GitLab Error: 401 - Unauthorized. The access token used is either expired, or does not have permission to read the project"
63
- );
64
- }
65
- throw new Error(
66
- `GitLab Error '${data.error}', ${data.error_description}`
67
- );
68
- }
69
- return Number(data.id);
70
- } catch (e) {
71
- throw new Error(`Could not get GitLab project ID for: ${target}, ${e}`);
43
+ let repo = url.pathname.split("/-/blob/")[0].split("/blob/")[0];
44
+ const relativePath = getGitLabIntegrationRelativePath(config);
45
+ if (relativePath) {
46
+ repo = repo.replace(relativePath, "");
72
47
  }
48
+ return repo.replace(/^\//, "");
73
49
  }
74
50
 
75
- export { buildProjectUrl, getGitLabFileFetchUrl, getGitLabRequestOptions, getProjectId };
51
+ export { buildProjectUrl, extractProjectPath, getGitLabFileFetchUrl, getGitLabRequestOptions };
76
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 fetch from 'cross-fetch';\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/projectId/repository/c.yaml?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/projectId/repository/files/filepath?ref=branch\n *\n * @param url - A URL pointing to a file\n * @param config - The relevant provider config\n * @public\n */\nexport async function getGitLabFileFetchUrl(\n url: string,\n config: GitLabIntegrationConfig,\n token?: string,\n): Promise<string> {\n const projectID = await getProjectId(url, config, token);\n return buildProjectUrl(url, projectID, 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/projectId/repository/files/filepath?ref=branch\nexport function buildProjectUrl(\n target: string,\n projectID: 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 url.pathname = [\n ...(relativePath ? [relativePath] : []),\n 'api/v4/projects',\n projectID,\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// Convert\n// from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath\n// to: The project ID that corresponds to the URL\nexport async function getProjectId(\n target: string,\n config: GitLabIntegrationConfig,\n token?: string,\n): Promise<number> {\n const url = new URL(target);\n\n if (!url.pathname.includes('/blob/')) {\n throw new Error(\n `Failed converting ${url.pathname} to a project id. Url path must include /blob/.`,\n );\n }\n\n try {\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 // Convert\n // to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FsubgroupA%2FteamA%2Frepo\n const repoIDLookup = new URL(\n `${url.origin}${relativePath}/api/v4/projects/${encodeURIComponent(\n repo.replace(/^\\//, ''),\n )}`,\n );\n\n const response = await fetch(\n repoIDLookup.toString(),\n getGitLabRequestOptions(config, token),\n );\n\n const data = await response.json();\n\n if (!response.ok) {\n if (response.status === 401) {\n throw new Error(\n 'GitLab Error: 401 - Unauthorized. The access token used is either expired, or does not have permission to read the project',\n );\n }\n\n throw new Error(\n `GitLab Error '${data.error}', ${data.error_description}`,\n );\n }\n\n return Number(data.id);\n } catch (e) {\n throw new Error(`Could not get GitLab project ID for: ${target}, ${e}`);\n }\n}\n"],"names":[],"mappings":";;;AAuCA,eAAsB,qBAAA,CACpB,GAAA,EACA,MAAA,EACA,KAAA,EACiB;AACjB,EAAA,MAAM,SAAA,GAAY,MAAM,YAAA,CAAa,GAAA,EAAK,QAAQ,KAAK,CAAA;AACvD,EAAA,OAAO,eAAA,CAAgB,GAAA,EAAK,SAAA,EAAW,MAAM,EAAE,QAAA,EAAS;AAC1D;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;AAKO,SAAS,eAAA,CACd,MAAA,EACA,SAAA,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,CAA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AA,EAAc;AAChB,MAAA,IAAA,GAAO,IAAA,CAAK,OAAA,CAAQ,YAAA,EAAc,EAAE,CAAA;AAAA,IACtC;AAIA,IAAA,MAAM,eAAe,IAAI,GAAA;AAAA,MACvB,CAAA,EAAG,GAAA,CAAI,MAAM,CAAA,EAAG,YAAY,CAAA,iBAAA,EAAoB,kBAAA;AAAA,QAC9C,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,EAAE;AAAA,OACvB,CAAA;AAAA,KACH;AAEA,IAAA,MAAM,WAAW,MAAM,KAAA;AAAA,MACrB,aAAa,QAAA,EAAS;AAAA,MACtB,uBAAA,CAAwB,QAAQ,KAAK;AAAA,KACvC;AAEA,IAAA,MAAM,IAAA,GAAO,MAAM,QAAA,CAAS,IAAA,EAAK;AAEjC,IAAA,IAAI,CAAC,SAAS,EAAA,EAAI;AAChB,MAAA,IAAI,QAAA,CAAS,WAAW,GAAA,EAAK;AAC3B,QAAA,MAAM,IAAI,KAAA;AAAA,UACR;AAAA,SACF;AAAA,MACF;AAEA,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,CAAA,cAAA,EAAiB,IAAA,CAAK,KAAK,CAAA,GAAA,EAAM,KAAK,iBAAiB,CAAA;AAAA,OACzD;AAAA,IACF;AAEA,IAAA,OAAO,MAAA,CAAO,KAAK,EAAE,CAAA;AAAA,EACvB,SAAS,CAAA,EAAG;AACV,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,qCAAA,EAAwC,MAAM,CAAA,EAAA,EAAK,CAAC,CAAA,CAAE,CAAA;AAAA,EACxE;AACF;;;;"}
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,Q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AA;AAAA,EAClD;AACF;AAOO,SAAS,kBAAA,CACd,QACA,MAAA,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,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;;;;"}
package/dist/index.d.ts CHANGED
@@ -996,6 +996,26 @@ type GitLabIntegrationConfig = {
996
996
  * Signing key to sign commits
997
997
  */
998
998
  commitSigningKey?: string;
999
+ /**
1000
+ * Retry configuration for failed requests.
1001
+ */
1002
+ retry?: {
1003
+ /**
1004
+ * Maximum number of retries for failed requests
1005
+ * @defaultValue 0
1006
+ */
1007
+ maxRetries?: number;
1008
+ /**
1009
+ * HTTP status codes that should trigger a retry
1010
+ * @defaultValue []
1011
+ */
1012
+ retryStatusCodes?: number[];
1013
+ /**
1014
+ * Rate limit for requests per minute
1015
+ * @defaultValue -1
1016
+ */
1017
+ maxApiRequestsPerMinute?: number;
1018
+ };
999
1019
  };
1000
1020
  /**
1001
1021
  * Reads a single GitLab integration config.
@@ -1029,6 +1049,7 @@ declare function getGitLabIntegrationRelativePath(config: GitLabIntegrationConfi
1029
1049
  declare class GitLabIntegration implements ScmIntegration {
1030
1050
  private readonly integrationConfig;
1031
1051
  static factory: ScmIntegrationsFactory<GitLabIntegration>;
1052
+ private readonly fetchImpl;
1032
1053
  constructor(integrationConfig: GitLabIntegrationConfig);
1033
1054
  get type(): string;
1034
1055
  get title(): string;
@@ -1039,6 +1060,9 @@ declare class GitLabIntegration implements ScmIntegration {
1039
1060
  lineNumber?: number;
1040
1061
  }): string;
1041
1062
  resolveEditUrl(url: string): string;
1063
+ fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
1064
+ private createFetchStrategy;
1065
+ private withRetry;
1042
1066
  }
1043
1067
  /**
1044
1068
  * Takes a GitLab URL and replaces the type part (blob, tree etc).
@@ -1840,16 +1864,17 @@ declare class SingleInstanceGithubCredentialsProvider implements GithubCredentia
1840
1864
  *
1841
1865
  * Converts
1842
1866
  * from: https://gitlab.example.com/a/b/blob/master/c.yaml
1843
- * to: https://gitlab.com/api/v4/projects/projectId/repository/c.yaml?ref=master
1867
+ * to: https://gitlab.com/api/v4/projects/a%2Fb/repository/files/c.yaml/raw?ref=master
1844
1868
  * -or-
1845
1869
  * from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath
1846
- * to: https://gitlab.com/api/v4/projects/projectId/repository/files/filepath?ref=branch
1870
+ * to: https://gitlab.com/api/v4/projects/groupA%2Fteams%2FteamA%2FsubgroupA%2FrepoA/repository/files/filepath/raw?ref=branch
1847
1871
  *
1848
1872
  * @param url - A URL pointing to a file
1849
1873
  * @param config - The relevant provider config
1874
+ * @param token - An optional auth token (not used in path extraction, kept for compatibility)
1850
1875
  * @public
1851
1876
  */
1852
- declare function getGitLabFileFetchUrl(url: string, config: GitLabIntegrationConfig, token?: string): Promise<string>;
1877
+ declare function getGitLabFileFetchUrl(url: string, config: GitLabIntegrationConfig, _token?: string): Promise<string>;
1853
1878
  /**
1854
1879
  * Gets the request options necessary to make requests to a given provider.
1855
1880
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/integration",
3
- "version": "1.20.0-next.2",
3
+ "version": "1.21.0-next.0",
4
4
  "description": "Helpers for managing integrations towards external systems",
5
5
  "backstage": {
6
6
  "role": "common-library"
@@ -46,11 +46,12 @@
46
46
  "cross-fetch": "^4.0.0",
47
47
  "git-url-parse": "^15.0.0",
48
48
  "lodash": "^4.17.21",
49
- "luxon": "^3.0.0"
49
+ "luxon": "^3.0.0",
50
+ "p-throttle": "^4.1.1"
50
51
  },
51
52
  "devDependencies": {
52
- "@backstage/cli": "0.35.4-next.2",
53
- "@backstage/config-loader": "1.10.8-next.0",
53
+ "@backstage/cli": "0.35.5-next.0",
54
+ "@backstage/config-loader": "1.10.9-next.0",
54
55
  "msw": "^1.0.0"
55
56
  },
56
57
  "configSchema": "config.d.ts",