Allow setup when the selected project exists only on another host by carrying its validated provider identity with the request instead of reverse-parsing project IDs. Preserve host-qualified provider identity and reject mismatched payloads before linking. Make linking atomic for local and runtime imports, including clone setup: roll back only newly registered repos and invalidate the same caches as canonical removal. Cover local, runtime, host-qualified identity, mismatch, clone rollback, and renderer routing paths. Co-authored-by: fanyunqian.1 <fanyunqian.1@bytedance.com>
158 lines
5.0 KiB
TypeScript
158 lines
5.0 KiB
TypeScript
import { z } from 'zod'
|
|
import { normalizeExecutionHostId } from '../../../../shared/execution-host'
|
|
import { defineMethod, type RpcMethod } from '../core'
|
|
import { OptionalString, requiredString } from '../schemas'
|
|
|
|
const ProjectProviderIdentity = z.object({
|
|
provider: z.literal('github'),
|
|
owner: requiredString('Missing project owner'),
|
|
repo: requiredString('Missing project repository'),
|
|
host: OptionalString
|
|
})
|
|
|
|
const ProjectHostSetupExistingFolder = z.object({
|
|
projectId: requiredString('Missing project ID'),
|
|
projectProviderIdentity: ProjectProviderIdentity.optional(),
|
|
hostId: requiredString('Missing host ID').transform((value, ctx) => {
|
|
const hostId = normalizeExecutionHostId(value)
|
|
if (!hostId) {
|
|
ctx.addIssue({ code: 'custom', message: 'Invalid host ID' })
|
|
return z.NEVER
|
|
}
|
|
return hostId
|
|
}),
|
|
path: requiredString('Missing project path'),
|
|
kind: z.enum(['git', 'folder']).optional(),
|
|
displayName: OptionalString,
|
|
setupMethod: z.enum(['imported-existing-folder', 'cloned']).optional()
|
|
})
|
|
|
|
const ProjectHostSetupClone = z.object({
|
|
projectId: requiredString('Missing project ID'),
|
|
projectProviderIdentity: ProjectProviderIdentity.optional(),
|
|
hostId: requiredString('Missing host ID').transform((value, ctx) => {
|
|
const hostId = normalizeExecutionHostId(value)
|
|
if (!hostId) {
|
|
ctx.addIssue({ code: 'custom', message: 'Invalid host ID' })
|
|
return z.NEVER
|
|
}
|
|
return hostId
|
|
}),
|
|
url: requiredString('Missing clone URL'),
|
|
destination: requiredString('Missing clone destination'),
|
|
displayName: OptionalString
|
|
})
|
|
|
|
const LocalWindowsRuntimePreference = z.discriminatedUnion('kind', [
|
|
z.object({ kind: z.literal('inherit-global') }),
|
|
z.object({ kind: z.literal('windows-host') }),
|
|
z.object({ kind: z.literal('wsl'), distro: requiredString('Missing WSL distro') })
|
|
])
|
|
|
|
const ProjectUpdate = z.object({
|
|
projectId: requiredString('Missing project ID'),
|
|
updates: z.object({
|
|
localWindowsRuntimePreference: LocalWindowsRuntimePreference.optional()
|
|
})
|
|
})
|
|
|
|
const ProjectHostSetupCreate = z.object({
|
|
projectId: requiredString('Missing project ID'),
|
|
hostId: requiredString('Missing host ID').transform((value, ctx) => {
|
|
const hostId = normalizeExecutionHostId(value)
|
|
if (!hostId) {
|
|
ctx.addIssue({ code: 'custom', message: 'Invalid host ID' })
|
|
return z.NEVER
|
|
}
|
|
return hostId
|
|
}),
|
|
setupId: OptionalString,
|
|
path: OptionalString,
|
|
kind: z.enum(['git', 'folder']).optional(),
|
|
displayName: OptionalString,
|
|
worktreeBasePath: OptionalString,
|
|
gitUsername: OptionalString,
|
|
setupState: z.enum(['ready', 'not-set-up', 'setting-up', 'error', 'unsupported']).optional(),
|
|
setupMethod: z.enum(['imported-existing-folder', 'cloned', 'provisioned']).optional()
|
|
})
|
|
|
|
const ProjectHostSetupUpdate = z.object({
|
|
setupId: requiredString('Missing setup ID'),
|
|
updates: z.object({
|
|
displayName: OptionalString,
|
|
path: OptionalString,
|
|
worktreeBasePath: OptionalString,
|
|
setupState: z.enum(['ready', 'not-set-up', 'setting-up', 'error', 'unsupported']).optional(),
|
|
setupMethod: z
|
|
.enum(['legacy-repo', 'imported-existing-folder', 'cloned', 'provisioned'])
|
|
.optional(),
|
|
gitUsername: OptionalString,
|
|
kind: z.enum(['git', 'folder']).optional()
|
|
})
|
|
})
|
|
|
|
const ProjectHostSetupDelete = z.object({
|
|
setupId: requiredString('Missing setup ID')
|
|
})
|
|
|
|
export const PROJECT_RUNTIME_METHODS: RpcMethod[] = [
|
|
defineMethod({
|
|
name: 'project.list',
|
|
params: null,
|
|
handler: (_params, { runtime }) => {
|
|
runtime.enrichMissingRepoGitRemoteIdentities?.()
|
|
return { projects: runtime.listProjects() }
|
|
}
|
|
}),
|
|
defineMethod({
|
|
name: 'project.update',
|
|
params: ProjectUpdate,
|
|
handler: (params, { runtime }) => ({
|
|
project: runtime.updateProject(params.projectId, params.updates)
|
|
})
|
|
}),
|
|
defineMethod({
|
|
name: 'projectHostSetup.list',
|
|
params: null,
|
|
handler: (_params, { runtime }) => {
|
|
runtime.enrichMissingRepoGitRemoteIdentities?.()
|
|
return { setups: runtime.listProjectHostSetups() }
|
|
}
|
|
}),
|
|
defineMethod({
|
|
name: 'projectHostSetup.create',
|
|
params: ProjectHostSetupCreate,
|
|
handler: (params, { runtime }) => ({
|
|
result: runtime.createProjectHostSetup(params)
|
|
})
|
|
}),
|
|
defineMethod({
|
|
name: 'projectHostSetup.setupExistingFolder',
|
|
params: ProjectHostSetupExistingFolder,
|
|
handler: async (params, { runtime }) => ({
|
|
result: await runtime.setupProjectExistingFolder(params)
|
|
})
|
|
}),
|
|
defineMethod({
|
|
name: 'projectHostSetup.clone',
|
|
params: ProjectHostSetupClone,
|
|
handler: async (params, { runtime }) => ({
|
|
result: await runtime.setupProjectClone(params)
|
|
})
|
|
}),
|
|
defineMethod({
|
|
name: 'projectHostSetup.update',
|
|
params: ProjectHostSetupUpdate,
|
|
handler: (params, { runtime }) => ({
|
|
result: runtime.updateProjectHostSetup(params)
|
|
})
|
|
}),
|
|
defineMethod({
|
|
name: 'projectHostSetup.delete',
|
|
params: ProjectHostSetupDelete,
|
|
handler: (params, { runtime }) => ({
|
|
result: runtime.deleteProjectHostSetup(params)
|
|
})
|
|
})
|
|
]
|