feat(task): reject duplicate repositories on multi-repo task create
Phase 8 of multi-repository task support: surface duplicate-repository input as a clean validation error instead of letting the unique (task_id, repository_id) constraint surface mid-loop with some rows already inserted. - createTaskRepositories tracks seen repository IDs and returns "repository <id> listed more than once for the task" before any CreateTaskRepository call. - Tests cover the rejection path and confirm two distinct repos still succeed with both rows persisted in Position order. Repository discovery and per-repo cloning were already covered by Phase 1.4 (resolveAllRepoInfo loops ensureRepoCloned) and the existing repository_discovery service is repo-list aware by design. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -38,14 +38,14 @@ func TestTaskPR_PerRepoStorage(t *testing.T) {
|
||||
prFront := &TaskPR{
|
||||
TaskID: "task-1", RepositoryID: "repo-front",
|
||||
Owner: "kdlbs", Repo: "kandev", PRNumber: 100,
|
||||
PRURL: "https://github.com/kdlbs/kandev/pull/100",
|
||||
PRURL: "https://github.com/kdlbs/kandev/pull/100",
|
||||
PRTitle: "frontend changes", HeadBranch: "feat/x", BaseBranch: "main",
|
||||
State: "open", CreatedAt: now,
|
||||
}
|
||||
prBack := &TaskPR{
|
||||
TaskID: "task-1", RepositoryID: "repo-back",
|
||||
Owner: "kdlbs", Repo: "kandev-backend", PRNumber: 200,
|
||||
PRURL: "https://github.com/kdlbs/kandev-backend/pull/200",
|
||||
PRURL: "https://github.com/kdlbs/kandev-backend/pull/200",
|
||||
PRTitle: "backend changes", HeadBranch: "feat/x", BaseBranch: "main",
|
||||
State: "open", CreatedAt: now,
|
||||
}
|
||||
|
||||
@@ -120,6 +120,7 @@ func (s *Service) createTaskRepositories(ctx context.Context, taskID, workspaceI
|
||||
}
|
||||
}
|
||||
|
||||
seen := make(map[string]bool, len(repositories))
|
||||
for i, repoInput := range repositories {
|
||||
repositoryID, baseBranch, err := s.resolveRepoInput(ctx, workspaceID, repoInput, repoByPath)
|
||||
if err != nil {
|
||||
@@ -128,6 +129,14 @@ func (s *Service) createTaskRepositories(ctx context.Context, taskID, workspaceI
|
||||
if repositoryID == "" {
|
||||
return fmt.Errorf("repository_id is required")
|
||||
}
|
||||
// Multi-repo validation: each repository may appear at most once per task.
|
||||
// Without this guard the unique (task_id, repository_id) constraint on
|
||||
// task_repositories would surface as an opaque DB error mid-loop, leaving
|
||||
// some rows already inserted.
|
||||
if seen[repositoryID] {
|
||||
return fmt.Errorf("repository %q listed more than once for the task", repositoryID)
|
||||
}
|
||||
seen[repositoryID] = true
|
||||
taskRepo := &models.TaskRepository{
|
||||
TaskID: taskID,
|
||||
RepositoryID: repositoryID,
|
||||
|
||||
@@ -167,6 +167,58 @@ func TestService_CreateTask(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_CreateTask_RejectsDuplicateRepositories(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
_ = repo.CreateWorkspace(ctx, &models.Workspace{ID: "ws-1", Name: "Workspace"})
|
||||
_ = repo.CreateWorkflow(ctx, &models.Workflow{ID: "wf-1", WorkspaceID: "ws-1", Name: "WF"})
|
||||
_ = repo.CreateRepository(ctx, &models.Repository{ID: "repo-dup", WorkspaceID: "ws-1", Name: "Dup"})
|
||||
|
||||
req := &CreateTaskRequest{
|
||||
WorkspaceID: "ws-1",
|
||||
WorkflowID: "wf-1",
|
||||
WorkflowStepID: "step-1",
|
||||
Title: "dup repos",
|
||||
Repositories: []TaskRepositoryInput{
|
||||
{RepositoryID: "repo-dup", BaseBranch: "main"},
|
||||
{RepositoryID: "repo-dup", BaseBranch: "main"},
|
||||
},
|
||||
}
|
||||
_, err := svc.CreateTask(ctx, req)
|
||||
if err == nil {
|
||||
t.Fatal("expected duplicate-repository error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_CreateTask_AcceptsMultipleDistinctRepositories(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
_ = repo.CreateWorkspace(ctx, &models.Workspace{ID: "ws-1", Name: "Workspace"})
|
||||
_ = repo.CreateWorkflow(ctx, &models.Workflow{ID: "wf-1", WorkspaceID: "ws-1", Name: "WF"})
|
||||
_ = repo.CreateRepository(ctx, &models.Repository{ID: "repo-front", WorkspaceID: "ws-1", Name: "front"})
|
||||
_ = repo.CreateRepository(ctx, &models.Repository{ID: "repo-back", WorkspaceID: "ws-1", Name: "back"})
|
||||
|
||||
req := &CreateTaskRequest{
|
||||
WorkspaceID: "ws-1",
|
||||
WorkflowID: "wf-1",
|
||||
WorkflowStepID: "step-1",
|
||||
Title: "multi",
|
||||
Repositories: []TaskRepositoryInput{
|
||||
{RepositoryID: "repo-front", BaseBranch: "main"},
|
||||
{RepositoryID: "repo-back", BaseBranch: "main"},
|
||||
},
|
||||
}
|
||||
task, err := svc.CreateTask(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateTask: %v", err)
|
||||
}
|
||||
if len(task.Repositories) != 2 {
|
||||
t.Errorf("expected 2 task repositories, got %d", len(task.Repositories))
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_CreateRepository_DefaultWorktreeBranchPrefix(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in New Issue
Block a user