Skip to content

Commit a400d13

Browse files
sergeibbbaxosoft-ramint
authored andcommitted
Retrieves pull request for a branch in Azure DevOps to show in Home
(#3977, #3999)
1 parent fba2740 commit a400d13

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

src/plus/integrations/providers/azure/azure.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
RequestNotFoundError,
1515
} from '../../../../errors';
1616
import type { IssueOrPullRequest } from '../../../../git/models/issueOrPullRequest';
17+
import { PullRequest } from '../../../../git/models/pullRequest';
1718
import type { Provider } from '../../../../git/models/remoteProvider';
1819
import { showIntegrationRequestFailed500WarningMessage } from '../../../../messages';
1920
import { configuration } from '../../../../system/-webview/configuration';
@@ -22,7 +23,13 @@ import { Logger } from '../../../../system/logger';
2223
import type { LogScope } from '../../../../system/logger.scope';
2324
import { getLogScope } from '../../../../system/logger.scope';
2425
import { maybeStopWatch } from '../../../../system/stopwatch';
25-
import type { AzurePullRequest, AzureWorkItemState, AzureWorkItemStateCategory, WorkItem } from './models';
26+
import type {
27+
AzurePullRequest,
28+
AzurePullRequestWithLinks,
29+
AzureWorkItemState,
30+
AzureWorkItemStateCategory,
31+
WorkItem,
32+
} from './models';
2633
import {
2734
azurePullRequestStatusToState,
2835
azureWorkItemsStateCategoryToState,
@@ -65,6 +72,61 @@ export class AzureDevOpsApi implements Disposable {
6572
this._workItemStates.clear();
6673
}
6774

75+
@debug<AzureDevOpsApi['getPullRequestForBranch']>({ args: { 0: p => p.name, 1: '<token>' } })
76+
public async getPullRequestForBranch(
77+
provider: Provider,
78+
token: string,
79+
owner: string,
80+
repo: string,
81+
branch: string,
82+
options: {
83+
baseUrl: string;
84+
},
85+
): Promise<PullRequest | undefined> {
86+
const scope = getLogScope();
87+
const [projectName, _, repoName] = repo.split('/');
88+
89+
try {
90+
const prResult = await this.request<{ value: AzurePullRequest[] }>(
91+
provider,
92+
token,
93+
options?.baseUrl,
94+
`${owner}/${projectName}/_apis/git/repositories/${repoName}/pullRequests`,
95+
{
96+
method: 'GET',
97+
},
98+
scope,
99+
);
100+
101+
const pr = prResult?.value.find(pr => pr.sourceRefName.endsWith(branch));
102+
if (pr == null) return undefined;
103+
104+
return new PullRequest(
105+
provider,
106+
{
107+
id: pr.createdBy.id,
108+
name: pr.createdBy.displayName,
109+
avatarUrl: pr.createdBy.imageUrl,
110+
url: pr.createdBy.url,
111+
},
112+
pr.pullRequestId.toString(),
113+
pr.pullRequestId.toString(),
114+
pr.title,
115+
getPullRequestUrl(options.baseUrl, owner, projectName, repoName, pr.pullRequestId),
116+
{
117+
owner: owner,
118+
repo: repo,
119+
},
120+
azurePullRequestStatusToState(pr.status),
121+
new Date(pr.creationDate),
122+
new Date(pr.creationDate),
123+
);
124+
} catch (ex) {
125+
Logger.error(ex, scope);
126+
return undefined;
127+
}
128+
}
129+
68130
@debug<AzureDevOpsApi['getIssueOrPullRequest']>({ args: { 0: p => p.name, 1: '<token>' } })
69131
public async getIssueOrPullRequest(
70132
provider: Provider,
@@ -126,7 +188,7 @@ export class AzureDevOpsApi implements Disposable {
126188
}
127189

128190
try {
129-
const prResult = await this.request<AzurePullRequest>(
191+
const prResult = await this.request<AzurePullRequestWithLinks>(
130192
provider,
131193
token,
132194
options?.baseUrl,

src/plus/integrations/providers/azure/models.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ export interface AzurePullRequest {
257257
lastMergeTargetCommit: AzureGitCommitRef;
258258
reviewers: AzureUserWithVote[];
259259
url: string;
260+
supportsIterations: boolean;
261+
}
262+
263+
export interface AzurePullRequestWithLinks extends AzurePullRequest {
260264
_links: {
261265
self: AzureLink;
262266
repository: AzureLink;
@@ -269,7 +273,6 @@ export interface AzurePullRequest {
269273
createdBy: AzureLink;
270274
iterations: AzureLink;
271275
};
272-
supportsIterations: boolean;
273276
artifactId: string;
274277
autoCompleteSetBy?: AzureUser;
275278
commits?: AzureGitCommitRef[];

src/plus/integrations/providers/azureDevOps.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,16 @@ export class AzureDevOpsIntegration extends HostingIntegration<
281281
include?: PullRequestState[];
282282
},
283283
): Promise<PullRequest | undefined> {
284-
return Promise.resolve(undefined);
284+
return (await this.container.azure)?.getPullRequestForBranch(
285+
this,
286+
_session.accessToken,
287+
_repo.owner,
288+
_repo.name,
289+
_branch,
290+
{
291+
baseUrl: this.apiBaseUrl,
292+
},
293+
);
285294
}
286295

287296
protected override async getProviderPullRequestForCommit(

0 commit comments

Comments
 (0)