Skip to content

Commit aecdedf

Browse files
committed
Retrieves Bitbucket PRs where a user is author or reviewer
(#4046)
1 parent 23d6633 commit aecdedf

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

src/plus/integrations/providers/bitbucket.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import type { Issue, IssueShape } from '../../../git/models/issue';
77
import type { IssueOrPullRequest, IssueOrPullRequestType } from '../../../git/models/issueOrPullRequest';
88
import type { PullRequest, PullRequestMergeMethod, PullRequestState } from '../../../git/models/pullRequest';
99
import type { RepositoryMetadata } from '../../../git/models/repositoryMetadata';
10+
import { groupBy } from '../../../system/iterable';
11+
import { getSettledValue } from '../../../system/promise';
1012
import type { IntegrationAuthenticationProviderDescriptor } from '../authentication/integrationAuthenticationProvider';
1113
import type { ProviderAuthenticationSession } from '../authentication/models';
1214
import type { ResourceDescriptor } from '../integration';
1315
import { HostingIntegration } from '../integration';
14-
import type { ProviderPullRequest } from './models';
15-
import { fromProviderPullRequest, providersMetadata } from './models';
16+
import { providersMetadata } from './models';
1617

1718
const metadata = providersMetadata[HostingIntegrationId.Bitbucket];
1819
const authProvider = Object.freeze({ id: metadata.id, scopes: metadata.scopes });
@@ -263,7 +264,6 @@ export class BitbucketIntegration extends HostingIntegration<
263264
session: ProviderAuthenticationSession,
264265
requestedRepositories?: BitbucketRepositoryDescriptor[],
265266
): Promise<PullRequest[] | undefined> {
266-
const api = await this.getProvidersApi();
267267
if (requestedRepositories != null) {
268268
// TODO: implement repos version
269269
return undefined;
@@ -278,14 +278,24 @@ export class BitbucketIntegration extends HostingIntegration<
278278
const repos = await this.getProviderProjectsForResources(session, workspaces);
279279
if (repos == null || repos.length === 0) return undefined;
280280

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-
},
281+
const api = await this.container.bitbucket;
282+
if (!api) return undefined;
283+
const prsResult = await Promise.allSettled(
284+
repos.map(repo =>
285+
api.getUsersPullRequestsForRepo(
286+
this,
287+
session.accessToken,
288+
user.id,
289+
repo.owner,
290+
repo.name,
291+
this.apiBaseUrl,
292+
),
293+
),
287294
);
288-
return prs.values.map(pr => this.fromBitbucketProviderPullRequest(pr));
295+
return prsResult
296+
.map(r => getSettledValue(r))
297+
.filter(r => r != null)
298+
.flat();
289299
}
290300

291301
protected override async searchProviderMyIssues(
@@ -295,14 +305,6 @@ export class BitbucketIntegration extends HostingIntegration<
295305
return Promise.resolve(undefined);
296306
}
297307

298-
private fromBitbucketProviderPullRequest(
299-
remotePullRequest: ProviderPullRequest,
300-
// repoDescriptors: BitbucketRemoteRepositoryDescriptor[],
301-
): PullRequest {
302-
remotePullRequest.graphQLId = remotePullRequest.id;
303-
return fromProviderPullRequest(remotePullRequest, this);
304-
}
305-
306308
protected override async providerOnConnect(): Promise<void> {
307309
if (this._session == null) return;
308310

src/plus/integrations/providers/bitbucket/bitbucket.ts

+33
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,39 @@ export class BitbucketApi implements Disposable {
221221
}
222222
}
223223

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

0 commit comments

Comments
 (0)