|
1 | 1 | import type { AuthenticationSession, CancellationToken } from 'vscode';
|
| 2 | +import { md5 } from '@env/crypto'; |
2 | 3 | import { HostingIntegrationId } from '../../../constants.integrations';
|
3 | 4 | import type { Account } from '../../../git/models/author';
|
4 | 5 | import type { DefaultBranch } from '../../../git/models/defaultBranch';
|
@@ -28,6 +29,7 @@ interface BitbucketWorkspaceDescriptor extends ResourceDescriptor {
|
28 | 29 | }
|
29 | 30 |
|
30 | 31 | interface BitbucketRemoteRepositoryDescriptor extends ResourceDescriptor {
|
| 32 | + resourceId: string; |
31 | 33 | owner: string;
|
32 | 34 | name: string;
|
33 | 35 | cloneUrlHttps?: string;
|
@@ -238,6 +240,7 @@ export class BitbucketIntegration extends HostingIntegration<
|
238 | 240 | `${accessToken}:${resource.id}`,
|
239 | 241 | resourceRepos.map(r => ({
|
240 | 242 | id: `${r.owner}/${r.name}`,
|
| 243 | + resourceId: r.owner, |
241 | 244 | owner: r.owner,
|
242 | 245 | name: r.name,
|
243 | 246 | key: `${r.owner}/${r.name}`,
|
@@ -299,6 +302,77 @@ export class BitbucketIntegration extends HostingIntegration<
|
299 | 302 | remotePullRequest.graphQLId = remotePullRequest.id;
|
300 | 303 | return fromProviderPullRequest(remotePullRequest, this);
|
301 | 304 | }
|
| 305 | + |
| 306 | + protected override async providerOnConnect(): Promise<void> { |
| 307 | + if (this._session == null) return; |
| 308 | + |
| 309 | + const accountStorageKey = md5(this._session.accessToken); |
| 310 | + |
| 311 | + const storedAccount = this.container.storage.get(`bitbucket:${accountStorageKey}:account`); |
| 312 | + const storedWorkspaces = this.container.storage.get(`bitbucket:${accountStorageKey}:workspaces`); |
| 313 | + const storedRepositories = this.container.storage.get(`bitbucket:${accountStorageKey}:repositories`); |
| 314 | + |
| 315 | + let account: Account | undefined = storedAccount?.data ? { ...storedAccount.data, provider: this } : undefined; |
| 316 | + let workspaces = storedWorkspaces?.data?.map(o => ({ ...o })); |
| 317 | + let repositories = storedRepositories?.data?.map(p => ({ ...p })); |
| 318 | + |
| 319 | + if (storedAccount == null) { |
| 320 | + account = await this.getProviderCurrentAccount(this._session); |
| 321 | + if (account != null) { |
| 322 | + // Clear all other stored workspaces and repositories and accounts when our session changes |
| 323 | + await this.container.storage.deleteWithPrefix('bitbucket'); |
| 324 | + await this.container.storage.store(`bitbucket:${accountStorageKey}:account`, { |
| 325 | + v: 1, |
| 326 | + timestamp: Date.now(), |
| 327 | + data: { |
| 328 | + id: account.id, |
| 329 | + name: account.name, |
| 330 | + email: account.email, |
| 331 | + avatarUrl: account.avatarUrl, |
| 332 | + username: account.username, |
| 333 | + }, |
| 334 | + }); |
| 335 | + } |
| 336 | + } |
| 337 | + this._accounts ??= new Map<string, Account | undefined>(); |
| 338 | + this._accounts.set(this._session.accessToken, account); |
| 339 | + |
| 340 | + if (storedWorkspaces == null) { |
| 341 | + workspaces = await this.getProviderResourcesForUser(this._session, true); |
| 342 | + await this.container.storage.store(`bitbucket:${accountStorageKey}:workspaces`, { |
| 343 | + v: 1, |
| 344 | + timestamp: Date.now(), |
| 345 | + data: workspaces, |
| 346 | + }); |
| 347 | + } |
| 348 | + this._workspaces ??= new Map<string, BitbucketWorkspaceDescriptor[] | undefined>(); |
| 349 | + this._workspaces.set(this._session.accessToken, workspaces); |
| 350 | + |
| 351 | + if (storedRepositories == null && workspaces?.length) { |
| 352 | + repositories = await this.getProviderProjectsForResources(this._session, workspaces); |
| 353 | + await this.container.storage.store(`bitbucket:${accountStorageKey}:repositories`, { |
| 354 | + v: 1, |
| 355 | + timestamp: Date.now(), |
| 356 | + data: repositories, |
| 357 | + }); |
| 358 | + } |
| 359 | + this._repositories ??= new Map<string, BitbucketRemoteRepositoryDescriptor[] | undefined>(); |
| 360 | + for (const repository of repositories ?? []) { |
| 361 | + const resourceKey = `${this._session.accessToken}:${repository.resourceId}`; |
| 362 | + const repos = this._repositories.get(resourceKey); |
| 363 | + if (repos == null) { |
| 364 | + this._repositories.set(resourceKey, [repository]); |
| 365 | + } else if (!repos.some(r => r.key === repository.key)) { |
| 366 | + repos.push(repository); |
| 367 | + } |
| 368 | + } |
| 369 | + } |
| 370 | + |
| 371 | + protected override providerOnDisconnect(): void { |
| 372 | + this._accounts = undefined; |
| 373 | + this._workspaces = undefined; |
| 374 | + this._repositories = undefined; |
| 375 | + } |
302 | 376 | }
|
303 | 377 |
|
304 | 378 | const bitbucketCloudDomainRegex = /^bitbucket\.org$/i;
|
|
0 commit comments