Add PR review queue badge foundation
- Add provider-neutral review queue types, adapter, and checks-panel badges.\n- Keep the design doc out of the PR diff.
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { hostedReviewSummaryFromGitHubPRInfo } from './hosted-review-github'
|
||||
import type { PRInfo } from './types'
|
||||
|
||||
const pr: PRInfo = {
|
||||
number: 12,
|
||||
title: 'Add queue badges',
|
||||
state: 'open',
|
||||
url: 'https://github.com/acme/orca/pull/12',
|
||||
checksStatus: 'pending',
|
||||
updatedAt: '2026-05-12T00:00:00.000Z',
|
||||
mergeable: 'MERGEABLE',
|
||||
headSha: 'abc123'
|
||||
}
|
||||
|
||||
describe('hostedReviewSummaryFromGitHubPRInfo', () => {
|
||||
it('maps PRInfo into provider-neutral summary with host identity', () => {
|
||||
const summary = hostedReviewSummaryFromGitHubPRInfo({
|
||||
pr,
|
||||
owner: 'acme',
|
||||
repo: 'orca',
|
||||
host: 'github.acme.internal'
|
||||
})
|
||||
|
||||
expect(summary.identity).toEqual({
|
||||
provider: 'github',
|
||||
host: 'github.acme.internal',
|
||||
owner: 'acme',
|
||||
repo: 'orca',
|
||||
number: 12
|
||||
})
|
||||
expect(summary.checksStatus).toBe('pending')
|
||||
expect(summary.threadSummary).toBeUndefined()
|
||||
})
|
||||
|
||||
it('derives unresolved thread count and failing status from enrichers', () => {
|
||||
const summary = hostedReviewSummaryFromGitHubPRInfo({
|
||||
pr: { ...pr, checksStatus: 'success' },
|
||||
owner: 'acme',
|
||||
repo: 'orca',
|
||||
comments: [
|
||||
{
|
||||
id: 1,
|
||||
author: 'a',
|
||||
authorAvatarUrl: '',
|
||||
body: '',
|
||||
createdAt: '',
|
||||
url: '',
|
||||
threadId: 't1',
|
||||
isResolved: false
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
author: 'b',
|
||||
authorAvatarUrl: '',
|
||||
body: '',
|
||||
createdAt: '',
|
||||
url: '',
|
||||
threadId: 't1',
|
||||
isResolved: false
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
author: 'c',
|
||||
authorAvatarUrl: '',
|
||||
body: '',
|
||||
createdAt: '',
|
||||
url: '',
|
||||
threadId: 't2',
|
||||
isResolved: true
|
||||
}
|
||||
],
|
||||
checks: [{ name: 'ci', status: 'completed', conclusion: 'failure', url: null }]
|
||||
})
|
||||
|
||||
expect(summary.threadSummary).toEqual({ unresolvedCount: 1, dataCompleteness: 'partial' })
|
||||
expect(summary.checksStatus).toBe('failure')
|
||||
})
|
||||
|
||||
it('distinguishes loaded empty comments from unknown comments', () => {
|
||||
expect(
|
||||
hostedReviewSummaryFromGitHubPRInfo({
|
||||
pr,
|
||||
owner: 'acme',
|
||||
repo: 'orca'
|
||||
}).threadSummary
|
||||
).toBeUndefined()
|
||||
|
||||
expect(
|
||||
hostedReviewSummaryFromGitHubPRInfo({
|
||||
pr,
|
||||
owner: 'acme',
|
||||
repo: 'orca',
|
||||
comments: []
|
||||
}).threadSummary
|
||||
).toEqual({ unresolvedCount: 0, dataCompleteness: 'partial' })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,88 @@
|
||||
import type { PRCheckDetail, PRComment, PRInfo } from './types'
|
||||
import type { HostedReviewQueueSummary } from './hosted-review'
|
||||
|
||||
export type HostedReviewFromGitHubPRInfoArgs = {
|
||||
pr: PRInfo
|
||||
owner: string
|
||||
repo: string
|
||||
host?: string
|
||||
authorLogin?: string | null
|
||||
authorIsBot?: boolean
|
||||
requestedReviewerLogins?: string[] | null
|
||||
comments?: PRComment[]
|
||||
checks?: PRCheckDetail[]
|
||||
lastViewedAt?: number
|
||||
}
|
||||
|
||||
function unresolvedThreadCount(comments?: PRComment[]): number | null {
|
||||
if (comments === undefined) {
|
||||
return null
|
||||
}
|
||||
const unresolved = new Set<string>()
|
||||
for (const comment of comments) {
|
||||
if (!comment.threadId || comment.isResolved !== false) {
|
||||
continue
|
||||
}
|
||||
unresolved.add(comment.threadId)
|
||||
}
|
||||
return unresolved.size
|
||||
}
|
||||
|
||||
function deriveChecksStatus(
|
||||
prChecksStatus: PRInfo['checksStatus'],
|
||||
checks?: PRCheckDetail[]
|
||||
): PRInfo['checksStatus'] {
|
||||
if (!checks || checks.length === 0) {
|
||||
return prChecksStatus
|
||||
}
|
||||
const hasFailure = checks.some(
|
||||
(check) => check.conclusion === 'failure' || check.conclusion === 'timed_out'
|
||||
)
|
||||
if (hasFailure) {
|
||||
return 'failure'
|
||||
}
|
||||
const hasPending = checks.some(
|
||||
(check) =>
|
||||
check.status !== 'completed' || check.conclusion === null || check.conclusion === 'pending'
|
||||
)
|
||||
if (hasPending) {
|
||||
return 'pending'
|
||||
}
|
||||
const hasSuccess = checks.some((check) => check.conclusion === 'success')
|
||||
if (hasSuccess) {
|
||||
return 'success'
|
||||
}
|
||||
return 'neutral'
|
||||
}
|
||||
|
||||
export function hostedReviewSummaryFromGitHubPRInfo(
|
||||
args: HostedReviewFromGitHubPRInfoArgs
|
||||
): HostedReviewQueueSummary {
|
||||
const unresolvedCount = unresolvedThreadCount(args.comments)
|
||||
return {
|
||||
identity: {
|
||||
provider: 'github',
|
||||
host: args.host ?? 'github.com',
|
||||
owner: args.owner,
|
||||
repo: args.repo,
|
||||
number: args.pr.number
|
||||
},
|
||||
title: args.pr.title,
|
||||
url: args.pr.url,
|
||||
state: args.pr.state,
|
||||
author: args.authorLogin ? { login: args.authorLogin, isBot: args.authorIsBot } : null,
|
||||
updatedAt: args.pr.updatedAt,
|
||||
mergeable: args.pr.mergeable,
|
||||
checksStatus: deriveChecksStatus(args.pr.checksStatus, args.checks),
|
||||
threadSummary:
|
||||
unresolvedCount === null
|
||||
? undefined
|
||||
: {
|
||||
unresolvedCount,
|
||||
dataCompleteness: 'partial'
|
||||
},
|
||||
requestedReviewerLogins: args.requestedReviewerLogins,
|
||||
lastViewedAt: args.lastViewedAt,
|
||||
draft: args.pr.state === 'draft'
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { HostedReviewQueueSummary } from './hosted-review'
|
||||
import {
|
||||
classifyHostedReview,
|
||||
hostedReviewIdentityKey,
|
||||
reviewNeedsResponse,
|
||||
reviewReadyToMerge
|
||||
} from './hosted-review-queue'
|
||||
|
||||
function baseSummary(overrides: Partial<HostedReviewQueueSummary> = {}): HostedReviewQueueSummary {
|
||||
return {
|
||||
identity: { provider: 'github', host: 'github.com', owner: 'acme', repo: 'orca', number: 42 },
|
||||
title: 'Improve checks panel',
|
||||
url: 'https://github.com/acme/orca/pull/42',
|
||||
state: 'open',
|
||||
author: { login: 'teammate' },
|
||||
updatedAt: '2026-05-10T00:00:00.000Z',
|
||||
mergeable: 'MERGEABLE',
|
||||
checksStatus: 'success',
|
||||
threadSummary: { unresolvedCount: 0 },
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
describe('hostedReviewIdentityKey', () => {
|
||||
it('includes provider and host for enterprise-safe keys', () => {
|
||||
const dotcom = hostedReviewIdentityKey({
|
||||
provider: 'github',
|
||||
host: 'github.com',
|
||||
owner: 'acme',
|
||||
repo: 'orca',
|
||||
number: 7
|
||||
})
|
||||
const ghe = hostedReviewIdentityKey({
|
||||
provider: 'github',
|
||||
host: 'github.acme.internal',
|
||||
owner: 'acme',
|
||||
repo: 'orca',
|
||||
number: 7
|
||||
})
|
||||
expect(dotcom).not.toBe(ghe)
|
||||
})
|
||||
})
|
||||
|
||||
describe('classifyHostedReview', () => {
|
||||
it('classifies mine/requested/agent/teammate', () => {
|
||||
expect(
|
||||
classifyHostedReview(baseSummary({ author: { login: 'me' } }), {
|
||||
viewer: { login: 'me' }
|
||||
}).state
|
||||
).toBe('mine')
|
||||
|
||||
expect(
|
||||
classifyHostedReview(baseSummary({ requestedReviewerLogins: ['me'] }), {
|
||||
viewer: { login: 'me' }
|
||||
}).state
|
||||
).toBe('requested')
|
||||
|
||||
expect(
|
||||
classifyHostedReview(baseSummary({ author: { login: 'orca-ci' } }), {
|
||||
agentAuthorLogins: ['orca-ci']
|
||||
}).state
|
||||
).toBe('agent')
|
||||
|
||||
expect(classifyHostedReview(baseSummary()).state).toBe('teammate')
|
||||
})
|
||||
})
|
||||
|
||||
describe('reviewNeedsResponse', () => {
|
||||
it('returns true for unresolved threads, failed checks, conflicts, and newer remote updates', () => {
|
||||
expect(reviewNeedsResponse(baseSummary({ threadSummary: { unresolvedCount: 1 } }))).toBe(true)
|
||||
expect(reviewNeedsResponse(baseSummary({ checksStatus: 'failure' }))).toBe(true)
|
||||
expect(reviewNeedsResponse(baseSummary({ mergeable: 'CONFLICTING' }))).toBe(true)
|
||||
expect(
|
||||
reviewNeedsResponse(
|
||||
baseSummary({
|
||||
updatedAt: '2026-05-11T00:00:00.000Z',
|
||||
lastViewedAt: Date.parse('2026-05-10T00:00:00.000Z')
|
||||
})
|
||||
)
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('does not mark needs-response from updatedAt alone when lastViewedAt is missing', () => {
|
||||
expect(reviewNeedsResponse(baseSummary({ updatedAt: '2026-05-11T00:00:00.000Z' }))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('reviewReadyToMerge', () => {
|
||||
it('rejects drafts, conflicts, failed/pending checks, unresolved threads, and unknown mergeability', () => {
|
||||
expect(reviewReadyToMerge(baseSummary({ state: 'draft', draft: true }))).toBe(false)
|
||||
expect(reviewReadyToMerge(baseSummary({ mergeable: 'CONFLICTING' }))).toBe(false)
|
||||
expect(reviewReadyToMerge(baseSummary({ checksStatus: 'failure' }))).toBe(false)
|
||||
expect(reviewReadyToMerge(baseSummary({ checksStatus: 'pending' }))).toBe(false)
|
||||
expect(reviewReadyToMerge(baseSummary({ threadSummary: { unresolvedCount: 2 } }))).toBe(false)
|
||||
expect(reviewReadyToMerge(baseSummary({ threadSummary: undefined }))).toBe(false)
|
||||
expect(reviewReadyToMerge(baseSummary({ mergeable: 'UNKNOWN' }))).toBe(false)
|
||||
})
|
||||
|
||||
it('accepts neutral checks when all other gates pass', () => {
|
||||
expect(reviewReadyToMerge(baseSummary({ checksStatus: 'neutral' }))).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,122 @@
|
||||
import type {
|
||||
HostedReviewIdentity,
|
||||
HostedReviewQueueClassification,
|
||||
HostedReviewQueueState,
|
||||
HostedReviewQueueSummary,
|
||||
HostedReviewUser
|
||||
} from './hosted-review'
|
||||
|
||||
export type HostedReviewClassificationOptions = {
|
||||
viewer?: HostedReviewUser | null
|
||||
agentAuthorLogins?: string[]
|
||||
}
|
||||
|
||||
export function hostedReviewIdentityKey(identity: HostedReviewIdentity): string {
|
||||
return `${identity.provider}::${identity.host.toLowerCase()}::${identity.owner.toLowerCase()}::${identity.repo.toLowerCase()}::${identity.number}`
|
||||
}
|
||||
|
||||
function hasRequestedReviewerSignal(
|
||||
summary: HostedReviewQueueSummary,
|
||||
viewer?: HostedReviewUser | null
|
||||
): boolean {
|
||||
if (!viewer?.login) {
|
||||
return false
|
||||
}
|
||||
const requested = summary.requestedReviewerLogins
|
||||
if (!requested || requested.length === 0) {
|
||||
return false
|
||||
}
|
||||
const viewerLogin = viewer.login.toLowerCase()
|
||||
return requested.some((login) => login.toLowerCase() === viewerLogin)
|
||||
}
|
||||
|
||||
function isAgentAuthored(
|
||||
summary: HostedReviewQueueSummary,
|
||||
options?: HostedReviewClassificationOptions
|
||||
): boolean {
|
||||
if (summary.author?.isBot) {
|
||||
return true
|
||||
}
|
||||
const author = summary.author?.login?.toLowerCase()
|
||||
if (!author) {
|
||||
return false
|
||||
}
|
||||
if (options?.agentAuthorLogins?.some((login) => login.toLowerCase() === author)) {
|
||||
return true
|
||||
}
|
||||
return author.endsWith('[bot]') || author.includes('bot')
|
||||
}
|
||||
|
||||
function getQueueState(
|
||||
summary: HostedReviewQueueSummary,
|
||||
options?: HostedReviewClassificationOptions
|
||||
): HostedReviewQueueState {
|
||||
const viewerLogin = options?.viewer?.login?.toLowerCase() ?? null
|
||||
const authorLogin = summary.author?.login?.toLowerCase() ?? null
|
||||
if (viewerLogin && authorLogin && viewerLogin === authorLogin) {
|
||||
return 'mine'
|
||||
}
|
||||
if (hasRequestedReviewerSignal(summary, options?.viewer)) {
|
||||
return 'requested'
|
||||
}
|
||||
if (isAgentAuthored(summary, options)) {
|
||||
return 'agent'
|
||||
}
|
||||
return 'teammate'
|
||||
}
|
||||
|
||||
export function reviewNeedsResponse(
|
||||
summary: HostedReviewQueueSummary,
|
||||
viewer?: HostedReviewUser | null
|
||||
): boolean {
|
||||
void viewer
|
||||
if (summary.state !== 'open' && summary.state !== 'draft') {
|
||||
return false
|
||||
}
|
||||
if ((summary.threadSummary?.unresolvedCount ?? 0) > 0) {
|
||||
return true
|
||||
}
|
||||
if (summary.checksStatus === 'failure') {
|
||||
return true
|
||||
}
|
||||
if (summary.mergeable === 'CONFLICTING') {
|
||||
return true
|
||||
}
|
||||
if (summary.lastViewedAt === undefined) {
|
||||
return false
|
||||
}
|
||||
const updatedAt = Date.parse(summary.updatedAt)
|
||||
return Number.isFinite(updatedAt) && updatedAt > summary.lastViewedAt
|
||||
}
|
||||
|
||||
export function reviewReadyToMerge(summary: HostedReviewQueueSummary): boolean {
|
||||
if (summary.state !== 'open') {
|
||||
return false
|
||||
}
|
||||
if (summary.draft) {
|
||||
return false
|
||||
}
|
||||
if (summary.mergeable !== 'MERGEABLE') {
|
||||
return false
|
||||
}
|
||||
if (summary.checksStatus !== 'success' && summary.checksStatus !== 'neutral') {
|
||||
return false
|
||||
}
|
||||
if (summary.threadSummary?.unresolvedCount !== 0) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
export function classifyHostedReview(
|
||||
summary: HostedReviewQueueSummary,
|
||||
options?: HostedReviewClassificationOptions
|
||||
): HostedReviewQueueClassification {
|
||||
const state = getQueueState(summary, options)
|
||||
return {
|
||||
state,
|
||||
requested: hasRequestedReviewerSignal(summary, options?.viewer),
|
||||
needsResponse: reviewNeedsResponse(summary, options?.viewer),
|
||||
readyToMerge: reviewReadyToMerge(summary)
|
||||
}
|
||||
}
|
||||
@@ -113,3 +113,56 @@ export type HostedReviewCreationEligibilityArgs = {
|
||||
linkedBitbucketPR?: number | null
|
||||
linkedGiteaPR?: number | null
|
||||
}
|
||||
|
||||
export type HostedReviewIdentity = {
|
||||
provider: HostedReviewProvider
|
||||
host: string
|
||||
owner: string
|
||||
repo: string
|
||||
number: number
|
||||
}
|
||||
|
||||
export type HostedReviewUser = {
|
||||
login: string | null
|
||||
isBot?: boolean
|
||||
}
|
||||
|
||||
export type HostedReviewDecision = 'approved' | 'changes_requested' | 'review_required' | null
|
||||
|
||||
export type HostedReviewThreadSummary = {
|
||||
unresolvedCount: number | null
|
||||
dataCompleteness?: 'full' | 'partial'
|
||||
}
|
||||
|
||||
export type HostedReviewQueueSummary = {
|
||||
identity: HostedReviewIdentity
|
||||
title: string
|
||||
url: string
|
||||
state: HostedReviewState
|
||||
author: HostedReviewUser | null
|
||||
updatedAt: string
|
||||
lastViewedAt?: number
|
||||
mergeable: PRMergeableState
|
||||
checksStatus: CheckStatus
|
||||
reviewDecision?: HostedReviewDecision
|
||||
threadSummary?: HostedReviewThreadSummary
|
||||
requestedReviewerLogins?: string[] | null
|
||||
draft?: boolean
|
||||
}
|
||||
|
||||
export type HostedReviewQueueKey =
|
||||
| 'mine'
|
||||
| 'requested'
|
||||
| 'agent'
|
||||
| 'teammate'
|
||||
| 'needs-response'
|
||||
| 'ready-to-merge'
|
||||
|
||||
export type HostedReviewQueueState = 'mine' | 'requested' | 'agent' | 'teammate'
|
||||
|
||||
export type HostedReviewQueueClassification = {
|
||||
state: HostedReviewQueueState
|
||||
needsResponse: boolean
|
||||
readyToMerge: boolean
|
||||
requested: boolean
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user