Skip to content

Commit 3ff04ed

Browse files
committed
Retrieves Bitbucket PRs where a user is author or reviewer
(#4046)
1 parent ff93e23 commit 3ff04ed

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

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

+23-17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type {
1212
SearchedPullRequest,
1313
} from '../../../git/models/pullRequest';
1414
import type { RepositoryMetadata } from '../../../git/models/repositoryMetadata';
15+
import { groupBy } from '../../../system/iterable';
16+
import { getSettledValue } from '../../../system/promise';
1517
import type { IntegrationAuthenticationProviderDescriptor } from '../authentication/integrationAuthenticationProvider';
1618
import type { ProviderAuthenticationSession } from '../authentication/models';
1719
import type { ResourceDescriptor } from '../integration';
@@ -263,7 +265,6 @@ export class BitbucketIntegration extends HostingIntegration<
263265
session: ProviderAuthenticationSession,
264266
requestedRepositories?: BitbucketRepositoryDescriptor[],
265267
): Promise<SearchedPullRequest[] | undefined> {
266-
const api = await this.getProvidersApi();
267268
if (requestedRepositories != null) {
268269
// TODO: implement repos version
269270
return undefined;
@@ -278,17 +279,30 @@ export class BitbucketIntegration extends HostingIntegration<
278279
const repos = await this.getProviderProjectsForResources(session, workspaces);
279280
if (repos == null || repos.length === 0) return undefined;
280281

281-
const prs = await api.getPullRequestsForRepos(
282-
HostingIntegrationId.Bitbucket,
283-
repos.map(repo => ({ namespace: repo.owner, name: repo.name })),
284-
{
285-
accessToken: session.accessToken,
286-
},
282+
const api = await this.container.bitbucket;
283+
if (!api) return undefined;
284+
const prsResult = await Promise.allSettled(
285+
repos.map(repo =>
286+
api.getUsersPullRequestsForRepo(this, session.accessToken, user.id, repo.owner, repo.name, {
287+
baseUrl: this.apiBaseUrl,
288+
}),
289+
),
287290
);
288-
return prs.values.map(pr => ({
289-
pullRequest: this.fromBitbucketProviderPullRequest(pr),
291+
const prs = prsResult
292+
.map(r => getSettledValue(r))
293+
.filter(r => r != null)
294+
.flat();
295+
296+
const groups = groupBy(prs, pr => (pr.author.id === user.id ? 'authored' : 'other'));
297+
const authoredPrs = groups.authored.map(pr => ({
298+
pullRequest: pr,
299+
reasons: ['authored'],
300+
}));
301+
const otherPrs = groups.other.map(pr => ({
302+
pullRequest: pr,
290303
reasons: [],
291304
}));
305+
return [...authoredPrs, ...otherPrs];
292306
}
293307

294308
protected override async searchProviderMyIssues(
@@ -298,14 +312,6 @@ export class BitbucketIntegration extends HostingIntegration<
298312
return Promise.resolve(undefined);
299313
}
300314

301-
private fromBitbucketProviderPullRequest(
302-
remotePullRequest: ProviderPullRequest,
303-
// repoDescriptors: BitbucketRemoteRepositoryDescriptor[],
304-
): PullRequest {
305-
remotePullRequest.graphQLId = remotePullRequest.id;
306-
return fromProviderPullRequest(remotePullRequest, this);
307-
}
308-
309315
protected override async providerOnConnect(): Promise<void> {
310316
if (this._session == null) return;
311317

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

+35
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,41 @@ export class BitbucketApi implements Disposable {
223223
}
224224
}
225225

226+
public async getUsersPullRequestsForRepo(
227+
provider: Provider,
228+
token: string,
229+
userUuid: string,
230+
owner: string,
231+
repo: string,
232+
options: {
233+
baseUrl: string;
234+
},
235+
): Promise<PullRequest[] | undefined> {
236+
const scope = getLogScope();
237+
238+
const query = encodeURIComponent(`reviewers.uuid="${userUuid}" OR author.uuid="${userUuid}"`);
239+
const response = await this.request<{
240+
values: BitbucketPullRequest[];
241+
pagelen: number;
242+
size: number;
243+
page: number;
244+
}>(
245+
provider,
246+
token,
247+
options.baseUrl,
248+
`repositories/${owner}/${repo}/pullrequests?q=${query}&state=OPEN&fields=%2Bvalues.reviewers,%2Bvalues.participants`,
249+
{
250+
method: 'GET',
251+
},
252+
scope,
253+
);
254+
255+
if (!response?.values?.length) {
256+
return undefined;
257+
}
258+
return response.values.map(pr => fromBitbucketPullRequest(pr, provider));
259+
}
260+
226261
private async request<T>(
227262
provider: Provider,
228263
token: string,

0 commit comments

Comments
 (0)