From 65d5ec7d3d0cacd3f4267906f84b3015c502db01 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 26 Jun 2026 21:12:48 -0700 Subject: [PATCH 1/6] Fix runtime GitHub work item details --- src/main/ipc/github.test.ts | 56 ++ src/main/ipc/github.ts | 40 +- src/preload/api-types.ts | 6 + src/preload/index.ts | 7 + .../src/components/GitHubItemDialog.tsx | 659 +++++++++++--- .../src/components/PullRequestPage.tsx | 848 +++++++++++++++--- src/renderer/src/components/TaskPage.tsx | 1 + ...github-item-dialog-source-boundary.test.ts | 167 ++++ .../github-project/ProjectViewWrapper.tsx | 12 + ...ew-wrapper-source-context-boundary.test.ts | 34 + .../pull-request-page-host-boundary.test.ts | 257 +++++- .../task-page-source-switch-boundary.test.ts | 2 + .../github-work-item-details-cache-events.ts | 32 + .../github-work-item-source-lookup.test.ts | 103 +++ .../src/lib/github-work-item-source-lookup.ts | 38 +- src/renderer/src/web/web-preload-api.ts | 1 + 16 files changed, 2005 insertions(+), 258 deletions(-) create mode 100644 src/renderer/src/components/github-project/project-view-wrapper-source-context-boundary.test.ts create mode 100644 src/renderer/src/lib/github-work-item-details-cache-events.ts create mode 100644 src/renderer/src/lib/github-work-item-source-lookup.test.ts diff --git a/src/main/ipc/github.test.ts b/src/main/ipc/github.test.ts index 5b1612e85c7d..168c791cfe32 100644 --- a/src/main/ipc/github.test.ts +++ b/src/main/ipc/github.test.ts @@ -282,6 +282,62 @@ describe('registerGitHubHandlers', () => { ) }) + it('broadcasts mutation notifications by repo id even when the payload path is remote', async () => { + const sendMock = vi.fn() + getAllWebContentsMock.mockReturnValue([ + { id: 1, isDestroyed: () => false, send: vi.fn() }, + { id: 2, isDestroyed: () => false, send: sendMock } + ]) + registerGitHubHandlers(store as never, stats as never) + + const result = await handlers['gh:notifyWorkItemMutated']( + { sender: { id: 1 } }, + { + repoPath: '/home/runtime/repo', + repoId: 'repo-1', + type: 'pr', + number: 42 + } + ) + + expect(result).toBe(true) + expect(sendMock).toHaveBeenCalledWith('gh:workItemMutated', { + repoPath: '/workspace/repo', + repoId: 'repo-1', + type: 'pr', + number: 42 + }) + }) + + it('broadcasts PR file viewed mutations with repo id for sibling cache invalidation', async () => { + const sendMock = vi.fn() + getAllWebContentsMock.mockReturnValue([ + { id: 1, isDestroyed: () => false, send: vi.fn() }, + { id: 2, isDestroyed: () => false, send: sendMock } + ]) + setPRFileViewedMock.mockResolvedValue(true) + registerGitHubHandlers(store as never, stats as never) + + const result = await handlers['gh:setPRFileViewed']( + { sender: { id: 1 } }, + { + repoPath: '/workspace/repo', + prNumber: 42, + pullRequestId: 'PR_kw', + path: 'src/app.ts', + viewed: true + } + ) + + expect(result).toBe(true) + expect(sendMock).toHaveBeenCalledWith('gh:workItemMutated', { + repoPath: '/workspace/repo', + repoId: 'repo-1', + type: 'pr', + number: 42 + }) + }) + it('rejects unknown repository paths', async () => { registerGitHubHandlers(store as never, stats as never) diff --git a/src/main/ipc/github.ts b/src/main/ipc/github.ts index 2a808f60325b..ae76b117c9a0 100644 --- a/src/main/ipc/github.ts +++ b/src/main/ipc/github.ts @@ -497,6 +497,44 @@ export function registerGitHubHandlers(store: Store, stats: StatsCollector): voi return dispatchWorkItem(args, repo, getWorkItemDetails, localGitOptionArgs(store, repo)[0]) }) + ipcMain.handle( + 'gh:notifyWorkItemMutated', + ( + event, + args: { + repoPath: string + repoId?: string + type: 'issue' | 'pr' + number: number + } + ) => { + const repo = args.repoId + ? store.getRepos().find((candidate) => candidate.id === args.repoId) + : assertRegisteredRepo(args, store) + if (!repo) { + return false + } + if ( + (args.type !== 'issue' && args.type !== 'pr') || + typeof args.number !== 'number' || + !Number.isInteger(args.number) || + args.number < 1 + ) { + return false + } + broadcastWorkItemMutated( + { + repoPath: repo.path, + repoId: args.repoId, + type: args.type, + number: args.number + }, + event.sender.id + ) + return true + } + ) + ipcMain.handle( 'gh:prFileContents', ( @@ -688,7 +726,7 @@ export function registerGitHubHandlers(store: Store, stats: StatsCollector): voi }) if (ok) { broadcastWorkItemMutated( - { repoPath: repo.path, type: 'pr', number: args.prNumber }, + { repoPath: repo.path, repoId: repo.id, type: 'pr', number: args.prNumber }, event.sender.id ) } diff --git a/src/preload/api-types.ts b/src/preload/api-types.ts index 7ae75a59aa31..4631dd33b6f9 100644 --- a/src/preload/api-types.ts +++ b/src/preload/api-types.ts @@ -1190,6 +1190,12 @@ export type PreloadApi = { type?: 'issue' | 'pr' } ) => Promise + notifyWorkItemMutated: (args: { + repoPath: string + repoId?: string + type: 'issue' | 'pr' + number: number + }) => Promise prFileContents: ( args: GitHubRepoSelectorArgs & { prNumber: number diff --git a/src/preload/index.ts b/src/preload/index.ts index a3adca161baf..0ca663716692 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1005,6 +1005,13 @@ const api = { type?: 'issue' | 'pr' }): Promise => ipcRenderer.invoke('gh:workItemDetails', args), + notifyWorkItemMutated: (args: { + repoPath: string + repoId?: string + type: 'issue' | 'pr' + number: number + }): Promise => ipcRenderer.invoke('gh:notifyWorkItemMutated', args), + prFileContents: (args: { repoPath: string repoId?: string diff --git a/src/renderer/src/components/GitHubItemDialog.tsx b/src/renderer/src/components/GitHubItemDialog.tsx index 1ff788a6c957..f741d28daa4b 100644 --- a/src/renderer/src/components/GitHubItemDialog.tsx +++ b/src/renderer/src/components/GitHubItemDialog.tsx @@ -159,6 +159,11 @@ import { getCommentBodySubmitState, hasBoundedCommentBodyText } from '@/lib/comment-body-submit-state' +import { + emitGitHubWorkItemDetailsCacheMutation, + onGitHubWorkItemDetailsCacheMutation +} from '@/lib/github-work-item-details-cache-events' +import { lookupGitHubWorkItemDetailsForSource } from '@/lib/github-work-item-source-lookup' import IssueSourceIndicator, { sameGitHubOwnerRepo } from '@/components/github/IssueSourceIndicator' import { getGitHubPRReviewerRows, @@ -175,6 +180,7 @@ import { GITHUB_PR_MERGE_METHOD_LABELS, resolveGitHubPRMergeMethods } from '../../../shared/github-pr-merge-methods' +import { parseExecutionHostId } from '../../../shared/execution-host' import { findGithubIssueWorkspaceAttachment, getGithubWorkItemWorkspaceAttachmentLabel @@ -200,6 +206,7 @@ import type { PRComment } from '../../../shared/types' import { + getTaskSourceCacheScope, getTaskSourceRuntimeSettings, type TaskSourceContext } from '../../../shared/task-source-context' @@ -939,12 +946,13 @@ function PRReviewersPanel({ } setSubmitting(true) try { + const runtimeRepo = sourceContext?.repoId ?? item.repoId const result = target.kind === 'environment' ? await callRuntimeRpc<{ ok: boolean; error?: string }>( target, 'github.requestPRReviewers', - { repo: item.repoId, prNumber: item.number, reviewers: logins }, + { repo: runtimeRepo, prNumber: item.number, reviewers: logins }, { timeoutMs: 30_000 } ) : await window.api.gh.requestPRReviewers({ @@ -974,6 +982,18 @@ function PRReviewersPanel({ sourceContext }) onReviewersRequested(nextReviewRequests) + if (target.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: repoPath ?? '', + repoId: item.repoId, + sourceContext, + type: 'pr', + number: item.number + }, + { local: false } + ) + } setReviewerInput('') useAppStore.getState().recordFeatureInteraction('github-tasks') toast.success( @@ -1017,12 +1037,13 @@ function PRReviewersPanel({ } setSubmitting(true) try { + const runtimeRepo = sourceContext?.repoId ?? item.repoId const result = target.kind === 'environment' ? await callRuntimeRpc<{ ok: boolean; error?: string }>( target, 'github.removePRReviewers', - { repo: item.repoId, prNumber: item.number, reviewers: logins }, + { repo: runtimeRepo, prNumber: item.number, reviewers: logins }, { timeoutMs: 30_000 } ) : await window.api.gh.removePRReviewers({ @@ -1051,6 +1072,18 @@ function PRReviewersPanel({ sourceContext }) onReviewersRequested(nextReviewRequests) + if (target.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: repoPath ?? '', + repoId: item.repoId, + sourceContext, + type: 'pr', + number: item.number + }, + { local: false } + ) + } setReviewerInput('') useAppStore.getState().recordFeatureInteraction('github-tasks') toast.success( @@ -1365,6 +1398,7 @@ function isPRFileViewed(file: GitHubPRFile): boolean { // background refetch on open. See docs/gh-work-item-drawer-cache.md. const WORK_ITEM_DETAILS_CACHE_MAX = 50 const WORK_ITEM_DETAILS_FRESH_MS = 30_000 +const WORK_ITEM_DETAILS_UNAVAILABLE_MESSAGE = 'Unable to load details for this GitHub item.' type WorkItemDetailsCacheEntry = { details: GitHubWorkItemDetails | null fetchedAt: number @@ -1394,12 +1428,16 @@ function getWorkItemDetailsCacheKey(args: { repoPath: string repoId: string issueSourcePreference: string | undefined + sourceCacheScope?: string | null type: 'issue' | 'pr' number: number }): string { // Why: include all axes that change which (repo, item) the IPC resolves to. // `\0` separator avoids ambiguity between fields that may contain `:` or `/`. - return [args.repoId, args.issueSourcePreference ?? 'auto', args.type, args.number].join('\0') + const keyParts = args.sourceCacheScope + ? [args.repoId, args.sourceCacheScope, args.issueSourcePreference ?? 'auto', args.type] + : [args.repoId, args.issueSourcePreference ?? 'auto', args.type] + return [...keyParts, args.number].join('\0') } function touchWorkItemDetailsCache(key: string, entry: WorkItemDetailsCacheEntry): void { @@ -1446,7 +1484,6 @@ function invalidateWorkItemDetailsCacheByMatch(args: { type: 'issue' | 'pr' number: number }): void { - workItemDetailsCacheGeneration += 1 const suffix = `\0${args.type}\0${args.number}` const prefix = `${args.repoId ?? args.repoPath}\0` let removed = false @@ -1457,6 +1494,7 @@ function invalidateWorkItemDetailsCacheByMatch(args: { } } if (removed) { + workItemDetailsCacheGeneration += 1 notifyWorkItemDetailsCache() } } @@ -1541,6 +1579,7 @@ function patchCachedWorkItemBody(cacheKey: string, body: string): void { // own cache when any window's mutation lands. We track the unsubscribe so // Vite HMR doesn't accumulate listeners across module reloads in dev. let workItemMutatedUnsub: (() => void) | undefined +let workItemDetailsCacheEventUnsub: (() => void) | undefined if (typeof window !== 'undefined' && window.api?.gh?.onWorkItemMutated) { workItemMutatedUnsub = window.api.gh.onWorkItemMutated((payload) => { invalidateWorkItemDetailsCacheByMatch({ @@ -1550,10 +1589,14 @@ if (typeof window !== 'undefined' && window.api?.gh?.onWorkItemMutated) { number: payload.number }) }) + workItemDetailsCacheEventUnsub = onGitHubWorkItemDetailsCacheMutation((payload) => { + invalidateWorkItemDetailsCacheByMatch(payload) + }) } if (typeof import.meta !== 'undefined' && import.meta.hot) { import.meta.hot.dispose(() => { workItemMutatedUnsub?.() + workItemDetailsCacheEventUnsub?.() }) } @@ -1650,14 +1693,20 @@ function touchPRFileContentCache( function getPRFileContentCacheKey(args: { repoPath: string repoId: string + sourceContext?: TaskSourceContext | null prNumber: number file: GitHubPRFile headSha: string baseSha: string }): string { const repositoryKey = args.repoId ? `repo:${args.repoId}` : `path:${args.repoPath}` + const sourceKey = + args.sourceContext?.provider === 'github' + ? `source:${getTaskSourceCacheScope(args.sourceContext)}` + : 'source:local' return [ repositoryKey, + sourceKey, args.prNumber, args.file.path, args.file.oldPath ?? '', @@ -1683,18 +1732,38 @@ function loadPRFileContents(args: { return Promise.resolve(cached.value) } let request: Promise - request = window.api.gh - .prFileContents({ - repoPath: args.repoPath, - repoId: args.repoId, - sourceContext: args.sourceContext, - prNumber: args.prNumber, - path: args.file.path, - oldPath: args.file.oldPath, - status: args.file.status, - headSha: args.headSha, - baseSha: args.baseSha - }) + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + request = ( + parsedHost?.kind === 'runtime' + ? callRuntimeRpc( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.prFileContents', + { + repo: args.sourceContext?.repoId ?? args.repoId, + prNumber: args.prNumber, + path: args.file.path, + oldPath: args.file.oldPath, + status: args.file.status, + headSha: args.headSha, + baseSha: args.baseSha + }, + { timeoutMs: 30_000 } + ) + : window.api.gh.prFileContents({ + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + prNumber: args.prNumber, + path: args.file.path, + oldPath: args.file.oldPath, + status: args.file.status, + headSha: args.headSha, + baseSha: args.baseSha + }) + ) .then((contents) => { if (prFileContentCache.get(cacheKey)?.value === request) { touchPRFileContentCache(cacheKey, contents) @@ -1721,6 +1790,36 @@ function addIssueCommentForRepo(args: { body: string type?: 'issue' | 'pr' }): Promise>> { + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + if (parsedHost?.kind === 'runtime') { + return callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.addIssueComment', + { + repo: args.sourceContext?.repoId ?? args.repoId, + number: args.number, + body: args.body + }, + { timeoutMs: 30_000 } + ).then((result) => { + if (result.ok) { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + type: args.type ?? 'issue', + number: args.number + }, + { local: false } + ) + } + return result + }) + } return window.api.gh.addIssueComment({ repoPath: args.repoPath, repoId: args.repoId, @@ -1742,6 +1841,40 @@ function addPRReviewCommentForRepo(args: { startLine?: number body: string }): Promise>> { + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + if (parsedHost?.kind === 'runtime') { + return callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.addPRReviewComment', + { + repo: args.sourceContext?.repoId ?? args.repoId, + prNumber: args.prNumber, + commitId: args.commitId, + path: args.path, + line: args.line, + startLine: args.startLine, + body: args.body + }, + { timeoutMs: 30_000 } + ).then((result) => { + if (result.ok) { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + type: 'pr', + number: args.prNumber + }, + { local: false } + ) + } + return result + }) + } return window.api.gh.addPRReviewComment({ repoPath: args.repoPath, repoId: args.repoId, @@ -1766,6 +1899,40 @@ function addPRReviewCommentReplyForRepo(args: { path?: string line?: number }): Promise>> { + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + if (parsedHost?.kind === 'runtime') { + return callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.addPRReviewCommentReply', + { + repo: args.sourceContext?.repoId ?? args.repoId, + prNumber: args.prNumber, + commentId: args.commentId, + body: args.body, + threadId: args.threadId, + path: args.path, + line: args.line + }, + { timeoutMs: 30_000 } + ).then((result) => { + if (result.ok) { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + type: 'pr', + number: args.prNumber + }, + { local: false } + ) + } + return result + }) + } return window.api.gh.addPRReviewCommentReply({ repoPath: args.repoPath, repoId: args.repoId, @@ -1779,8 +1946,31 @@ function addPRReviewCommentReplyForRepo(args: { }) } +function notifyWorkItemDetailsMutation( + args: { + repoPath: string + repoId?: string + sourceContext?: TaskSourceContext | null + type: 'issue' | 'pr' + number: number + }, + options: { local?: boolean } = {} +): void { + if (options.local !== false) { + emitGitHubWorkItemDetailsCacheMutation(args) + } + void window.api.gh + .notifyWorkItemMutated({ + repoPath: args.repoPath, + repoId: args.repoId, + type: args.type, + number: args.number + }) + .catch(() => undefined) +} + function setPRFileViewedForRepo(args: { - repoId?: string + repoId: string repoPath: string sourceContext?: TaskSourceContext | null prNumber: number @@ -1788,6 +1978,37 @@ function setPRFileViewedForRepo(args: { path: string viewed: boolean }): Promise { + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + if (parsedHost?.kind === 'runtime') { + return callRuntimeRpc( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.setPRFileViewed', + { + repo: args.sourceContext?.repoId ?? args.repoId, + pullRequestId: args.pullRequestId, + path: args.path, + viewed: args.viewed + }, + { timeoutMs: 30_000 } + ).then((ok) => { + if (ok) { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + type: 'pr', + number: args.prNumber + }, + { local: false } + ) + } + return ok + }) + } return window.api.gh.setPRFileViewed({ repoPath: args.repoPath, repoId: args.repoId, @@ -1799,22 +2020,6 @@ function setPRFileViewedForRepo(args: { }) } -function getWorkItemDetailsForRepo(args: { - repoId?: string - repoPath: string - sourceContext?: TaskSourceContext | null - number: number - type: 'issue' | 'pr' -}): Promise { - return window.api.gh.workItemDetails({ - repoPath: args.repoPath, - repoId: args.repoId, - sourceContext: args.sourceContext, - number: args.number, - type: args.type - }) -} - function PRViewedCheckbox({ checked, pending, @@ -3643,9 +3848,13 @@ function PRActionsPanel({ const actionItem = { ...item, state: localState } const mergePresentation = presentGitHubPRMergeState(actionItem) const mergeMethods = resolveGitHubPRMergeMethods(actionItem.mergeMethodSettings) + const sourceSettings = getTaskSourceRuntimeSettings(sourceContext) + const mergeTarget = getActiveRuntimeTarget(sourceSettings) const canMutateState = localState !== 'merged' && (!!repoPath || !!projectOrigin) const nextState: 'open' | 'closed' = localState === 'closed' ? 'open' : 'closed' - const mergeDisabled = !repoPath || mergePending || !mergePresentation.directMergeAvailable + const canMergeWithRepoContext = !!repoPath || mergeTarget.kind === 'environment' + const mergeDisabled = + !canMergeWithRepoContext || mergePending || !mergePresentation.directMergeAvailable const patchProjectRowIfNeeded = useCallback( (state: GitHubWorkItem['state']) => { @@ -3727,7 +3936,7 @@ function PRActionsPanel({ } const handleMerge = async (method: GitHubPRMergeMethod): Promise => { - if (!repoPath || mergeDisabled) { + if (mergeDisabled) { return } const label = GITHUB_PR_MERGE_METHOD_LABELS[method] @@ -3748,19 +3957,44 @@ function PRActionsPanel({ } setMergePending(true) try { - const result = await window.api.gh.mergePR({ - repoPath, - repoId: repoId ?? undefined, - sourceContext, - prNumber: item.number, - method, - prRepo: item.prRepo ?? null - }) + const result = + mergeTarget.kind === 'environment' + ? await callRuntimeRpc>>( + mergeTarget, + 'github.mergePR', + { + repo: sourceContext?.repoId ?? repoId ?? item.repoId, + prNumber: item.number, + method, + prRepo: item.prRepo ?? null + }, + { timeoutMs: 30_000 } + ) + : await window.api.gh.mergePR({ + repoPath: repoPath ?? '', + repoId: repoId ?? undefined, + sourceContext, + prNumber: item.number, + method, + prRepo: item.prRepo ?? null + }) if (!result.ok) { toast.error(result.error) return } applyStatePatch('merged') + if (mergeTarget.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: repoPath ?? '', + repoId: item.repoId, + sourceContext, + type: 'pr', + number: item.number + }, + { local: false } + ) + } useAppStore.getState().recordFeatureInteraction('github-tasks') toast.success(translate('auto.components.GitHubItemDialog.dbe5e2448e', 'Pull request merged')) onMutated() @@ -3774,25 +4008,51 @@ function PRActionsPanel({ } const handleAutoMerge = async (): Promise => { - if (!repoPath || !mergePresentation.autoMergeAction) { + if (!canMergeWithRepoContext || !mergePresentation.autoMergeAction) { return } const enabled = mergePresentation.autoMergeAction.kind === 'enable' setMergePending(true) try { - const result = await window.api.gh.setPRAutoMerge({ - repoPath, - repoId: repoId ?? undefined, - sourceContext, - prNumber: item.number, - enabled, - method: enabled ? mergeMethods.defaultMethod : undefined, - prRepo: item.prRepo ?? null - }) + const result = + mergeTarget.kind === 'environment' + ? await callRuntimeRpc>>( + mergeTarget, + 'github.setPRAutoMerge', + { + repo: sourceContext?.repoId ?? repoId ?? item.repoId, + prNumber: item.number, + enabled, + method: enabled ? mergeMethods.defaultMethod : undefined, + prRepo: item.prRepo ?? null + }, + { timeoutMs: 30_000 } + ) + : await window.api.gh.setPRAutoMerge({ + repoPath: repoPath ?? '', + repoId: repoId ?? undefined, + sourceContext, + prNumber: item.number, + enabled, + method: enabled ? mergeMethods.defaultMethod : undefined, + prRepo: item.prRepo ?? null + }) if (!result.ok) { toast.error(result.error) return } + if (mergeTarget.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: repoPath ?? '', + repoId: item.repoId, + sourceContext, + type: 'pr', + number: item.number + }, + { local: false } + ) + } useAppStore.getState().recordFeatureInteraction('github-tasks') toast.success( enabled @@ -4161,6 +4421,8 @@ function ChecksTab({ const { localChecks, expandedCheckKey, detailsByCheckKey } = resolvedChecksState const list = useMemo(() => localChecks ?? checks ?? [], [checks, localChecks]) const prRepo = useMemo(() => parseOwnerRepoFromItemUrl(item.url), [item.url]) + const parsedSourceHost = + sourceContext?.provider === 'github' ? parseExecutionHostId(sourceContext.hostId) : null const sorted = [...list].sort( (a, b) => (CHECK_SORT_ORDER[getCheckConclusion(a)] ?? 3) - @@ -4199,14 +4461,27 @@ function ChecksTab({ } setRefreshing(true) try { - const nextChecks = (await window.api.gh.prChecks({ - repoPath, - repoId: repoId ?? undefined, - sourceContext, - prNumber: item.number, - headSha, - noCache: true - })) as PRCheckDetail[] + const nextChecks = (await (parsedSourceHost?.kind === 'runtime' + ? callRuntimeRpc( + { kind: 'environment', environmentId: parsedSourceHost.environmentId }, + 'github.prChecks', + { + repo: sourceContext?.repoId ?? repoId ?? item.repoId, + prNumber: item.number, + headSha, + prRepo, + noCache: true + }, + { timeoutMs: 30_000 } + ) + : window.api.gh.prChecks({ + repoPath, + repoId: repoId ?? undefined, + sourceContext, + prNumber: item.number, + headSha, + noCache: true + }))) as PRCheckDetail[] setChecksState((current) => updateGitHubChecksTabLocalChecks(current, nextChecks)) onChecksUpdated(nextChecks) return nextChecks @@ -4220,7 +4495,17 @@ function ChecksTab({ } finally { setRefreshing(false) } - }, [headSha, item.number, onChecksUpdated, repoId, repoPath, sourceContext]) + }, [ + headSha, + item.number, + item.repoId, + onChecksUpdated, + parsedSourceHost, + prRepo, + repoId, + repoPath, + sourceContext + ]) const handleRerun = useCallback( async (failedOnly: boolean): Promise => { @@ -4229,14 +4514,27 @@ function ChecksTab({ } setRerunning(true) try { - const result = await window.api.gh.rerunPRChecks({ - repoPath, - repoId: repoId ?? undefined, - sourceContext, - prNumber: item.number, - headSha, - failedOnly - }) + const result = + parsedSourceHost?.kind === 'runtime' + ? await callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedSourceHost.environmentId }, + 'github.rerunPRChecks', + { + repo: sourceContext?.repoId ?? repoId ?? item.repoId, + prNumber: item.number, + headSha, + failedOnly + }, + { timeoutMs: 30_000 } + ) + : await window.api.gh.rerunPRChecks({ + repoPath, + repoId: repoId ?? undefined, + sourceContext, + prNumber: item.number, + headSha, + failedOnly + }) if (!result.ok) { toast.error(result.error) return @@ -4257,7 +4555,17 @@ function ChecksTab({ setRerunning(false) } }, - [handleRefresh, headSha, item.number, rerunning, repoId, repoPath, sourceContext] + [ + handleRefresh, + headSha, + item.number, + item.repoId, + parsedSourceHost, + rerunning, + repoId, + repoPath, + sourceContext + ] ) const handleFixBrokenChecks = useCallback(async (): Promise => { @@ -4337,16 +4645,32 @@ function ChecksTab({ error: null }) ) - void window.api.gh - .prCheckDetails({ - repoPath, - repoId: repoId ?? undefined, - checkRunId: check.checkRunId, - workflowRunId: check.workflowRunId, - checkName: check.name, - url: check.url, - prRepo - }) + const detailsRequest = + parsedSourceHost?.kind === 'runtime' + ? callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedSourceHost.environmentId }, + 'github.prCheckDetails', + { + repo: sourceContext?.repoId ?? repoId ?? item.repoId, + checkRunId: check.checkRunId, + workflowRunId: check.workflowRunId, + checkName: check.name, + url: check.url, + prRepo + }, + { timeoutMs: 30_000 } + ) + : window.api.gh.prCheckDetails({ + repoPath, + repoId: repoId ?? undefined, + sourceContext, + checkRunId: check.checkRunId, + workflowRunId: check.workflowRunId, + checkName: check.name, + url: check.url, + prRepo + }) + void detailsRequest .then((details) => { if (!mountedRef.current) { return @@ -4372,7 +4696,16 @@ function ChecksTab({ ) }) }, - [detailsByCheckKey, mountedRef, prRepo, repoId, repoPath] + [ + detailsByCheckKey, + item.repoId, + mountedRef, + parsedSourceHost, + prRepo, + repoId, + repoPath, + sourceContext + ] ) const refreshAction = ( @@ -4891,7 +5224,11 @@ async function runIssueUpdate(args: { updates: Parameters[0]['updates'] }): Promise { if (args.projectOrigin) { - const target = getActiveRuntimeTarget(getGitHubMutationSettings(args.repoId)) + const targetSettings = + args.sourceContext?.provider === 'github' + ? getTaskSourceRuntimeSettings(args.sourceContext) + : getGitHubMutationSettings(args.repoId) + const target = getActiveRuntimeTarget(targetSettings) const updateArgs = { owner: args.projectOrigin.owner, repo: args.projectOrigin.repo, @@ -4912,21 +5249,61 @@ async function runIssueUpdate(args: { if (!res.ok) { throw new Error(res.error.message) } + if (target.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath ?? '', + repoId: args.repoId ?? undefined, + sourceContext: args.sourceContext, + type: 'issue', + number: args.number + }, + { local: false } + ) + } return } if (!args.repoPath) { throw new Error('No repo context available for this edit.') } - const res = await window.api.gh.updateIssue({ - repoPath: args.repoPath, - repoId: args.repoId ?? undefined, - sourceContext: args.sourceContext, - number: args.number, - updates: args.updates - }) + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + const res = + parsedHost?.kind === 'runtime' + ? await callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.updateIssue', + { + repo: args.sourceContext?.repoId ?? args.repoId, + number: args.number, + updates: args.updates + }, + { timeoutMs: 30_000 } + ) + : await window.api.gh.updateIssue({ + repoPath: args.repoPath, + repoId: args.repoId ?? undefined, + sourceContext: args.sourceContext, + number: args.number, + updates: args.updates + }) if (!res.ok) { throw new Error(res.error) } + if (parsedHost?.kind === 'runtime') { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId ?? undefined, + sourceContext: args.sourceContext, + type: 'issue', + number: args.number + }, + { local: false } + ) + } } async function runWorkItemBodyUpdate(args: { @@ -4944,7 +5321,11 @@ async function runWorkItemBodyUpdate(args: { if (!targetSlug) { throw new Error('No GitHub repository context available for this pull request.') } - const target = getActiveRuntimeTarget(getGitHubMutationSettings(args.item.repoId)) + const targetSettings = + args.sourceContext?.provider === 'github' + ? getTaskSourceRuntimeSettings(args.sourceContext) + : getGitHubMutationSettings(args.item.repoId) + const target = getActiveRuntimeTarget(targetSettings) const updateArgs = { owner: targetSlug.owner, repo: targetSlug.repo, @@ -4965,6 +5346,18 @@ async function runWorkItemBodyUpdate(args: { if (!res.ok) { throw new Error(res.error.message) } + if (target.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath ?? '', + repoId: args.item.repoId, + sourceContext: args.sourceContext, + type: 'pr', + number: args.item.number + }, + { local: false } + ) + } return } @@ -4987,7 +5380,11 @@ async function runPullRequestStateUpdate(args: { updates: { state: 'open' | 'closed' } }): Promise { if (args.projectOrigin) { - const target = getActiveRuntimeTarget(getGitHubMutationSettings(args.repoId)) + const targetSettings = + args.sourceContext?.provider === 'github' + ? getTaskSourceRuntimeSettings(args.sourceContext) + : getGitHubMutationSettings(args.repoId) + const target = getActiveRuntimeTarget(targetSettings) const updateArgs = { owner: args.projectOrigin.owner, repo: args.projectOrigin.repo, @@ -5008,21 +5405,61 @@ async function runPullRequestStateUpdate(args: { if (!res.ok) { throw new Error(res.error.message) } + if (target.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath ?? '', + repoId: args.repoId ?? undefined, + sourceContext: args.sourceContext, + type: 'pr', + number: args.number + }, + { local: false } + ) + } return } if (!args.repoPath) { throw new Error('No repo context available for this pull request.') } - const res = await window.api.gh.updatePRState({ - repoPath: args.repoPath, - repoId: args.repoId ?? undefined, - sourceContext: args.sourceContext, - prNumber: args.number, - updates: args.updates - }) + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + const res = + parsedHost?.kind === 'runtime' + ? await callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.updatePRState', + { + repo: args.sourceContext?.repoId ?? args.repoId, + prNumber: args.number, + updates: args.updates + }, + { timeoutMs: 30_000 } + ) + : await window.api.gh.updatePRState({ + repoPath: args.repoPath, + repoId: args.repoId ?? undefined, + sourceContext: args.sourceContext, + prNumber: args.number, + updates: args.updates + }) if (!res.ok) { throw new Error(res.error) } + if (parsedHost?.kind === 'runtime') { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId ?? undefined, + sourceContext: args.sourceContext, + type: 'pr', + number: args.number + }, + { local: false } + ) + } } function GitHubLabelsSettingsLink({ @@ -6386,10 +6823,12 @@ export default function GitHubItemDialog({ repoPath, repoId: effectiveRepoId, issueSourcePreference, + sourceCacheScope: + sourceContext?.provider === 'github' ? getTaskSourceCacheScope(sourceContext) : null, type: workItem.type, number: workItem.number }) - }, [repoPath, effectiveRepoId, workItem, issueSourcePreference]) + }, [repoPath, effectiveRepoId, sourceContext, workItem, issueSourcePreference]) // Why: track comments added optimistically before the detail fetch resolves // so they can be merged into the fetch result instead of being overwritten. @@ -6503,9 +6942,7 @@ export default function GitHubItemDialog({ const loading = !!cachedEntry?.pending && !cachedEntry?.details const error = cachedEntry?.error && !cachedEntry?.details ? cachedEntry.error : null - const detailsLoaded = - Boolean(cachedEntry?.details) || - Boolean(cachedEntry && !cachedEntry.pending && !cachedEntry.error && cachedEntry.fetchedAt > 0) + const detailsLoaded = Boolean(cachedEntry?.details) // Why: if a cross-window mutation invalidates the open drawer's entry // (cachedEntry becomes undefined while workItem is still set), the main @@ -6519,7 +6956,7 @@ export default function GitHubItemDialog({ }, [workItem, detailsCacheKey, cachedEntry]) useEffect(() => { - if (!workItem || !repoPath || !detailsCacheKey) { + if (!workItem || !repoPath || !effectiveRepoId || !detailsCacheKey) { return } // Why: only clear optimistic comments when switching to a genuinely @@ -6546,9 +6983,9 @@ export default function GitHubItemDialog({ // racing two `gh` subprocesses against each other. const inflight: Promise = cached?.pending ?? - getWorkItemDetailsForRepo({ + lookupGitHubWorkItemDetailsForSource({ repoPath, - repoId: effectiveRepoId ?? undefined, + repoId: effectiveRepoId, sourceContext, number: workItem.number, type: workItem.type @@ -6577,15 +7014,19 @@ export default function GitHubItemDialog({ // entry still exists (later open repopulated it) leave it alone too. return } - // Why: 404/unauthorized must not overwrite valid cached data. When the - // IPC resolves to null and we already have cached details, keep the - // stale data — only blank entries get the null payload. + // Why: null means unavailable/not found, not loaded empty content. if (result === null && prev?.details) { touchWorkItemDetailsCache(detailsCacheKey, { details: prev.details, fetchedAt: prev.fetchedAt, error: undefined }) + } else if (result === null) { + touchWorkItemDetailsCache(detailsCacheKey, { + details: null, + fetchedAt: 0, + error: WORK_ITEM_DETAILS_UNAVAILABLE_MESSAGE + }) } else { touchWorkItemDetailsCache(detailsCacheKey, { details: result, diff --git a/src/renderer/src/components/PullRequestPage.tsx b/src/renderer/src/components/PullRequestPage.tsx index c42fdff91196..408695a8fd4b 100644 --- a/src/renderer/src/components/PullRequestPage.tsx +++ b/src/renderer/src/components/PullRequestPage.tsx @@ -160,11 +160,17 @@ import { getCommentBodySubmitState, hasBoundedCommentBodyText } from '@/lib/comment-body-submit-state' +import { + emitGitHubWorkItemDetailsCacheMutation, + onGitHubWorkItemDetailsCacheMutation +} from '@/lib/github-work-item-details-cache-events' +import { lookupGitHubWorkItemDetailsForSource } from '@/lib/github-work-item-source-lookup' import { presentGitHubPRMergeState } from '@/components/github-pr-merge-state' import { GITHUB_PR_MERGE_METHOD_LABELS, resolveGitHubPRMergeMethods } from '../../../shared/github-pr-merge-methods' +import { parseExecutionHostId } from '../../../shared/execution-host' import { findGithubPrWorkspaceAttachment, getGithubPrWorkspaceAttachmentLabel @@ -201,6 +207,11 @@ import type { PRCheckDetail, PRComment } from '../../../shared/types' +import { + getTaskSourceCacheScope, + getTaskSourceRuntimeSettings, + type TaskSourceContext +} from '../../../shared/task-source-context' import { translate } from '@/i18n/i18n' import { getSettingsForRepoRuntimeOwner } from '@/lib/repo-runtime-owner' @@ -288,6 +299,7 @@ type PullRequestPageProps = { workItem: GitHubWorkItem | null repoPath: string | null repoId?: string | null + sourceContext?: TaskSourceContext | null initialTab?: ItemDialogTab backLabel?: string /** Called when the user clicks the primary CTA to start work from this item. */ @@ -503,11 +515,13 @@ function PRAssigneesPanel({ item, repoPath, projectOrigin, + sourceContext, onMutated }: { item: GitHubWorkItem repoPath: string | null projectOrigin: PullRequestPageProjectOrigin | undefined + sourceContext?: TaskSourceContext | null onMutated: () => void }): React.JSX.Element { const [assigneePopoverOpen, setAssigneePopoverOpen] = useState(false) @@ -524,6 +538,16 @@ function PRAssigneesPanel({ const repoOwnerSettings = useAppStore( useShallow((s) => getSettingsForRepoRuntimeOwner(s, item.repoId ?? null)) ) + const sourceSettings = useMemo( + () => + sourceContext?.provider === 'github' + ? ({ + ...repoOwnerSettings, + ...getTaskSourceRuntimeSettings(sourceContext) + } as typeof repoOwnerSettings) + : repoOwnerSettings, + [repoOwnerSettings, sourceContext] + ) const { isPending, run } = useImmediateMutation() // Why: PR assignees can change through background refetches; sync them @@ -554,9 +578,9 @@ function PRAssigneesPanel({ slugOwner, slugRepo, assigneeLogins, - repoOwnerSettings + sourceSettings ) - const repoAssigneesByPath = useRepoAssignees(repoPath, item.repoId, repoOwnerSettings) + const repoAssigneesByPath = useRepoAssignees(repoPath, item.repoId, sourceSettings) const repoAssignees = slugOwner && slugRepo ? repoAssigneesBySlug : repoAssigneesByPath const canEditAssignees = Boolean(projectOrigin || repoPath) const assigneesByLogin = useMemo( @@ -581,18 +605,19 @@ function PRAssigneesPanel({ runIssueUpdate({ repoId: item.repoId, repoPath, + sourceContext, projectOrigin, number: item.number, updates: isAssigned ? { removeAssignees: [login] } : { addAssignees: [login] } }), onOptimistic: () => { setLocalAssignees(nextAssignees) - patchWorkItem(item.id, { assignees: nextAssignees }, item.repoId) + patchWorkItem(item.id, { assignees: nextAssignees }, item.repoId, { sourceContext }) patchProjectRowIfNeeded(nextLogins) }, onRevert: () => { setLocalAssignees(prevAssignees) - patchWorkItem(item.id, { assignees: prevAssignees }, item.repoId) + patchWorkItem(item.id, { assignees: prevAssignees }, item.repoId, { sourceContext }) patchProjectRowIfNeeded(prevLogins) }, onSuccess: () => { @@ -613,7 +638,8 @@ function PRAssigneesPanel({ patchWorkItem, projectOrigin, repoPath, - run + run, + sourceContext ] ) @@ -719,11 +745,13 @@ function PRReviewersPanel({ item, loading, repoPath, + sourceContext, onReviewersRequested }: { item: GitHubWorkItem loading: boolean repoPath: string | null + sourceContext?: TaskSourceContext | null onReviewersRequested: (reviewRequests: GitHubAssignableUser[]) => void }): React.JSX.Element { const [open, setOpen] = useState(false) @@ -745,6 +773,16 @@ function PRReviewersPanel({ const repoOwnerSettings = useAppStore( useShallow((s) => getSettingsForRepoRuntimeOwner(s, item.repoId ?? null)) ) + const sourceSettings = useMemo( + () => + sourceContext?.provider === 'github' + ? ({ + ...repoOwnerSettings, + ...getTaskSourceRuntimeSettings(sourceContext) + } as typeof repoOwnerSettings) + : repoOwnerSettings, + [repoOwnerSettings, sourceContext] + ) const reviewerInputRef = useRef(null) const reviewerInputFocusFrameRef = useRef(null) const reviewerPanelMountedRef = useRef(true) @@ -819,12 +857,12 @@ function PRReviewersPanel({ open && reviewSlug ? reviewSlug.owner : null, open && reviewSlug ? reviewSlug.repo : null, reviewerSeedUsers.map((user) => user.login), - repoOwnerSettings + sourceSettings ) const reviewerMetadataByPath = useRepoAssignees( open && !reviewSlug ? repoPath : null, open && !reviewSlug ? item.repoId : null, - repoOwnerSettings + sourceSettings ) const reviewerMetadata = reviewSlug ? reviewerMetadataBySlug : reviewerMetadataByPath const displayItem = { ...item, reviewRequests: localReviewRequests } @@ -915,7 +953,7 @@ function PRReviewersPanel({ item.reviewRequests !== undefined || item.latestReviews !== undefined const canRequestReview = - !!repoPath || getActiveRuntimeTarget(repoOwnerSettings).kind === 'environment' + !!repoPath || getActiveRuntimeTarget(sourceSettings).kind === 'environment' const handleRequestReview = async (requestedLogins?: string[]): Promise => { if (submitting) { @@ -938,7 +976,7 @@ function PRReviewersPanel({ ) return } - const target = getActiveRuntimeTarget(repoOwnerSettings) + const target = getActiveRuntimeTarget(sourceSettings) if (target.kind !== 'environment' && !repoPath) { toast.error( translate( @@ -950,17 +988,19 @@ function PRReviewersPanel({ } setSubmitting(true) try { + const runtimeRepo = sourceContext?.repoId ?? item.repoId const result = target.kind === 'environment' ? await callRuntimeRpc<{ ok: boolean; error?: string }>( target, 'github.requestPRReviewers', - { repo: item.repoId, prNumber: item.number, reviewers: logins }, + { repo: runtimeRepo, prNumber: item.number, reviewers: logins }, { timeoutMs: 30_000 } ) : await window.api.gh.requestPRReviewers({ repoPath: repoPath ?? '', repoId: item.repoId, + sourceContext, prNumber: item.number, reviewers: logins }) @@ -980,8 +1020,22 @@ function PRReviewersPanel({ localReviewRequests ) setLocalReviewRequests(nextReviewRequests) - patchWorkItem(item.id, { reviewRequests: nextReviewRequests }, item.repoId) + patchWorkItem(item.id, { reviewRequests: nextReviewRequests }, item.repoId, { + sourceContext + }) onReviewersRequested(nextReviewRequests) + if (target.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: repoPath ?? '', + repoId: item.repoId, + sourceContext, + type: 'pr', + number: item.number + }, + { local: false } + ) + } setReviewerInput('') toast.success( logins.length === 1 @@ -1012,7 +1066,7 @@ function PRReviewersPanel({ if (logins.length === 0) { return } - const target = getActiveRuntimeTarget(repoOwnerSettings) + const target = getActiveRuntimeTarget(sourceSettings) if (target.kind !== 'environment' && !repoPath) { toast.error( translate( @@ -1024,17 +1078,19 @@ function PRReviewersPanel({ } setSubmitting(true) try { + const runtimeRepo = sourceContext?.repoId ?? item.repoId const result = target.kind === 'environment' ? await callRuntimeRpc<{ ok: boolean; error?: string }>( target, 'github.removePRReviewers', - { repo: item.repoId, prNumber: item.number, reviewers: logins }, + { repo: runtimeRepo, prNumber: item.number, reviewers: logins }, { timeoutMs: 30_000 } ) : await window.api.gh.removePRReviewers({ repoPath: repoPath ?? '', repoId: item.repoId, + sourceContext, prNumber: item.number, reviewers: logins }) @@ -1053,8 +1109,22 @@ function PRReviewersPanel({ (reviewer) => !removed.has(reviewer.login.toLowerCase()) ) setLocalReviewRequests(nextReviewRequests) - patchWorkItem(item.id, { reviewRequests: nextReviewRequests }, item.repoId) + patchWorkItem(item.id, { reviewRequests: nextReviewRequests }, item.repoId, { + sourceContext + }) onReviewersRequested(nextReviewRequests) + if (target.kind === 'environment') { + notifyWorkItemDetailsMutation( + { + repoPath: repoPath ?? '', + repoId: item.repoId, + sourceContext, + type: 'pr', + number: item.number + }, + { local: false } + ) + } setReviewerInput('') toast.success( logins.length === 1 @@ -1368,6 +1438,7 @@ function isPRFileViewed(file: GitHubPRFile): boolean { // background refetch on open. See docs/gh-work-item-drawer-cache.md. const WORK_ITEM_DETAILS_CACHE_MAX = 50 const WORK_ITEM_DETAILS_FRESH_MS = 30_000 +const WORK_ITEM_DETAILS_UNAVAILABLE_MESSAGE = 'Unable to load details for this GitHub item.' type WorkItemDetailsCacheEntry = { details: GitHubWorkItemDetails | null fetchedAt: number @@ -1397,12 +1468,16 @@ function getWorkItemDetailsCacheKey(args: { repoPath: string repoId: string issueSourcePreference: string | undefined + sourceCacheScope?: string | null type: 'issue' | 'pr' number: number }): string { // Why: include all axes that change which (repo, item) the IPC resolves to. // `\0` separator avoids ambiguity between fields that may contain `:` or `/`. - return [args.repoId, args.issueSourcePreference ?? 'auto', args.type, args.number].join('\0') + const keyParts = args.sourceCacheScope + ? [args.repoId, args.sourceCacheScope, args.issueSourcePreference ?? 'auto', args.type] + : [args.repoId, args.issueSourcePreference ?? 'auto', args.type] + return [...keyParts, args.number].join('\0') } function touchWorkItemDetailsCache(key: string, entry: WorkItemDetailsCacheEntry): void { @@ -1449,7 +1524,6 @@ function invalidateWorkItemDetailsCacheByMatch(args: { type: 'issue' | 'pr' number: number }): void { - workItemDetailsCacheGeneration += 1 const suffix = `\0${args.type}\0${args.number}` const prefix = `${args.repoId ?? args.repoPath}\0` let removed = false @@ -1460,6 +1534,7 @@ function invalidateWorkItemDetailsCacheByMatch(args: { } } if (removed) { + workItemDetailsCacheGeneration += 1 notifyWorkItemDetailsCache() } } @@ -1544,6 +1619,7 @@ function patchCachedWorkItemBody(cacheKey: string, body: string): void { // own cache when any window's mutation lands. We track the unsubscribe so // Vite HMR doesn't accumulate listeners across module reloads in dev. let workItemMutatedUnsub: (() => void) | undefined +let workItemDetailsCacheEventUnsub: (() => void) | undefined if (typeof window !== 'undefined' && window.api?.gh?.onWorkItemMutated) { workItemMutatedUnsub = window.api.gh.onWorkItemMutated((payload) => { invalidateWorkItemDetailsCacheByMatch({ @@ -1553,10 +1629,14 @@ if (typeof window !== 'undefined' && window.api?.gh?.onWorkItemMutated) { number: payload.number }) }) + workItemDetailsCacheEventUnsub = onGitHubWorkItemDetailsCacheMutation((payload) => { + invalidateWorkItemDetailsCacheByMatch(payload) + }) } if (typeof import.meta !== 'undefined' && import.meta.hot) { import.meta.hot.dispose(() => { workItemMutatedUnsub?.() + workItemDetailsCacheEventUnsub?.() }) } @@ -1653,14 +1733,20 @@ function touchPRFileContentCache( function getPRFileContentCacheKey(args: { repoPath: string repoId: string + sourceContext?: TaskSourceContext | null prNumber: number file: GitHubPRFile headSha: string baseSha: string }): string { const repositoryKey = args.repoId ? `repo:${args.repoId}` : `path:${args.repoPath}` + const sourceKey = + args.sourceContext?.provider === 'github' + ? `source:${getTaskSourceCacheScope(args.sourceContext)}` + : 'source:local' return [ repositoryKey, + sourceKey, args.prNumber, args.file.path, args.file.oldPath ?? '', @@ -1673,6 +1759,7 @@ function getPRFileContentCacheKey(args: { function loadPRFileContents(args: { repoPath: string repoId: string + sourceContext?: TaskSourceContext | null prNumber: number file: GitHubPRFile headSha: string @@ -1685,17 +1772,38 @@ function loadPRFileContents(args: { return Promise.resolve(cached.value) } let request: Promise - request = window.api.gh - .prFileContents({ - repoPath: args.repoPath, - repoId: args.repoId, - prNumber: args.prNumber, - path: args.file.path, - oldPath: args.file.oldPath, - status: args.file.status, - headSha: args.headSha, - baseSha: args.baseSha - }) + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + request = ( + parsedHost?.kind === 'runtime' + ? callRuntimeRpc( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.prFileContents', + { + repo: args.sourceContext?.repoId ?? args.repoId, + prNumber: args.prNumber, + path: args.file.path, + oldPath: args.file.oldPath, + status: args.file.status, + headSha: args.headSha, + baseSha: args.baseSha + }, + { timeoutMs: 30_000 } + ) + : window.api.gh.prFileContents({ + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + prNumber: args.prNumber, + path: args.file.path, + oldPath: args.file.oldPath, + status: args.file.status, + headSha: args.headSha, + baseSha: args.baseSha + }) + ) .then((contents) => { if (prFileContentCache.get(cacheKey)?.value === request) { touchPRFileContentCache(cacheKey, contents) @@ -1717,13 +1825,45 @@ function loadPRFileContents(args: { function addIssueCommentForRepo(args: { repoId?: string repoPath: string + sourceContext?: TaskSourceContext | null number: number body: string type?: 'issue' | 'pr' }): Promise>> { + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + if (parsedHost?.kind === 'runtime') { + return callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.addIssueComment', + { + repo: args.sourceContext?.repoId ?? args.repoId, + number: args.number, + body: args.body + }, + { timeoutMs: 30_000 } + ).then((result) => { + if (result.ok) { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + type: args.type ?? 'issue', + number: args.number + }, + { local: false } + ) + } + return result + }) + } return window.api.gh.addIssueComment({ repoPath: args.repoPath, repoId: args.repoId, + sourceContext: args.sourceContext, number: args.number, body: args.body, type: args.type @@ -1733,6 +1873,7 @@ function addIssueCommentForRepo(args: { function addPRReviewCommentForRepo(args: { repoId?: string repoPath: string + sourceContext?: TaskSourceContext | null prNumber: number commitId: string path: string @@ -1740,9 +1881,44 @@ function addPRReviewCommentForRepo(args: { startLine?: number body: string }): Promise>> { + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + if (parsedHost?.kind === 'runtime') { + return callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.addPRReviewComment', + { + repo: args.sourceContext?.repoId ?? args.repoId, + prNumber: args.prNumber, + commitId: args.commitId, + path: args.path, + line: args.line, + startLine: args.startLine, + body: args.body + }, + { timeoutMs: 30_000 } + ).then((result) => { + if (result.ok) { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + type: 'pr', + number: args.prNumber + }, + { local: false } + ) + } + return result + }) + } return window.api.gh.addPRReviewComment({ repoPath: args.repoPath, repoId: args.repoId, + sourceContext: args.sourceContext, prNumber: args.prNumber, commitId: args.commitId, path: args.path, @@ -1755,6 +1931,7 @@ function addPRReviewCommentForRepo(args: { function addPRReviewCommentReplyForRepo(args: { repoId?: string repoPath: string + sourceContext?: TaskSourceContext | null prNumber: number commentId: number body: string @@ -1762,9 +1939,44 @@ function addPRReviewCommentReplyForRepo(args: { path?: string line?: number }): Promise>> { + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + if (parsedHost?.kind === 'runtime') { + return callRuntimeRpc>>( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.addPRReviewCommentReply', + { + repo: args.sourceContext?.repoId ?? args.repoId, + prNumber: args.prNumber, + commentId: args.commentId, + body: args.body, + threadId: args.threadId, + path: args.path, + line: args.line + }, + { timeoutMs: 30_000 } + ).then((result) => { + if (result.ok) { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + type: 'pr', + number: args.prNumber + }, + { local: false } + ) + } + return result + }) + } return window.api.gh.addPRReviewCommentReply({ repoPath: args.repoPath, repoId: args.repoId, + sourceContext: args.sourceContext, prNumber: args.prNumber, commentId: args.commentId, body: args.body, @@ -1774,17 +1986,73 @@ function addPRReviewCommentReplyForRepo(args: { }) } +function notifyWorkItemDetailsMutation( + args: { + repoPath: string + repoId?: string + sourceContext?: TaskSourceContext | null + type: 'issue' | 'pr' + number: number + }, + options: { local?: boolean } = {} +): void { + if (options.local !== false) { + emitGitHubWorkItemDetailsCacheMutation(args) + } + void window.api.gh + .notifyWorkItemMutated({ + repoPath: args.repoPath, + repoId: args.repoId, + type: args.type, + number: args.number + }) + .catch(() => undefined) +} + function setPRFileViewedForRepo(args: { - repoId?: string + repoId: string repoPath: string + sourceContext?: TaskSourceContext | null prNumber: number pullRequestId: string path: string viewed: boolean }): Promise { + const parsedHost = + args.sourceContext?.provider === 'github' + ? parseExecutionHostId(args.sourceContext.hostId) + : null + if (parsedHost?.kind === 'runtime') { + return callRuntimeRpc( + { kind: 'environment', environmentId: parsedHost.environmentId }, + 'github.setPRFileViewed', + { + repo: args.sourceContext?.repoId ?? args.repoId, + pullRequestId: args.pullRequestId, + path: args.path, + viewed: args.viewed + }, + { timeoutMs: 30_000 } + ).then((ok) => { + if (ok) { + notifyWorkItemDetailsMutation( + { + repoPath: args.repoPath, + repoId: args.repoId, + sourceContext: args.sourceContext, + type: 'pr', + number: args.prNumber + }, + { local: false } + ) + } + return ok + }) + } return window.api.gh.setPRFileViewed({ repoPath: args.repoPath, repoId: args.repoId, + sourceContext: args.sourceContext, prNumber: args.prNumber, pullRequestId: args.pullRequestId, path: args.path, @@ -1792,20 +2060,6 @@ function setPRFileViewedForRepo(args: { }) } -function getWorkItemDetailsForRepo(args: { - repoId?: string - repoPath: string - number: number - type: 'issue' | 'pr' -}): Promise { - return window.api.gh.workItemDetails({ - repoPath: args.repoPath, - repoId: args.repoId, - number: args.number, - type: args.type - }) -} - function PRViewedCheckbox({ checked, pending, @@ -1971,6 +2225,7 @@ type PRFilesCombinedDiffViewerProps = { comments: PRComment[] repoPath: string repoId: string + sourceContext?: TaskSourceContext | null prNumber: number prUrl: string headSha: string | undefined @@ -1985,6 +2240,7 @@ function PRFilesCombinedDiffViewer({ comments, repoPath, repoId, + sourceContext, prNumber, prUrl, headSha, @@ -2184,6 +2440,7 @@ function PRFilesCombinedDiffViewer({ const contents = await loadPRFileContents({ repoPath, repoId, + sourceContext, prNumber, file, headSha, @@ -2233,7 +2490,7 @@ function PRFilesCombinedDiffViewer({ ) }) }, - [baseSha, fileByPath, headSha, prNumber, repoId, repoPath] + [baseSha, fileByPath, headSha, prNumber, repoId, repoPath, sourceContext] ) const retrySection = useCallback( @@ -2466,6 +2723,7 @@ function PRFilesCombinedDiffViewer({ const result = await addPRReviewCommentForRepo({ repoPath, repoId, + sourceContext, prNumber, commitId: headSha, path: section.path, @@ -2486,7 +2744,7 @@ function PRFilesCombinedDiffViewer({ ) return true }, - [headSha, onCommentAdded, prNumber, repoId, repoPath] + [headSha, onCommentAdded, prNumber, repoId, repoPath, sourceContext] ) const renderViewedCheckbox = useCallback( @@ -2632,6 +2890,7 @@ function CommentCodeContext({ comment, repoPath, repoId, + sourceContext, prNumber, files, headSha, @@ -2640,6 +2899,7 @@ function CommentCodeContext({ comment: PRComment repoPath: string | null repoId: string + sourceContext?: TaskSourceContext | null prNumber: number files: GitHubPRFile[] headSha: string | undefined @@ -2664,7 +2924,7 @@ function CommentCodeContext({ return } let cancelled = false - loadPRFileContents({ repoPath, repoId, prNumber, file, headSha, baseSha }) + loadPRFileContents({ repoPath, repoId, sourceContext, prNumber, file, headSha, baseSha }) .then((result) => { if (!cancelled) { setContents(result) @@ -2678,7 +2938,7 @@ function CommentCodeContext({ return () => { cancelled = true } - }, [baseSha, file, headSha, line, prNumber, repoId, repoPath]) + }, [baseSha, file, headSha, line, prNumber, repoId, repoPath, sourceContext]) const resolvedContextExpansionState = resolveCommentCodeContextExpansionState( contextExpansionState, @@ -2931,6 +3191,7 @@ function ConversationTab({ item, repoPath, repoId, + sourceContext, body, comments, files, @@ -2952,6 +3213,7 @@ function ConversationTab({ item: GitHubWorkItem repoPath: string | null repoId: string | null + sourceContext?: TaskSourceContext | null body: string comments: PRComment[] files: GitHubPRFile[] @@ -2981,7 +3243,17 @@ function ConversationTab({ const repoOwnerSettings = useAppStore( useShallow((s) => getSettingsForRepoRuntimeOwner(s, item.repoId ?? repoId ?? null)) ) - const repoAssignees = useRepoAssignees(repoPath, item.repoId, repoOwnerSettings) + const sourceSettings = useMemo( + () => + sourceContext?.provider === 'github' + ? ({ + ...repoOwnerSettings, + ...getTaskSourceRuntimeSettings(sourceContext) + } as typeof repoOwnerSettings) + : repoOwnerSettings, + [repoOwnerSettings, sourceContext] + ) + const repoAssignees = useRepoAssignees(repoPath, item.repoId, sourceSettings) const commentCounts = useMemo(() => getPRCommentAudienceCounts(comments), [comments]) const visibleComments = useMemo( () => filterPRCommentsByAudience(comments, commentFilter), @@ -3052,6 +3324,7 @@ function ConversationTab({ await runWorkItemBodyUpdate({ item, repoPath, + sourceContext, projectOrigin, body: resolvedBodyDraft, parsedSlug: bodySlug @@ -3076,7 +3349,8 @@ function ConversationTab({ item, onBodyUpdated, projectOrigin, - repoPath + repoPath, + sourceContext ]) const handleReply = useCallback( @@ -3095,6 +3369,7 @@ function ConversationTab({ ? await addPRReviewCommentReplyForRepo({ repoPath, repoId: item.repoId, + sourceContext, prNumber: item.number, commentId: comment.id, body: replyBody, @@ -3105,6 +3380,7 @@ function ConversationTab({ : await addIssueCommentForRepo({ repoPath, repoId: item.repoId, + sourceContext, number: item.number, body: `@${comment.author} ${replyBody}`, type: item.type @@ -3122,7 +3398,7 @@ function ConversationTab({ toast.success(translate('auto.components.PullRequestPage.11505c7a71', 'Reply posted.')) return true }, - [item.number, item.repoId, item.type, onCommentAdded, repoPath] + [item.number, item.repoId, item.type, onCommentAdded, repoPath, sourceContext] ) const rightPanel = @@ -3132,6 +3408,7 @@ function ConversationTab({ item={item} repoPath={repoPath} repoId={item.repoId} + sourceContext={sourceContext} projectOrigin={projectOrigin} localState={localState} onStateChange={onStateChange} @@ -3141,12 +3418,14 @@ function ConversationTab({ item={item} repoPath={repoPath} projectOrigin={projectOrigin} + sourceContext={sourceContext} onMutated={onMutated} />