Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 2 additions & 29 deletions src/main/git/remote-name-listing.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

const {
gitExecFileAsyncMock,
getSshGitProviderMock,
getSshGitProviderGenerationMock,
readLocalGitConfigSignatureMock
} = vi.hoisted(() => ({
const { gitExecFileAsyncMock, readLocalGitConfigSignatureMock } = vi.hoisted(() => ({
gitExecFileAsyncMock: vi.fn(),
getSshGitProviderMock: vi.fn(),
getSshGitProviderGenerationMock: vi.fn(() => 0),
readLocalGitConfigSignatureMock: vi.fn<() => Promise<string | undefined>>(async () => 'sig-1')
}))

vi.mock('./runner', () => ({ gitExecFileAsync: gitExecFileAsyncMock }))
vi.mock('../providers/ssh-git-dispatch', () => ({
getSshGitProvider: getSshGitProviderMock,
getSshGitProviderGeneration: getSshGitProviderGenerationMock
}))
vi.mock('../github/local-git-config-signature', () => ({
readLocalGitConfigSignature: readLocalGitConfigSignatureMock
}))
Expand All @@ -38,9 +27,6 @@ describe('cached git remote name listing', () => {
beforeEach(() => {
_resetRemoteNameListingCache()
gitExecFileAsyncMock.mockReset()
getSshGitProviderMock.mockReset()
getSshGitProviderGenerationMock.mockReset()
getSshGitProviderGenerationMock.mockReturnValue(0)
readLocalGitConfigSignatureMock.mockReset()
readLocalGitConfigSignatureMock.mockImplementation(async () => 'sig-1')
})
Expand Down Expand Up @@ -180,20 +166,7 @@ describe('cached git remote name listing', () => {
})
})

it('lists remotes through the SSH git provider', async () => {
const exec = vi.fn(async () => ({ stdout: 'origin\n', stderr: '' }))
getSshGitProviderMock.mockReturnValue({ exec })

await expect(shouldProbeGitRemote('/remote/repo', 'upstream', 'ssh-1')).resolves.toBe(false)
expect(gitExecFileAsyncMock).not.toHaveBeenCalled()
expect(exec).toHaveBeenCalledWith(['remote'], '/remote/repo', {
signal: expect.any(AbortSignal)
})
})

it('fails open when the SSH git provider is missing instead of listing locally', async () => {
getSshGitProviderMock.mockReturnValue(undefined)

it('fails open for an SSH repo instead of listing locally', async () => {
await expect(shouldProbeGitRemote('/remote/repo', 'upstream', 'ssh-1')).resolves.toBe(true)
expect(gitExecFileAsyncMock).not.toHaveBeenCalled()
})
Expand Down
41 changes: 12 additions & 29 deletions src/main/git/remote-name-listing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readLocalGitConfigSignature } from '../github/local-git-config-signature'
import { getSshGitProvider, getSshGitProviderGeneration } from '../providers/ssh-git-dispatch'
import { runCoalescedProbe, type CoalescedProbes } from './coalesced-probe'
import type { GitAdmissionTier } from './command-runner/git-exec-options'
import { REMOTE_URL_PROBE_TIMEOUT_MS } from './remote-url-probe'
Expand Down Expand Up @@ -38,13 +37,9 @@ function parseRemoteNames(stdout: string): string[] {

function remoteNameListingCacheKey(
repoPath: string,
connectionId?: string | null,
localGitOptions: RemoteNameListingGitOptions = {}
): string {
const runtimeKey = connectionId
? `ssh:${connectionId}:${getSshGitProviderGeneration(connectionId)}`
: `local:${localGitOptions.wslDistro ?? 'host'}`
return `${runtimeKey}\0${repoPath}`
return `local:${localGitOptions.wslDistro ?? 'host'}\0${repoPath}`
}

function pruneRemoteNameListingCache(now: number): void {
Expand All @@ -64,34 +59,37 @@ function pruneRemoteNameListingCache(now: number): void {

function listingGitConfigContext(
repoPath: string,
connectionId?: string | null,
localGitOptions: RemoteNameListingGitOptions = {}
): { repoPath: string; connectionId: string | null; wslDistro?: string } {
): { repoPath: string; connectionId: null; wslDistro?: string } {
return {
repoPath,
connectionId: connectionId ?? null,
connectionId: null,
...(localGitOptions.wslDistro ? { wslDistro: localGitOptions.wslDistro } : {})
}
}

/**
* `git remote` names for one repo/runtime. Failed listings are not cached: a
* missed `upstream` would otherwise send issue/PR resolvers to origin on a
* contributor clone (#7331).
* contributor clone (#7331). An `ssh:` repo is not served here, so it lists as
* failed (`null`), never as this machine's remotes.
*/
export async function listCachedRemoteNames(
repoPath: string,
connectionId?: string | null,
localGitOptions: RemoteNameListingGitOptions = {}
): Promise<string[] | null> {
const cacheKey = remoteNameListingCacheKey(repoPath, connectionId, localGitOptions)
if (connectionId) {
return null
}
const cacheKey = remoteNameListingCacheKey(repoPath, localGitOptions)
const now = Date.now()
pruneRemoteNameListingCache(now)
const cached = remoteNameListingCache.get(cacheKey)
if (cached && cached.expiresAt > now) {
if (cached.configSignature !== undefined) {
const currentSignature = await readLocalGitConfigSignature(
listingGitConfigContext(repoPath, connectionId, localGitOptions)
listingGitConfigContext(repoPath, localGitOptions)
)
if (currentSignature === cached.configSignature) {
return cached.remotes
Expand All @@ -103,9 +101,9 @@ export async function listCachedRemoteNames(
}

return runCoalescedProbe(remoteNameListingInFlight, cacheKey, async (ownsKey) => {
const configContext = listingGitConfigContext(repoPath, connectionId, localGitOptions)
const configContext = listingGitConfigContext(repoPath, localGitOptions)
const configSignatureBefore = await readLocalGitConfigSignature(configContext)
const remotes = await listUncachedRemoteNames(repoPath, connectionId, localGitOptions)
const remotes = await listUncachedRemoteNames(repoPath, localGitOptions)
if (remotes === null) {
return null
}
Expand All @@ -132,23 +130,8 @@ export async function listCachedRemoteNames(

async function listUncachedRemoteNames(
repoPath: string,
connectionId?: string | null,
localGitOptions: RemoteNameListingGitOptions = {}
): Promise<string[] | null> {
if (connectionId) {
const provider = getSshGitProvider(connectionId)
if (!provider) {
return null
}
try {
const { stdout } = await provider.exec(['remote'], repoPath, {
signal: AbortSignal.timeout(REMOTE_URL_PROBE_TIMEOUT_MS)
})
return parseRemoteNames(stdout)
} catch {
return null
}
}
try {
const { stdout } = await gitExecFileAsync(['remote'], {
cwd: repoPath,
Expand Down
33 changes: 18 additions & 15 deletions src/main/ipc/hooks/worktree-hook-execution-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,16 @@ function makeRepo(fields: Pick<Repo, 'connectionId' | 'executionHostId'>): Repo
)
}

const localCalls = [
mocks.hasHooks,
mocks.localRead,
mocks.localWrite,
mocks.localFileRead,
mocks.localStat,
mocks.effectiveHooks
]

function expectRoute(target: string | null): void {
const localCalls = [
mocks.hasHooks,
mocks.localRead,
mocks.localWrite,
mocks.localFileRead,
mocks.localStat,
mocks.effectiveHooks
]
if (target) {
expect(mocks.provider).toHaveBeenCalledWith(target)
for (const [connectionId] of mocks.provider.mock.calls) {
Expand Down Expand Up @@ -209,13 +210,15 @@ describe.each(owners)('runtime hooks: $label', ({ fields, target }) => {
} else {
await issue.write(repo.id, 'save')
}
expectRoute(target)
if (method === 'write' && target) {
expect(mocks.ignored).toHaveBeenCalledWith(repo.path, target)
expect(mocks.remoteWrite).toHaveBeenCalledWith(
'/remote/fixture/.orca/issue-command',
'save\n'
)
if (target) {
// The runtime serves no SSH filesystem: an SSH owner is neither read remotely nor locally.
expect(mocks.provider).not.toHaveBeenCalled()
expect(mocks.remoteWrite).not.toHaveBeenCalled()
for (const method of localCalls) {
expect(method).not.toHaveBeenCalled()
}
} else {
expectRoute(target)
}
}
)
Expand Down
69 changes: 5 additions & 64 deletions src/main/ports/workspace-port-execution-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,8 @@ function localWorkspaceScan(): WorkspacePortScanResult {
}
}

function deps(
request?: (method: string) => Promise<unknown>,
connected = true
): WorkspacePortExecutionHostDeps & { request: ReturnType<typeof vi.fn> } {
const requestMock = vi.fn(request ?? (async () => ({ ports: [], platform: 'linux' })))
return {
request: requestMock,
getLocalProbes: async () => [LOCAL_PROBE],
getSshMultiplexer: (targetId) =>
connected && targetId === 'box' ? { request: requestMock } : undefined
}
function deps(): WorkspacePortExecutionHostDeps {
return { getLocalProbes: async () => [LOCAL_PROBE] }
}

describe('scanWorkspacePortsOnExecutionHost', () => {
Expand All @@ -69,72 +60,23 @@ describe('scanWorkspacePortsOnExecutionHost', () => {
const result = await scanWorkspacePortsOnExecutionHost('local', d)

expect(scanWorkspacePortsMock).toHaveBeenCalledWith([LOCAL_PROBE], undefined, undefined)
expect(d.request).not.toHaveBeenCalled()
expect(result).toMatchObject({ executionHostId: 'local', platform: 'linux' })
expect(result.ports).toHaveLength(1)
})

it("scans the workspace's SSH host through its relay, never this host", async () => {
const d = deps(async () => ({
platform: 'linux',
ports: [{ port: PORT, host: '0.0.0.0', pid: PID, processName: 'node' }]
}))

const result = await scanWorkspacePortsOnExecutionHost('ssh:box', d)
it('reports an SSH host as unavailable, not as an empty host, and never scans this host', async () => {
const result = await scanWorkspacePortsOnExecutionHost('ssh:box', deps())

expect(scanWorkspacePortsMock).not.toHaveBeenCalled()
expect(d.request).toHaveBeenCalledWith('ports.detect', undefined, expect.any(Object))
expect(result).toMatchObject({
executionHostId: 'ssh:box',
platform: 'linux',
ports: [
{
kind: 'external',
bindHost: '0.0.0.0',
connectHost: 'localhost',
port: PORT,
pid: PID,
processName: 'node'
}
]
})
expect(result.unavailableReason).toBeUndefined()
})

it('reports a disconnected SSH host as unavailable, not as an empty host', async () => {
const result = await scanWorkspacePortsOnExecutionHost('ssh:box', deps(undefined, false))

expect(result).toMatchObject({ executionHostId: 'ssh:box', ports: [] })
expect(result.unavailableReason).toMatch(/not connected/)
})

it('reports a failed or unsupported relay scan as unavailable', async () => {
const failed = await scanWorkspacePortsOnExecutionHost(
'ssh:box',
deps(async () => {
throw new Error('Request "ports.detect" timed out after 10000ms')
})
)
const unsupported = await scanWorkspacePortsOnExecutionHost(
'ssh:box',
deps(async () => ({ ports: [], platform: 'darwin' }))
)
const malformed = await scanWorkspacePortsOnExecutionHost(
'ssh:box',
deps(async () => ({ ports: [{ port: 'x' }], platform: 'linux' }))
)

expect(failed.unavailableReason).toMatch(/timed out/)
expect(unsupported.unavailableReason).toMatch(/not supported/)
expect(malformed.unavailableReason).toMatch(/invalid/)
expect(result.unavailableReason).toMatch(/Reconnect/)
})

it('refuses a workspace hosted by another server instead of scanning here', async () => {
const d = deps()
const result = await scanWorkspacePortsOnExecutionHost('runtime:env-2', d)

expect(scanWorkspacePortsMock).not.toHaveBeenCalled()
expect(d.request).not.toHaveBeenCalled()
expect(result.unavailableReason).toBeTruthy()
})
})
Expand Down Expand Up @@ -192,6 +134,5 @@ describe('killWorkspacePortOnExecutionHost', () => {
reason: 'Stopping processes on an SSH host is not supported yet.'
})
expect(kill).not.toHaveBeenCalled()
expect(d.request).not.toHaveBeenCalled()
})
})
Loading
Loading