* 创建 Workspace * * - 默认分支名: at/{workspace-short-id}(ID 前 8 位) * - 支持用户自定义分支名 * - 创建前校验分支名合法性 & 是否已存在 * - 创建后自动将关联 Task 状态改为 IN_PROGRESS * - 失败时回滚已创建的数据库记录
(taskId: string, branchNameOrOptions?: string | CreateWorkspaceOptions)
| 1163 | conflictedFiles: Array.isArray(conflict.conflictedFiles) ? conflict.conflictedFiles : [], |
| 1164 | sourceBranch: conflict.sourceBranch, |
| 1165 | targetBranch: conflict.targetBranch, |
| 1166 | sourceWorkspaceId: conflict.sourceWorkspaceId, |
| 1167 | targetWorkspaceId: conflict.targetWorkspaceId, |
| 1168 | }; |
| 1169 | } |
| 1170 | |
| 1171 | const code = errorCode || 'UNKNOWN'; |
| 1172 | const message = error instanceof Error ? error.message : String(error); |
| 1173 | |
| 1174 | return { |
| 1175 | workspaceId: item.workspaceId, |
| 1176 | ownerMemberId: item.owner.memberId, |
| 1177 | status: 'FAILED', |
| 1178 | code, |
| 1179 | message, |
| 1180 | }; |
| 1181 | } |
| 1182 | |
| 1183 | // ── Create ─────────────────────────────────────────────────────────────────── |
| 1184 | |
| 1185 | /** |
| 1186 | * 创建 Workspace |
| 1187 | * |
| 1188 | * - 默认分支名: at/{workspace-short-id}(ID 前 8 位) |
| 1189 | * - 支持用户自定义分支名 |
| 1190 | * - 创建前校验分支名合法性 & 是否已存在 |
| 1191 | * - 创建后自动将关联 Task 状态改为 IN_PROGRESS |
| 1192 | * - 失败时回滚已创建的数据库记录 |
| 1193 | */ |
| 1194 | async create(taskId: string, branchNameOrOptions?: string | CreateWorkspaceOptions) { |
| 1195 | const options = normalizeCreateOptions(branchNameOrOptions); |
| 1196 | const task = await prisma.task.findUnique({ |
| 1197 | where: { id: taskId }, |
| 1198 | include: { project: true, teamRun: { select: { id: true } } }, |
| 1199 | }); |
| 1200 | |
| 1201 | if (!task) { |
| 1202 | throw new NotFoundError('Task', taskId); |
| 1203 | } |
| 1204 | ensureTaskNotDeleted(task); |
| 1205 | ensureProjectIsMutable(task.project, 'create workspaces'); |
| 1206 | |
| 1207 | if (options.workspaceKind === WorkspaceKind.MAIN_DIRECTORY) { |
| 1208 | return this.createMainDirectoryWorkspace(taskId, task, options); |
| 1209 | } |
| 1210 | |
| 1211 | await ensureProjectSupportsWorktrees(task.project, 'create a worktree workspace'); |
| 1212 | |
| 1213 | const worktreeManager = new WorktreeManager(task.project.repoPath); |
| 1214 | |
| 1215 | // 查找可复用的 MERGED 或 HIBERNATED workspace |
| 1216 | if (!options.branchName && options.reuseInactive) { |
| 1217 | const reusableWorkspace = await prisma.workspace.findFirst({ |
| 1218 | where: { |
| 1219 | taskId, |
| 1220 | parentWorkspaceId: options.parentWorkspaceId, |
| 1221 | ownerMemberId: options.ownerMemberId, |
| 1222 | workspaceKind: WorkspaceKind.WORKTREE, |
no test coverage detected