diff --git a/internal/batches/executor/check_cache.go b/internal/batches/executor/check_cache.go deleted file mode 100644 index be0ba73e6d..0000000000 --- a/internal/batches/executor/check_cache.go +++ /dev/null @@ -1 +0,0 @@ -package executor diff --git a/internal/batches/service/task_builder.go b/internal/batches/service/build_tasks.go similarity index 66% rename from internal/batches/service/task_builder.go rename to internal/batches/service/build_tasks.go index 63906b2df7..8591dd36e8 100644 --- a/internal/batches/service/task_builder.go +++ b/internal/batches/service/build_tasks.go @@ -10,32 +10,49 @@ import ( "github.com/sourcegraph/src-cli/internal/batches/template" ) -type directoryFinder interface { - FindDirectoriesInRepos(ctx context.Context, fileName string, repos ...*graphql.Repository) (map[*graphql.Repository][]string, error) -} +// buildTasks returns tasks for all the workspaces determined for the given spec. +func buildTasks(ctx context.Context, spec *batcheslib.BatchSpec, repos []*graphql.Repository, workspaces []RepoWorkspaces) ([]*executor.Task, error) { + repoByID := make(map[string]*graphql.Repository) + for _, repo := range repos { + repoByID[repo.ID] = repo + } -type taskBuilder struct { - spec *batcheslib.BatchSpec - finder directoryFinder -} + tasks := []*executor.Task{} + for _, ws := range workspaces { + repo, ok := repoByID[ws.RepoID] + if !ok { + return nil, errors.New("invalid state, didn't find repo for workspace definition") + } + for _, path := range ws.Paths { + fetchWorkspace := ws.OnlyFetchWorkspace + if path == "" { + fetchWorkspace = false + } + t, ok, err := buildTask(spec, repo, path, fetchWorkspace) + if err != nil { + return nil, err + } -// buildTasks returns tasks for all the workspaces determined for the given spec. -func buildTasks(ctx context.Context, spec *batcheslib.BatchSpec, finder directoryFinder, repos []*graphql.Repository, workspaces []RepoWorkspaces) ([]*executor.Task, error) { - tb := &taskBuilder{spec: spec, finder: finder} - return tb.buildAll(ctx, repos, workspaces) + if ok { + tasks = append(tasks, t) + } + } + } + + return tasks, nil } -func (tb *taskBuilder) buildTask(r *graphql.Repository, path string, onlyWorkspace bool) (*executor.Task, bool, error) { +func buildTask(spec *batcheslib.BatchSpec, r *graphql.Repository, path string, onlyWorkspace bool) (*executor.Task, bool, error) { stepCtx := &template.StepContext{ Repository: *r, BatchChange: template.BatchChangeAttributes{ - Name: tb.spec.Name, - Description: tb.spec.Description, + Name: spec.Name, + Description: spec.Description, }, } var taskSteps []batcheslib.Step - for _, step := range tb.spec.Steps { + for _, step := range spec.Steps { if step.IfCondition() == "" { taskSteps = append(taskSteps, step) continue @@ -71,42 +88,11 @@ func (tb *taskBuilder) buildTask(r *graphql.Repository, path string, onlyWorkspa Steps: taskSteps, OnlyFetchWorkspace: onlyWorkspace, - TransformChanges: tb.spec.TransformChanges, - Template: tb.spec.ChangesetTemplate, + TransformChanges: spec.TransformChanges, + Template: spec.ChangesetTemplate, BatchChangeAttributes: &template.BatchChangeAttributes{ - Name: tb.spec.Name, - Description: tb.spec.Description, + Name: spec.Name, + Description: spec.Description, }, }, true, nil } - -func (tb *taskBuilder) buildAll(ctx context.Context, repos []*graphql.Repository, workspaces []RepoWorkspaces) ([]*executor.Task, error) { - repoByID := make(map[string]*graphql.Repository) - for _, repo := range repos { - repoByID[repo.ID] = repo - } - - tasks := []*executor.Task{} - for _, ws := range workspaces { - repo, ok := repoByID[ws.RepoID] - if !ok { - return nil, errors.New("invalid state, didn't find repo for workspace definition") - } - for _, path := range ws.Paths { - fetchWorkspace := ws.OnlyFetchWorkspace - if path == "" { - fetchWorkspace = false - } - t, ok, err := tb.buildTask(repo, path, fetchWorkspace) - if err != nil { - return nil, err - } - - if ok { - tasks = append(tasks, t) - } - } - } - - return tasks, nil -} diff --git a/internal/batches/service/task_builder_test.go b/internal/batches/service/build_tasks_test.go similarity index 94% rename from internal/batches/service/task_builder_test.go rename to internal/batches/service/build_tasks_test.go index 5c1f6d62d8..73c3c8fe5e 100644 --- a/internal/batches/service/task_builder_test.go +++ b/internal/batches/service/build_tasks_test.go @@ -8,7 +8,7 @@ import ( batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" ) -func TestTaskBuilder_BuildTask_IfConditions(t *testing.T) { +func TestBuildTask_IfConditions(t *testing.T) { tests := map[string]struct { spec *batcheslib.BatchSpec @@ -117,8 +117,7 @@ func TestTaskBuilder_BuildTask_IfConditions(t *testing.T) { for name, tt := range tests { t.Run(name, func(t *testing.T) { - builder := &taskBuilder{spec: tt.spec} - task, ok, err := builder.buildTask(testRepo1, "", false) + task, ok, err := buildTask(tt.spec, testRepo1, "", false) if err != nil { t.Fatalf("unexpected err: %s", err) } diff --git a/internal/batches/service/service.go b/internal/batches/service/service.go index a8f146ee78..fd03c1dae0 100644 --- a/internal/batches/service/service.go +++ b/internal/batches/service/service.go @@ -187,7 +187,7 @@ func (svc *Service) DetermineWorkspaces(ctx context.Context, repos []*graphql.Re } func (svc *Service) BuildTasks(ctx context.Context, repos []*graphql.Repository, spec *batcheslib.BatchSpec, workspaces []RepoWorkspaces) ([]*executor.Task, error) { - return buildTasks(ctx, spec, svc, repos, workspaces) + return buildTasks(ctx, spec, repos, workspaces) } func (svc *Service) NewCoordinator(opts executor.NewCoordinatorOpts) *executor.Coordinator { diff --git a/internal/batches/service/workspaces.go b/internal/batches/service/workspaces.go index 1febb1d58c..a3406ee288 100644 --- a/internal/batches/service/workspaces.go +++ b/internal/batches/service/workspaces.go @@ -16,6 +16,10 @@ type RepoWorkspaces struct { OnlyFetchWorkspace bool } +type directoryFinder interface { + FindDirectoriesInRepos(ctx context.Context, fileName string, repos ...*graphql.Repository) (map[*graphql.Repository][]string, error) +} + // findWorkspaces matches the given repos to the workspace configs and // searches, via the Sourcegraph instance, the locations of the workspaces in // each repository.