Skip to content

Commit 8f8d20c

Browse files
committed
Caches and stores Bitbucket inermediate data e.g. workspaces and repos
(#4046, #4099)
1 parent f8da766 commit 8f8d20c

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Diff for: src/constants.storage.ts

+17
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export type GlobalStorage = {
9393
[key in `azure:${string}:organizations`]: Stored<StoredAzureOrganization[] | undefined>;
9494
} & {
9595
[key in `azure:${string}:projects`]: Stored<StoredAzureProject[] | undefined>;
96+
} & { [key in `bitbucket:${string}:account`]: Stored<StoredBitbucketAccount | undefined> } & {
97+
[key in `bitbucket:${string}:workspaces`]: Stored<StoredBitbucketWorkspace[] | undefined>;
9698
};
9799

98100
export type StoredIntegrationConfigurations = Record<string, StoredConfiguredIntegrationDescriptor[] | undefined>;
@@ -245,6 +247,21 @@ export interface StoredAzureProject {
245247
resourceName: string;
246248
}
247249

250+
export interface StoredBitbucketAccount {
251+
id: string;
252+
name: string | undefined;
253+
username: string | undefined;
254+
email: string | undefined;
255+
avatarUrl: string | undefined;
256+
}
257+
258+
export interface StoredBitbucketWorkspace {
259+
key: string;
260+
id: string;
261+
name: string;
262+
slug: string;
263+
}
264+
248265
export interface StoredAvatar {
249266
uri: string;
250267
timestamp: number;

Diff for: src/plus/integrations/providers/bitbucket.ts

+51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { AuthenticationSession, CancellationToken } from 'vscode';
2+
import { md5 } from '@env/crypto';
23
import { HostingIntegrationId } from '../../../constants.integrations';
34
import type { Account } from '../../../git/models/author';
45
import type { DefaultBranch } from '../../../git/models/defaultBranch';
@@ -222,6 +223,7 @@ export class BitbucketIntegration extends HostingIntegration<
222223
`${accessToken}:${resource.id}`,
223224
resourceRepos.map(r => ({
224225
id: `${r.owner}/${r.name}`,
226+
resourceId: r.owner,
225227
owner: r.owner,
226228
name: r.name,
227229
key: `${r.owner}/${r.name}`,
@@ -283,6 +285,55 @@ export class BitbucketIntegration extends HostingIntegration<
283285
remotePullRequest.graphQLId = remotePullRequest.id;
284286
return fromProviderPullRequest(remotePullRequest, this);
285287
}
288+
289+
protected override async providerOnConnect(): Promise<void> {
290+
if (this._session == null) return;
291+
292+
const accountStorageKey = md5(this._session.accessToken);
293+
294+
const storedAccount = this.container.storage.get(`bitbucket:${accountStorageKey}:account`);
295+
const storedWorkspaces = this.container.storage.get(`bitbucket:${accountStorageKey}:workspaces`);
296+
297+
let account: Account | undefined = storedAccount?.data ? { ...storedAccount.data, provider: this } : undefined;
298+
let workspaces = storedWorkspaces?.data?.map(o => ({ ...o }));
299+
300+
if (storedAccount == null) {
301+
account = await this.getProviderCurrentAccount(this._session);
302+
if (account != null) {
303+
// Clear all other stored workspaces and repositories and accounts when our session changes
304+
await this.container.storage.deleteWithPrefix('bitbucket');
305+
await this.container.storage.store(`bitbucket:${accountStorageKey}:account`, {
306+
v: 1,
307+
timestamp: Date.now(),
308+
data: {
309+
id: account.id,
310+
name: account.name,
311+
email: account.email,
312+
avatarUrl: account.avatarUrl,
313+
username: account.username,
314+
},
315+
});
316+
}
317+
}
318+
this._accounts ??= new Map<string, Account | undefined>();
319+
this._accounts.set(this._session.accessToken, account);
320+
321+
if (storedWorkspaces == null) {
322+
workspaces = await this.getProviderResourcesForUser(this._session, true);
323+
await this.container.storage.store(`bitbucket:${accountStorageKey}:workspaces`, {
324+
v: 1,
325+
timestamp: Date.now(),
326+
data: workspaces,
327+
});
328+
}
329+
this._workspaces ??= new Map<string, BitbucketWorkspaceDescriptor[] | undefined>();
330+
this._workspaces.set(this._session.accessToken, workspaces);
331+
}
332+
333+
protected override providerOnDisconnect(): void {
334+
this._accounts = undefined;
335+
this._workspaces = undefined;
336+
}
286337
}
287338

288339
const bitbucketCloudDomainRegex = /^bitbucket\.org$/i;

0 commit comments

Comments
 (0)