Skip to content

Commit

Permalink
Uses shared library to retrieve user's Bitbucket PRs per repo.
Browse files Browse the repository at this point in the history
Integrates changes from
gitkraken/provider-apis-package-js#223
Solves #4128
  • Loading branch information
sergeibbb committed Mar 10, 2025
1 parent 5601400 commit 1845881
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 66 deletions.
56 changes: 23 additions & 33 deletions src/plus/integrations/providers/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,18 @@ export class BitbucketIntegration extends HostingIntegration<
return undefined;
}

const api = await this.getProvidersApi();
if (!api) {
return undefined;
}

const remotes = await flatSettled(this.container.git.openRepositories.map(r => r.git.remotes().getRemotes()));
const workspaceRepos = await nonnullSettled(
remotes.map(async r => ((await r.getIntegration())?.id === this.id ? r.path : undefined)),
remotes.map(async r => {
const integration = await r.getIntegration();
const [namespace, name] = r.path.split('/');
return integration?.id === this.id ? { name: name, namespace: namespace } : undefined;
}),
);

const user = await this.getProviderCurrentAccount(session);
Expand All @@ -218,44 +227,25 @@ export class BitbucketIntegration extends HostingIntegration<
const workspaces = await this.getProviderResourcesForUser(session);
if (workspaces == null || workspaces.length === 0) return undefined;

const providersApi = await this.getProvidersApi();
const api = await this.container.bitbucket;
if (!providersApi && !api) {
return undefined;
}

const integration = await this.container.integrations.get(this.id);

const authoredPrs = providersApi
? workspaces.map(async ws => {
const prs = await providersApi.getBitbucketPullRequestsAuthoredByUserForWorkspace(
user.id,
ws.slug,
{
accessToken: session.accessToken,
},
);
return prs?.map(pr => fromProviderPullRequest(pr, integration));
})
: [];
const authoredPrs = workspaces.map(async ws => {
const prs = await api.getBitbucketPullRequestsAuthoredByUserForWorkspace(user.id, ws.slug, {
accessToken: session.accessToken,
});
return prs?.map(pr => fromProviderPullRequest(pr, integration));
});

const reviewingPrs = api
? workspaceRepos.map(repo => {
const [owner, name] = repo.split('/');
return api.getUsersReviewingPullRequestsForRepo(
this,
session.accessToken,
user.id,
owner,
name,
this.apiBaseUrl,
);
})
: [];
.getPullRequestsForRepos(this.id, workspaceRepos, {
query: `state="OPEN" AND reviewers.uuid="${user.id}"`,
accessToken: session.accessToken,
})
.then(r => r.values?.map(pr => fromProviderPullRequest(pr, integration)));

return [
...uniqueBy(
await flatSettled([...authoredPrs, ...reviewingPrs]),
await flatSettled([...authoredPrs, reviewingPrs]),
pr => pr.url,
(orig, _cur) => orig,
),
Expand Down Expand Up @@ -346,7 +336,7 @@ export function isBitbucketCloudDomain(domain: string | undefined): boolean {
return domain != null && bitbucketCloudDomainRegex.test(domain);
}

type MaybePromiseArr<T> = Promise<T | undefined>[] | (T | undefined)[];
type MaybePromiseArr<T> = (Promise<T | undefined> | T | undefined)[];

async function nonnullSettled<T>(arr: MaybePromiseArr<T>): Promise<T[]> {
const all = await Promise.allSettled(arr);
Expand Down
33 changes: 0 additions & 33 deletions src/plus/integrations/providers/bitbucket/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,39 +289,6 @@ export class BitbucketApi implements Disposable {
}
}

async getUsersReviewingPullRequestsForRepo(
provider: Provider,
token: string,
userUuid: string,
owner: string,
repo: string,
baseUrl: string,
): Promise<PullRequest[] | undefined> {
const scope = getLogScope();

const query = encodeURIComponent(`state="OPEN" AND reviewers.uuid="${userUuid}"`);
const response = await this.request<{
values: BitbucketPullRequest[];
pagelen: number;
size: number;
page: number;
}>(
provider,
token,
baseUrl,
`repositories/${owner}/${repo}/pullrequests?q=${query}&state=OPEN&fields=%2Bvalues.reviewers,%2Bvalues.participants`,
{
method: 'GET',
},
scope,
);

if (!response?.values?.length) {
return undefined;
}
return response.values.map(pr => fromBitbucketPullRequest(pr, provider));
}

private async request<T>(
provider: Provider,
token: string,
Expand Down
1 change: 1 addition & 0 deletions src/plus/integrations/providers/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export interface GetPullRequestsOptions {
assigneeLogins?: string[];
reviewRequestedLogin?: string;
mentionLogin?: string;
query?: string;
cursor?: string; // stringified JSON object of type { type: 'cursor' | 'page'; value: string | number } | {}
baseUrl?: string;
}
Expand Down

0 comments on commit 1845881

Please sign in to comment.