Skip to content

Commit 87fcc38

Browse files
authored
Add batch_change.{name,description} as templating variables (#491)
* Add BatchChange name/description to templating vars * Add changelog * Add PR number to changelog
1 parent 6b722ff commit 87fcc38

File tree

7 files changed

+95
-26
lines changed

7 files changed

+95
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file.
1313

1414
### Added
1515

16+
- Two new [templating](https://docs.sourcegraph.com/campaigns/references/batch_spec_templating) variables have been added: `batch_change.name` and `batch_change.description`. [#491](https://github.com/sourcegraph/src-cli/pull/491)
17+
1618
### Changed
1719

1820
- Campaigns are now known as Batch Changes! The `src campaign` set of commands have been renamed to `src batch`; however, `src campaign` and `src campaigns` will be retained as aliases for `src batch` until the next major version of Sourcegraph. There should be no breaking changes as a result of this change. [#489](https://github.com/sourcegraph/src-cli/pull/489)

internal/batches/executor.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ type Task struct {
6969
Steps []Step
7070
Outputs map[string]interface{}
7171

72-
Template *ChangesetTemplate `json:"-"`
73-
TransformChanges *TransformChanges `json:"-"`
72+
BatchChangeAttributes *BatchChangeAttributes `json:"-"`
73+
Template *ChangesetTemplate `json:"-"`
74+
TransformChanges *TransformChanges `json:"-"`
7475

7576
Archive RepoZip `json:"-"`
7677
}
@@ -363,13 +364,14 @@ func (x *executor) do(ctx context.Context, task *Task) (err error) {
363364

364365
// Actually execute the steps.
365366
opts := &executionOpts{
366-
archive: task.Archive,
367-
wc: x.creator,
368-
repo: task.Repository,
369-
path: task.Path,
370-
steps: task.Steps,
371-
logger: log,
372-
tempDir: x.tempDir,
367+
archive: task.Archive,
368+
wc: x.creator,
369+
batchChangeAttributes: task.BatchChangeAttributes,
370+
repo: task.Repository,
371+
path: task.Path,
372+
steps: task.Steps,
373+
logger: log,
374+
tempDir: x.tempDir,
373375
reportProgress: func(currentlyExecuting string) {
374376
x.updateTaskStatus(task, func(status *TaskStatus) {
375377
status.CurrentlyExecuting = currentlyExecuting
@@ -463,6 +465,7 @@ func createChangesetSpecs(task *Task, result executionResult, features featureFl
463465
repo := task.Repository.Name
464466

465467
tmplCtx := &ChangesetTemplateContext{
468+
BatchChangeAttributes: *task.BatchChangeAttributes,
466469
Steps: StepsContext{
467470
Changes: result.ChangedFiles,
468471
Path: result.Path,

internal/batches/executor_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func TestExecutor_Integration(t *testing.T) {
5050

5151
changesetTemplateBranch := "my-branch"
5252
defaultTemplate := &ChangesetTemplate{Branch: changesetTemplateBranch}
53+
defaultBatchChangeAttributes := &BatchChangeAttributes{
54+
Name: "integration-test-batch-change",
55+
Description: "this is an integration test",
56+
}
5357

5458
type filesByBranch map[string][]string
5559
type filesByRepository map[string]filesByBranch
@@ -249,6 +253,9 @@ func TestExecutor_Integration(t *testing.T) {
249253
"myOutputName3": Output{
250254
Value: "cool-suffix",
251255
},
256+
"myOutputName4": Output{
257+
Value: "${{ batch_change.name }}",
258+
},
252259
},
253260
},
254261
},
@@ -262,7 +269,11 @@ modified_files=${{ steps.modified_files }}
262269
added_files=${{ steps.added_files }}
263270
deleted_files=${{ steps.deleted_files }}
264271
renamed_files=${{ steps.renamed_files }}
265-
repository_name=${{ repository.name }}`,
272+
repository_name=${{ repository.name }}
273+
batch_change_name=${{ batch_change.name }}
274+
batch_change_description=${{ batch_change.description }}
275+
output4=${{ outputs.myOutputName4 }}
276+
`,
266277
Branch: "templated-branch-${{ outputs.myOutputName3 }}",
267278
Commit: ExpandedGitCommitDescription{
268279
Message: "myOutputName1=${{ outputs.myOutputName1}},myOutputName2=${{ outputs.myOutputName2.thisStepStdout }}",
@@ -286,7 +297,10 @@ modified_files=[main.go]
286297
added_files=[]
287298
deleted_files=[]
288299
renamed_files=[]
289-
repository_name=github.com/sourcegraph/src-cli`,
300+
repository_name=github.com/sourcegraph/src-cli
301+
batch_change_name=integration-test-batch-change
302+
batch_change_description=this is an integration test
303+
output4=integration-test-batch-change`,
290304
wantCommitMessage: "myOutputName1=main.go,myOutputName2=Hello World!",
291305
wantAuthorName: "myOutputName1=main.go",
292306
wantAuthorEmail: "myOutputName1=main.go",
@@ -381,7 +395,6 @@ repository_name=github.com/sourcegraph/src-cli`,
381395

382396
middle := func(next http.Handler) http.Handler {
383397
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
384-
fmt.Printf("PATH=%q\n", r.URL.Path)
385398
next.ServeHTTP(w, r)
386399
})
387400
}
@@ -433,6 +446,9 @@ repository_name=github.com/sourcegraph/src-cli`,
433446
if task.Template == nil {
434447
task.Template = defaultTemplate
435448
}
449+
if task.BatchChangeAttributes == nil {
450+
task.BatchChangeAttributes = defaultBatchChangeAttributes
451+
}
436452
if tc.transform != nil {
437453
task.TransformChanges = tc.transform
438454
}
@@ -772,6 +788,10 @@ func TestCreateChangesetSpecs(t *testing.T) {
772788
}
773789

774790
defaultTask := &Task{
791+
BatchChangeAttributes: &BatchChangeAttributes{
792+
Name: "the name",
793+
Description: "The description",
794+
},
775795
Template: &ChangesetTemplate{
776796
Title: "The title",
777797
Body: "The body",

internal/batches/run_steps.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ type executionOpts struct {
4141
wc WorkspaceCreator
4242
path string
4343

44-
repo *graphql.Repository
45-
steps []Step
44+
batchChangeAttributes *BatchChangeAttributes
45+
repo *graphql.Repository
46+
steps []Step
4647

4748
tempDir string
4849

@@ -74,7 +75,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
7475
for i, step := range opts.steps {
7576
opts.reportProgress(fmt.Sprintf("Preparing step %d", i+1))
7677

77-
stepContext := StepContext{Repository: *opts.repo, Outputs: execResult.Outputs}
78+
stepContext := StepContext{BatchChange: *opts.batchChangeAttributes, Repository: *opts.repo, Outputs: execResult.Outputs}
7879
if i > 0 {
7980
stepContext.PreviousStep = results[i-1]
8081
}

internal/batches/service.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ func (svc *Service) BuildTasks(ctx context.Context, repos []*graphql.Repository,
256256

257257
var tasks []*Task
258258

259+
attr := &BatchChangeAttributes{Name: spec.Name, Description: spec.Description}
260+
259261
for configIndex, repos := range reposByWorkspaceConfig {
260262
workspaceConfig := workspaceConfigs[configIndex]
261263
repoDirs, err := svc.FindDirectoriesInRepos(ctx, workspaceConfig.RootAtLocationOf, repos...)
@@ -276,24 +278,26 @@ func (svc *Service) BuildTasks(ctx context.Context, repos []*graphql.Repository,
276278
}
277279

278280
tasks = append(tasks, &Task{
279-
Repository: repo,
280-
Path: d,
281-
Steps: spec.Steps,
282-
TransformChanges: spec.TransformChanges,
283-
Template: spec.ChangesetTemplate,
284-
OnlyFetchWorkspace: workspaceConfig.OnlyFetchWorkspace,
281+
Repository: repo,
282+
Path: d,
283+
Steps: spec.Steps,
284+
TransformChanges: spec.TransformChanges,
285+
Template: spec.ChangesetTemplate,
286+
BatchChangeAttributes: attr,
287+
OnlyFetchWorkspace: workspaceConfig.OnlyFetchWorkspace,
285288
})
286289
}
287290
}
288291
}
289292

290293
for r := range rootWorkspace {
291294
tasks = append(tasks, &Task{
292-
Repository: r,
293-
Path: "",
294-
Steps: spec.Steps,
295-
TransformChanges: spec.TransformChanges,
296-
Template: spec.ChangesetTemplate,
295+
Repository: r,
296+
Path: "",
297+
Steps: spec.Steps,
298+
TransformChanges: spec.TransformChanges,
299+
Template: spec.ChangesetTemplate,
300+
BatchChangeAttributes: attr,
297301
})
298302
}
299303

internal/batches/templating.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ func renderStepMap(m map[string]string, stepCtx *StepContext) (map[string]string
4040
return rendered, nil
4141
}
4242

43+
type BatchChangeAttributes struct {
44+
Name string
45+
Description string
46+
}
47+
4348
// StepContext represents the contextual information available when rendering a
4449
// step's fields, such as "run" or "outputs", as templates.
4550
type StepContext struct {
51+
// BatchChange are the attributes in the BatchSpec that are set on the BatchChange.
52+
BatchChange BatchChangeAttributes
4653
// Outputs are the outputs set by the current and all previous steps.
4754
Outputs map[string]interface{}
4855
// Step is the result of the current step. Empty when evaluating the "run" field
@@ -115,6 +122,12 @@ func (stepCtx *StepContext) ToFuncMap() template.FuncMap {
115122
"name": stepCtx.Repository.Name,
116123
}
117124
},
125+
"batch_change": func() map[string]interface{} {
126+
return map[string]interface{}{
127+
"name": stepCtx.BatchChange.Name,
128+
"description": stepCtx.BatchChange.Description,
129+
}
130+
},
118131
}
119132
}
120133

@@ -180,6 +193,10 @@ type StepsContext struct {
180193
// ChangesetTemplateContext represents the contextual information available
181194
// when rendering a field of the ChangesetTemplate as a template.
182195
type ChangesetTemplateContext struct {
196+
// BatchChangeAttributes are the attributes of the BatchChange that will be
197+
// created/updated.
198+
BatchChangeAttributes BatchChangeAttributes
199+
183200
// Steps are the changes made by all steps that were executed.
184201
Steps StepsContext
185202

@@ -212,6 +229,12 @@ func (tmplCtx *ChangesetTemplateContext) ToFuncMap() template.FuncMap {
212229
"name": tmplCtx.Repository.Name,
213230
}
214231
},
232+
"batch_change": func() map[string]interface{} {
233+
return map[string]interface{}{
234+
"name": tmplCtx.BatchChangeAttributes.Name,
235+
"description": tmplCtx.BatchChangeAttributes.Description,
236+
}
237+
},
215238
"outputs": func() map[string]interface{} {
216239
return tmplCtx.Outputs
217240
},

internal/batches/templating_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func TestRenderStepTemplate(t *testing.T) {
5555
}
5656

5757
stepCtx := &StepContext{
58+
BatchChange: BatchChangeAttributes{
59+
Name: "test-batch-change",
60+
Description: "This batch change is just an experiment",
61+
},
5862
PreviousStep: StepResult{
5963
files: &StepChanges{
6064
Modified: []string{"go.mod"},
@@ -99,6 +103,8 @@ func TestRenderStepTemplate(t *testing.T) {
99103
stepCtx: stepCtx,
100104
run: `${{ repository.search_result_paths }}
101105
${{ repository.name }}
106+
${{ batch_change.name }}
107+
${{ batch_change.description }}
102108
${{ previous_step.modified_files }}
103109
${{ previous_step.added_files }}
104110
${{ previous_step.deleted_files }}
@@ -116,6 +122,8 @@ ${{ step.stderr}}
116122
`,
117123
want: `README.md main.go
118124
github.com/sourcegraph/src-cli
125+
test-batch-change
126+
This batch change is just an experiment
119127
[go.mod]
120128
[main.go.swp]
121129
[.DS_Store]
@@ -242,6 +250,10 @@ func TestRenderChangesetTemplateField(t *testing.T) {
242250
}
243251

244252
tmplCtx := &ChangesetTemplateContext{
253+
BatchChangeAttributes: BatchChangeAttributes{
254+
Name: "test-batch-change",
255+
Description: "This batch change is just an experiment",
256+
},
245257
Outputs: map[string]interface{}{
246258
"lastLine": "lastLine is this",
247259
"project": parsedYaml,
@@ -275,6 +287,8 @@ func TestRenderChangesetTemplateField(t *testing.T) {
275287
tmplCtx: tmplCtx,
276288
run: `${{ repository.search_result_paths }}
277289
${{ repository.name }}
290+
${{ batch_change.name }}
291+
${{ batch_change.description }}
278292
${{ outputs.lastLine }}
279293
${{ index outputs.project.env 1 }}
280294
${{ steps.modified_files }}
@@ -285,6 +299,8 @@ ${{ steps.path }}
285299
`,
286300
want: `README.md main.go
287301
github.com/sourcegraph/src-cli
302+
test-batch-change
303+
This batch change is just an experiment
288304
lastLine is this
289305
CGO_ENABLED=0
290306
[modified-file.txt]

0 commit comments

Comments
 (0)