fix: address review findings (#2470)
This commit is contained in:
@@ -57,7 +57,7 @@ vi.mock('./rate-limit', () => ({
|
||||
noteRateLimitSpend: noteRateLimitSpendMock
|
||||
}))
|
||||
|
||||
import { listWorkItems, _resetOwnerRepoCache } from './client'
|
||||
import { countWorkItems, listWorkItems, _resetOwnerRepoCache } from './client'
|
||||
|
||||
describe('listWorkItems', () => {
|
||||
beforeEach(() => {
|
||||
@@ -263,6 +263,135 @@ describe('listWorkItems', () => {
|
||||
])
|
||||
})
|
||||
|
||||
it('routes merged queries to PR search only and maps MERGED PR state', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
ghExecFileAsyncMock.mockResolvedValueOnce({
|
||||
stdout: JSON.stringify([
|
||||
{
|
||||
number: 8,
|
||||
title: 'Merged work',
|
||||
state: 'MERGED',
|
||||
url: 'https://github.com/acme/widgets/pull/8',
|
||||
labels: [],
|
||||
updatedAt: '2026-03-31T00:00:00Z',
|
||||
author: { login: 'octocat' },
|
||||
isDraft: false,
|
||||
headRefName: 'feature/merged',
|
||||
headRefOid: 'head-8',
|
||||
baseRefName: 'main'
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
const { items } = await listWorkItems('/repo-root', 10, 'is:merged')
|
||||
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(1)
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
|
||||
[
|
||||
'pr',
|
||||
'list',
|
||||
'--limit',
|
||||
'10',
|
||||
'--json',
|
||||
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName,headRefOid,headRepositoryOwner,reviewRequests',
|
||||
'--repo',
|
||||
'acme/widgets',
|
||||
'--state',
|
||||
'merged'
|
||||
],
|
||||
{ cwd: '/repo-root' }
|
||||
)
|
||||
expect(items).toMatchObject([
|
||||
{
|
||||
id: 'pr:8',
|
||||
type: 'pr',
|
||||
number: 8,
|
||||
state: 'merged'
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
it('passes state:all through to gh instead of using the default open state', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
ghExecFileAsyncMock.mockResolvedValueOnce({ stdout: '[]' })
|
||||
|
||||
await listWorkItems('/repo-root', 10, 'is:pr state:all')
|
||||
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(1)
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(expect.arrayContaining(['--state', 'all']), {
|
||||
cwd: '/repo-root'
|
||||
})
|
||||
})
|
||||
|
||||
it('excludes merged PRs from closed PR searches', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
ghExecFileAsyncMock.mockResolvedValueOnce({
|
||||
stdout: JSON.stringify([
|
||||
{
|
||||
number: 9,
|
||||
title: 'Closed without merge',
|
||||
state: 'CLOSED',
|
||||
url: 'https://github.com/acme/widgets/pull/9',
|
||||
labels: [],
|
||||
updatedAt: '2026-04-01T00:00:00Z',
|
||||
author: { login: 'octocat' },
|
||||
isDraft: false,
|
||||
headRefName: 'feature/closed',
|
||||
headRefOid: 'head-9',
|
||||
baseRefName: 'main'
|
||||
},
|
||||
{
|
||||
number: 8,
|
||||
title: 'Merged work',
|
||||
state: 'MERGED',
|
||||
url: 'https://github.com/acme/widgets/pull/8',
|
||||
labels: [],
|
||||
updatedAt: '2026-03-31T00:00:00Z',
|
||||
author: { login: 'octocat' },
|
||||
isDraft: false,
|
||||
headRefName: 'feature/merged',
|
||||
headRefOid: 'head-8',
|
||||
baseRefName: 'main'
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
const { items } = await listWorkItems('/repo-root', 10, 'is:pr is:closed')
|
||||
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
|
||||
expect.arrayContaining(['--state', 'closed', '--search', '-is:merged']),
|
||||
{ cwd: '/repo-root' }
|
||||
)
|
||||
expect(items).toMatchObject([{ id: 'pr:9', type: 'pr', state: 'closed' }])
|
||||
})
|
||||
|
||||
it('quotes spaced label qualifiers when counting search results', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
ghExecFileAsyncMock.mockResolvedValueOnce({ stdout: '12' })
|
||||
|
||||
const count = await countWorkItems('/repo-root', 'is:pr label:"needs review"')
|
||||
|
||||
const apiPath = ghExecFileAsyncMock.mock.calls[0][0][3] as string
|
||||
expect(count).toBe(12)
|
||||
expect(decodeURIComponent(apiPath)).toContain('label:"needs review"')
|
||||
})
|
||||
|
||||
it('does not add the merged exclusion to issue-only closed count queries', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
ghExecFileAsyncMock.mockResolvedValueOnce({ stdout: '4' })
|
||||
|
||||
await countWorkItems('/repo-root', 'is:issue is:closed')
|
||||
|
||||
const apiPath = decodeURIComponent(ghExecFileAsyncMock.mock.calls[0][0][3] as string)
|
||||
expect(apiPath).toContain('is:issue is:closed')
|
||||
expect(apiPath).not.toContain('-is:merged')
|
||||
})
|
||||
|
||||
it('passes review-requested as a --search qualifier (gh CLI has no dedicated flag)', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
|
||||
+41
-21
@@ -588,13 +588,13 @@ function mapPullRequestWorkItem(
|
||||
number: Number(item.number),
|
||||
title: String(item.title ?? ''),
|
||||
state:
|
||||
state === 'closed'
|
||||
? item.merged_at || item.mergedAt
|
||||
? 'merged'
|
||||
: 'closed'
|
||||
: item.isDraft || item.draft
|
||||
? 'draft'
|
||||
: 'open',
|
||||
state === 'merged' || item.merged_at || item.mergedAt
|
||||
? 'merged'
|
||||
: state === 'closed'
|
||||
? 'closed'
|
||||
: item.isDraft || item.draft
|
||||
? 'draft'
|
||||
: 'open',
|
||||
url: String(item.html_url ?? item.url ?? ''),
|
||||
labels: Array.isArray(item.labels)
|
||||
? item.labels
|
||||
@@ -718,14 +718,13 @@ function buildWorkItemListArgs(args: {
|
||||
out.push('--repo', `${ownerRepo.owner}/${ownerRepo.repo}`)
|
||||
}
|
||||
|
||||
const state = query.state
|
||||
if (state && !(kind === 'issue' && state === 'merged')) {
|
||||
out.push('--state', state === 'all' ? 'all' : state)
|
||||
}
|
||||
|
||||
if (kind === 'pr' && query.state === 'merged') {
|
||||
out.push('--state', 'merged')
|
||||
if (query.state) {
|
||||
out.push('--state', query.state)
|
||||
}
|
||||
// Why: GitHub considers merged PRs as "closed". When the user filters for
|
||||
// closed-only, exclude merged PRs via the search predicate so the displayed
|
||||
// and counted results match the user's intent.
|
||||
const excludeMergedFromClosed = kind === 'pr' && query.state === 'closed'
|
||||
|
||||
if (query.assignee) {
|
||||
out.push('--assignee', query.assignee)
|
||||
@@ -746,6 +745,9 @@ function buildWorkItemListArgs(args: {
|
||||
}
|
||||
|
||||
const searchParts: string[] = []
|
||||
if (excludeMergedFromClosed) {
|
||||
searchParts.push('-is:merged')
|
||||
}
|
||||
// Why: cursor-based pagination. GitHub search supports updated:<DATE to
|
||||
// fetch items older than the cursor. We use the oldest item's updatedAt
|
||||
// from the previous page as the cursor.
|
||||
@@ -944,7 +946,12 @@ async function listQueriedWorkItems(
|
||||
connectionId?: string | null
|
||||
): Promise<PartialWorkItemsResult> {
|
||||
const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId))
|
||||
const issueScope = query.scope !== 'pr'
|
||||
const hasPrOnlyFilter =
|
||||
query.state === 'merged' ||
|
||||
query.draft ||
|
||||
query.reviewRequested !== null ||
|
||||
query.reviewedBy !== null
|
||||
const issueScope = query.scope !== 'pr' && !hasPrOnlyFilter
|
||||
const prScope = query.scope !== 'issue'
|
||||
|
||||
// Why: run the issue and PR fetches in parallel but surface the
|
||||
@@ -985,9 +992,13 @@ async function listQueriedWorkItems(
|
||||
})
|
||||
try {
|
||||
const { stdout } = await ghExecFileAsync(args, ghOptions)
|
||||
return (JSON.parse(stdout) as Record<string, unknown>[]).map((item) =>
|
||||
const mapped = (JSON.parse(stdout) as Record<string, unknown>[]).map((item) =>
|
||||
mapPullRequestWorkItem(item, prOwnerRepo)
|
||||
)
|
||||
if (query.state === 'closed') {
|
||||
return mapped.filter((item) => item.state !== 'merged')
|
||||
}
|
||||
return mapped
|
||||
} catch (err) {
|
||||
console.warn('listQueriedWorkItems PRs partial failure:', err)
|
||||
return []
|
||||
@@ -1068,7 +1079,12 @@ function buildSearchQueryString(
|
||||
if (query.state === 'open') {
|
||||
parts.push('is:open')
|
||||
} else if (query.state === 'closed') {
|
||||
// Why: GitHub search treats merged PRs as closed. Exclude merged so the
|
||||
// "Closed" filter actually means closed-without-merge.
|
||||
parts.push('is:closed')
|
||||
if (query.scope !== 'issue') {
|
||||
parts.push('-is:merged')
|
||||
}
|
||||
} else if (query.state === 'merged') {
|
||||
parts.push('is:merged')
|
||||
}
|
||||
@@ -1076,19 +1092,19 @@ function buildSearchQueryString(
|
||||
parts.push('draft:true')
|
||||
}
|
||||
if (query.assignee) {
|
||||
parts.push(`assignee:${query.assignee}`)
|
||||
parts.push(`assignee:${quoteGitHubSearchValue(query.assignee)}`)
|
||||
}
|
||||
if (query.author) {
|
||||
parts.push(`author:${query.author}`)
|
||||
parts.push(`author:${quoteGitHubSearchValue(query.author)}`)
|
||||
}
|
||||
if (query.reviewRequested) {
|
||||
parts.push(`review-requested:${query.reviewRequested}`)
|
||||
parts.push(`review-requested:${quoteGitHubSearchValue(query.reviewRequested)}`)
|
||||
}
|
||||
if (query.reviewedBy) {
|
||||
parts.push(`reviewed-by:${query.reviewedBy}`)
|
||||
parts.push(`reviewed-by:${quoteGitHubSearchValue(query.reviewedBy)}`)
|
||||
}
|
||||
for (const label of query.labels) {
|
||||
parts.push(`label:${label}`)
|
||||
parts.push(`label:${quoteGitHubSearchValue(label)}`)
|
||||
}
|
||||
if (query.freeText) {
|
||||
parts.push(query.freeText)
|
||||
@@ -1096,6 +1112,10 @@ function buildSearchQueryString(
|
||||
return parts.join(' ')
|
||||
}
|
||||
|
||||
function quoteGitHubSearchValue(value: string): string {
|
||||
return /\s/.test(value) ? `"${value.replaceAll('"', '\\"')}"` : value
|
||||
}
|
||||
|
||||
async function countWorkItemsForQuery(
|
||||
repoPath: string,
|
||||
ownerRepo: OwnerRepo,
|
||||
|
||||
Reference in New Issue
Block a user