Skip to content

Commit bda967f

Browse files
committed
Implements gitPullRequestForCommit for Bitbucket
(#4192)
1 parent 3693f71 commit bda967f

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/plus/integrations/providers/bitbucket.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,18 @@ export class BitbucketIntegration extends HostingIntegration<
131131
}
132132

133133
protected override async getProviderPullRequestForCommit(
134-
_session: AuthenticationSession,
135-
_repo: BitbucketRepositoryDescriptor,
136-
_rev: string,
134+
{ accessToken }: AuthenticationSession,
135+
repo: BitbucketRepositoryDescriptor,
136+
rev: string,
137137
): Promise<PullRequest | undefined> {
138-
return Promise.resolve(undefined);
138+
return (await this.container.bitbucket)?.getPullRequestForCommit(
139+
this,
140+
accessToken,
141+
repo.owner,
142+
repo.name,
143+
rev,
144+
this.apiBaseUrl,
145+
);
139146
}
140147

141148
protected override async getProviderRepositoryMetadata(

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,55 @@ export class BitbucketApi implements Disposable {
369369
}
370370
}
371371

372+
@debug<BitbucketApi['getPullRequestForCommit']>({ args: { 0: p => p.name, 1: '<token>' } })
373+
async getPullRequestForCommit(
374+
provider: Provider,
375+
token: string,
376+
owner: string,
377+
repo: string,
378+
rev: string,
379+
baseUrl: string,
380+
_options?: {
381+
avatarSize?: number;
382+
},
383+
cancellation?: CancellationToken,
384+
): Promise<PullRequest | undefined> {
385+
const scope = getLogScope();
386+
387+
try {
388+
const fields = [
389+
'+values.*',
390+
'+values.destination.repository',
391+
'+values.destination.branch.*',
392+
'+values.destination.commit.*',
393+
'+values.source.repository.*',
394+
'+values.source.branch.*',
395+
'+values.source.commit.*',
396+
];
397+
const fieldsParam = encodeURIComponent(fields.join(','));
398+
const response = await this.request<{ values: BitbucketPullRequest[] }>(
399+
provider,
400+
token,
401+
baseUrl,
402+
`repositories/${owner}/${repo}/commit/${rev}/pullrequests?fields=${fieldsParam}`,
403+
{
404+
method: 'GET',
405+
},
406+
scope,
407+
cancellation,
408+
);
409+
const pr = response?.values?.reduce<BitbucketPullRequest | undefined>(
410+
(acc, pr) => (!acc || pr.updated_on > acc.updated_on ? pr : acc),
411+
undefined,
412+
);
413+
if (!pr) return undefined;
414+
return fromBitbucketPullRequest(pr, provider);
415+
} catch (ex) {
416+
Logger.error(ex, scope);
417+
return undefined;
418+
}
419+
}
420+
372421
private async request<T>(
373422
provider: Provider,
374423
token: string,

0 commit comments

Comments
 (0)