Skip to content

Commit 27dd60a

Browse files
authored
Slight refactor of package structures (#586)
1 parent e505af8 commit 27dd60a

16 files changed

+122
-103
lines changed

internal/batches/executor/changeset_specs.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ import (
88
"github.com/sourcegraph/go-diff/diff"
99
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
1010
"github.com/sourcegraph/src-cli/internal/batches"
11+
"github.com/sourcegraph/src-cli/internal/batches/template"
1112
)
1213

1314
var errOptionalPublishedUnsupported = errors.New(`This Sourcegraph version requires the "published" field to be specified in the batch spec; upgrade to version 3.30.0 or later to be able to omit the published field and control publication from the UI.`)
1415

1516
func createChangesetSpecs(task *Task, result executionResult, features batches.FeatureFlags) ([]*batches.ChangesetSpec, error) {
1617
repo := task.Repository.Name
1718

18-
tmplCtx := &ChangesetTemplateContext{
19+
tmplCtx := &template.ChangesetTemplateContext{
1920
BatchChangeAttributes: *task.BatchChangeAttributes,
20-
Steps: StepsContext{
21+
Steps: template.StepsContext{
2122
Changes: result.ChangedFiles,
2223
Path: result.Path,
2324
},
@@ -36,35 +37,35 @@ func createChangesetSpecs(task *Task, result executionResult, features batches.F
3637
}
3738
} else {
3839
var err error
39-
authorName, err = renderChangesetTemplateField("authorName", task.Template.Commit.Author.Name, tmplCtx)
40+
authorName, err = template.RenderChangesetTemplateField("authorName", task.Template.Commit.Author.Name, tmplCtx)
4041
if err != nil {
4142
return nil, err
4243
}
43-
authorEmail, err = renderChangesetTemplateField("authorEmail", task.Template.Commit.Author.Email, tmplCtx)
44+
authorEmail, err = template.RenderChangesetTemplateField("authorEmail", task.Template.Commit.Author.Email, tmplCtx)
4445
if err != nil {
4546
return nil, err
4647
}
4748
}
4849

49-
title, err := renderChangesetTemplateField("title", task.Template.Title, tmplCtx)
50+
title, err := template.RenderChangesetTemplateField("title", task.Template.Title, tmplCtx)
5051
if err != nil {
5152
return nil, err
5253
}
5354

54-
body, err := renderChangesetTemplateField("body", task.Template.Body, tmplCtx)
55+
body, err := template.RenderChangesetTemplateField("body", task.Template.Body, tmplCtx)
5556
if err != nil {
5657
return nil, err
5758
}
5859

59-
message, err := renderChangesetTemplateField("message", task.Template.Commit.Message, tmplCtx)
60+
message, err := template.RenderChangesetTemplateField("message", task.Template.Commit.Message, tmplCtx)
6061
if err != nil {
6162
return nil, err
6263
}
6364

6465
// TODO: As a next step, we should extend the ChangesetTemplateContext to also include
6566
// TransformChanges.Group and then change validateGroups and groupFileDiffs to, for each group,
6667
// render the branch name *before* grouping the diffs.
67-
defaultBranch, err := renderChangesetTemplateField("branch", task.Template.Branch, tmplCtx)
68+
defaultBranch, err := template.RenderChangesetTemplateField("branch", task.Template.Branch, tmplCtx)
6869
if err != nil {
6970
return nil, err
7071
}

internal/batches/executor/changeset_specs_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
1111
"github.com/sourcegraph/src-cli/internal/batches"
1212
"github.com/sourcegraph/src-cli/internal/batches/git"
13+
"github.com/sourcegraph/src-cli/internal/batches/template"
1314
)
1415

1516
func TestCreateChangesetSpecs(t *testing.T) {
@@ -46,7 +47,7 @@ func TestCreateChangesetSpecs(t *testing.T) {
4647
}
4748

4849
defaultTask := &Task{
49-
BatchChangeAttributes: &BatchChangeAttributes{
50+
BatchChangeAttributes: &template.BatchChangeAttributes{
5051
Name: "the name",
5152
Description: "The description",
5253
},

internal/batches/executor/coordinator_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/sourcegraph/src-cli/internal/batches/git"
1616
"github.com/sourcegraph/src-cli/internal/batches/graphql"
1717
"github.com/sourcegraph/src-cli/internal/batches/mock"
18+
"github.com/sourcegraph/src-cli/internal/batches/template"
1819
)
1920

2021
func TestCoordinator_Execute(t *testing.T) {
@@ -266,7 +267,7 @@ func TestCoordinator_Execute(t *testing.T) {
266267
for _, t := range tc.tasks {
267268
t.TransformChanges = tc.batchSpec.TransformChanges
268269
t.Template = tc.batchSpec.ChangesetTemplate
269-
t.BatchChangeAttributes = &BatchChangeAttributes{
270+
t.BatchChangeAttributes = &template.BatchChangeAttributes{
270271
Name: tc.batchSpec.Name,
271272
Description: tc.batchSpec.Description,
272273
}
@@ -359,7 +360,7 @@ func TestCoordinator_Execute_StepCaching(t *testing.T) {
359360
{Run: `echo "three"`},
360361
},
361362
Repository: testRepo1,
362-
BatchChangeAttributes: &BatchChangeAttributes{},
363+
BatchChangeAttributes: &template.BatchChangeAttributes{},
363364
}
364365

365366
executor := &dummyExecutor{}
@@ -530,9 +531,7 @@ func (d *dummyTaskExecutionUI) TaskFinished(t *Task, err error) {
530531
d.mu.Lock()
531532
defer d.mu.Unlock()
532533

533-
if _, ok := d.started[t]; ok {
534-
delete(d.started, t)
535-
}
534+
delete(d.started, t)
536535
if err != nil {
537536
d.finishedWithErr[t] = struct{}{}
538537
} else {

internal/batches/executor/executor_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/sourcegraph/src-cli/internal/batches/docker"
2424
"github.com/sourcegraph/src-cli/internal/batches/git"
2525
"github.com/sourcegraph/src-cli/internal/batches/mock"
26+
"github.com/sourcegraph/src-cli/internal/batches/template"
2627
"github.com/sourcegraph/src-cli/internal/batches/workspace"
2728
)
2829

@@ -33,7 +34,7 @@ func TestExecutor_Integration(t *testing.T) {
3334

3435
addToPath(t, "testdata/dummydocker")
3536

36-
defaultBatchChangeAttributes := &BatchChangeAttributes{
37+
defaultBatchChangeAttributes := &template.BatchChangeAttributes{
3738
Name: "integration-test-batch-change",
3839
Description: "this is an integration test",
3940
}
@@ -488,7 +489,7 @@ index 02a19af..c9644dd 100644
488489
`)
489490

490491
task := &Task{
491-
BatchChangeAttributes: &BatchChangeAttributes{},
492+
BatchChangeAttributes: &template.BatchChangeAttributes{},
492493
Steps: []batcheslib.Step{
493494
{Run: `echo -e "foobar\n" >> README.md`},
494495
},
@@ -497,7 +498,7 @@ index 02a19af..c9644dd 100644
497498
StepIndex: 0,
498499
Diff: cachedDiff,
499500
Outputs: map[string]interface{}{},
500-
PreviousStepResult: StepResult{},
501+
PreviousStepResult: template.StepResult{},
501502
},
502503
Repository: testRepo1,
503504
}
@@ -594,7 +595,7 @@ index 0000000..257ae8e
594595

595596
task := &Task{
596597
Repository: testRepo1,
597-
BatchChangeAttributes: &BatchChangeAttributes{},
598+
BatchChangeAttributes: &template.BatchChangeAttributes{},
598599
Steps: []batcheslib.Step{
599600
{Run: `echo "this is step 1" >> README.txt`},
600601
{Run: `echo "this is step 2" >> README.md`},
@@ -615,7 +616,7 @@ echo "previous_step.modified_files=${{ previous_step.modified_files }}" >> READM
615616
Outputs: map[string]interface{}{
616617
"myOutput": "my-output.txt",
617618
},
618-
PreviousStepResult: StepResult{
619+
PreviousStepResult: template.StepResult{
619620
Files: &git.Changes{
620621
Modified: []string{"README.md"},
621622
Added: []string{"README.txt"},

internal/batches/executor/run_steps.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
1818
"github.com/sourcegraph/src-cli/internal/batches/git"
1919
"github.com/sourcegraph/src-cli/internal/batches/log"
20+
"github.com/sourcegraph/src-cli/internal/batches/template"
2021
"github.com/sourcegraph/src-cli/internal/batches/workspace"
2122

2223
yamlv3 "gopkg.in/yaml.v3"
@@ -33,7 +34,7 @@ type stepExecutionResult struct {
3334
Outputs map[string]interface{} `json:"outputs"`
3435
// PreviousStepResult is the StepResult of the step before Step, if Step !=
3536
// 0.
36-
PreviousStepResult StepResult `json:"previousStepResult"`
37+
PreviousStepResult template.StepResult `json:"previousStepResult"`
3738
}
3839

3940
type executionResult struct {
@@ -86,7 +87,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
8687
Outputs: make(map[string]interface{}),
8788
Path: opts.task.Path,
8889
}
89-
previousStepResult StepResult
90+
previousStepResult template.StepResult
9091
startStep int
9192
)
9293

@@ -122,11 +123,11 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
122123
for i := startStep; i < len(opts.task.Steps); i++ {
123124
step := opts.task.Steps[i]
124125

125-
stepContext := StepContext{
126+
stepContext := template.StepContext{
126127
BatchChange: *opts.task.BatchChangeAttributes,
127128
Repository: *opts.task.Repository,
128129
Outputs: execResult.Outputs,
129-
Steps: StepsContext{
130+
Steps: template.StepsContext{
130131
Path: execResult.Path,
131132
Changes: previousStepResult.Files,
132133
},
@@ -145,7 +146,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
145146
}
146147
}
147148

148-
cond, err := evalStepCondition(step.IfCondition(), &stepContext)
149+
cond, err := template.EvalStepCondition(step.IfCondition(), &stepContext)
149150
if err != nil {
150151
return execResult, nil, errors.Wrap(err, "evaluating step condition")
151152
}
@@ -172,7 +173,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
172173
return execResult, nil, errors.Wrap(err, "getting changed files in step")
173174
}
174175

175-
result := StepResult{Files: changes, Stdout: &stdoutBuffer, Stderr: &stderrBuffer}
176+
result := template.StepResult{Files: changes, Stdout: &stdoutBuffer, Stderr: &stderrBuffer}
176177

177178
// Set stepContext.Step to current step's results before rendering outputs
178179
stepContext.Step = result
@@ -220,7 +221,7 @@ func executeSingleStep(
220221
i int,
221222
step batcheslib.Step,
222223
imageDigest string,
223-
stepContext *StepContext,
224+
stepContext *template.StepContext,
224225
) (bytes.Buffer, bytes.Buffer, error) {
225226
// ----------
226227
// PREPARATION
@@ -259,7 +260,7 @@ func executeSingleStep(
259260
}
260261

261262
// Render the step.Env variables as templates.
262-
env, err := renderStepMap(stepEnv, stepContext)
263+
env, err := template.RenderStepMap(stepEnv, stepContext)
263264
if err != nil {
264265
return bytes.Buffer{}, bytes.Buffer{}, errors.Wrap(err, "parsing step environment")
265266
}
@@ -334,11 +335,11 @@ func executeSingleStep(
334335
return stdoutBuffer, stderrBuffer, nil
335336
}
336337

337-
func setOutputs(stepOutputs batcheslib.Outputs, global map[string]interface{}, stepCtx *StepContext) error {
338+
func setOutputs(stepOutputs batcheslib.Outputs, global map[string]interface{}, stepCtx *template.StepContext) error {
338339
for name, output := range stepOutputs {
339340
var value bytes.Buffer
340341

341-
if err := renderStepTemplate("outputs-"+name, output.Value, &value, stepCtx); err != nil {
342+
if err := template.RenderStepTemplate("outputs-"+name, output.Value, &value, stepCtx); err != nil {
342343
return errors.Wrap(err, "parsing step run")
343344
}
344345

@@ -414,9 +415,9 @@ func probeImageForShell(ctx context.Context, image string) (shell, tempfile stri
414415

415416
// createFilesToMount creates temporary files with the contents of Step.Files
416417
// that are to be mounted into the container that executes the step.
417-
func createFilesToMount(tempDir string, step batcheslib.Step, stepContext *StepContext) (map[string]*os.File, func(), error) {
418+
func createFilesToMount(tempDir string, step batcheslib.Step, stepContext *template.StepContext) (map[string]*os.File, func(), error) {
418419
// Parse and render the step.Files.
419-
files, err := renderStepMap(step.Files, stepContext)
420+
files, err := template.RenderStepMap(step.Files, stepContext)
420421
if err != nil {
421422
return nil, nil, errors.Wrap(err, "parsing step files")
422423
}
@@ -455,7 +456,7 @@ func createFilesToMount(tempDir string, step batcheslib.Step, stepContext *StepC
455456
// createRunScriptFile creates a temporary file and renders stepRun into it.
456457
//
457458
// It returns the location of the file, its content, a function to cleanup the file and possible errors.
458-
func createRunScriptFile(ctx context.Context, tempDir string, stepRun string, stepCtx *StepContext) (string, string, func(), error) {
459+
func createRunScriptFile(ctx context.Context, tempDir string, stepRun string, stepCtx *template.StepContext) (string, string, func(), error) {
459460
// Set up a temporary file on the host filesystem to contain the
460461
// script.
461462
runScriptFile, err := ioutil.TempFile(tempDir, "")
@@ -468,7 +469,7 @@ func createRunScriptFile(ctx context.Context, tempDir string, stepRun string, st
468469
// temp file we just created.
469470
var runScript bytes.Buffer
470471
out := io.MultiWriter(&runScript, runScriptFile)
471-
if err := renderStepTemplate("step-run", stepRun, out, stepCtx); err != nil {
472+
if err := template.RenderStepTemplate("step-run", stepRun, out, stepCtx); err != nil {
472473
return "", "", nil, errors.Wrap(err, "parsing step run")
473474
}
474475

internal/batches/executor/task.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
55
"github.com/sourcegraph/src-cli/internal/batches"
66
"github.com/sourcegraph/src-cli/internal/batches/graphql"
7+
"github.com/sourcegraph/src-cli/internal/batches/template"
78
)
89

910
type Task struct {
@@ -22,9 +23,9 @@ type Task struct {
2223

2324
// TODO(mrnugget): this should just be a single BatchSpec field instead, if
2425
// we can make it work with caching
25-
BatchChangeAttributes *BatchChangeAttributes `json:"-"`
26-
Template *batcheslib.ChangesetTemplate `json:"-"`
27-
TransformChanges *batcheslib.TransformChanges `json:"-"`
26+
BatchChangeAttributes *template.BatchChangeAttributes `json:"-"`
27+
Template *batcheslib.ChangesetTemplate `json:"-"`
28+
TransformChanges *batcheslib.TransformChanges `json:"-"`
2829

2930
Archive batches.RepoZip `json:"-"`
3031

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package service
2+
3+
import "github.com/sourcegraph/src-cli/internal/batches/graphql"
4+
5+
var testRepo1 = &graphql.Repository{
6+
ID: "src-cli",
7+
Name: "github.com/sourcegraph/src-cli",
8+
DefaultBranch: &graphql.Branch{Name: "main", Target: graphql.Target{OID: "d34db33f"}},
9+
FileMatches: map[string]bool{
10+
"README.md": true,
11+
"main.go": true,
12+
},
13+
}

0 commit comments

Comments
 (0)