Skip to content

Commit d6bd10a

Browse files
committed
Make steps resolution part of workspaces detection
1 parent 31e168b commit d6bd10a

File tree

4 files changed

+219
-206
lines changed

4 files changed

+219
-206
lines changed

internal/batches/service/build_tasks.go

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/sourcegraph/sourcegraph/lib/batches/template"
99
"github.com/sourcegraph/src-cli/internal/batches/executor"
1010
"github.com/sourcegraph/src-cli/internal/batches/graphql"
11-
"github.com/sourcegraph/src-cli/internal/batches/util"
1211
)
1312

1413
// buildTasks returns tasks for all the workspaces determined for the given spec.
@@ -24,56 +23,24 @@ func buildTasks(ctx context.Context, spec *batcheslib.BatchSpec, repos []*graphq
2423
if !ok {
2524
return nil, errors.New("invalid state, didn't find repo for workspace definition")
2625
}
27-
t, ok, err := buildTask(spec, repo, ws.Path, ws.OnlyFetchWorkspace)
26+
27+
t, err := buildTask(spec, repo, ws.Path, ws.OnlyFetchWorkspace, ws.Steps)
2828
if err != nil {
2929
return nil, err
3030
}
3131

32-
if ok {
33-
tasks = append(tasks, t)
34-
}
32+
tasks = append(tasks, t)
3533
}
3634

3735
return tasks, nil
3836
}
3937

40-
func buildTask(spec *batcheslib.BatchSpec, r *graphql.Repository, path string, onlyWorkspace bool) (*executor.Task, bool, error) {
38+
func buildTask(spec *batcheslib.BatchSpec, r *graphql.Repository, path string, onlyWorkspace bool, steps []batcheslib.Step) (*executor.Task, error) {
4139
batchChange := template.BatchChangeAttributes{
4240
Name: spec.Name,
4341
Description: spec.Description,
4442
}
4543

46-
taskSteps := []batcheslib.Step{}
47-
for _, step := range spec.Steps {
48-
// If no if condition is given, just go ahead and add the step to the list.
49-
if step.IfCondition() == "" {
50-
taskSteps = append(taskSteps, step)
51-
continue
52-
}
53-
54-
stepCtx := &template.StepContext{
55-
Repository: util.GraphQLRepoToTemplatingRepo(r),
56-
BatchChange: batchChange,
57-
}
58-
static, boolVal, err := template.IsStaticBool(step.IfCondition(), stepCtx)
59-
if err != nil {
60-
return nil, false, err
61-
}
62-
63-
// If we could evaluate the condition statically and the resulting
64-
// boolean is false, we don't add that step.
65-
if !static {
66-
taskSteps = append(taskSteps, step)
67-
} else if boolVal {
68-
taskSteps = append(taskSteps, step)
69-
}
70-
}
71-
72-
// If the task doesn't have any steps we don't need to execute it
73-
if len(taskSteps) == 0 {
74-
return nil, false, nil
75-
}
76-
7744
// "." means the path is root, but in the executor we use "" to signify root
7845
if path == "." {
7946
path = ""
@@ -82,11 +49,11 @@ func buildTask(spec *batcheslib.BatchSpec, r *graphql.Repository, path string, o
8249
return &executor.Task{
8350
Repository: r,
8451
Path: path,
85-
Steps: taskSteps,
52+
Steps: steps,
8653
OnlyFetchWorkspace: onlyWorkspace,
8754

8855
TransformChanges: spec.TransformChanges,
8956
Template: spec.ChangesetTemplate,
9057
BatchChangeAttributes: &batchChange,
91-
}, true, nil
58+
}, nil
9259
}

internal/batches/service/build_tasks_test.go

Lines changed: 0 additions & 138 deletions
This file was deleted.

internal/batches/service/workspaces.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ package service
33
import (
44
"context"
55
"fmt"
6+
"sort"
67

78
"github.com/gobwas/glob"
9+
"github.com/pkg/errors"
810
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
11+
"github.com/sourcegraph/sourcegraph/lib/batches/template"
912
"github.com/sourcegraph/src-cli/internal/batches"
1013
"github.com/sourcegraph/src-cli/internal/batches/graphql"
14+
"github.com/sourcegraph/src-cli/internal/batches/util"
1115
)
1216

1317
type RepoWorkspace struct {
1418
RepoID string
1519
Path string
20+
Steps []batcheslib.Step
1621
OnlyFetchWorkspace bool
1722
}
1823

@@ -31,6 +36,11 @@ func findWorkspaces(
3136
finder directoryFinder,
3237
repos []*graphql.Repository,
3338
) ([]RepoWorkspace, error) {
39+
repoByID := make(map[string]*graphql.Repository)
40+
for _, repo := range repos {
41+
repoByID[repo.ID] = repo
42+
}
43+
3444
// Pre-compile all globs.
3545
workspaceMatchers := make(map[batcheslib.WorkspaceConfiguration]glob.Glob)
3646
for _, conf := range spec.Workspaces {
@@ -117,12 +127,71 @@ func findWorkspaces(
117127
fetchWorkspace = false
118128
}
119129

130+
repo, ok := repoByID[workspace.RepoID]
131+
if !ok {
132+
return nil, errors.New("invalid state, repo not found")
133+
}
134+
135+
steps, err := stepsForRepo(spec, repo)
136+
if err != nil {
137+
return nil, err
138+
}
139+
140+
// If the workspace doesn't have any steps we don't need to include it.
141+
if len(steps) == 0 {
142+
continue
143+
}
144+
120145
workspaces = append(workspaces, RepoWorkspace{
121146
RepoID: workspace.RepoID,
122147
Path: path,
148+
Steps: steps,
123149
OnlyFetchWorkspace: fetchWorkspace,
124150
})
125151
}
126152
}
153+
154+
// Stable sorting.
155+
sort.Slice(workspaces, func(i, j int) bool {
156+
if workspaces[i].RepoID == workspaces[j].RepoID {
157+
return workspaces[i].Path < workspaces[j].Path
158+
}
159+
return workspaces[i].RepoID < workspaces[j].RepoID
160+
})
161+
127162
return workspaces, nil
128163
}
164+
165+
// stepsForRepo calculates the steps required to run on the given repo.
166+
func stepsForRepo(spec *batcheslib.BatchSpec, r *graphql.Repository) ([]batcheslib.Step, error) {
167+
taskSteps := []batcheslib.Step{}
168+
for _, step := range spec.Steps {
169+
// If no if condition is given, just go ahead and add the step to the list.
170+
if step.IfCondition() == "" {
171+
taskSteps = append(taskSteps, step)
172+
continue
173+
}
174+
175+
batchChange := template.BatchChangeAttributes{
176+
Name: spec.Name,
177+
Description: spec.Description,
178+
}
179+
stepCtx := &template.StepContext{
180+
Repository: util.GraphQLRepoToTemplatingRepo(r),
181+
BatchChange: batchChange,
182+
}
183+
static, boolVal, err := template.IsStaticBool(step.IfCondition(), stepCtx)
184+
if err != nil {
185+
return nil, err
186+
}
187+
188+
// If we could evaluate the condition statically and the resulting
189+
// boolean is false, we don't add that step.
190+
if !static {
191+
taskSteps = append(taskSteps, step)
192+
} else if boolVal {
193+
taskSteps = append(taskSteps, step)
194+
}
195+
}
196+
return taskSteps, nil
197+
}

0 commit comments

Comments
 (0)