mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 19:34:29 +00:00
Show cancelled PR checks as Cancelled with separate counts on desktop and mobile, preserving genuine failures, pending checks, existing action permissions and older-client fallback. Carry complete provider summaries through caches and presentation; preserve Azure source completeness, folder attention priority and muted cancellation details. Tracked in #15847 and STA-5102.
300 lines
9.1 KiB
TypeScript
300 lines
9.1 KiB
TypeScript
import {
|
|
deriveAzureDevOpsStatus,
|
|
summarizeAzureDevOpsStatuses,
|
|
mapAzureDevOpsPullRequest,
|
|
mapAzureDevOpsPullRequestState,
|
|
type AzureDevOpsPullRequestInfo,
|
|
type RawAzureDevOpsPullRequest,
|
|
type RawAzureDevOpsStatus
|
|
} from './pull-request-mappers'
|
|
import { shouldHideNonOpenReviewOnDefaultBranch } from '../source-control/repo-default-branch'
|
|
import { getAzureDevOpsRepoRef, type AzureDevOpsRepoRef } from './repository-ref'
|
|
import {
|
|
getHostedReviewLocalGitOptions,
|
|
type HostedReviewExecutionOptions
|
|
} from '../source-control/hosted-review-git-options'
|
|
import {
|
|
azureDevOpsTokenConfigured,
|
|
getAzureDevOpsAuthConfig,
|
|
normalizeAzureDevOpsApiBaseUrl,
|
|
requestAzureDevOpsJson,
|
|
requestAzureDevOpsJsonAtBase
|
|
} from './azure-devops-api-request'
|
|
export { normalizeAzureDevOpsApiBaseUrl } from './azure-devops-api-request'
|
|
|
|
export type AzureDevOpsAuthStatus = {
|
|
configured: boolean
|
|
authenticated: boolean
|
|
account: string | null
|
|
baseUrl: string | null
|
|
tokenConfigured: boolean
|
|
}
|
|
|
|
type RawAzureDevOpsRepository = {
|
|
id?: string | null
|
|
name?: string | null
|
|
webUrl?: string | null
|
|
_links?: {
|
|
web?: {
|
|
href?: string | null
|
|
} | null
|
|
} | null
|
|
}
|
|
|
|
function encodePathSegment(value: string): string {
|
|
return encodeURIComponent(value)
|
|
}
|
|
|
|
async function getRepository(
|
|
repo: AzureDevOpsRepoRef
|
|
): Promise<{ idOrName: string; webBaseUrl: string } | null> {
|
|
const raw = await requestAzureDevOpsJson<RawAzureDevOpsRepository>(
|
|
repo,
|
|
`/_apis/git/repositories/${encodePathSegment(repo.repository)}`
|
|
)
|
|
if (!raw) {
|
|
return { idOrName: repo.repository, webBaseUrl: repo.webBaseUrl }
|
|
}
|
|
return {
|
|
idOrName: raw.id?.trim() || repo.repository,
|
|
webBaseUrl: raw.webUrl ?? raw._links?.web?.href ?? repo.webBaseUrl
|
|
}
|
|
}
|
|
|
|
function readStatusList(
|
|
raw: RawAzureDevOpsStatus[] | { value?: RawAzureDevOpsStatus[] } | null
|
|
): RawAzureDevOpsStatus[] {
|
|
if (Array.isArray(raw)) {
|
|
return raw
|
|
}
|
|
return raw?.value ?? []
|
|
}
|
|
|
|
async function getPullRequestStatuses(
|
|
repo: AzureDevOpsRepoRef,
|
|
repoIdOrName: string,
|
|
pr: RawAzureDevOpsPullRequest
|
|
): Promise<{ statuses: RawAzureDevOpsStatus[]; complete: boolean }> {
|
|
const read = async (path: string) => {
|
|
let hasMore = false
|
|
const raw = await requestAzureDevOpsJson<
|
|
RawAzureDevOpsStatus[] | { value?: RawAzureDevOpsStatus[] }
|
|
>(repo, path, {
|
|
onResponse: (response) => {
|
|
hasMore = Boolean(response.headers.get('x-ms-continuationtoken'))
|
|
}
|
|
})
|
|
return {
|
|
statuses: readStatusList(raw),
|
|
complete: raw !== null && !hasMore && (Array.isArray(raw) || Array.isArray(raw.value))
|
|
}
|
|
}
|
|
const root = `/_apis/git/repositories/${encodePathSegment(repoIdOrName)}`
|
|
const prStatuses = await read(
|
|
`${root}/pullRequests/${encodePathSegment(String(pr.pullRequestId))}/statuses`
|
|
)
|
|
if (prStatuses.statuses.length > 0) {
|
|
return prStatuses
|
|
}
|
|
const commitId = pr.lastMergeSourceCommit?.commitId?.trim()
|
|
if (!commitId) {
|
|
return { statuses: pr.statuses ?? [], complete: false }
|
|
}
|
|
const commitStatuses = await read(`${root}/commits/${encodePathSegment(commitId)}/statuses`)
|
|
return {
|
|
statuses: commitStatuses.statuses,
|
|
complete: prStatuses.complete && commitStatuses.complete
|
|
}
|
|
}
|
|
|
|
async function normalizePullRequest(
|
|
repo: AzureDevOpsRepoRef,
|
|
repoIdOrName: string,
|
|
webBaseUrl: string,
|
|
raw: RawAzureDevOpsPullRequest
|
|
): Promise<AzureDevOpsPullRequestInfo | null> {
|
|
const { statuses, complete } = await getPullRequestStatuses(repo, repoIdOrName, raw)
|
|
return mapAzureDevOpsPullRequest(
|
|
raw,
|
|
deriveAzureDevOpsStatus(statuses),
|
|
webBaseUrl,
|
|
complete ? summarizeAzureDevOpsStatuses(statuses) : undefined
|
|
)
|
|
}
|
|
|
|
function sortPullRequestsForBranch(
|
|
left: RawAzureDevOpsPullRequest,
|
|
right: RawAzureDevOpsPullRequest
|
|
): number {
|
|
const leftStatus = left.status?.trim().toLowerCase()
|
|
const rightStatus = right.status?.trim().toLowerCase()
|
|
const abandonedOrder = Number(leftStatus === 'abandoned') - Number(rightStatus === 'abandoned')
|
|
// Newer abandoned PRs must not hide usable branch reviews.
|
|
if (abandonedOrder !== 0) {
|
|
return abandonedOrder
|
|
}
|
|
const leftTime = Date.parse(left.closedDate ?? left.creationDate ?? '') || 0
|
|
const rightTime = Date.parse(right.closedDate ?? right.creationDate ?? '') || 0
|
|
if (leftTime !== rightTime) {
|
|
return rightTime - leftTime
|
|
}
|
|
return Number(rightStatus === 'active') - Number(leftStatus === 'active')
|
|
}
|
|
|
|
export async function getAzureDevOpsAuthStatus(): Promise<AzureDevOpsAuthStatus> {
|
|
const config = getAzureDevOpsAuthConfig()
|
|
const baseUrl = config.apiBaseUrl ? normalizeAzureDevOpsApiBaseUrl(config.apiBaseUrl) : null
|
|
const hasToken = azureDevOpsTokenConfigured(config)
|
|
if (!baseUrl && !hasToken) {
|
|
return {
|
|
configured: false,
|
|
authenticated: false,
|
|
account: null,
|
|
baseUrl: null,
|
|
tokenConfigured: false
|
|
}
|
|
}
|
|
if (!baseUrl) {
|
|
return {
|
|
configured: true,
|
|
authenticated: false,
|
|
account: null,
|
|
baseUrl: null,
|
|
tokenConfigured: hasToken
|
|
}
|
|
}
|
|
|
|
const connection = await requestAzureDevOpsJsonAtBase<{
|
|
authenticatedUser?: {
|
|
providerDisplayName?: string | null
|
|
customDisplayName?: string | null
|
|
uniqueName?: string | null
|
|
} | null
|
|
}>(baseUrl, '/_apis/connectionData', { timeoutMs: 4000 })
|
|
const user = connection?.authenticatedUser
|
|
return {
|
|
configured: hasToken || connection !== null,
|
|
authenticated: connection !== null && (hasToken || user !== null),
|
|
account: user?.providerDisplayName ?? user?.customDisplayName ?? user?.uniqueName ?? null,
|
|
baseUrl,
|
|
tokenConfigured: hasToken
|
|
}
|
|
}
|
|
|
|
export async function getAzureDevOpsPullRequest(
|
|
repoPath: string,
|
|
prNumber: number,
|
|
connectionId?: string | null,
|
|
options: HostedReviewExecutionOptions = {}
|
|
): Promise<AzureDevOpsPullRequestInfo | null> {
|
|
const repo = await getAzureDevOpsRepoRef(
|
|
repoPath,
|
|
connectionId,
|
|
getHostedReviewLocalGitOptions(options)
|
|
)
|
|
const repository = repo ? await getRepository(repo) : null
|
|
if (!repo || !repository) {
|
|
return null
|
|
}
|
|
const raw = await requestAzureDevOpsJson<RawAzureDevOpsPullRequest>(
|
|
repo,
|
|
`/_apis/git/repositories/${encodePathSegment(repository.idOrName)}/pullRequests/${encodePathSegment(
|
|
String(prNumber)
|
|
)}`
|
|
)
|
|
return raw ? normalizePullRequest(repo, repository.idOrName, repository.webBaseUrl, raw) : null
|
|
}
|
|
|
|
export async function getAzureDevOpsPullRequestForBranch(
|
|
repoPath: string,
|
|
branch: string,
|
|
linkedPRNumber?: number | null,
|
|
connectionId?: string | null,
|
|
options: HostedReviewExecutionOptions = {},
|
|
throwOnFailure = false
|
|
): Promise<AzureDevOpsPullRequestInfo | null> {
|
|
const branchName = branch.replace(/^refs\/heads\//, '')
|
|
if (!branchName && linkedPRNumber == null) {
|
|
return null
|
|
}
|
|
|
|
const repo = await getAzureDevOpsRepoRef(
|
|
repoPath,
|
|
connectionId,
|
|
getHostedReviewLocalGitOptions(options)
|
|
)
|
|
const repository = repo ? await getRepository(repo) : null
|
|
if (!repo || !repository) {
|
|
return null
|
|
}
|
|
|
|
if (branchName) {
|
|
const list = await requestAzureDevOpsJson<{ value?: RawAzureDevOpsPullRequest[] }>(
|
|
repo,
|
|
`/_apis/git/repositories/${encodePathSegment(repository.idOrName)}/pullRequests`,
|
|
{
|
|
searchParams: {
|
|
'searchCriteria.sourceRefName': `refs/heads/${branchName}`,
|
|
'searchCriteria.status': 'all',
|
|
$top: 10
|
|
}
|
|
},
|
|
throwOnFailure
|
|
)
|
|
const raw = (list?.value ?? []).sort(sortPullRequestsForBranch)[0]
|
|
if (raw) {
|
|
const hideOnDefaultBranch = await shouldHideNonOpenReviewOnDefaultBranch({
|
|
state: mapAzureDevOpsPullRequestState(raw),
|
|
reviewNumber: raw.pullRequestId ?? null,
|
|
linkedReviewNumber: linkedPRNumber,
|
|
branchName,
|
|
repoPath,
|
|
connectionId,
|
|
localGitOptions: getHostedReviewLocalGitOptions(options)
|
|
})
|
|
if (!hideOnDefaultBranch) {
|
|
return normalizePullRequest(repo, repository.idOrName, repository.webBaseUrl, raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
if (typeof linkedPRNumber !== 'number') {
|
|
return null
|
|
}
|
|
const raw = await requestAzureDevOpsJson<RawAzureDevOpsPullRequest>(
|
|
repo,
|
|
`/_apis/git/repositories/${encodePathSegment(repository.idOrName)}/pullRequests/${encodePathSegment(
|
|
String(linkedPRNumber)
|
|
)}`,
|
|
{},
|
|
throwOnFailure
|
|
)
|
|
return raw ? normalizePullRequest(repo, repository.idOrName, repository.webBaseUrl, raw) : null
|
|
}
|
|
|
|
/** Preserve lookup failures so creation preflight can report review state as unavailable. */
|
|
export function getAzureDevOpsPullRequestForBranchOrThrow(
|
|
repoPath: string,
|
|
branch: string,
|
|
linkedPRNumber?: number | null,
|
|
connectionId?: string | null,
|
|
options: HostedReviewExecutionOptions = {}
|
|
): Promise<AzureDevOpsPullRequestInfo | null> {
|
|
return getAzureDevOpsPullRequestForBranch(
|
|
repoPath,
|
|
branch,
|
|
linkedPRNumber,
|
|
connectionId,
|
|
options,
|
|
true
|
|
)
|
|
}
|
|
|
|
export async function getAzureDevOpsRepoSlug(
|
|
repoPath: string,
|
|
connectionId?: string | null,
|
|
options: HostedReviewExecutionOptions = {}
|
|
): Promise<AzureDevOpsRepoRef | null> {
|
|
return getAzureDevOpsRepoRef(repoPath, connectionId, getHostedReviewLocalGitOptions(options))
|
|
}
|