Skip to content

Commit

Permalink
Caches and stores Bitbucket inermediate data e.g. workspaces and repos
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Mar 5, 2025
1 parent e261b86 commit f17a325
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/constants.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export type GlobalStorage = {
[key in `azure:${string}:organizations`]: Stored<StoredAzureOrganization[] | undefined>;
} & {
[key in `azure:${string}:projects`]: Stored<StoredAzureProject[] | undefined>;
} & { [key in `bitbucket:${string}:account`]: Stored<StoredBitbucketAccount | undefined> } & {
[key in `bitbucket:${string}:workspaces`]: Stored<StoredBitbucketWorkspace[] | undefined>;
};

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

export interface StoredBitbucketAccount {
id: string;
name: string | undefined;
username: string | undefined;
email: string | undefined;
avatarUrl: string | undefined;
}

export interface StoredBitbucketWorkspace {
key: string;
id: string;
name: string;
slug: string;
}

export interface StoredAvatar {
uri: string;
timestamp: number;
Expand Down
51 changes: 51 additions & 0 deletions src/plus/integrations/providers/bitbucket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AuthenticationSession, CancellationToken } from 'vscode';
import { md5 } from '@env/crypto';
import { HostingIntegrationId } from '../../../constants.integrations';
import type { Account } from '../../../git/models/author';
import type { DefaultBranch } from '../../../git/models/defaultBranch';
Expand Down Expand Up @@ -222,6 +223,7 @@ export class BitbucketIntegration extends HostingIntegration<
`${accessToken}:${resource.id}`,
resourceRepos.map(r => ({
id: `${r.owner}/${r.name}`,
resourceId: r.owner,
owner: r.owner,
name: r.name,
key: `${r.owner}/${r.name}`,
Expand Down Expand Up @@ -283,6 +285,55 @@ export class BitbucketIntegration extends HostingIntegration<
remotePullRequest.graphQLId = remotePullRequest.id;
return fromProviderPullRequest(remotePullRequest, this);
}

protected override async providerOnConnect(): Promise<void> {
if (this._session == null) return;

const accountStorageKey = md5(this._session.accessToken);

const storedAccount = this.container.storage.get(`bitbucket:${accountStorageKey}:account`);
const storedWorkspaces = this.container.storage.get(`bitbucket:${accountStorageKey}:workspaces`);

let account: Account | undefined = storedAccount?.data ? { ...storedAccount.data, provider: this } : undefined;
let workspaces = storedWorkspaces?.data?.map(o => ({ ...o }));

if (storedAccount == null) {
account = await this.getProviderCurrentAccount(this._session);
if (account != null) {
// Clear all other stored workspaces and repositories and accounts when our session changes
await this.container.storage.deleteWithPrefix('bitbucket');
await this.container.storage.store(`bitbucket:${accountStorageKey}:account`, {
v: 1,
timestamp: Date.now(),
data: {
id: account.id,
name: account.name,
email: account.email,
avatarUrl: account.avatarUrl,
username: account.username,
},
});
}
}
this._accounts ??= new Map<string, Account | undefined>();
this._accounts.set(this._session.accessToken, account);

if (storedWorkspaces == null) {
workspaces = await this.getProviderResourcesForUser(this._session, true);
await this.container.storage.store(`bitbucket:${accountStorageKey}:workspaces`, {
v: 1,
timestamp: Date.now(),
data: workspaces,
});
}
this._workspaces ??= new Map<string, BitbucketWorkspaceDescriptor[] | undefined>();
this._workspaces.set(this._session.accessToken, workspaces);
}

protected override providerOnDisconnect(): void {
this._accounts = undefined;
this._workspaces = undefined;
}
}

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

0 comments on commit f17a325

Please sign in to comment.