@backstage/integration 1.10.0 → 1.11.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.
Files changed (94) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/config.d.ts +18 -0
  3. package/dist/ScmIntegrations.esm.js +114 -0
  4. package/dist/ScmIntegrations.esm.js.map +1 -0
  5. package/dist/awsCodeCommit/AwsCodeCommitIntegration.esm.js +63 -0
  6. package/dist/awsCodeCommit/AwsCodeCommitIntegration.esm.js.map +1 -0
  7. package/dist/awsCodeCommit/config.esm.js +28 -0
  8. package/dist/awsCodeCommit/config.esm.js.map +1 -0
  9. package/dist/awsS3/AwsS3Integration.esm.js +44 -0
  10. package/dist/awsS3/AwsS3Integration.esm.js.map +1 -0
  11. package/dist/awsS3/config.esm.js +51 -0
  12. package/dist/awsS3/config.esm.js.map +1 -0
  13. package/dist/azure/AzureIntegration.esm.js +69 -0
  14. package/dist/azure/AzureIntegration.esm.js.map +1 -0
  15. package/dist/azure/AzureUrl.esm.js +191 -0
  16. package/dist/azure/AzureUrl.esm.js.map +1 -0
  17. package/dist/azure/CachedAzureDevOpsCredentialsProvider.esm.js +80 -0
  18. package/dist/azure/CachedAzureDevOpsCredentialsProvider.esm.js.map +1 -0
  19. package/dist/azure/DefaultAzureDevOpsCredentialsProvider.esm.js +76 -0
  20. package/dist/azure/DefaultAzureDevOpsCredentialsProvider.esm.js.map +1 -0
  21. package/dist/azure/config.esm.js +153 -0
  22. package/dist/azure/config.esm.js.map +1 -0
  23. package/dist/azure/core.esm.js +14 -0
  24. package/dist/azure/core.esm.js.map +1 -0
  25. package/dist/azure/deprecated.esm.js +25 -0
  26. package/dist/azure/deprecated.esm.js.map +1 -0
  27. package/dist/bitbucket/BitbucketIntegration.esm.js +67 -0
  28. package/dist/bitbucket/BitbucketIntegration.esm.js.map +1 -0
  29. package/dist/bitbucket/config.esm.js +45 -0
  30. package/dist/bitbucket/config.esm.js.map +1 -0
  31. package/dist/bitbucket/core.esm.js +85 -0
  32. package/dist/bitbucket/core.esm.js.map +1 -0
  33. package/dist/bitbucketCloud/BitbucketCloudIntegration.esm.js +56 -0
  34. package/dist/bitbucketCloud/BitbucketCloudIntegration.esm.js.map +1 -0
  35. package/dist/bitbucketCloud/config.esm.js +28 -0
  36. package/dist/bitbucketCloud/config.esm.js.map +1 -0
  37. package/dist/bitbucketCloud/core.esm.js +68 -0
  38. package/dist/bitbucketCloud/core.esm.js.map +1 -0
  39. package/dist/bitbucketServer/BitbucketServerIntegration.esm.js +54 -0
  40. package/dist/bitbucketServer/BitbucketServerIntegration.esm.js.map +1 -0
  41. package/dist/bitbucketServer/config.esm.js +34 -0
  42. package/dist/bitbucketServer/config.esm.js.map +1 -0
  43. package/dist/bitbucketServer/core.esm.js +63 -0
  44. package/dist/bitbucketServer/core.esm.js.map +1 -0
  45. package/dist/gerrit/GerritIntegration.esm.js +58 -0
  46. package/dist/gerrit/GerritIntegration.esm.js.map +1 -0
  47. package/dist/gerrit/config.esm.js +58 -0
  48. package/dist/gerrit/config.esm.js.map +1 -0
  49. package/dist/gerrit/core.esm.js +106 -0
  50. package/dist/gerrit/core.esm.js.map +1 -0
  51. package/dist/gitea/GiteaIntegration.esm.js +40 -0
  52. package/dist/gitea/GiteaIntegration.esm.js.map +1 -0
  53. package/dist/gitea/config.esm.js +33 -0
  54. package/dist/gitea/config.esm.js.map +1 -0
  55. package/dist/gitea/core.esm.js +53 -0
  56. package/dist/gitea/core.esm.js.map +1 -0
  57. package/dist/github/DefaultGithubCredentialsProvider.esm.js +52 -0
  58. package/dist/github/DefaultGithubCredentialsProvider.esm.js.map +1 -0
  59. package/dist/github/GithubIntegration.esm.js +56 -0
  60. package/dist/github/GithubIntegration.esm.js.map +1 -0
  61. package/dist/github/SingleInstanceGithubCredentialsProvider.esm.js +222 -0
  62. package/dist/github/SingleInstanceGithubCredentialsProvider.esm.js.map +1 -0
  63. package/dist/github/config.esm.js +53 -0
  64. package/dist/github/config.esm.js.map +1 -0
  65. package/dist/github/core.esm.js +38 -0
  66. package/dist/github/core.esm.js.map +1 -0
  67. package/dist/github/deprecated.esm.js +26 -0
  68. package/dist/github/deprecated.esm.js.map +1 -0
  69. package/dist/gitlab/DefaultGitlabCredentialsProvider.esm.js +28 -0
  70. package/dist/gitlab/DefaultGitlabCredentialsProvider.esm.js.map +1 -0
  71. package/dist/gitlab/GitLabIntegration.esm.js +46 -0
  72. package/dist/gitlab/GitLabIntegration.esm.js.map +1 -0
  73. package/dist/gitlab/SingleInstanceGitlabCredentialsProvider.esm.js +29 -0
  74. package/dist/gitlab/SingleInstanceGitlabCredentialsProvider.esm.js.map +1 -0
  75. package/dist/gitlab/config.esm.js +57 -0
  76. package/dist/gitlab/config.esm.js.map +1 -0
  77. package/dist/gitlab/core.esm.js +69 -0
  78. package/dist/gitlab/core.esm.js.map +1 -0
  79. package/dist/googleGcs/config.esm.js +14 -0
  80. package/dist/googleGcs/config.esm.js.map +1 -0
  81. package/dist/harness/HarnessIntegration.esm.js +40 -0
  82. package/dist/harness/HarnessIntegration.esm.js.map +1 -0
  83. package/dist/harness/config.esm.js +20 -0
  84. package/dist/harness/config.esm.js.map +1 -0
  85. package/dist/harness/core.esm.js +73 -0
  86. package/dist/harness/core.esm.js.map +1 -0
  87. package/dist/helpers.esm.js +62 -0
  88. package/dist/helpers.esm.js.map +1 -0
  89. package/dist/index.cjs.js +214 -85
  90. package/dist/index.cjs.js.map +1 -1
  91. package/dist/index.d.ts +73 -1
  92. package/dist/index.esm.js +40 -2323
  93. package/dist/index.esm.js.map +1 -1
  94. package/package.json +2 -2
