Skip to content

Commit

Permalink
Retrieves pull request for a branch in Azure DevOps to show in Home
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Jan 30, 2025
1 parent 7e383a0 commit 5fd960f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
66 changes: 64 additions & 2 deletions src/plus/integrations/providers/azure/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RequestNotFoundError,
} from '../../../../errors';
import type { IssueOrPullRequest } from '../../../../git/models/issueOrPullRequest';
import { PullRequest } from '../../../../git/models/pullRequest';
import type { Provider } from '../../../../git/models/remoteProvider';
import { showIntegrationRequestFailed500WarningMessage } from '../../../../messages';
import { configuration } from '../../../../system/-webview/configuration';
Expand All @@ -22,7 +23,13 @@ import { Logger } from '../../../../system/logger';
import type { LogScope } from '../../../../system/logger.scope';
import { getLogScope } from '../../../../system/logger.scope';
import { maybeStopWatch } from '../../../../system/stopwatch';
import type { AzurePullRequest, AzureWorkItemState, AzureWorkItemStateCategory, WorkItem } from './models';
import type {
AzurePullRequest,
AzurePullRequestWithLinks,
AzureWorkItemState,
AzureWorkItemStateCategory,
WorkItem,
} from './models';
import {
azurePullRequestStatusToState,
azureWorkItemsStateCategoryToState,
Expand Down Expand Up @@ -65,6 +72,61 @@ export class AzureDevOpsApi implements Disposable {
this._workItemStates.clear();
}

@debug<AzureDevOpsApi['getPullRequestForBranch']>({ args: { 0: p => p.name, 1: '<token>' } })
public async getPullRequestForBranch(
provider: Provider,
token: string,
owner: string,
repo: string,
branch: string,
options: {
baseUrl: string;
},
): Promise<PullRequest | undefined> {
const scope = getLogScope();
const [projectName, _, repoName] = repo.split('/');

try {
const prResult = await this.request<{ value: AzurePullRequest[] }>(
provider,
token,
options?.baseUrl,
`${owner}/${projectName}/_apis/git/repositories/${repoName}/pullRequests`,
{
method: 'GET',
},
scope,
);

const pr = prResult?.value.find(pr => pr.sourceRefName.endsWith(branch));
if (pr == null) return undefined;

return new PullRequest(
provider,
{
id: pr.createdBy.id,
name: pr.createdBy.displayName,
avatarUrl: pr.createdBy.imageUrl,
url: pr.createdBy.url,
},
pr.pullRequestId.toString(),
pr.pullRequestId.toString(),
pr.title,
getPullRequestUrl(options.baseUrl, owner, projectName, repoName, pr.pullRequestId),
{
owner: owner,
repo: repo,
},
azurePullRequestStatusToState(pr.status),
new Date(pr.creationDate),
new Date(pr.creationDate),
);
} catch (ex) {
Logger.error(ex, scope);
return undefined;
}
}

@debug<AzureDevOpsApi['getIssueOrPullRequest']>({ args: { 0: p => p.name, 1: '<token>' } })
public async getIssueOrPullRequest(
provider: Provider,
Expand Down Expand Up @@ -126,7 +188,7 @@ export class AzureDevOpsApi implements Disposable {
}

try {
const prResult = await this.request<AzurePullRequest>(
const prResult = await this.request<AzurePullRequestWithLinks>(
provider,
token,
options?.baseUrl,
Expand Down
5 changes: 4 additions & 1 deletion src/plus/integrations/providers/azure/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export interface AzurePullRequest {
};
reviewers: unknown[];
url: string;
supportsIterations: boolean;
}

export interface AzurePullRequestWithLinks extends AzurePullRequest {
_links: {
self: AzureLink;
repository: AzureLink;
Expand All @@ -139,6 +143,5 @@ export interface AzurePullRequest {
createdBy: AzureLink;
iterations: AzureLink;
};
supportsIterations: boolean;
artifactId: string;
}
11 changes: 10 additions & 1 deletion src/plus/integrations/providers/azureDevOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,16 @@ export class AzureDevOpsIntegration extends HostingIntegration<
include?: PullRequestState[];
},
): Promise<PullRequest | undefined> {
return Promise.resolve(undefined);
return (await this.container.azure)?.getPullRequestForBranch(
this,
_session.accessToken,
_repo.owner,
_repo.name,
_branch,
{
baseUrl: this.apiBaseUrl,
},
);
}

protected override async getProviderPullRequestForCommit(
Expand Down

0 comments on commit 5fd960f

Please sign in to comment.