Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/batches/executor/check_cache.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/batches/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions internal/batches/service/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down