fix: address review findings (#1078)

- fix: address review findings
- wip
- wip
This commit is contained in:
Jinjing
2026-04-24 21:58:21 -07:00
committed by GitHub
parent a2928e62be
commit aa10811780
15 changed files with 1407 additions and 147 deletions
+3 -3
View File
@@ -100,7 +100,7 @@ describe('listWorkItems', () => {
'--limit',
'10',
'--json',
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName',
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName,headRepositoryOwner',
'--repo',
'acme/widgets',
'--assignee',
@@ -163,7 +163,7 @@ describe('listWorkItems', () => {
'--limit',
'10',
'--json',
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName',
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName,headRepositoryOwner',
'--repo',
'acme/widgets',
'--state',
@@ -261,7 +261,7 @@ describe('listWorkItems', () => {
'--limit',
'10',
'--json',
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName',
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName,headRepositoryOwner',
'--repo',
'acme/widgets',
'--state',
+60 -11
View File
@@ -131,7 +131,44 @@ function mapIssueWorkItem(item: Record<string, unknown>): MainWorkItem {
}
}
function mapPullRequestWorkItem(item: Record<string, unknown>): MainWorkItem {
function extractHeadOwnerLogin(item: Record<string, unknown>): string | null {
// gh CLI `pr list --json headRepositoryOwner` shape: { login }
if (typeof item.headRepositoryOwner === 'object' && item.headRepositoryOwner !== null) {
const login = (item.headRepositoryOwner as { login?: unknown }).login
if (typeof login === 'string' && login.trim()) {
return login
}
}
// REST API `pull_request` shape: head.repo.owner.login
if (typeof item.head === 'object' && item.head !== null) {
const repo = (item.head as { repo?: unknown }).repo
if (typeof repo === 'object' && repo !== null) {
const owner = (repo as { owner?: unknown }).owner
if (typeof owner === 'object' && owner !== null) {
const login = (owner as { login?: unknown }).login
if (typeof login === 'string' && login.trim()) {
return login
}
}
}
}
return null
}
function mapPullRequestWorkItem(
item: Record<string, unknown>,
baseOwnerLogin: string | null = null
): MainWorkItem {
// Why: fork PRs are disabled in the Start-from picker. We compare the PR head's
// owner to the selected repo's owner; when baseOwnerLogin is unknown we default
// to false so non-picker call sites see the same shape as before.
const headOwnerLogin = extractHeadOwnerLogin(item)
// Why: only emit isCrossRepository when we actually know the head owner. If
// the gh response lacks `headRepositoryOwner` (older callers, tests without
// that fixture, or gh not returning it), leave the field undefined instead
// of falsely claiming "not a fork".
const isCrossRepository =
headOwnerLogin !== null && baseOwnerLogin !== null ? headOwnerLogin !== baseOwnerLogin : null
return {
id: `pr:${String(item.number)}`,
type: 'pr',
@@ -169,7 +206,8 @@ function mapPullRequestWorkItem(item: Record<string, unknown>): MainWorkItem {
baseRefName:
typeof item.base === 'object' && item.base !== null && 'ref' in item.base
? String((item.base as { ref?: unknown }).ref ?? '')
: String(item.baseRefName ?? '')
: String(item.baseRefName ?? ''),
...(isCrossRepository !== null ? { isCrossRepository } : {})
}
}
@@ -183,7 +221,7 @@ function buildWorkItemListArgs(args: {
const fields =
kind === 'issue'
? 'number,title,state,url,labels,updatedAt,author'
: 'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName'
: 'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName,headRepositoryOwner'
const command = kind === 'issue' ? ['issue', 'list'] : ['pr', 'list']
const out = [...command, '--limit', String(limit), '--json', fields]
@@ -274,8 +312,8 @@ async function listRecentWorkItems(
.filter((item) => !('pull_request' in item))
.map(mapIssueWorkItem)
const prs = (JSON.parse(prsResult.stdout) as Record<string, unknown>[]).map(
mapPullRequestWorkItem
const prs = (JSON.parse(prsResult.stdout) as Record<string, unknown>[]).map((item) =>
mapPullRequestWorkItem(item, ownerRepo.owner)
)
return sortWorkItemsByUpdatedAt([...issues, ...prs]).slice(0, limit)
@@ -304,7 +342,7 @@ async function listRecentWorkItems(
'--state',
'open',
'--json',
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName'
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName,headRepositoryOwner'
],
{ cwd: repoPath }
)
@@ -313,8 +351,8 @@ async function listRecentWorkItems(
const issues = (JSON.parse(issuesResult.stdout) as Record<string, unknown>[]).map(
mapIssueWorkItem
)
const prs = (JSON.parse(prsResult.stdout) as Record<string, unknown>[]).map(
mapPullRequestWorkItem
const prs = (JSON.parse(prsResult.stdout) as Record<string, unknown>[]).map((item) =>
mapPullRequestWorkItem(item, null)
)
return sortWorkItemsByUpdatedAt([...issues, ...prs]).slice(0, limit)
@@ -350,7 +388,9 @@ async function listQueriedWorkItems(
const args = buildWorkItemListArgs({ kind: 'pr', ownerRepo, limit, query })
try {
const { stdout } = await ghExecFileAsync(args, { cwd: repoPath })
return (JSON.parse(stdout) as Record<string, unknown>[]).map(mapPullRequestWorkItem)
return (JSON.parse(stdout) as Record<string, unknown>[]).map((item) =>
mapPullRequestWorkItem(item, ownerRepo?.owner ?? null)
)
} catch {
return []
}
@@ -408,6 +448,7 @@ export async function getWorkItem(repoPath: string, number: number): Promise<Mai
{ cwd: repoPath }
)
const pr = JSON.parse(prResult.stdout) as Record<string, unknown>
const prHeadOwner = extractHeadOwnerLogin(pr)
return {
id: `pr:${String(pr.number)}`,
type: 'pr',
@@ -443,7 +484,11 @@ export async function getWorkItem(repoPath: string, number: number): Promise<Mai
baseRefName:
typeof pr.base === 'object' && pr.base !== null && 'ref' in pr.base
? String((pr.base as { ref?: unknown }).ref ?? '')
: undefined
: undefined,
// Why: only emit isCrossRepository when we actually know the head
// owner. Falsely claiming "not a fork" would let the picker try a
// normal-PR fetch against a fork head and fail.
...(prHeadOwner !== null ? { isCrossRepository: prHeadOwner !== ownerRepo.owner } : {})
}
}
@@ -512,7 +557,7 @@ export async function getWorkItem(repoPath: string, number: number): Promise<Mai
'view',
String(number),
'--json',
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName'
'number,title,state,url,labels,updatedAt,author,isDraft,headRefName,baseRefName,headRepositoryOwner'
],
{ cwd: repoPath }
)
@@ -540,6 +585,10 @@ export async function getWorkItem(repoPath: string, number: number): Promise<Mai
: null,
branchName: String(item.headRefName ?? ''),
baseRefName: String(item.baseRefName ?? '')
// Why: ownerRepo is null on this path so we can't compare head vs base
// owners. Leave isCrossRepository undefined rather than guessing —
// falsely claiming "not a fork" would let the picker try a normal-PR
// fetch against a fork head and fail.
}
}
} catch {