Skip to content

Commit e16c481

Browse files
committed
Implements getPullRequestForCommit for Bitbucket DataCenter
(#4192, #4243)
1 parent f10f500 commit e16c481

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

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

+13-17
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,13 @@ export class BitbucketServerIntegration extends HostingIntegration<
101101
if (type === 'issue') {
102102
return undefined;
103103
}
104-
const integration = await this.container.integrations.get(this.id);
105-
if (!integration) {
106-
return undefined;
107-
}
108104
return (await this.container.bitbucket)?.getServerPullRequestById(
109105
this,
110106
accessToken,
111107
repo.owner,
112108
repo.name,
113109
id,
114110
this.apiBaseUrl,
115-
integration,
116111
);
117112
}
118113

@@ -133,27 +128,29 @@ export class BitbucketServerIntegration extends HostingIntegration<
133128
include?: PullRequestState[];
134129
},
135130
): Promise<PullRequest | undefined> {
136-
const integration = await this.container.integrations.get(this.id);
137-
if (!integration) {
138-
return undefined;
139-
}
140131
return (await this.container.bitbucket)?.getServerPullRequestForBranch(
141132
this,
142133
accessToken,
143134
repo.owner,
144135
repo.name,
145136
branch,
146137
this.apiBaseUrl,
147-
integration,
148138
);
149139
}
150140

151141
protected override async getProviderPullRequestForCommit(
152-
_session: AuthenticationSession,
153-
_repo: BitbucketRepositoryDescriptor,
154-
_rev: string,
142+
{ accessToken }: AuthenticationSession,
143+
repo: BitbucketRepositoryDescriptor,
144+
rev: string,
155145
): Promise<PullRequest | undefined> {
156-
return Promise.resolve(undefined);
146+
return (await this.container.bitbucket)?.getServerPullRequestForCommit(
147+
this,
148+
accessToken,
149+
repo.owner,
150+
repo.name,
151+
rev,
152+
this.apiBaseUrl,
153+
);
157154
}
158155

159156
public override async getRepoInfo(repo: { owner: string; name: string }): Promise<ProviderRepository | undefined> {
@@ -210,14 +207,13 @@ export class BitbucketServerIntegration extends HostingIntegration<
210207
}
211208

212209
const api = await this.getProvidersApi();
213-
const integration = await this.container.integrations.get(this.id);
214-
if (!api || !integration) {
210+
if (!api) {
215211
return undefined;
216212
}
217213
const prs = await api.getBitbucketServerPullRequestsForCurrentUser(this.apiBaseUrl, {
218214
accessToken: session.accessToken,
219215
});
220-
return prs?.map(pr => fromProviderPullRequest(pr, integration));
216+
return prs?.map(pr => fromProviderPullRequest(pr, this));
221217
}
222218

223219
protected override async searchProviderMyIssues(

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

+43-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import { Logger } from '../../../../system/logger';
2525
import type { LogScope } from '../../../../system/logger.scope';
2626
import { getLogScope } from '../../../../system/logger.scope';
2727
import { maybeStopWatch } from '../../../../system/stopwatch';
28-
import type { Integration } from '../../integration';
2928
import type { BitbucketServerPullRequest } from '../bitbucket-server/models';
3029
import { normalizeBitbucketServerPullRequest } from '../bitbucket-server/models';
3130
import { fromProviderPullRequest } from '../models';
@@ -105,7 +104,6 @@ export class BitbucketApi implements Disposable {
105104
repo: string,
106105
branch: string,
107106
baseUrl: string,
108-
integration: Integration,
109107
): Promise<PullRequest | undefined> {
110108
const scope = getLogScope();
111109

@@ -130,7 +128,7 @@ export class BitbucketApi implements Disposable {
130128
}
131129

132130
const providersPr = normalizeBitbucketServerPullRequest(response.values[0]);
133-
const gitlensPr = fromProviderPullRequest(providersPr, integration);
131+
const gitlensPr = fromProviderPullRequest(providersPr, provider);
134132
return gitlensPr;
135133
}
136134

@@ -283,7 +281,6 @@ export class BitbucketApi implements Disposable {
283281
repo: string,
284282
id: string,
285283
baseUrl: string,
286-
integration: Integration,
287284
): Promise<IssueOrPullRequest | undefined> {
288285
const scope = getLogScope();
289286

@@ -301,7 +298,7 @@ export class BitbucketApi implements Disposable {
301298

302299
if (prResponse) {
303300
const providersPr = normalizeBitbucketServerPullRequest(prResponse);
304-
const gitlensPr = fromProviderPullRequest(providersPr, integration);
301+
const gitlensPr = fromProviderPullRequest(providersPr, provider);
305302
return gitlensPr;
306303
}
307304
} catch (ex) {
@@ -369,6 +366,47 @@ export class BitbucketApi implements Disposable {
369366
}
370367
}
371368

369+
@debug<BitbucketApi['getServerPullRequestForCommit']>({ args: { 0: p => p.name, 1: '<token>' } })
370+
async getServerPullRequestForCommit(
371+
provider: Provider,
372+
token: string,
373+
owner: string,
374+
repo: string,
375+
rev: string,
376+
baseUrl: string,
377+
_options?: {
378+
avatarSize?: number;
379+
},
380+
cancellation?: CancellationToken,
381+
): Promise<PullRequest | undefined> {
382+
const scope = getLogScope();
383+
384+
try {
385+
const response = await this.request<{ values: BitbucketServerPullRequest[] }>(
386+
provider,
387+
token,
388+
baseUrl,
389+
`projects/${owner}/repos/${repo}/commits/${rev}/pull-requests`, //?fields=${fieldsParam}`,
390+
{
391+
method: 'GET',
392+
},
393+
scope,
394+
cancellation,
395+
);
396+
const prResponse = response?.values?.reduce<BitbucketServerPullRequest | undefined>(
397+
(acc, pr) => (!acc || pr.updatedDate > acc.updatedDate ? pr : acc),
398+
undefined,
399+
);
400+
if (!prResponse) return undefined;
401+
const providersPr = normalizeBitbucketServerPullRequest(prResponse);
402+
const gitlensPr = fromProviderPullRequest(providersPr, provider);
403+
return gitlensPr;
404+
} catch (ex) {
405+
Logger.error(ex, scope);
406+
return undefined;
407+
}
408+
}
409+
372410
@debug<BitbucketApi['getPullRequestForCommit']>({ args: { 0: p => p.name, 1: '<token>' } })
373411
async getPullRequestForCommit(
374412
provider: Provider,

src/plus/integrations/providers/models.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
PullRequestReviewState,
5656
PullRequestStatusCheckRollupState,
5757
} from '../../../git/models/pullRequest';
58-
import type { ProviderReference } from '../../../git/models/remoteProvider';
58+
import type { Provider, ProviderReference } from '../../../git/models/remoteProvider';
5959
import { equalsIgnoreCase } from '../../../system/string';
6060
import type { EnrichableItem } from '../../launchpad/models/enrichedItem';
6161
import type { Integration, IntegrationType } from '../integration';
@@ -959,11 +959,11 @@ export function toProviderPullRequest(pr: PullRequest): ProviderPullRequest {
959959

960960
export function fromProviderPullRequest(
961961
pr: ProviderPullRequest,
962-
integration: Integration,
962+
provider: Provider,
963963
options?: { project?: IssueProject },
964964
): PullRequest {
965965
return new PullRequest(
966-
integration,
966+
provider,
967967
fromProviderAccount(pr.author),
968968
pr.id,
969969
pr.graphQLId || pr.id,

0 commit comments

Comments
 (0)