diff --git a/server/src/__tests__/issues-service.test.ts b/server/src/__tests__/issues-service.test.ts index 0f4f9e7fe75..1fdae88939d 100644 --- a/server/src/__tests__/issues-service.test.ts +++ b/server/src/__tests__/issues-service.test.ts @@ -3060,6 +3060,74 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { }); }); + it("preserves the parent project when a generic child create inherits workspace linkage", async () => { + const companyId = randomUUID(); + const projectId = randomUUID(); + const parentIssueId = randomUUID(); + const projectWorkspaceId = randomUUID(); + const executionWorkspaceId = randomUUID(); + + await db.insert(companies).values({ + id: companyId, + name: "Paperclip", + issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`, + requireBoardApprovalForNewAgents: false, + }); + await instanceSettingsService(db).updateExperimental({ enableIsolatedWorkspaces: true }); + + await db.insert(projects).values({ + id: projectId, + companyId, + name: "Workspace project", + status: "in_progress", + }); + + await db.insert(projectWorkspaces).values({ + id: projectWorkspaceId, + companyId, + projectId, + name: "Primary workspace", + isPrimary: true, + }); + + await db.insert(executionWorkspaces).values({ + id: executionWorkspaceId, + companyId, + projectId, + projectWorkspaceId, + mode: "isolated_workspace", + strategyType: "git_worktree", + name: "Issue worktree", + status: "active", + providerType: "git_worktree", + }); + + await db.insert(issues).values({ + id: parentIssueId, + companyId, + projectId, + projectWorkspaceId, + title: "Parent issue", + status: "in_progress", + priority: "medium", + executionWorkspaceId, + executionWorkspacePreference: "reuse_existing", + executionWorkspaceSettings: { + mode: "isolated_workspace", + }, + }); + + const child = await svc.create(companyId, { + parentId: parentIssueId, + title: "Generic child issue", + }); + + expect(child.parentId).toBe(parentIssueId); + expect(child.projectId).toBe(projectId); + expect(child.projectWorkspaceId).toBe(projectWorkspaceId); + expect(child.executionWorkspaceId).toBe(executionWorkspaceId); + }); + it("keeps explicit workspace fields instead of inheriting the parent linkage", async () => { const companyId = randomUUID(); const projectId = randomUUID(); @@ -3216,12 +3284,12 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { }); const followUp = await svc.create(companyId, { - projectId, title: "Follow-up issue", inheritExecutionWorkspaceFromIssueId: sourceIssueId, }); expect(followUp.parentId).toBeNull(); + expect(followUp.projectId).toBe(projectId); expect(followUp.projectWorkspaceId).toBe(projectWorkspaceId); expect(followUp.executionWorkspaceId).toBe(executionWorkspaceId); expect(followUp.executionWorkspacePreference).toBe("reuse_existing"); @@ -3230,6 +3298,51 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { }); }); + it("derives project identity when an update adds workspace linkage to a projectless issue", async () => { + const companyId = randomUUID(); + const projectId = randomUUID(); + const projectWorkspaceId = randomUUID(); + const issueId = randomUUID(); + + await db.insert(companies).values({ + id: companyId, + name: "Paperclip", + issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`, + requireBoardApprovalForNewAgents: false, + }); + await instanceSettingsService(db).updateExperimental({ enableIsolatedWorkspaces: true }); + + await db.insert(projects).values({ + id: projectId, + companyId, + name: "Workspace project", + status: "in_progress", + }); + + await db.insert(projectWorkspaces).values({ + id: projectWorkspaceId, + companyId, + projectId, + name: "Primary workspace", + isPrimary: true, + }); + + await db.insert(issues).values({ + id: issueId, + companyId, + title: "Projectless issue", + status: "todo", + priority: "medium", + }); + + const updated = await svc.update(issueId, { + projectWorkspaceId, + }); + + expect(updated?.projectId).toBe(projectId); + expect(updated?.projectWorkspaceId).toBe(projectWorkspaceId); + }); + it("syncs reused execution workspace config when issue workspace settings are updated", async () => { const companyId = randomUUID(); const projectId = randomUUID(); diff --git a/server/src/services/issues.ts b/server/src/services/issues.ts index e7dd67f0ceb..03511e68f4f 100644 --- a/server/src/services/issues.ts +++ b/server/src/services/issues.ts @@ -3461,6 +3461,7 @@ export function issueService(db: Db) { if (projectId && workspace.projectId !== projectId) { throw unprocessable("Project workspace must belong to the selected project"); } + return workspace; } async function assertValidExecutionWorkspace( @@ -3483,6 +3484,7 @@ export function issueService(db: Db) { if (projectId && workspace.projectId !== projectId) { throw unprocessable("Execution workspace must belong to the selected project"); } + return workspace; } async function assertValidLabelIds(companyId: string, labelIds: string[], dbOrTx: any = db) { @@ -4741,7 +4743,6 @@ export function issueService(db: Db) { } return db.transaction(async (tx) => { const defaultCompanyGoal = await getDefaultCompanyGoal(tx, companyId); - const projectGoalId = await getProjectDefaultGoalId(tx, companyId, issueData.projectId); let projectWorkspaceId = issueData.projectWorkspaceId ?? null; let executionWorkspaceId = issueData.executionWorkspaceId ?? null; let executionWorkspacePreference = issueData.executionWorkspacePreference ?? null; @@ -4754,6 +4755,9 @@ export function issueService(db: Db) { issueData.executionWorkspaceSettings !== undefined; if (workspaceInheritanceIssueId) { const workspaceSource = await getWorkspaceInheritanceIssue(tx, companyId, workspaceInheritanceIssueId); + if (issueData.projectId == null && workspaceSource.projectId) { + issueData.projectId = workspaceSource.projectId; + } if (projectWorkspaceId == null && workspaceSource.projectWorkspaceId) { projectWorkspaceId = workspaceSource.projectWorkspaceId; } @@ -4780,6 +4784,15 @@ export function issueService(db: Db) { } } } + if (issueData.projectId == null && projectWorkspaceId) { + const workspace = await assertValidProjectWorkspace(companyId, null, projectWorkspaceId, tx); + issueData.projectId = workspace.projectId; + } + if (issueData.projectId == null && executionWorkspaceId) { + const workspace = await assertValidExecutionWorkspace(companyId, null, executionWorkspaceId, tx); + issueData.projectId = workspace.projectId; + } + const projectGoalId = await getProjectDefaultGoalId(tx, companyId, issueData.projectId); // Cache the project policy lookup for this insert. Both the // default-settings block and the assignee-environment-promotion block // need the same row; without caching they'd issue two round-trips. @@ -5014,7 +5027,7 @@ export function issueService(db: Db) { if (issueData.assigneeUserId) { await assertAssignableUser(existing.companyId, issueData.assigneeUserId); } - const nextProjectId = issueData.projectId !== undefined ? issueData.projectId : existing.projectId; + let nextProjectId = issueData.projectId !== undefined ? issueData.projectId : existing.projectId; const nextProjectWorkspaceId = issueData.projectWorkspaceId !== undefined ? issueData.projectWorkspaceId : existing.projectWorkspaceId; const nextExecutionWorkspaceId = @@ -5027,6 +5040,16 @@ export function issueService(db: Db) { issueData.executionWorkspaceSettings !== undefined ? parseIssueExecutionWorkspaceSettings(issueData.executionWorkspaceSettings) : parseIssueExecutionWorkspaceSettings(existing.executionWorkspaceSettings); + if (!nextProjectId && nextProjectWorkspaceId) { + const workspace = await assertValidProjectWorkspace(existing.companyId, null, nextProjectWorkspaceId); + nextProjectId = workspace.projectId; + patch.projectId = workspace.projectId; + } + if (!nextProjectId && nextExecutionWorkspaceId) { + const workspace = await assertValidExecutionWorkspace(existing.companyId, null, nextExecutionWorkspaceId); + nextProjectId = workspace.projectId; + patch.projectId = workspace.projectId; + } if (nextProjectWorkspaceId) { await assertValidProjectWorkspace(existing.companyId, nextProjectId, nextProjectWorkspaceId); }