Skip to content

Commit 3693f71

Browse files
committed
Implements getPullRequestForCommit for AzureDevOps
(#4192)
1 parent bdfcf69 commit 3693f71

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,63 @@ export class AzureDevOpsApi implements Disposable {
112112
}
113113
}
114114

115+
@debug<AzureDevOpsApi['getPullRequestForCommit']>({ args: { 0: p => p.name, 1: '<token>' } })
116+
async getPullRequestForCommit(
117+
provider: Provider,
118+
token: string,
119+
owner: string,
120+
repo: string,
121+
rev: string,
122+
baseUrl: string,
123+
_options?: {
124+
avatarSize?: number;
125+
},
126+
cancellation?: CancellationToken,
127+
): Promise<PullRequest | undefined> {
128+
const scope = getLogScope();
129+
const [projectName, _, repoName] = repo.split('/');
130+
try {
131+
const prResult = await this.request<{ results: Record<string, AzurePullRequest[]>[] }>(
132+
provider,
133+
token,
134+
baseUrl,
135+
`${owner}/${projectName}/_apis/git/repositories/${repoName}/pullrequestquery`,
136+
{
137+
method: 'POST',
138+
body: JSON.stringify({
139+
queries: [
140+
{
141+
items: [rev],
142+
type: 'commit',
143+
},
144+
],
145+
}),
146+
},
147+
scope,
148+
cancellation,
149+
);
150+
151+
const pr = prResult?.results[0]?.[rev]?.[0];
152+
if (pr == null) return undefined;
153+
154+
const pullRequest = await this.request<AzurePullRequestWithLinks>(
155+
provider,
156+
token,
157+
undefined,
158+
pr.url,
159+
{ method: 'GET' },
160+
scope,
161+
cancellation,
162+
);
163+
if (pullRequest == null) return undefined;
164+
165+
return fromAzurePullRequest(pullRequest, provider, owner);
166+
} catch (ex) {
167+
Logger.error(ex, scope);
168+
return undefined;
169+
}
170+
}
171+
115172
@debug<AzureDevOpsApi['getIssueOrPullRequest']>({ args: { 0: p => p.name, 1: '<token>' } })
116173
public async getIssueOrPullRequest(
117174
provider: Provider,
@@ -310,13 +367,13 @@ export class AzureDevOpsApi implements Disposable {
310367
private async request<T>(
311368
provider: Provider,
312369
token: string,
313-
baseUrl: string,
370+
baseUrl: string | undefined,
314371
route: string,
315372
options: { method: RequestInit['method'] } & Record<string, unknown>,
316373
scope: LogScope | undefined,
317374
cancellation?: CancellationToken | undefined,
318375
): Promise<T | undefined> {
319-
const url = `${baseUrl}/${route}`;
376+
const url = baseUrl ? `${baseUrl}/${route}` : route;
320377

321378
let rsp: Response;
322379
try {

src/plus/integrations/providers/azureDevOps.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,18 @@ export class AzureDevOpsIntegration extends HostingIntegration<
293293
}
294294

295295
protected override async getProviderPullRequestForCommit(
296-
_session: AuthenticationSession,
297-
_repo: AzureRepositoryDescriptor,
298-
_rev: string,
296+
{ accessToken }: AuthenticationSession,
297+
repo: AzureRepositoryDescriptor,
298+
rev: string,
299299
): Promise<PullRequest | undefined> {
300-
return Promise.resolve(undefined);
300+
return (await this.container.azure)?.getPullRequestForCommit(
301+
this,
302+
accessToken,
303+
repo.owner,
304+
repo.name,
305+
rev,
306+
this.apiBaseUrl,
307+
);
301308
}
302309

303310
public override async getRepoInfo(repo: {

0 commit comments

Comments
 (0)