Default repo avatars to the GitHub upstream owner and flag forks (#4522)

* Default repo avatars to the GitHub upstream owner and flag forks

Make the GitHub owner avatar the default repo icon and surface it as the
primary choice in the icon picker (the "Image" tab becomes "Avatar",
moves first, and opens by default). For forks, resolve the upstream/
parent owner so the avatar reflects the source repo instead of the
personal fork, and show a fork indicator (GitFork glyph + "Fork of
owner/repo" tooltip) next to the repo in the sidebar and settings.

Fork detection prefers the offline `upstream` remote, falling back to a
`gh repo view --json isFork,parent` lookup; the resolved upstream is
stored on the repo. Existing repos self-correct via a one-time startup
backfill (local repos) and a lazy backfill when their icon settings open
(SSH repos); new repos resolve at add-time. Reset now restores the
default (re-detecting the upstream) instead of clearing to the Folder
icon.

* Harden repo upstream avatar handling

---------

Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
This commit is contained in:
Trevin Chow
2026-06-02 20:55:35 -07:00
committed by GitHub
co-authored by Neil
parent da3c359a74
commit abfe9bd329
24 changed files with 577 additions and 116 deletions
+53
View File
@@ -93,6 +93,7 @@ vi.mock('./rate-limit', () => ({
import {
getPRComments,
getPRForBranch,
getRepoUpstream,
getWorkItem,
getPullRequestPushTarget,
mergePR,
@@ -1414,6 +1415,58 @@ describe('getPRForBranch', () => {
expect(gitExecFileAsyncMock).not.toHaveBeenCalled()
})
it('resolves a distinct upstream remote as the repo upstream', async () => {
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'tmchow', repo: 'orca' })
getOwnerRepoForRemoteMock.mockResolvedValueOnce({ owner: 'stablyai', repo: 'orca' })
await expect(getRepoUpstream('/repo-root')).resolves.toEqual({
owner: 'stablyai',
repo: 'orca'
})
expect(ghExecFileAsyncMock).not.toHaveBeenCalled()
})
it('does not treat a same-repository upstream remote as a fork', async () => {
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'StablyAI', repo: 'Orca' })
getOwnerRepoForRemoteMock.mockResolvedValueOnce({ owner: 'stablyai', repo: 'orca' })
ghExecFileAsyncMock.mockResolvedValueOnce({
stdout: JSON.stringify({ isFork: false, parent: null })
})
await expect(getRepoUpstream('/repo-root')).resolves.toBeNull()
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['repo', 'view', 'StablyAI/Orca', '--json', 'isFork,parent'],
{ cwd: '/repo-root', timeout: 10_000 }
)
})
it('does not mark an upstream-only GitHub remote as a fork', async () => {
getOwnerRepoMock.mockResolvedValueOnce(null)
await expect(getRepoUpstream('/repo-root')).resolves.toBeNull()
expect(getOwnerRepoForRemoteMock).not.toHaveBeenCalled()
expect(ghExecFileAsyncMock).not.toHaveBeenCalled()
})
it('falls back to the GitHub parent when no upstream remote is configured', async () => {
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'tmchow', repo: 'orca' })
getOwnerRepoForRemoteMock.mockResolvedValueOnce(null)
ghExecFileAsyncMock.mockResolvedValueOnce({
stdout: JSON.stringify({
isFork: true,
parent: { name: 'orca', owner: { login: 'stablyai' } }
})
})
await expect(getRepoUpstream('/repo-root')).resolves.toEqual({
owner: 'stablyai',
repo: 'orca'
})
})
it('probes additional PR repo candidates when the first lookup is not found', async () => {
resolvePRRepositoryCandidatesMock.mockResolvedValueOnce({
candidates: [
+42
View File
@@ -1413,6 +1413,48 @@ export async function getRepoSlug(
return getOwnerRepo(repoPath, connectionId)
}
/**
* Resolve a fork's upstream/parent owner/repo, or null when the repo is not a
* fork. Why: a fork's `origin` points at the personal copy, so repo identity
* (notably the avatar) should prefer the upstream. Fast-paths the `upstream`
* remote (offline); otherwise asks the GitHub API for the fork parent. The API
* call targets the explicit origin slug, so it works for SSH repos too.
* Best-effort: any failure (offline, unauthed, non-GitHub) resolves to null.
*/
export async function getRepoUpstream(
repoPath: string,
connectionId?: string | null
): Promise<OwnerRepo | null> {
const origin = await getOwnerRepo(repoPath, connectionId)
if (!origin) {
return null
}
const upstreamRemote = await getOwnerRepoForRemote(repoPath, 'upstream', connectionId)
if (upstreamRemote && !sameOwnerRepo(upstreamRemote, origin)) {
return upstreamRemote
}
await acquire()
try {
const { stdout } = await ghExecFileAsync(
['repo', 'view', `${origin.owner}/${origin.repo}`, '--json', 'isFork,parent'],
// Why: best-effort fork lookup runs at add-time; cap latency so a stalled
// gh process can't hold up repo creation.
{ ...ghRepoExecOptions(githubRepoContext(repoPath, connectionId)), timeout: 10_000 }
)
const data = JSON.parse(stdout) as {
isFork?: boolean
parent?: { name?: string; owner?: { login?: string } } | null
}
const owner = data.parent?.owner?.login
const repo = data.parent?.name
return data.isFork && owner && repo ? { owner, repo } : null
} catch {
return null
} finally {
release()
}
}
function classifyCreatePRError(error: unknown): CreateHostedReviewResult {
const { stderr, stdout } = extractExecError(error)
const message = `${stderr}\n${stdout}`.trim()