Skip to content

Commit

Permalink
Retrieves a pull request from Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Jan 29, 2025
1 parent 9b13082 commit 0f3673a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/plus/integrations/providers/azure/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,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 { AzureWorkItemState, AzureWorkItemStateCategory, WorkItem } from './models';
import { azureWorkItemsStateCategoryToState, isClosedAzureWorkItemStateCategory } from './models';
import type { AzurePullRequest, AzureWorkItemState, AzureWorkItemStateCategory, WorkItem } from './models';
import {
azurePullRequestStatusToState,
azureWorkItemsStateCategoryToState,
isClosedAzurePullRequestStatus,
isClosedAzureWorkItemStateCategory,
} from './models';

export class AzureDevOpsApi implements Disposable {
private readonly _disposable: Disposable;
Expand Down Expand Up @@ -71,7 +76,7 @@ export class AzureDevOpsApi implements Disposable {
},
): Promise<IssueOrPullRequest | undefined> {
const scope = getLogScope();
const [projectName] = repo.split('/');
const [projectName, _, repoName] = repo.split('/');

try {
// Try to get the Work item (wit) first with specific fields
Expand Down Expand Up @@ -112,6 +117,39 @@ export class AzureDevOpsApi implements Disposable {
url: issueResult._links.html.href,
};
}
} catch (ex) {
if (ex.original?.status !== 404) {
Logger.error(ex, scope);
return undefined;
}
}

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

if (prResult != null) {
return {
id: prResult.pullRequestId.toString(),
type: 'pullrequest',
nodeId: prResult.pullRequestId.toString(), // prResult.artifactId maybe?
provider: provider,
createdDate: new Date(prResult.creationDate),
updatedDate: new Date(prResult.creationDate),
state: azurePullRequestStatusToState(prResult.status),
closed: isClosedAzurePullRequestStatus(prResult.status),
title: prResult.title,
url: prResult.url,
};
}

return undefined;
} catch (ex) {
Expand Down
57 changes: 57 additions & 0 deletions src/plus/integrations/providers/azure/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,60 @@ export interface AzureWorkItemState {
color: string;
category: AzureWorkItemStateCategory;
}

export type AzurePullRequestStatus = 'abandoned' | 'active' | 'completed' | 'notSet';
export function azurePullRequestStatusToState(status: AzurePullRequestStatus): IssueOrPullRequestState {
switch (status) {
case 'abandoned':
return 'closed';
case 'completed':
return 'merged';
case 'active':
case 'notSet':
default:
return 'opened';
}
}
export function isClosedAzurePullRequestStatus(status: AzurePullRequestStatus): boolean {
return azurePullRequestStatusToState(status) !== 'opened';
}

export interface AzurePullRequest {
repository: unknown;
pullRequestId: number;
codeReviewId: number;
status: AzurePullRequestStatus;
createdBy: AzureUser;
creationDate: string;
closedDate: string;
title: string;
description: string;
sourceRefName: string;
targetRefName: string;
isDraft: boolean;
mergeId: string;
lastMergeSourceCommit: {
commitId: string;
url: string;
};
lastMergeTargetCommit: {
commitId: string;
url: string;
};
reviewers: unknown[];
url: string;
_links: {
self: AzureLink;
repository: AzureLink;
workItems: AzureLink;
sourceBranch: AzureLink;
targetBranch: AzureLink;
statuses: AzureLink;
sourceCommit: AzureLink;
targetCommit: AzureLink;
createdBy: AzureLink;
iterations: AzureLink;
};
supportsIterations: boolean;
artifactId: string;
}

0 comments on commit 0f3673a

Please sign in to comment.