Show linked workspaces for GitHub issues (#3820)

* fix: show linked workspaces for github issues

* fix ci
This commit is contained in:
Jinjing
2026-05-30 12:48:52 -07:00
committed by GitHub
parent 66efd938e6
commit 69a9c5124a
7 changed files with 431 additions and 175 deletions
+212 -35
View File
@@ -25,6 +25,7 @@ import {
Copy,
ExternalLink,
FileText,
FolderKanban,
GitMerge,
GitPullRequest,
GitPullRequestClosed,
@@ -34,6 +35,7 @@ import {
MessageSquarePlus,
PanelLeftOpen,
Pencil,
Plus,
RefreshCw,
Send,
UndoDot,
@@ -102,6 +104,7 @@ import {
type PRCommentGroup
} from '@/lib/pr-comment-groups'
import { useAppStore } from '@/store'
import { useAllWorktrees } from '@/store/selectors'
import { callRuntimeRpc, getActiveRuntimeTarget } from '@/runtime/runtime-rpc-client'
import { useRepoLabels, useRepoAssignees, useImmediateMutation } from '@/hooks/useIssueMetadata'
import { useRepoLabelsBySlug, useRepoAssigneesBySlug } from '@/hooks/useGitHubSlugMetadata'
@@ -115,6 +118,11 @@ import { AGENT_CATALOG } from '@/lib/agent-catalog'
import { filterEnabledTuiAgents } from '../../../shared/tui-agent-selection'
import { getConnectionId } from '@/lib/connection-context'
import { focusTerminalTabSurface } from '@/lib/focus-terminal-tab-surface'
import {
findGithubIssueWorkspaceAttachment,
findGithubPrWorkspaceAttachment,
getGithubWorkItemWorkspaceAttachmentLabel
} from '@/lib/github-work-item-workspace-attachment'
import { launchAgentInNewTab } from '@/lib/launch-agent-in-new-tab'
import { launchWorkItemDirect } from '@/lib/launch-work-item-direct'
import { activateAndRevealWorktree } from '@/lib/worktree-activation'
@@ -132,8 +140,7 @@ import type {
PRCheckDetail,
PRCheckRunDetails,
PRComment,
TuiAgent,
Worktree
TuiAgent
} from '../../../shared/types'
import { PER_REPO_FETCH_LIMIT } from '../../../shared/work-items'
@@ -3317,19 +3324,6 @@ function buildFixBrokenChecksPrompt(item: GitHubWorkItem, checks: PRCheckDetail[
].join('\n')
}
function findWorkspaceAttachedToPR(
worktrees: Worktree[],
repoId: string,
prNumber: number
): Worktree | null {
return (
worktrees.find(
(worktree) =>
worktree.repoId === repoId && worktree.linkedPR === prNumber && !worktree.isArchived
) ?? null
)
}
function pickDefaultAgent(
defaultAgent: TuiAgent | 'blank' | null | undefined,
detectedAgents: TuiAgent[],
@@ -3498,7 +3492,7 @@ function ChecksTab({
try {
const prompt = buildFixBrokenChecksPrompt(item, list)
const store = useAppStore.getState()
const attachedWorkspace = findWorkspaceAttachedToPR(
const attachedWorkspace = findGithubPrWorkspaceAttachment(
store.allWorktrees(),
targetRepoId,
item.number
@@ -4352,6 +4346,8 @@ function GHEditSection({
onMutated,
assignees,
onUse,
onOpenOrUse,
attachedWorkspaceLabel,
layout = 'horizontal'
}: {
item: GitHubWorkItem
@@ -4368,6 +4364,8 @@ function GHEditSection({
onMutated: () => void
assignees: string[]
onUse: (item: GitHubWorkItem) => void
onOpenOrUse?: (item: GitHubWorkItem) => void
attachedWorkspaceLabel?: string | null
/** `'horizontal'` is the legacy strip rendered above the conversation; the
* `'sidebar'` layout matches the GitHub issue page's right rail with each
* metadata row stacked under a section heading. */
@@ -4412,6 +4410,15 @@ function GHEditSection({
)
const repoAssigneesBySlug = useRepoAssigneesBySlug(slugOwner, slugRepo, assignees)
const repoAssignees = projectOrigin ? repoAssigneesBySlug : repoAssigneesByPath
const hasAttachedWorkspace =
attachedWorkspaceLabel !== null && attachedWorkspaceLabel !== undefined
const handleOpenOrUseWorkspace = useCallback((): void => {
if (onOpenOrUse) {
onOpenOrUse(item)
return
}
onUse(item)
}, [item, onOpenOrUse, onUse])
// Why: sync local assignees when item changes or when the detail fetch
// resolves with real data — but skip if the user already made an
@@ -4846,6 +4853,56 @@ function GHEditSection({
</div>
)}
</section>
<section>
<div className="mb-2 text-[11px] font-semibold uppercase tracking-[0.05em] text-muted-foreground">
Workspace
</div>
{attachedWorkspaceLabel ? (
<div className="mb-2 flex min-w-0 items-center gap-1.5 text-[12px] text-muted-foreground">
<FolderKanban className="size-3.5 shrink-0" />
<span className="truncate">{attachedWorkspaceLabel}</span>
</div>
) : null}
{hasAttachedWorkspace ? (
<DropdownMenu modal={false}>
<ButtonGroup className="w-full">
<Button
type="button"
size="sm"
onClick={handleOpenOrUseWorkspace}
className="flex-1 gap-1.5"
aria-label="Open workspace attached to issue"
>
Open workspace
<ArrowRight className="size-3.5" />
</Button>
<DropdownMenuTrigger asChild>
<Button type="button" size="icon-sm" aria-label="More issue workspace actions">
<ChevronDown className="size-3.5" />
</Button>
</DropdownMenuTrigger>
</ButtonGroup>
<DropdownMenuContent align="end">
<DropdownMenuItem onSelect={() => onUse(item)}>
<Plus className="size-4" />
Start new workspace
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
) : (
<Button
type="button"
size="sm"
onClick={() => onUse(item)}
className="w-full gap-1.5"
aria-label="Start workspace from issue"
>
Start workspace from issue
<ArrowRight className="size-3.5" />
</Button>
)}
</section>
</aside>
)
}
@@ -5011,15 +5068,52 @@ function GHEditSection({
</PopoverContent>
</Popover>
<Button
size="sm"
onClick={() => onUse(item)}
className="ml-auto gap-2"
aria-label="Start workspace from issue"
>
Start workspace from issue
<ArrowRight className="size-4" />
</Button>
<div className="ml-auto flex min-w-0 items-center gap-2">
{attachedWorkspaceLabel ? (
<span className="inline-flex min-w-0 items-center gap-1 text-[11px] text-muted-foreground">
<FolderKanban className="size-3 shrink-0" />
<span className="truncate">{attachedWorkspaceLabel}</span>
</span>
) : null}
{hasAttachedWorkspace ? (
<DropdownMenu modal={false}>
<ButtonGroup>
<Button
type="button"
size="sm"
onClick={handleOpenOrUseWorkspace}
className="gap-2"
aria-label="Open workspace attached to issue"
>
Open workspace
<ArrowRight className="size-4" />
</Button>
<DropdownMenuTrigger asChild>
<Button type="button" size="icon-sm" aria-label="More issue workspace actions">
<ChevronDown className="size-3.5" />
</Button>
</DropdownMenuTrigger>
</ButtonGroup>
<DropdownMenuContent align="end">
<DropdownMenuItem onSelect={() => onUse(item)}>
<Plus className="size-4" />
Start new workspace
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
) : (
<Button
type="button"
size="sm"
onClick={() => onUse(item)}
className="gap-2"
aria-label="Start workspace from issue"
>
Start workspace from issue
<ArrowRight className="size-4" />
</Button>
)}
</div>
</div>
)
}
@@ -5224,6 +5318,37 @@ export default function GitHubItemDialog({
const workItemState = workItem?.state
const workItemLabels = workItem?.labels
const effectiveRepoId = repoId ?? workItem?.repoId ?? null
const allWorktrees = useAllWorktrees()
const issueAttachedWorkspace = useMemo(
() =>
workItem?.type === 'issue'
? findGithubIssueWorkspaceAttachment(allWorktrees, effectiveRepoId, workItem.number)
: null,
[allWorktrees, effectiveRepoId, workItem]
)
const issueAttachedWorkspaceLabel = issueAttachedWorkspace
? getGithubWorkItemWorkspaceAttachmentLabel(issueAttachedWorkspace)
: null
const handleOpenOrUseIssueWorkspace = useCallback(
(item: GitHubWorkItem): void => {
const currentAttached = findGithubIssueWorkspaceAttachment(
useAppStore.getState().allWorktrees(),
effectiveRepoId,
item.number
)
if (!currentAttached) {
onUse(item)
return
}
const result = activateAndRevealWorktree(currentAttached.id)
if (result === false) {
toast.error('Unable to open the workspace attached to this issue.')
}
},
[effectiveRepoId, onUse]
)
// Why: the cache key has to include the issue source preference so a user
// toggling between origin/upstream for the same issue number doesn't read
@@ -5687,16 +5812,52 @@ export default function GitHubItemDialog({
<div className="flex shrink-0 items-center gap-2">
{/* Why: Orca's signature affordance — keep this primary so it
stands out against GitHub's familiar surface. */}
<Button
type="button"
size="sm"
onClick={() => onUse(workItem)}
className="gap-1.5 whitespace-nowrap"
aria-label="Start workspace from issue"
>
Start workspace from issue
<ArrowRight className="size-3.5" />
</Button>
{issueAttachedWorkspace ? (
<DropdownMenu modal={false}>
<ButtonGroup>
<Button
type="button"
size="sm"
onClick={() => handleOpenOrUseIssueWorkspace(workItem)}
className="gap-1.5 whitespace-nowrap"
aria-label="Open workspace attached to issue"
>
Open workspace
<ArrowRight className="size-3.5" />
</Button>
<DropdownMenuTrigger asChild>
<Button
type="button"
size="icon-sm"
aria-label="More issue workspace actions"
>
<ChevronDown className="size-3.5" />
</Button>
</DropdownMenuTrigger>
</ButtonGroup>
<DropdownMenuContent align="end">
<DropdownMenuItem onSelect={() => onUse(workItem)}>
<Plus className="size-4" />
Start new workspace
</DropdownMenuItem>
<DropdownMenuItem onSelect={() => window.api.shell.openUrl(workItem.url)}>
<ExternalLink className="size-4" />
Open on GitHub
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
) : (
<Button
type="button"
size="sm"
onClick={() => onUse(workItem)}
className="gap-1.5 whitespace-nowrap"
aria-label="Start workspace from issue"
>
Start workspace from issue
<ArrowRight className="size-3.5" />
</Button>
)}
</div>
</div>
<div className="mt-3 flex flex-wrap items-center gap-2 text-[13px] text-muted-foreground">
@@ -5723,6 +5884,12 @@ export default function GitHubItemDialog({
</span>
</span>
<WorkItemIssueSourceIndicator url={workItem.url} repoId={effectiveRepoId} />
{issueAttachedWorkspaceLabel ? (
<span className="inline-flex min-w-0 items-center gap-1.5">
<FolderKanban className="size-3.5 shrink-0" />
<span className="truncate">{issueAttachedWorkspaceLabel}</span>
</span>
) : null}
</div>
</div>
</>
@@ -5762,6 +5929,12 @@ export default function GitHubItemDialog({
{workItem.branchName}
</span>
)}
{issueAttachedWorkspaceLabel ? (
<span className="inline-flex min-w-0 items-center gap-1">
<FolderKanban className="size-3 shrink-0" />
<span className="truncate">{issueAttachedWorkspaceLabel}</span>
</span>
) : null}
</div>
{workItem.type === 'issue' && (
<WorkItemIssueSourceIndicator url={workItem.url} repoId={effectiveRepoId} />
@@ -5865,6 +6038,8 @@ export default function GitHubItemDialog({
}}
assignees={details?.assignees ?? []}
onUse={onUse}
onOpenOrUse={handleOpenOrUseIssueWorkspace}
attachedWorkspaceLabel={issueAttachedWorkspaceLabel}
/>
)}
@@ -5947,6 +6122,8 @@ export default function GitHubItemDialog({
}}
assignees={details?.assignees ?? []}
onUse={onUse}
onOpenOrUse={handleOpenOrUseIssueWorkspace}
attachedWorkspaceLabel={issueAttachedWorkspaceLabel}
layout="sidebar"
/>
</div>
@@ -119,7 +119,7 @@ import { focusTerminalTabSurface } from '@/lib/focus-terminal-tab-surface'
import {
findGithubPrWorkspaceAttachment,
getGithubPrWorkspaceAttachmentLabel
} from '@/lib/github-pr-workspace-attachment'
} from '@/lib/github-work-item-workspace-attachment'
import { launchAgentInNewTab } from '@/lib/launch-agent-in-new-tab'
import { launchWorkItemDirect } from '@/lib/launch-work-item-direct'
import { activateAndRevealWorktree } from '@/lib/worktree-activation'
+32 -14
View File
@@ -100,9 +100,9 @@ import {
import PRFilterDropdowns, { type PRFilterChange } from '@/components/github/PRFilterDropdowns'
import { buildGitHubRepoUrl, parseGitHubIssueOrPRLink } from '@/lib/github-links'
import {
findGithubPrWorkspaceAttachment,
getGithubPrWorkspaceAttachmentLabel
} from '@/lib/github-pr-workspace-attachment'
findGithubWorkItemWorkspaceAttachment,
getGithubWorkItemWorkspaceAttachmentLabel
} from '@/lib/github-work-item-workspace-attachment'
import { activateAndRevealWorktree } from '@/lib/worktree-activation'
import { useRepoAssigneesBySlug } from '@/hooks/useGitHubSlugMetadata'
import GitHubItemDialog, { type ItemDialogTab } from '@/components/GitHubItemDialog'
@@ -3801,11 +3801,12 @@ export default function TaskPage(): React.JSX.Element {
[openComposerForItem]
)
const handleOpenOrUseGitHubPR = useCallback(
const handleOpenOrUseGitHubWorkItem = useCallback(
(item: GitHubWorkItem): void => {
const currentAttached = findGithubPrWorkspaceAttachment(
const currentAttached = findGithubWorkItemWorkspaceAttachment(
useAppStore.getState().allWorktrees(),
item.repoId,
item.type,
item.number
)
if (!currentAttached) {
@@ -3815,7 +3816,11 @@ export default function TaskPage(): React.JSX.Element {
const result = activateAndRevealWorktree(currentAttached.id)
if (result === false) {
toast.error('Unable to open the workspace attached to this pull request.')
toast.error(
item.type === 'pr'
? 'Unable to open the workspace attached to this pull request.'
: 'Unable to open the workspace attached to this issue.'
)
}
},
[handleUseWorkItem]
@@ -5173,12 +5178,14 @@ export default function TaskPage(): React.JSX.Element {
{!showGitHubTaskSkeletons &&
filteredWorkItems.map((item) => {
const itemRepo = repoMap.get(item.repoId) ?? null
const attachedWorkspace =
item.type === 'pr'
? findGithubPrWorkspaceAttachment(allWorktrees, item.repoId, item.number)
: null
const attachedWorkspace = findGithubWorkItemWorkspaceAttachment(
allWorktrees,
item.repoId,
item.type,
item.number
)
const attachedWorkspaceLabel = attachedWorkspace
? getGithubPrWorkspaceAttachmentLabel(attachedWorkspace)
? getGithubWorkItemWorkspaceAttachmentLabel(attachedWorkspace)
: null
return (
// Why: the row is a clickable container rather than a
@@ -5334,7 +5341,7 @@ export default function TaskPage(): React.JSX.Element {
size="xs"
onClick={(event) => {
event.stopPropagation()
handleOpenOrUseGitHubPR(item)
handleOpenOrUseGitHubWorkItem(item)
}}
className={cn(
'min-w-[72px] gap-1 font-semibold',
@@ -5387,11 +5394,16 @@ export default function TaskPage(): React.JSX.Element {
type="button"
onClick={(event) => {
event.stopPropagation()
handleUseWorkItem(item)
handleOpenOrUseGitHubWorkItem(item)
}}
aria-label={
attachedWorkspace
? 'Open workspace attached to issue'
: 'Start workspace from issue'
}
className="inline-flex items-center gap-1 rounded-md border border-border/50 bg-background/80 px-2 py-1 text-[11px] text-foreground transition hover:bg-muted/60"
>
Start
{attachedWorkspace ? 'Open' : 'Start'}
<ArrowRight className="size-3" />
</button>
)}
@@ -5411,6 +5423,12 @@ export default function TaskPage(): React.JSX.Element {
align="end"
onClick={(e) => e.stopPropagation()}
>
{attachedWorkspace ? (
<DropdownMenuItem onSelect={() => handleUseWorkItem(item)}>
<Plus className="size-4" />
Start new workspace
</DropdownMenuItem>
) : null}
<DropdownMenuItem
onSelect={() => window.api.shell.openUrl(item.url)}
>
@@ -1,79 +0,0 @@
import { describe, expect, it } from 'vitest'
import {
findGithubPrWorkspaceAttachment,
getGithubPrWorkspaceAttachmentLabel
} from './github-pr-workspace-attachment'
import type { Worktree } from '../../../shared/types'
function worktree(overrides: Partial<Worktree> = {}): Worktree {
return {
id: overrides.id ?? 'wt-1',
repoId: overrides.repoId ?? 'repo-1',
path: overrides.path ?? '/tmp/repo-1/wt-1',
head: 'abc123',
branch: overrides.branch ?? 'refs/heads/feature/pr-workspace',
isBare: false,
isMainWorktree: false,
displayName: overrides.displayName ?? 'PR workspace',
comment: '',
linkedIssue: null,
linkedPR: null,
linkedLinearIssue: null,
isArchived: false,
isUnread: false,
isPinned: false,
sortOrder: 0,
lastActivityAt: 0,
...overrides
}
}
describe('github PR workspace attachment', () => {
it('finds the first non-archived workspace linked to the repo PR', () => {
const first = worktree({ id: 'first', linkedPR: 42 })
const second = worktree({ id: 'second', linkedPR: 42 })
expect(findGithubPrWorkspaceAttachment([first, second], 'repo-1', 42)).toBe(first)
})
it('does not match workspaces from a different repo', () => {
const attachedElsewhere = worktree({ repoId: 'repo-2', linkedPR: 42 })
expect(findGithubPrWorkspaceAttachment([attachedElsewhere], 'repo-1', 42)).toBeNull()
})
it('does not match archived workspaces', () => {
const archived = worktree({ linkedPR: 42, isArchived: true })
expect(findGithubPrWorkspaceAttachment([archived], 'repo-1', 42)).toBeNull()
})
it('returns null when no GitHub PR attachment exists', () => {
const unlinked = worktree({ linkedPR: null })
expect(findGithubPrWorkspaceAttachment([unlinked], 'repo-1', 42)).toBeNull()
})
it('does not treat GitLab MR metadata as a GitHub PR attachment', () => {
const gitlabOnly = worktree({ linkedPR: null, linkedGitLabMR: 42 })
expect(findGithubPrWorkspaceAttachment([gitlabOnly], 'repo-1', 42)).toBeNull()
})
it('labels attachments without exposing a full path when display or branch is available', () => {
expect(getGithubPrWorkspaceAttachmentLabel(worktree({ displayName: ' Named PR ' }))).toBe(
'Named PR'
)
expect(
getGithubPrWorkspaceAttachmentLabel(
worktree({ displayName: '', branch: 'refs/heads/fix-ci' })
)
).toBe('fix-ci')
expect(
getGithubPrWorkspaceAttachmentLabel(
worktree({ displayName: '', branch: '', path: 'C:\\repo\\workspace-tail' })
)
).toBe('workspace-tail')
})
})
@@ -1,46 +0,0 @@
import type { Worktree } from '../../../shared/types'
import { basename } from './path'
export function findGithubPrWorkspaceAttachment(
worktrees: readonly Worktree[],
repoId: string | null | undefined,
prNumber: number
): Worktree | null {
if (!repoId) {
return null
}
return (
worktrees.find(
(worktree) =>
worktree.repoId === repoId && worktree.linkedPR === prNumber && !worktree.isArchived
) ?? null
)
}
export function getGithubPrWorkspaceAttachmentLabel(worktree: Worktree): string {
const displayName = worktree.displayName.trim()
if (displayName) {
return displayName
}
const branch = getBranchLabel(worktree.branch)
if (branch) {
return branch
}
return basename(worktree.path) || worktree.path
}
function getBranchLabel(branch: string | null | undefined): string | null {
const trimmed = branch?.trim()
if (!trimmed) {
return null
}
if (trimmed.startsWith('refs/heads/')) {
return trimmed.slice('refs/heads/'.length)
}
return trimmed
}
@@ -0,0 +1,114 @@
import { describe, expect, it } from 'vitest'
import {
findGithubIssueWorkspaceAttachment,
findGithubPrWorkspaceAttachment,
findGithubWorkItemWorkspaceAttachment,
getGithubWorkItemWorkspaceAttachmentLabel,
getGithubPrWorkspaceAttachmentLabel
} from './github-work-item-workspace-attachment'
import type { Worktree } from '../../../shared/types'
function worktree(overrides: Partial<Worktree> = {}): Worktree {
return {
id: overrides.id ?? 'wt-1',
repoId: overrides.repoId ?? 'repo-1',
path: overrides.path ?? '/tmp/repo-1/wt-1',
head: 'abc123',
branch: overrides.branch ?? 'refs/heads/feature/workspace-attachment',
isBare: false,
isMainWorktree: false,
displayName: overrides.displayName ?? 'GitHub workspace',
comment: '',
linkedIssue: null,
linkedPR: null,
linkedLinearIssue: null,
isArchived: false,
isUnread: false,
isPinned: false,
sortOrder: 0,
lastActivityAt: 0,
...overrides
}
}
describe('GitHub work-item workspace attachment', () => {
it('finds the first non-archived workspace linked to the repo PR', () => {
const first = worktree({ id: 'first', linkedPR: 42 })
const second = worktree({ id: 'second', linkedPR: 42 })
expect(findGithubPrWorkspaceAttachment([first, second], 'repo-1', 42)).toBe(first)
})
it('finds the first non-archived workspace linked to the repo issue', () => {
const first = worktree({ id: 'first', linkedIssue: 42 })
const second = worktree({ id: 'second', linkedIssue: 42 })
expect(findGithubIssueWorkspaceAttachment([first, second], 'repo-1', 42)).toBe(first)
})
it('matches PRs and issues through the generic helper', () => {
const pr = worktree({ id: 'pr', linkedPR: 42 })
const issue = worktree({ id: 'issue', linkedIssue: 42 })
expect(findGithubWorkItemWorkspaceAttachment([issue, pr], 'repo-1', 'pr', 42)).toBe(pr)
expect(findGithubWorkItemWorkspaceAttachment([pr, issue], 'repo-1', 'issue', 42)).toBe(issue)
})
it('does not match workspaces from a different repo', () => {
const attachedElsewhere = worktree({ repoId: 'repo-2', linkedIssue: 42, linkedPR: 42 })
expect(findGithubPrWorkspaceAttachment([attachedElsewhere], 'repo-1', 42)).toBeNull()
expect(findGithubIssueWorkspaceAttachment([attachedElsewhere], 'repo-1', 42)).toBeNull()
})
it('does not match archived workspaces', () => {
const archived = worktree({ linkedIssue: 42, linkedPR: 42, isArchived: true })
expect(findGithubPrWorkspaceAttachment([archived], 'repo-1', 42)).toBeNull()
expect(findGithubIssueWorkspaceAttachment([archived], 'repo-1', 42)).toBeNull()
})
it('returns null when no repo ID is available', () => {
const attached = worktree({ linkedIssue: 42, linkedPR: 42 })
expect(findGithubPrWorkspaceAttachment([attached], null, 42)).toBeNull()
expect(findGithubIssueWorkspaceAttachment([attached], undefined, 42)).toBeNull()
})
it('does not cross-match PR and issue slots with the same number', () => {
const prOnly = worktree({ id: 'pr', linkedPR: 42 })
const issueOnly = worktree({ id: 'issue', linkedIssue: 42 })
expect(findGithubIssueWorkspaceAttachment([prOnly], 'repo-1', 42)).toBeNull()
expect(findGithubPrWorkspaceAttachment([issueOnly], 'repo-1', 42)).toBeNull()
})
it('does not treat GitLab metadata as a GitHub work-item attachment', () => {
const gitlabOnly = worktree({
linkedIssue: null,
linkedPR: null,
linkedGitLabIssue: 42,
linkedGitLabMR: 42
})
expect(findGithubPrWorkspaceAttachment([gitlabOnly], 'repo-1', 42)).toBeNull()
expect(findGithubIssueWorkspaceAttachment([gitlabOnly], 'repo-1', 42)).toBeNull()
})
it('labels attachments without exposing a full path when display or branch is available', () => {
expect(
getGithubWorkItemWorkspaceAttachmentLabel(worktree({ displayName: ' Named GH ' }))
).toBe('Named GH')
expect(
getGithubWorkItemWorkspaceAttachmentLabel(
worktree({ displayName: '', branch: 'refs/heads/fix-ci' })
)
).toBe('fix-ci')
expect(
getGithubPrWorkspaceAttachmentLabel(
worktree({ displayName: '', branch: '', path: 'C:\\repo\\workspace-tail' })
)
).toBe('workspace-tail')
})
})
@@ -0,0 +1,72 @@
import type { GitHubWorkItem, Worktree } from '../../../shared/types'
import { basename } from './path'
type GitHubWorkItemType = GitHubWorkItem['type']
export function findGithubWorkItemWorkspaceAttachment(
worktrees: readonly Worktree[],
repoId: string | null | undefined,
type: GitHubWorkItemType,
number: number
): Worktree | null {
if (!repoId) {
return null
}
return (
worktrees.find((worktree) => {
if (worktree.repoId !== repoId || worktree.isArchived) {
return false
}
return type === 'pr' ? worktree.linkedPR === number : worktree.linkedIssue === number
}) ?? null
)
}
export function findGithubPrWorkspaceAttachment(
worktrees: readonly Worktree[],
repoId: string | null | undefined,
prNumber: number
): Worktree | null {
return findGithubWorkItemWorkspaceAttachment(worktrees, repoId, 'pr', prNumber)
}
export function findGithubIssueWorkspaceAttachment(
worktrees: readonly Worktree[],
repoId: string | null | undefined,
issueNumber: number
): Worktree | null {
return findGithubWorkItemWorkspaceAttachment(worktrees, repoId, 'issue', issueNumber)
}
export function getGithubWorkItemWorkspaceAttachmentLabel(worktree: Worktree): string {
const displayName = worktree.displayName.trim()
if (displayName) {
return displayName
}
const branch = getBranchLabel(worktree.branch)
if (branch) {
return branch
}
return basename(worktree.path) || worktree.path
}
export function getGithubPrWorkspaceAttachmentLabel(worktree: Worktree): string {
return getGithubWorkItemWorkspaceAttachmentLabel(worktree)
}
function getBranchLabel(branch: string | null | undefined): string | null {
const trimmed = branch?.trim()
if (!trimmed) {
return null
}
if (trimmed.startsWith('refs/heads/')) {
return trimmed.slice('refs/heads/'.length)
}
return trimmed
}