@@ -0,0 +1,222 @@
1
+ import parseGitUrl from 'git-url-parse';
2
+ import { createAppAuth } from '@octokit/auth-app';
3
+ import { Octokit } from '@octokit/rest';
4
+ import { DateTime } from 'luxon';
5
+
6
+ var __defProp = Object.defineProperty;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __publicField = (obj, key, value) => {
9
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
10
+ return value;
11
+ };
12
+ class Cache {
13
+ constructor() {
14
+ __publicField(this, "tokenCache", /* @__PURE__ */ new Map());
15
+ __publicField(this, "isExpired", (date) => DateTime.local() > date);
16
+ }
17
+ async getOrCreateToken(owner, repo, supplier) {
18
+ let existingInstallationData = this.tokenCache.get(owner);
19
+ if (!existingInstallationData || this.isExpired(existingInstallationData.expiresAt)) {
20
+ existingInstallationData = await supplier();
21
+ existingInstallationData.expiresAt = existingInstallationData.expiresAt.minus({ minutes: 10 });
22
+ this.tokenCache.set(owner, existingInstallationData);
23
+ }
24
+ if (!this.appliesToRepo(existingInstallationData, repo)) {
25
+ throw new Error(
26
+ `The Backstage GitHub application used in the ${owner} organization does not have access to a repository with the name ${repo}`
27
+ );
28
+ }
29
+ return { accessToken: existingInstallationData.token };
30
+ }
31
+ appliesToRepo(tokenData, repo) {
32
+ if (repo === void 0) {
33
+ return true;
34
+ }
35
+ if (tokenData.repositories !== void 0) {
36
+ return tokenData.repositories.includes(repo);
37
+ }
38
+ return true;
39
+ }
40
+ }
41
+ const HEADERS = {
42
+ Accept: "application/vnd.github.machine-man-preview+json"
43
+ };
44
+ class GithubAppManager {
45
+ // undefined allows all installations
46
+ constructor(config, baseUrl) {
47
+ __publicField(this, "appClient");
48
+ __publicField(this, "baseUrl");
49
+ __publicField(this, "baseAuthConfig");
50
+ __publicField(this, "cache", new Cache());
51
+ __publicField(this, "allowedInstallationOwners");
52
+ this.allowedInstallationOwners = config.allowedInstallationOwners;
53
+ this.baseUrl = baseUrl;
54
+ this.baseAuthConfig = {
55
+ appId: config.appId,
56
+ privateKey: config.privateKey.replace(/\\n/gm, "\n")
57
+ };
58
+ this.appClient = new Octokit({
59
+ baseUrl,
60
+ headers: HEADERS,
61
+ authStrategy: createAppAuth,
62
+ auth: this.baseAuthConfig
63
+ });
64
+ }
65
+ async getInstallationCredentials(owner, repo) {
66
+ var _a;
67
+ if (this.allowedInstallationOwners) {
68
+ if (!((_a = this.allowedInstallationOwners) == null ? void 0 : _a.includes(owner))) {
69
+ return { accessToken: void 0 };
70
+ }
71
+ }
72
+ return this.cache.getOrCreateToken(owner, repo, async () => {
73
+ var _a2;
74
+ const { installationId, suspended } = await this.getInstallationData(
75
+ owner
76
+ );
77
+ if (suspended) {
78
+ throw new Error(`The GitHub application for ${owner} is suspended`);
79
+ }
80
+ const result = await this.appClient.apps.createInstallationAccessToken({
81
+ installation_id: installationId,
82
+ headers: HEADERS
83
+ });
84
+ let repositoryNames;
85
+ if (result.data.repository_selection === "selected") {
86
+ const installationClient = new Octokit({
87
+ baseUrl: this.baseUrl,
88
+ auth: result.data.token
89
+ });
90
+ const repos = await installationClient.paginate(
91
+ installationClient.apps.listReposAccessibleToInstallation
92
+ );
93
+ const repositories = (_a2 = repos.repositories) != null ? _a2 : repos;
94
+ repositoryNames = repositories.map((repository) => repository.name);
95
+ }
96
+ return {
97
+ token: result.data.token,
98
+ expiresAt: DateTime.fromISO(result.data.expires_at),
99
+ repositories: repositoryNames
100
+ };
101
+ });
102
+ }
103
+ getInstallations() {
104
+ return this.appClient.paginate(this.appClient.apps.listInstallations);
105
+ }
106
+ async getInstallationData(owner) {
107
+ const allInstallations = await this.getInstallations();
108
+ const installation = allInstallations.find(
109
+ (inst) => {
110
+ var _a;
111
+ return inst.account && "login" in inst.account && ((_a = inst.account.login) == null ? void 0 : _a.toLocaleLowerCase("en-US")) === owner.toLocaleLowerCase("en-US");
112
+ }
113
+ );
114
+ if (installation) {
115
+ return {
116
+ installationId: installation.id,
117
+ suspended: Boolean(installation.suspended_by)
118
+ };
119
+ }
120
+ const notFoundError = new Error(
121
+ `No app installation found for ${owner} in ${this.baseAuthConfig.appId}`
122
+ );
123
+ notFoundError.name = "NotFoundError";
124
+ throw notFoundError;
125
+ }
126
+ }
127
+ class GithubAppCredentialsMux {
128
+ constructor(config) {
129
+ __publicField(this, "apps");
130
+ var _a, _b;
131
+ this.apps = (_b = (_a = config.apps) == null ? void 0 : _a.map((ac) => new GithubAppManager(ac, config.apiBaseUrl))) != null ? _b : [];
132
+ }
133
+ async getAllInstallations() {
134
+ if (!this.apps.length) {
135
+ return [];
136
+ }
137
+ const installs = await Promise.all(
138
+ this.apps.map((app) => app.getInstallations())
139
+ );
140
+ return installs.flat();
141
+ }
142
+ async getAppToken(owner, repo) {
143
+ if (this.apps.length === 0) {
144
+ return void 0;
145
+ }
146
+ const results = await Promise.all(
147
+ this.apps.map(
148
+ (app) => app.getInstallationCredentials(owner, repo).then(
149
+ (credentials) => ({ credentials, error: void 0 }),
150
+ (error) => ({ credentials: void 0, error })
151
+ )
152
+ )
153
+ );
154
+ const result = results.find(
155
+ (resultItem) => {
156
+ var _a;
157
+ return (_a = resultItem.credentials) == null ? void 0 : _a.accessToken;
158
+ }
159
+ );
160
+ if (result) {
161
+ return result.credentials.accessToken;
162
+ }
163
+ const errors = results.map((r) => r.error);
164
+ const notNotFoundError = errors.find((err) => (err == null ? void 0 : err.name) !== "NotFoundError");
165
+ if (notNotFoundError) {
166
+ throw notNotFoundError;
167
+ }
168
+ return void 0;
169
+ }
170
+ }
171
+ const _SingleInstanceGithubCredentialsProvider = class _SingleInstanceGithubCredentialsProvider {
172
+ constructor(githubAppCredentialsMux, token) {
173
+ this.githubAppCredentialsMux = githubAppCredentialsMux;
174
+ this.token = token;
175
+ }
176
+ /**
177
+ * Returns {@link GithubCredentials} for a given URL.
178
+ *
179
+ * @remarks
180
+ *
181
+ * Consecutive calls to this method with the same URL will return cached
182
+ * credentials.
183
+ *
184
+ * The shortest lifetime for a token returned is 10 minutes.
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * const { token, headers } = await getCredentials({
189
+ * url: 'github.com/backstage/foobar'
190
+ * })
191
+ * ```
192
+ *
193
+ * @param opts - The organization or repository URL
194
+ * @returns A promise of {@link GithubCredentials}.
195
+ */
196
+ async getCredentials(opts) {
197
+ const parsed = parseGitUrl(opts.url);
198
+ const owner = parsed.owner || parsed.name;
199
+ const repo = parsed.owner ? parsed.name : void 0;
200
+ let type = "app";
201
+ let token = await this.githubAppCredentialsMux.getAppToken(owner, repo);
202
+ if (!token) {
203
+ type = "token";
204
+ token = this.token;
205
+ }
206
+ return {
207
+ headers: token ? { Authorization: `Bearer ${token}` } : void 0,
208
+ token,
209
+ type
210
+ };
211
+ }
212
+ };
213
+ __publicField(_SingleInstanceGithubCredentialsProvider, "create", (config) => {
214
+ return new _SingleInstanceGithubCredentialsProvider(
215
+ new GithubAppCredentialsMux(config),
216
+ config.token
217
+ );
218
+ });
219
+ let SingleInstanceGithubCredentialsProvider = _SingleInstanceGithubCredentialsProvider;
220
+
221
+ export { GithubAppCredentialsMux, SingleInstanceGithubCredentialsProvider };
222
+ //# sourceMappingURL=SingleInstanceGithubCredentialsProvider.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SingleInstanceGithubCredentialsProvider.esm.js","sources":["../../src/github/SingleInstanceGithubCredentialsProvider.ts"],"sourcesContent":["/*\n * Copyright 2021 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 { GithubAppConfig, GithubIntegrationConfig } from './config';\nimport { createAppAuth } from '@octokit/auth-app';\nimport { Octokit, RestEndpointMethodTypes } from '@octokit/rest';\nimport { DateTime } from 'luxon';\nimport {\n GithubCredentials,\n GithubCredentialsProvider,\n GithubCredentialType,\n} from './types';\n\ntype InstallationData = {\n installationId: number;\n suspended: boolean;\n};\n\ntype InstallationTokenData = {\n token: string;\n expiresAt: DateTime;\n repositories?: String[];\n};\n\nclass Cache {\n private readonly tokenCache = new Map<string, InstallationTokenData>();\n\n async getOrCreateToken(\n owner: string,\n repo: string | undefined,\n supplier: () => Promise<InstallationTokenData>,\n ): Promise<{ accessToken: string }> {\n let existingInstallationData = this.tokenCache.get(owner);\n\n if (\n !existingInstallationData ||\n this.isExpired(existingInstallationData.expiresAt)\n ) {\n existingInstallationData = await supplier();\n // Allow 10 minutes grace to account for clock skew\n existingInstallationData.expiresAt =\n existingInstallationData.expiresAt.minus({ minutes: 10 });\n this.tokenCache.set(owner, existingInstallationData);\n }\n\n if (!this.appliesToRepo(existingInstallationData, repo)) {\n throw new Error(\n `The Backstage GitHub application used in the ${owner} organization does not have access to a repository with the name ${repo}`,\n );\n }\n\n return { accessToken: existingInstallationData.token };\n }\n\n private isExpired = (date: DateTime) => DateTime.local() > date;\n\n private appliesToRepo(tokenData: InstallationTokenData, repo?: string) {\n // If no specific repo has been requested the token is applicable\n if (repo === undefined) {\n return true;\n }\n // If the token is restricted to repositories, the token only applies if the repo is in the allow list\n if (tokenData.repositories !== undefined) {\n return tokenData.repositories.includes(repo);\n }\n // Otherwise the token is applicable\n return true;\n }\n}\n\n/**\n * This accept header is required when calling App APIs in GitHub Enterprise.\n * It has no effect on calls to github.com and can probably be removed entirely\n * once GitHub Apps is out of preview.\n */\nconst HEADERS = {\n Accept: 'application/vnd.github.machine-man-preview+json',\n};\n\n/**\n * GithubAppManager issues and caches tokens for a specific GitHub App.\n */\nclass GithubAppManager {\n private readonly appClient: Octokit;\n private readonly baseUrl?: string;\n private readonly baseAuthConfig: { appId: number; privateKey: string };\n private readonly cache = new Cache();\n private readonly allowedInstallationOwners: string[] | undefined; // undefined allows all installations\n\n constructor(config: GithubAppConfig, baseUrl?: string) {\n this.allowedInstallationOwners = config.allowedInstallationOwners;\n this.baseUrl = baseUrl;\n this.baseAuthConfig = {\n appId: config.appId,\n privateKey: config.privateKey.replace(/\\\\n/gm, '\\n'),\n };\n this.appClient = new Octokit({\n baseUrl,\n headers: HEADERS,\n authStrategy: createAppAuth,\n auth: this.baseAuthConfig,\n });\n }\n\n async getInstallationCredentials(\n owner: string,\n repo?: string,\n ): Promise<{ accessToken: string | undefined }> {\n if (this.allowedInstallationOwners) {\n if (!this.allowedInstallationOwners?.includes(owner)) {\n return { accessToken: undefined }; // An empty token allows anonymous access to public repos\n }\n }\n\n // Go and grab an access token for the app scoped to a repository if provided, if not use the organisation installation.\n return this.cache.getOrCreateToken(owner, repo, async () => {\n const { installationId, suspended } = await this.getInstallationData(\n owner,\n );\n if (suspended) {\n throw new Error(`The GitHub application for ${owner} is suspended`);\n }\n\n const result = await this.appClient.apps.createInstallationAccessToken({\n installation_id: installationId,\n headers: HEADERS,\n });\n\n let repositoryNames;\n\n if (result.data.repository_selection === 'selected') {\n const installationClient = new Octokit({\n baseUrl: this.baseUrl,\n auth: result.data.token,\n });\n const repos = await installationClient.paginate(\n installationClient.apps.listReposAccessibleToInstallation,\n );\n // The return type of the paginate method is incorrect.\n const repositories: RestEndpointMethodTypes['apps']['listReposAccessibleToInstallation']['response']['data']['repositories'] =\n repos.repositories ?? repos;\n\n repositoryNames = repositories.map(repository => repository.name);\n }\n return {\n token: result.data.token,\n expiresAt: DateTime.fromISO(result.data.expires_at),\n repositories: repositoryNames,\n };\n });\n }\n\n getInstallations(): Promise<\n RestEndpointMethodTypes['apps']['listInstallations']['response']['data']\n > {\n return this.appClient.paginate(this.appClient.apps.listInstallations);\n }\n\n private async getInstallationData(owner: string): Promise<InstallationData> {\n const allInstallations = await this.getInstallations();\n const installation = allInstallations.find(\n inst =>\n inst.account &&\n 'login' in inst.account &&\n inst.account.login?.toLocaleLowerCase('en-US') ===\n owner.toLocaleLowerCase('en-US'),\n );\n if (installation) {\n return {\n installationId: installation.id,\n suspended: Boolean(installation.suspended_by),\n };\n }\n const notFoundError = new Error(\n `No app installation found for ${owner} in ${this.baseAuthConfig.appId}`,\n );\n notFoundError.name = 'NotFoundError';\n throw notFoundError;\n }\n}\n\n/**\n * Corresponds to a Github installation which internally could hold several GitHub Apps.\n *\n * @public\n */\nexport class GithubAppCredentialsMux {\n private readonly apps: GithubAppManager[];\n\n constructor(config: GithubIntegrationConfig) {\n this.apps =\n config.apps?.map(ac => new GithubAppManager(ac, config.apiBaseUrl)) ?? [];\n }\n\n async getAllInstallations(): Promise<\n RestEndpointMethodTypes['apps']['listInstallations']['response']['data']\n > {\n if (!this.apps.length) {\n return [];\n }\n\n const installs = await Promise.all(\n this.apps.map(app => app.getInstallations()),\n );\n\n return installs.flat();\n }\n\n async getAppToken(owner: string, repo?: string): Promise<string | undefined> {\n if (this.apps.length === 0) {\n return undefined;\n }\n\n const results = await Promise.all(\n this.apps.map(app =>\n app.getInstallationCredentials(owner, repo).then(\n credentials => ({ credentials, error: undefined }),\n error => ({ credentials: undefined, error }),\n ),\n ),\n );\n\n const result = results.find(\n resultItem => resultItem.credentials?.accessToken,\n );\n if (result) {\n return result.credentials!.accessToken;\n }\n\n const errors = results.map(r => r.error);\n const notNotFoundError = errors.find(err => err?.name !== 'NotFoundError');\n if (notNotFoundError) {\n throw notNotFoundError;\n }\n\n return undefined;\n }\n}\n\n/**\n * Handles the creation and caching of credentials for GitHub integrations.\n *\n * @public\n * @remarks\n *\n * TODO: Possibly move this to a backend only package so that it's not used in the frontend by mistake\n */\nexport class SingleInstanceGithubCredentialsProvider\n implements GithubCredentialsProvider\n{\n static create: (\n config: GithubIntegrationConfig,\n ) => GithubCredentialsProvider = config => {\n return new SingleInstanceGithubCredentialsProvider(\n new GithubAppCredentialsMux(config),\n config.token,\n );\n };\n\n private constructor(\n private readonly githubAppCredentialsMux: GithubAppCredentialsMux,\n private readonly token?: string,\n ) {}\n\n /**\n * Returns {@link GithubCredentials} for a given URL.\n *\n * @remarks\n *\n * Consecutive calls to this method with the same URL will return cached\n * credentials.\n *\n * The shortest lifetime for a token returned is 10 minutes.\n *\n * @example\n * ```ts\n * const { token, headers } = await getCredentials({\n * url: 'github.com/backstage/foobar'\n * })\n * ```\n *\n * @param opts - The organization or repository URL\n * @returns A promise of {@link GithubCredentials}.\n */\n async getCredentials(opts: { url: string }): Promise<GithubCredentials> {\n const parsed = parseGitUrl(opts.url);\n\n const owner = parsed.owner || parsed.name;\n const repo = parsed.owner ? parsed.name : undefined;\n\n let type: GithubCredentialType = 'app';\n let token = await this.githubAppCredentialsMux.getAppToken(owner, repo);\n if (!token) {\n type = 'token';\n token = this.token;\n }\n\n return {\n headers: token ? { Authorization: `Bearer ${token}` } : undefined,\n token,\n type,\n };\n }\n}\n"],"names":["_a"],"mappings":";;;;;;;;;;;AAsCA,MAAM,KAAM,CAAA;AAAA,EAAZ,WAAA,GAAA;AACE,IAAiB,aAAA,CAAA,IAAA,EAAA,YAAA,sBAAiB,GAAmC,EAAA,CAAA,CAAA;AA6BrE,IAAA,aAAA,CAAA,IAAA,EAAQ,WAAY,EAAA,CAAC,IAAmB,KAAA,QAAA,CAAS,OAAU,GAAA,IAAA,CAAA,CAAA;AAAA,GAAA;AAAA,EA3B3D,MAAM,gBAAA,CACJ,KACA,EAAA,IAAA,EACA,QACkC,EAAA;AAClC,IAAA,IAAI,wBAA2B,GAAA,IAAA,CAAK,UAAW,CAAA,GAAA,CAAI,KAAK,CAAA,CAAA;AAExD,IAAA,IACE,CAAC,wBACD,IAAA,IAAA,CAAK,SAAU,CAAA,wBAAA,CAAyB,SAAS,CACjD,EAAA;AACA,MAAA,wBAAA,GAA2B,MAAM,QAAS,EAAA,CAAA;AAE1C,MAAA,wBAAA,CAAyB,YACvB,wBAAyB,CAAA,SAAA,CAAU,MAAM,EAAE,OAAA,EAAS,IAAI,CAAA,CAAA;AAC1D,MAAK,IAAA,CAAA,UAAA,CAAW,GAAI,CAAA,KAAA,EAAO,wBAAwB,CAAA,CAAA;AAAA,KACrD;AAEA,IAAA,IAAI,CAAC,IAAA,CAAK,aAAc,CAAA,wBAAA,EAA0B,IAAI,CAAG,EAAA;AACvD,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,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AAO,IAAI,wCAAA;AAAA,IACT,IAAI,wBAAwB,MAAM,CAAA;AAAA,IAClC,MAAO,CAAA,KAAA;AAAA,GACT,CAAA;AACF,CAAA,CAAA,CAAA;AAVK,IAAM,uCAAN,GAAA;;;;"}
@@ -0,0 +1,53 @@
1
+ import { trimEnd } from 'lodash';
2
+ import { isValidHost } from '../helpers.esm.js';
3
+
4
+ const GITHUB_HOST = "github.com";
5
+ const GITHUB_API_BASE_URL = "https://api.github.com";
6
+ const GITHUB_RAW_BASE_URL = "https://raw.githubusercontent.com";
7
+ function readGithubIntegrationConfig(config) {
8
+ var _a, _b, _c;
9
+ const host = (_a = config.getOptionalString("host")) != null ? _a : GITHUB_HOST;
10
+ let apiBaseUrl = config.getOptionalString("apiBaseUrl");
11
+ let rawBaseUrl = config.getOptionalString("rawBaseUrl");
12
+ const token = (_b = config.getOptionalString("token")) == null ? void 0 : _b.trim();
13
+ const apps = (_c = config.getOptionalConfigArray("apps")) == null ? void 0 : _c.map((c) => ({
14
+ appId: c.getNumber("appId"),
15
+ clientId: c.getString("clientId"),
16
+ clientSecret: c.getString("clientSecret"),
17
+ webhookSecret: c.getString("webhookSecret"),
18
+ privateKey: c.getString("privateKey"),
19
+ allowedInstallationOwners: c.getOptionalStringArray(
20
+ "allowedInstallationOwners"
21
+ )
22
+ }));
23
+ if (!isValidHost(host)) {
24
+ throw new Error(
25
+ `Invalid GitHub integration config, '${host}' is not a valid host`
26
+ );
27
+ }
28
+ if (apiBaseUrl) {
29
+ apiBaseUrl = trimEnd(apiBaseUrl, "/");
30
+ } else if (host === GITHUB_HOST) {
31
+ apiBaseUrl = GITHUB_API_BASE_URL;
32
+ }
33
+ if (rawBaseUrl) {
34
+ rawBaseUrl = trimEnd(rawBaseUrl, "/");
35
+ } else if (host === GITHUB_HOST) {
36
+ rawBaseUrl = GITHUB_RAW_BASE_URL;
37
+ }
38
+ return { host, apiBaseUrl, rawBaseUrl, token, apps };
39
+ }
40
+ function readGithubIntegrationConfigs(configs) {
41
+ const result = configs.map(readGithubIntegrationConfig);
42
+ if (!result.some((c) => c.host === GITHUB_HOST)) {
43
+ result.push({
44
+ host: GITHUB_HOST,
45
+ apiBaseUrl: GITHUB_API_BASE_URL,
46
+ rawBaseUrl: GITHUB_RAW_BASE_URL
47
+ });
48
+ }
49
+ return result;
50
+ }
51
+
52
+ export { readGithubIntegrationConfig, readGithubIntegrationConfigs };
53
+ //# sourceMappingURL=config.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.esm.js","sources":["../../src/github/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 } from '../helpers';\n\nconst GITHUB_HOST = 'github.com';\nconst GITHUB_API_BASE_URL = 'https://api.github.com';\nconst GITHUB_RAW_BASE_URL = 'https://raw.githubusercontent.com';\n\n/**\n * The configuration parameters for a single GitHub integration.\n *\n * @public\n */\nexport type GithubIntegrationConfig = {\n /**\n * The host of the target that this matches on, e.g. \"github.com\"\n */\n host: string;\n\n /**\n * The base URL of the API of this provider, e.g. \"https://api.github.com\",\n * with no trailing slash.\n *\n * May be omitted specifically for GitHub; then it will be deduced.\n *\n * The API will always be preferred if both its base URL and a token are\n * present.\n */\n apiBaseUrl?: string;\n\n /**\n * The base URL of the raw fetch endpoint of this provider, e.g.\n * \"https://raw.githubusercontent.com\", with no trailing slash.\n *\n * May be omitted specifically for GitHub; then it will be deduced.\n *\n * The API will always be preferred if both its base URL and a token are\n * present.\n */\n rawBaseUrl?: 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 GitHub Apps configuration to use for requests to this provider.\n *\n * If no apps are specified, token or anonymous is used.\n */\n apps?: GithubAppConfig[];\n};\n\n/**\n * The configuration parameters for authenticating a GitHub Application.\n *\n * @remarks\n *\n * A GitHub Apps configuration can be generated using the `backstage-cli create-github-app` command.\n *\n * @public\n */\nexport type GithubAppConfig = {\n /**\n * Unique app identifier, found at https://github.com/organizations/$org/settings/apps/$AppName\n */\n appId: number;\n /**\n * The private key is used by the GitHub App integration to authenticate the app.\n * A private key can be generated from the app at https://github.com/organizations/$org/settings/apps/$AppName\n */\n privateKey: string;\n /**\n * Webhook secret can be configured at https://github.com/organizations/$org/settings/apps/$AppName\n */\n webhookSecret: string;\n /**\n * Found at https://github.com/organizations/$org/settings/apps/$AppName\n */\n clientId: string;\n /**\n * Client secrets can be generated at https://github.com/organizations/$org/settings/apps/$AppName\n */\n clientSecret: string;\n /**\n * List of installation owners allowed to be used by this GitHub app. The GitHub UI does not provide a way to list the installations.\n * However you can list the installations with the GitHub API. You can find the list of installations here:\n * https://api.github.com/app/installations\n * The relevant documentation for this is here.\n * https://docs.github.com/en/rest/reference/apps#list-installations-for-the-authenticated-app--code-samples\n */\n allowedInstallationOwners?: string[];\n};\n\n/**\n * Reads a single GitHub integration config.\n *\n * @param config - The config object of a single integration\n * @public\n */\nexport function readGithubIntegrationConfig(\n config: Config,\n): GithubIntegrationConfig {\n const host = config.getOptionalString('host') ?? GITHUB_HOST;\n let apiBaseUrl = config.getOptionalString('apiBaseUrl');\n let rawBaseUrl = config.getOptionalString('rawBaseUrl');\n const token = config.getOptionalString('token')?.trim();\n const apps = config.getOptionalConfigArray('apps')?.map(c => ({\n appId: c.getNumber('appId'),\n clientId: c.getString('clientId'),\n clientSecret: c.getString('clientSecret'),\n webhookSecret: c.getString('webhookSecret'),\n privateKey: c.getString('privateKey'),\n allowedInstallationOwners: c.getOptionalStringArray(\n 'allowedInstallationOwners',\n ),\n }));\n\n if (!isValidHost(host)) {\n throw new Error(\n `Invalid GitHub integration config, '${host}' is not a valid host`,\n );\n }\n\n if (apiBaseUrl) {\n apiBaseUrl = trimEnd(apiBaseUrl, '/');\n } else if (host === GITHUB_HOST) {\n apiBaseUrl = GITHUB_API_BASE_URL;\n }\n\n if (rawBaseUrl) {\n rawBaseUrl = trimEnd(rawBaseUrl, '/');\n } else if (host === GITHUB_HOST) {\n rawBaseUrl = GITHUB_RAW_BASE_URL;\n }\n\n return { host, apiBaseUrl, rawBaseUrl, token, apps };\n}\n\n/**\n * Reads a set of GitHub integration configs, and inserts some defaults for\n * public GitHub if not specified.\n *\n * @param configs - All of the integration config objects\n * @public\n */\nexport function readGithubIntegrationConfigs(\n configs: Config[],\n): GithubIntegrationConfig[] {\n // First read all the explicit integrations\n const result = configs.map(readGithubIntegrationConfig);\n\n // If no explicit github.com integration was added, put one in the list as\n // a convenience\n if (!result.some(c => c.host === GITHUB_HOST)) {\n result.push({\n host: GITHUB_HOST,\n apiBaseUrl: GITHUB_API_BASE_URL,\n rawBaseUrl: GITHUB_RAW_BASE_URL,\n });\n }\n\n return result;\n}\n"],"names":[],"mappings":";;;AAoBA,MAAM,WAAc,GAAA,YAAA,CAAA;AACpB,MAAM,mBAAsB,GAAA,wBAAA,CAAA;AAC5B,MAAM,mBAAsB,GAAA,mCAAA,CAAA;AAiGrB,SAAS,4BACd,MACyB,EAAA;AAzH3B,EAAA,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA,CAAA;AA0HE,EAAA,MAAM,IAAO,GAAA,CAAA,EAAA,GAAA,MAAA,CAAO,iBAAkB,CAAA,MAAM,MAA/B,IAAoC,GAAA,EAAA,GAAA,WAAA,CAAA;AACjD,EAAI,IAAA,UAAA,GAAa,MAAO,CAAA,iBAAA,CAAkB,YAAY,CAAA,CAAA;AACtD,EAAI,IAAA,UAAA,GAAa,MAAO,CAAA,iBAAA,CAAkB,YAAY,CAAA,CA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A,EAAA,OAAO,EAAE,IAAA,EAAM,UAAY,EAAA,UAAA,EAAY,OAAO,IAAK,EAAA,CAAA;AACrD,CAAA;AASO,SAAS,6BACd,OAC2B,EAAA;AAE3B,EAAM,MAAA,MAAA,GAAS,OAAQ,CAAA,GAAA,CAAI,2BAA2B,CAAA,CAAA;AAItD,EAAA,IAAI,CAAC,MAAO,CAAA,IAAA,CAAK,OAAK,CAAE,CAAA,IAAA,KAAS,WAAW,CAAG,EAAA;AAC7C,IAAA,MAAA,CAAO,IAAK,CAAA;AAAA,MACV,IAAM,EAAA,WAAA;AAAA,MACN,UAAY,EAAA,mBAAA;AAAA,MACZ,UAAY,EAAA,mBAAA;AAAA,KACb,CAAA,CAAA;AAAA,GACH;AAEA,EAAO,OAAA,MAAA,CAAA;AACT;;;;"}
@@ -0,0 +1,38 @@
1
+ import parseGitUrl from 'git-url-parse';
2
+
3
+ function getGithubFileFetchUrl(url, config, credentials) {
4
+ try {
5
+ const { owner, name, ref, filepathtype, filepath } = parseGitUrl(url);
6
+ if (!owner || !name || !ref || // GitHub is automatically redirecting tree urls to blob urls so it's
7
+ // fine to pass a tree url.
8
+ filepathtype !== "blob" && filepathtype !== "raw" && filepathtype !== "tree") {
9
+ throw new Error("Invalid GitHub URL or file path");
10
+ }
11
+ const pathWithoutSlash = filepath.replace(/^\//, "");
12
+ if (chooseEndpoint(config, credentials) === "api") {
13
+ return `${config.apiBaseUrl}/repos/${owner}/${name}/contents/${pathWithoutSlash}?ref=${ref}`;
14
+ }
15
+ return `${config.rawBaseUrl}/${owner}/${name}/${ref}/${pathWithoutSlash}`;
16
+ } catch (e) {
17
+ throw new Error(`Incorrect URL: ${url}, ${e}`);
18
+ }
19
+ }
20
+ function getGitHubRequestOptions(config, credentials) {
21
+ const headers = {};
22
+ if (chooseEndpoint(config, credentials) === "api") {
23
+ headers.Accept = "application/vnd.github.v3.raw";
24
+ }
25
+ if (credentials.token) {
26
+ headers.Authorization = `token ${credentials.token}`;
27
+ }
28
+ return { headers };
29
+ }
30
+ function chooseEndpoint(config, credentials) {
31
+ if (config.apiBaseUrl && (credentials.token || !config.rawBaseUrl)) {
32
+ return "api";
33
+ }
34
+ return "raw";
35
+ }
36
+
37
+ export { chooseEndpoint, getGitHubRequestOptions, getGithubFileFetchUrl };
38
+ //# sourceMappingURL=core.esm.js.map
@@ -0,0 +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 parseGitUrl from 'git-url-parse';\nimport { GithubIntegrationConfig } from './config';\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 } = parseGitUrl(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\n/**\n * Gets the request options necessary to make requests to a given provider.\n *\n * @deprecated This function is no longer used internally\n * @param config - The relevant provider config\n * @public\n */\nexport function getGitHubRequestOptions(\n config: GithubIntegrationConfig,\n credentials: GithubCredentials,\n): { headers: Record<string, string> } {\n const headers: Record<string, string> = {};\n\n if (chooseEndpoint(config, credentials) === 'api') {\n headers.Accept = 'application/vnd.github.v3.raw';\n }\n\n if (credentials.token) {\n headers.Authorization = `token ${credentials.token}`;\n }\n\n return { headers };\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":[],"mappings":";;AAmCgB,SAAA,qBAAA,CACd,GACA,EAAA,MAAA,EACA,WACQ,EAAA;AACR,EAAI,IAAA;AACF,IAAM,MAAA,EAAE,OAAO,IAAM,EAAA,GAAA,EAAK,cAAc,QAAS,EAAA,GAAI,YAAY,GAAG,CAAA,CAAA;AACpE,IAAA,IACE,CAAC,KAAA,IACD,CAAC,IAAA,IACD,CAAC,GAAA;AAAA;AAAA,IAGA,YAAiB,KAAA,MAAA,IAChB,YAAiB,KAAA,KAAA,IACjB,iBAAiB,MACnB,EAAA;AACA,MAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA,CAAA;AAAA,KACnD;AAEA,IAAA,MAAM,gBAAmB,GAAA,QAAA,CAAS,OAAQ,CAAA,KAAA,EAAO,EAAE,CAAA,CAAA;AACnD,IAAA,IAAI,cAAe,CAAA,MAAA,EAAQ,WAAW,CAAA,KAAM,KAAO,EAAA;AACjD,MAAO,OAAA,CAAA,EAAG,MAAO,CAAA,UAAU,CAAU,OAAA,EAAA,KAAK,IAAI,IAAI,CAAA,UAAA,EAAa,gBAAgB,CAAA,KAAA,EAAQ,GAAG,CAAA,CAAA,CAAA;AAAA,KAC5F;AACA,IAAO,OAAA,CAAA,EAAG,MAAO,CAAA,UAAU,CAAI,CAAA,EAAA,KAAK,IAAI,IAAI,CAAA,CAAA,EAAI,GAAG,CAAA,CAAA,EAAI,gBAAgB,CAAA,CAAA,CAAA;AAAA,WAChE,CAAG,EAAA;AACV,IAAA,MAAM,IAAI,KAAM,CAAA,CAAA,eAAA,EAAkB,GAAG,CAAA,EAAA,EAAK,CAAC,CAAE,CAAA,CAAA,CAAA;AAAA,GAC/C;AACF,CAAA;AASgB,SAAA,uBAAA,CACd,QACA,WACqC,EAAA;AACrC,EAAA,MAAM,UAAkC,EAAC,CAAA;AAEzC,EAAA,IAAI,cAAe,CAAA,MAAA,EAAQ,WAAW,CAAA,KAAM,KAAO,EAAA;AACjD,IAAA,OAAA,CAAQ,MAAS,GAAA,+BAAA,CAAA;AAAA,GACnB;AAEA,EAAA,IAAI,YAAY,KAAO,EAAA;AACrB,IAAQ,OAAA,CAAA,aAAA,GAAgB,CAAS,MAAA,EAAA,WAAA,CAAY,KAAK,CAAA,CAAA,CAAA;AAAA,GACpD;AAEA,EAAA,OAAO,EAAE,OAAQ,EAAA,CAAA;AACnB,CAAA;AAEgB,SAAA,cAAA,CACd,QACA,WACe,EAAA;AACf,EAAA,IAAI,OAAO,UAAe,KAAA,WAAA,CAAY,KAAS,IAAA,CAAC,OAAO,UAAa,CAAA,EAAA;AAClE,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACA,EAAO,OAAA,KAAA,CAAA;AACT;;;;"}
@@ -0,0 +1,26 @@
1
+ import { readGithubIntegrationConfig, readGithubIntegrationConfigs } from './config.esm.js';
2
+ import { getGithubFileFetchUrl } from './core.esm.js';
3
+ import { GithubIntegration, replaceGithubUrlType } from './GithubIntegration.esm.js';
4
+
5
+ var __defProp = Object.defineProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __publicField = (obj, key, value) => {
8
+ __defNormalProp(obj, key + "" , value);
9
+ return value;
10
+ };
11
+ const getGitHubFileFetchUrl = getGithubFileFetchUrl;
12
+ class GitHubIntegration extends GithubIntegration {
13
+ constructor(integrationConfig) {
14
+ super(integrationConfig);
15
+ }
16
+ get config() {
17
+ return super.config;
18
+ }
19
+ }
20
+ __publicField(GitHubIntegration, "factory", GithubIntegration.factory);
21
+ const readGitHubIntegrationConfig = readGithubIntegrationConfig;
22
+ const readGitHubIntegrationConfigs = readGithubIntegrationConfigs;
23
+ const replaceGitHubUrlType = replaceGithubUrlType;
24
+
25
+ export { GitHubIntegration, getGitHubFileFetchUrl, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, replaceGitHubUrlType };
26
+ //# sourceMappingURL=deprecated.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deprecated.esm.js","sources":["../../src/github/deprecated.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 */\n\nimport {\n GithubIntegrationConfig,\n readGithubIntegrationConfig,\n readGithubIntegrationConfigs,\n} from './config';\nimport { getGithubFileFetchUrl } from './core';\nimport { GithubIntegration, replaceGithubUrlType } from './GithubIntegration';\nimport { ScmIntegrationsFactory } from '../types';\n\n/**\n * @public\n * @deprecated Use {@link getGithubFileFetchUrl} instead.\n */\nexport const getGitHubFileFetchUrl = getGithubFileFetchUrl;\n\n/**\n * @public\n * @deprecated Use {@link GithubIntegrationConfig} instead.\n */\nexport type GitHubIntegrationConfig = GithubIntegrationConfig;\n\n/**\n * @public\n * @deprecated Use {@link GithubIntegration} instead.\n */\nexport class GitHubIntegration extends GithubIntegration {\n static factory: ScmIntegrationsFactory<GitHubIntegration> =\n GithubIntegration.factory;\n\n constructor(integrationConfig: GitHubIntegrationConfig) {\n super(integrationConfig as GithubIntegrationConfig);\n }\n\n get config(): GitHubIntegrationConfig {\n return super.config as GitHubIntegrationConfig;\n }\n}\n\n/**\n * @public\n * @deprecated Use {@link readGithubIntegrationConfig} instead.\n */\nexport const readGitHubIntegrationConfig = readGithubIntegrationConfig;\n\n/**\n * @public\n * @deprecated Use {@link readGithubIntegrationConfigs} instead.\n */\nexport const readGitHubIntegrationConfigs = readGithubIntegrationConfigs;\n\n/**\n * @public\n * @deprecated Use {@link replaceGithubUrlType} instead.\n */\nexport const replaceGitHubUrlType = replaceGithubUrlType;\n"],"names":[],"mappings":";;;;;;;;;;AA6BO,MAAM,qBAAwB,GAAA,sBAAA;AAY9B,MAAM,0BAA0B,iBAAkB,CAAA;AAAA,EAIvD,YAAY,iBAA4C,EAAA;AACtD,IAAA,KAAA,CAAM,iBAA4C,CAAA,CAAA;AAAA,GACpD;AAAA,EAEA,IAAI,MAAkC,GAAA;AACpC,IAAA,OAAO,KAAM,CAAA,MAAA,CAAA;AAAA,GACf;AACF,CAAA;AAVE,aADW,CAAA,iBAAA,EACJ,WACL,iBAAkB,CAAA,OAAA,CAAA,CAAA;AAef,MAAM,2BAA8B,GAAA,4BAAA;AAMpC,MAAM,4BAA+B,GAAA,6BAAA;AAMrC,MAAM,oBAAuB,GAAA;;;;"}
@@ -0,0 +1,28 @@
1
+ import { SingleInstanceGitlabCredentialsProvider } from './SingleInstanceGitlabCredentialsProvider.esm.js';
2
+
3
+ class DefaultGitlabCredentialsProvider {
4
+ constructor(providers) {
5
+ this.providers = providers;
6
+ }
7
+ static fromIntegrations(integrations) {
8
+ const credentialsProviders = /* @__PURE__ */ new Map();
9
+ integrations.gitlab.list().forEach((integration) => {
10
+ const credentialsProvider = SingleInstanceGitlabCredentialsProvider.create(integration.config);
11
+ credentialsProviders.set(integration.config.host, credentialsProvider);
12
+ });
13
+ return new DefaultGitlabCredentialsProvider(credentialsProviders);
14
+ }
15
+ async getCredentials(opts) {
16
+ const parsed = new URL(opts.url);
17
+ const provider = this.providers.get(parsed.host);
18
+ if (!provider) {
19
+ throw new Error(
20
+ `There is no GitLab integration that matches ${opts.url}. Please add a configuration for an integration.`
21
+ );
22
+ }
23
+ return provider.getCredentials(opts);
24
+ }
25
+ }
26
+
27
+ export { DefaultGitlabCredentialsProvider };
28
+ //# sourceMappingURL=DefaultGitlabCredentialsProvider.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DefaultGitlabCredentialsProvider.esm.js","sources":["../../src/gitlab/DefaultGitlabCredentialsProvider.ts"],"sourcesContent":["/*\n * Copyright 2023 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 { ScmIntegrationRegistry } from '../registry';\nimport { SingleInstanceGitlabCredentialsProvider } from './SingleInstanceGitlabCredentialsProvider';\nimport { GitlabCredentials, GitlabCredentialsProvider } from './types';\n\n/**\n * Handles the creation and caching of credentials for GitLab integrations.\n *\n * @public\n */\nexport class DefaultGitlabCredentialsProvider\n implements GitlabCredentialsProvider\n{\n static fromIntegrations(integrations: ScmIntegrationRegistry) {\n const credentialsProviders: Map<string, GitlabCredentialsProvider> =\n new Map<string, GitlabCredentialsProvider>();\n\n integrations.gitlab.list().forEach(integration => {\n const credentialsProvider =\n SingleInstanceGitlabCredentialsProvider.create(integration.config);\n credentialsProviders.set(integration.config.host, credentialsProvider);\n });\n return new DefaultGitlabCredentialsProvider(credentialsProviders);\n }\n\n private constructor(\n private readonly providers: Map<string, GitlabCredentialsProvider>,\n ) {}\n\n async getCredentials(opts: { url: string }): Promise<GitlabCredentials> {\n const parsed = new URL(opts.url);\n const provider = this.providers.get(parsed.host);\n\n if (!provider) {\n throw new Error(\n `There is no GitLab integration that matches ${opts.url}. Please add a configuration for an integration.`,\n );\n }\n\n return provider.getCredentials(opts);\n }\n}\n"],"names":[],"mappings":";;AAyBO,MAAM,gCAEb,CAAA;AAAA,EAaU,YACW,SACjB,EAAA;AADiB,IAAA,IAAA,CAAA,SAAA,GAAA,SAAA,CAAA;AAAA,GAChB;AAAA,EAdH,OAAO,iBAAiB,YAAsC,EAAA;AAC5D,IAAM,MAAA,oBAAA,uBACA,GAAuC,EAAA,CAAA;AAE7C,IAAA,YAAA,CAAa,MAAO,CAAA,IAAA,EAAO,CAAA,OAAA,CAAQ,CAAe,WAAA,KAAA;AAChD,MAAA,MAAM,mBACJ,GAAA,uCAAA,CAAwC,MAAO,CAAA,WAAA,CAAY,MAAM,CAAA,CAAA;AACnE,MAAA,oBAAA,CAAqB,GAAI,CAAA,WAAA,CAAY,MAAO,CAAA,IAAA,EAAM,mBAAmB,CAAA,CAAA;AAAA,KACtE,CAAA,CAAA;AACD,IAAO,OAAA,IAAI,iCAAiC,oBAAoB,CAAA,CAAA;AAAA,GAClE;AAAA,EAMA,MAAM,eAAe,IAAmD,EAAA;AACtE,IAAA,MAAM,MAAS,GAAA,IAAI,GAAI,CAAA,IAAA,CAAK,GAAG,CAAA,CAAA;AAC/B,IAAA,MAAM,QAAW,GAAA,IAAA,CAAK,SAAU,CAAA,GAAA,CAAI,OAAO,IAAI,CAAA,CAAA;AAE/C,IAAA,IAAI,CAAC,QAAU,EAAA;AACb,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,CAAA,4CAAA,EAA+C,KAAK,GAAG,CAAA,gDAAA,CAAA;AAAA,OACzD,CAAA;AAAA,KACF;AAEA,IAAO,OAAA,QAAA,CAAS,eAAe,IAAI,CAAA,CAAA;AAAA,GACrC;AACF;;;;"}
@@ -0,0 +1,46 @@
1
+ import { basicIntegrations, defaultScmResolveUrl } from '../helpers.esm.js';
2
+ import { readGitLabIntegrationConfigs } from './config.esm.js';
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => {
7
+ __defNormalProp(obj, key + "" , value);
8
+ return value;
9
+ };
10
+ const _GitLabIntegration = class _GitLabIntegration {
11
+ constructor(integrationConfig) {
12
+ this.integrationConfig = integrationConfig;
13
+ }
14
+ get type() {
15
+ return "gitlab";
16
+ }
17
+ get title() {
18
+ return this.integrationConfig.host;
19
+ }
20
+ get config() {
21
+ return this.integrationConfig;
22
+ }
23
+ resolveUrl(options) {
24
+ return defaultScmResolveUrl(options);
25
+ }
26
+ resolveEditUrl(url) {
27
+ return replaceGitLabUrlType(url, "edit");
28
+ }
29
+ };
30
+ __publicField(_GitLabIntegration, "factory", ({ config }) => {
31
+ var _a;
32
+ const configs = readGitLabIntegrationConfigs(
33
+ (_a = config.getOptionalConfigArray("integrations.gitlab")) != null ? _a : []
34
+ );
35
+ return basicIntegrations(
36
+ configs.map((c) => new _GitLabIntegration(c)),
37
+ (i) => i.config.host
38
+ );
39
+ });
40
+ let GitLabIntegration = _GitLabIntegration;
41
+ function replaceGitLabUrlType(url, type) {
42
+ return url.replace(/\/\-\/(blob|tree|edit)\//, `/-/${type}/`);
43
+ }
44
+
45
+ export { GitLabIntegration, replaceGitLabUrlType };
46
+ //# sourceMappingURL=GitLabIntegration.esm.js.map
@@ -0,0 +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,kBAAA,GAAN,MAAM,kBAA4C,CAAA;AAAA,EAWvD,YAA6B,i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 var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, key + "" , value);
5
+ return value;
6
+ };
7
+ const _SingleInstanceGitlabCredentialsProvider = class _SingleInstanceGitlabCredentialsProvider {
8
+ constructor(token) {
9
+ this.token = token;
10
+ }
11
+ async getCredentials(_opts) {
12
+ if (!this.token) {
13
+ return {};
14
+ }
15
+ return {
16
+ headers: {
17
+ Authorization: `Bearer ${this.token}`
18
+ },
19
+ token: this.token
20
+ };
21
+ }
22
+ };
23
+ __publicField(_SingleInstanceGitlabCredentialsProvider, "create", (config) => {
24
+ return new _SingleInstanceGitlabCredentialsProvider(config.token);
25
+ });
26
+ let SingleInstanceGitlabCredentialsProvider = _SingleInstanceGitlabCredentialsProvider;
27
+
28
+ export { SingleInstanceGitlabCredentialsProvider };
29
+ //# sourceMappingURL=SingleInstanceGitlabCredentialsProvider.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SingleInstanceGitlabCredentialsProvider.esm.js","sources":["../../src/gitlab/SingleInstanceGitlabCredentialsProvider.ts"],"sourcesContent":["/*\n * Copyright 2023 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 { GitLabIntegrationConfig } from './config';\nimport { GitlabCredentials, GitlabCredentialsProvider } from './types';\n\nexport class SingleInstanceGitlabCredentialsProvider\n implements GitlabCredentialsProvider\n{\n static create: (\n config: GitLabIntegrationConfig,\n ) => GitlabCredentialsProvider = config => {\n return new SingleInstanceGitlabCredentialsProvider(config.token);\n };\n\n private constructor(private readonly token?: string) {}\n\n async getCredentials(_opts: { url: string }): Promise<GitlabCredentials> {\n if (!this.token) {\n return {};\n }\n\n return {\n headers: {\n Authorization: `Bearer ${this.token}`,\n },\n token: this.token,\n };\n }\n}\n"],"names":[],"mappings":";;;;;;AAmBO,MAAM,wCAAA,GAAN,MAAM,wCAEb,CAAA;AAAA,EAOU,YAA6B,KAAgB,EAAA;AAAhB,IAAA,IAAA,CAAA,KAAA,GAAA,KAAA,CAAA;AAAA,GAAiB;AAAA,EAEtD,MAAM,eAAe,KAAoD,EAAA;AACvE,IAAI,IAAA,CAAC,KAAK,KAAO,EAAA;AACf,MAAA,OAAO,EAAC,CAAA;AAAA,KACV;AAEA,IAAO,OAAA;AAAA,MACL,OAAS,EAAA;AAAA,QACP,aAAA,EAAe,CAAU,OAAA,EAAA,IAAA,CAAK,KAAK,CAAA,CAAA;AAAA,OACrC;AAAA,MACA,OAAO,IAAK,CAAA,KAAA;AAAA,KACd,CAAA;AAAA,GACF;AACF,CAAA,CAAA;AApBE,aAHW,CAAA,wCAAA,EAGJ,UAE0B,CAAU,MAAA,KAAA;AACzC,EAAO,OAAA,IAAI,wCAAwC,CAAA,MAAA,CAAO,KAAK,CAAA,CAAA;AACjE,CAAA,CAAA,CAAA;AAPK,IAAM,uCAAN,GAAA;;;;"}
@@ -0,0 +1,57 @@
1
+ import { trimEnd } from 'lodash';
2
+ import { isValidHost, isValidUrl } from '../helpers.esm.js';
3
+
4
+ const GITLAB_HOST = "gitlab.com";
5
+ const GITLAB_API_BASE_URL = "https://gitlab.com/api/v4";
6
+ function readGitLabIntegrationConfig(config) {
7
+ var _a;
8
+ const host = config.getString("host");
9
+ let apiBaseUrl = config.getOptionalString("apiBaseUrl");
10
+ const token = (_a = config.getOptionalString("token")) == null ? void 0 : _a.trim();
11
+ let baseUrl = config.getOptionalString("baseUrl");
12
+ if (apiBaseUrl) {
13
+ apiBaseUrl = trimEnd(apiBaseUrl, "/");
14
+ } else if (host === GITLAB_HOST) {
15
+ apiBaseUrl = GITLAB_API_BASE_URL;
16
+ }
17
+ if (baseUrl) {
18
+ baseUrl = trimEnd(baseUrl, "/");
19
+ } else {
20
+ baseUrl = `https://${host}`;
21
+ }
22
+ if (!isValidHost(host)) {
23
+ throw new Error(
24
+ `Invalid GitLab integration config, '${host}' is not a valid host`
25
+ );
26
+ } else if (!apiBaseUrl || !isValidUrl(apiBaseUrl)) {
27
+ throw new Error(
28
+ `Invalid GitLab integration config, '${apiBaseUrl}' is not a valid apiBaseUrl`
29
+ );
30
+ } else if (!isValidUrl(baseUrl)) {
31
+ throw new Error(
32
+ `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl`
33
+ );
34
+ }
35
+ return { host, token, apiBaseUrl, baseUrl };
36
+ }
37
+ function readGitLabIntegrationConfigs(configs) {
38
+ const result = configs.map(readGitLabIntegrationConfig);
39
+ if (!result.some((c) => c.host === GITLAB_HOST)) {
40
+ result.push({
41
+ host: GITLAB_HOST,
42
+ apiBaseUrl: GITLAB_API_BASE_URL,
43
+ baseUrl: `https://${GITLAB_HOST}`
44
+ });
45
+ }
46
+ return result;
47
+ }
48
+ function getGitLabIntegrationRelativePath(config) {
49
+ let relativePath = "";
50
+ if (config.host !== GITLAB_HOST) {
51
+ relativePath = new URL(config.baseUrl).pathname;
52
+ }
53
+ return trimEnd(relativePath, "/");
54
+ }
55
+
56
+ export { getGitLabIntegrationRelativePath, readGitLabIntegrationConfig, readGitLabIntegrationConfigs };
57
+ //# sourceMappingURL=config.esm.js.map
@@ -0,0 +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/**\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 { host, token, apiBaseUrl, baseUrl };\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,WAAc,GAAA,YAAA,CAAA;AACpB,MAAM,mBAAsB,GAAA,2BAAA,CAAA;AA2CrB,SAAS,4BACd,MACyB,EAAA;AAlE3B,EAAA,IAAA,EAAA,CAAA;AAmEE,EAAM,MAAA,IAAA,GAAO,MAAO,CAAA,SAAA,CAAU,MAAM,CAAA,CAAA;AACpC,EAAI,IAAA,UAAA,GAAa,MAAO,CAAA,iBAAA,CAAkB,YAAY,CAAA,CAAA;AACtD,EAAA,MAAM,KAAQ,GAAA,CAAA,EAAA,GAAA,MAAA,CAAO,iBAAkB,CAAA,OAAO,MAAhC,IAAmC,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,EAAA,CAAA;AACjD,EAAI,IAAA,OAAA,GAAU,MAAO,CAAA,iBAAA,CAAkB,SAAS,CAAA,CAAA;AAChD,EAAA,IA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