From caa83cdc8d60ef10d549258f3d944df32b78bb61 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Wed, 10 Mar 2021 10:57:02 +0100 Subject: [PATCH 1/3] Add BatchChange name/description to templating vars --- internal/batches/executor.go | 21 ++++++++++++--------- internal/batches/executor_test.go | 26 +++++++++++++++++++++++--- internal/batches/run_steps.go | 7 ++++--- internal/batches/service.go | 26 +++++++++++++++----------- internal/batches/templating.go | 23 +++++++++++++++++++++++ internal/batches/templating_test.go | 16 ++++++++++++++++ 6 files changed, 93 insertions(+), 26 deletions(-) diff --git a/internal/batches/executor.go b/internal/batches/executor.go index a2917fdba8..a3ab9d8614 100644 --- a/internal/batches/executor.go +++ b/internal/batches/executor.go @@ -69,8 +69,9 @@ type Task struct { Steps []Step Outputs map[string]interface{} - Template *ChangesetTemplate `json:"-"` - TransformChanges *TransformChanges `json:"-"` + BatchChangeAttributes *BatchChangeAttributes `json:"-"` + Template *ChangesetTemplate `json:"-"` + TransformChanges *TransformChanges `json:"-"` Archive RepoZip `json:"-"` } @@ -363,13 +364,14 @@ func (x *executor) do(ctx context.Context, task *Task) (err error) { // Actually execute the steps. opts := &executionOpts{ - archive: task.Archive, - wc: x.creator, - repo: task.Repository, - path: task.Path, - steps: task.Steps, - logger: log, - tempDir: x.tempDir, + archive: task.Archive, + wc: x.creator, + batchChangeAttributes: task.BatchChangeAttributes, + repo: task.Repository, + path: task.Path, + steps: task.Steps, + logger: log, + tempDir: x.tempDir, reportProgress: func(currentlyExecuting string) { x.updateTaskStatus(task, func(status *TaskStatus) { status.CurrentlyExecuting = currentlyExecuting @@ -463,6 +465,7 @@ func createChangesetSpecs(task *Task, result executionResult, features featureFl repo := task.Repository.Name tmplCtx := &ChangesetTemplateContext{ + BatchChangeAttributes: *task.BatchChangeAttributes, Steps: StepsContext{ Changes: result.ChangedFiles, Path: result.Path, diff --git a/internal/batches/executor_test.go b/internal/batches/executor_test.go index 73513e4c4b..afc9acf92d 100644 --- a/internal/batches/executor_test.go +++ b/internal/batches/executor_test.go @@ -50,6 +50,10 @@ func TestExecutor_Integration(t *testing.T) { changesetTemplateBranch := "my-branch" defaultTemplate := &ChangesetTemplate{Branch: changesetTemplateBranch} + defaultBatchChangeAttributes := &BatchChangeAttributes{ + Name: "integration-test-batch-change", + Description: "this is an integration test", + } type filesByBranch map[string][]string type filesByRepository map[string]filesByBranch @@ -249,6 +253,9 @@ func TestExecutor_Integration(t *testing.T) { "myOutputName3": Output{ Value: "cool-suffix", }, + "myOutputName4": Output{ + Value: "${{ batch_change.name }}", + }, }, }, }, @@ -262,7 +269,11 @@ modified_files=${{ steps.modified_files }} added_files=${{ steps.added_files }} deleted_files=${{ steps.deleted_files }} renamed_files=${{ steps.renamed_files }} -repository_name=${{ repository.name }}`, +repository_name=${{ repository.name }} +batch_change_name=${{ batch_change.name }} +batch_change_description=${{ batch_change.description }} +output4=${{ outputs.myOutputName4 }} +`, Branch: "templated-branch-${{ outputs.myOutputName3 }}", Commit: ExpandedGitCommitDescription{ Message: "myOutputName1=${{ outputs.myOutputName1}},myOutputName2=${{ outputs.myOutputName2.thisStepStdout }}", @@ -286,7 +297,10 @@ modified_files=[main.go] added_files=[] deleted_files=[] renamed_files=[] -repository_name=github.com/sourcegraph/src-cli`, +repository_name=github.com/sourcegraph/src-cli +batch_change_name=integration-test-batch-change +batch_change_description=this is an integration test +output4=integration-test-batch-change`, wantCommitMessage: "myOutputName1=main.go,myOutputName2=Hello World!", wantAuthorName: "myOutputName1=main.go", wantAuthorEmail: "myOutputName1=main.go", @@ -381,7 +395,6 @@ repository_name=github.com/sourcegraph/src-cli`, middle := func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Printf("PATH=%q\n", r.URL.Path) next.ServeHTTP(w, r) }) } @@ -433,6 +446,9 @@ repository_name=github.com/sourcegraph/src-cli`, if task.Template == nil { task.Template = defaultTemplate } + if task.BatchChangeAttributes == nil { + task.BatchChangeAttributes = defaultBatchChangeAttributes + } if tc.transform != nil { task.TransformChanges = tc.transform } @@ -772,6 +788,10 @@ func TestCreateChangesetSpecs(t *testing.T) { } defaultTask := &Task{ + BatchChangeAttributes: &BatchChangeAttributes{ + Name: "the name", + Description: "The description", + }, Template: &ChangesetTemplate{ Title: "The title", Body: "The body", diff --git a/internal/batches/run_steps.go b/internal/batches/run_steps.go index aa2f708cf7..4cba100650 100644 --- a/internal/batches/run_steps.go +++ b/internal/batches/run_steps.go @@ -41,8 +41,9 @@ type executionOpts struct { wc WorkspaceCreator path string - repo *graphql.Repository - steps []Step + batchChangeAttributes *BatchChangeAttributes + repo *graphql.Repository + steps []Step tempDir string @@ -74,7 +75,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult, for i, step := range opts.steps { opts.reportProgress(fmt.Sprintf("Preparing step %d", i+1)) - stepContext := StepContext{Repository: *opts.repo, Outputs: execResult.Outputs} + stepContext := StepContext{BatchChange: *opts.batchChangeAttributes, Repository: *opts.repo, Outputs: execResult.Outputs} if i > 0 { stepContext.PreviousStep = results[i-1] } diff --git a/internal/batches/service.go b/internal/batches/service.go index 42131269f0..8cbce73d5a 100644 --- a/internal/batches/service.go +++ b/internal/batches/service.go @@ -256,6 +256,8 @@ func (svc *Service) BuildTasks(ctx context.Context, repos []*graphql.Repository, var tasks []*Task + attr := &BatchChangeAttributes{Name: spec.Name, Description: spec.Description} + for configIndex, repos := range reposByWorkspaceConfig { workspaceConfig := workspaceConfigs[configIndex] repoDirs, err := svc.FindDirectoriesInRepos(ctx, workspaceConfig.RootAtLocationOf, repos...) @@ -276,12 +278,13 @@ func (svc *Service) BuildTasks(ctx context.Context, repos []*graphql.Repository, } tasks = append(tasks, &Task{ - Repository: repo, - Path: d, - Steps: spec.Steps, - TransformChanges: spec.TransformChanges, - Template: spec.ChangesetTemplate, - OnlyFetchWorkspace: workspaceConfig.OnlyFetchWorkspace, + Repository: repo, + Path: d, + Steps: spec.Steps, + TransformChanges: spec.TransformChanges, + Template: spec.ChangesetTemplate, + BatchChangeAttributes: attr, + OnlyFetchWorkspace: workspaceConfig.OnlyFetchWorkspace, }) } } @@ -289,11 +292,12 @@ func (svc *Service) BuildTasks(ctx context.Context, repos []*graphql.Repository, for r := range rootWorkspace { tasks = append(tasks, &Task{ - Repository: r, - Path: "", - Steps: spec.Steps, - TransformChanges: spec.TransformChanges, - Template: spec.ChangesetTemplate, + Repository: r, + Path: "", + Steps: spec.Steps, + TransformChanges: spec.TransformChanges, + Template: spec.ChangesetTemplate, + BatchChangeAttributes: attr, }) } diff --git a/internal/batches/templating.go b/internal/batches/templating.go index 4e026bac36..c537c4682a 100644 --- a/internal/batches/templating.go +++ b/internal/batches/templating.go @@ -40,9 +40,16 @@ func renderStepMap(m map[string]string, stepCtx *StepContext) (map[string]string return rendered, nil } +type BatchChangeAttributes struct { + Name string + Description string +} + // StepContext represents the contextual information available when rendering a // step's fields, such as "run" or "outputs", as templates. type StepContext struct { + // BatchChange are the attributes in the BatchSpec that are set on the BatchChange. + BatchChange BatchChangeAttributes // Outputs are the outputs set by the current and all previous steps. Outputs map[string]interface{} // Step is the result of the current step. Empty when evaluating the "run" field @@ -115,6 +122,12 @@ func (stepCtx *StepContext) ToFuncMap() template.FuncMap { "name": stepCtx.Repository.Name, } }, + "batch_change": func() map[string]interface{} { + return map[string]interface{}{ + "name": stepCtx.BatchChange.Name, + "description": stepCtx.BatchChange.Description, + } + }, } } @@ -180,6 +193,10 @@ type StepsContext struct { // ChangesetTemplateContext represents the contextual information available // when rendering a field of the ChangesetTemplate as a template. type ChangesetTemplateContext struct { + // BatchChangeAttributes are the attributes of the BatchChange that will be + // created/updated. + BatchChangeAttributes BatchChangeAttributes + // Steps are the changes made by all steps that were executed. Steps StepsContext @@ -212,6 +229,12 @@ func (tmplCtx *ChangesetTemplateContext) ToFuncMap() template.FuncMap { "name": tmplCtx.Repository.Name, } }, + "batch_change": func() map[string]interface{} { + return map[string]interface{}{ + "name": tmplCtx.BatchChangeAttributes.Name, + "description": tmplCtx.BatchChangeAttributes.Description, + } + }, "outputs": func() map[string]interface{} { return tmplCtx.Outputs }, diff --git a/internal/batches/templating_test.go b/internal/batches/templating_test.go index ad0b484241..7e95ee5364 100644 --- a/internal/batches/templating_test.go +++ b/internal/batches/templating_test.go @@ -55,6 +55,10 @@ func TestRenderStepTemplate(t *testing.T) { } stepCtx := &StepContext{ + BatchChange: BatchChangeAttributes{ + Name: "test-batch-change", + Description: "This batch change is just an experiment", + }, PreviousStep: StepResult{ files: &StepChanges{ Modified: []string{"go.mod"}, @@ -99,6 +103,8 @@ func TestRenderStepTemplate(t *testing.T) { stepCtx: stepCtx, run: `${{ repository.search_result_paths }} ${{ repository.name }} +${{ batch_change.name }} +${{ batch_change.description }} ${{ previous_step.modified_files }} ${{ previous_step.added_files }} ${{ previous_step.deleted_files }} @@ -116,6 +122,8 @@ ${{ step.stderr}} `, want: `README.md main.go github.com/sourcegraph/src-cli +test-batch-change +This batch change is just an experiment [go.mod] [main.go.swp] [.DS_Store] @@ -242,6 +250,10 @@ func TestRenderChangesetTemplateField(t *testing.T) { } tmplCtx := &ChangesetTemplateContext{ + BatchChangeAttributes: BatchChangeAttributes{ + Name: "test-batch-change", + Description: "This batch change is just an experiment", + }, Outputs: map[string]interface{}{ "lastLine": "lastLine is this", "project": parsedYaml, @@ -275,6 +287,8 @@ func TestRenderChangesetTemplateField(t *testing.T) { tmplCtx: tmplCtx, run: `${{ repository.search_result_paths }} ${{ repository.name }} +${{ batch_change.name }} +${{ batch_change.description }} ${{ outputs.lastLine }} ${{ index outputs.project.env 1 }} ${{ steps.modified_files }} @@ -285,6 +299,8 @@ ${{ steps.path }} `, want: `README.md main.go github.com/sourcegraph/src-cli +test-batch-change +This batch change is just an experiment lastLine is this CGO_ENABLED=0 [modified-file.txt] From 3fbb64145d2112b7ebc852bca05b4cbbb5e035d2 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Wed, 10 Mar 2021 10:58:23 +0100 Subject: [PATCH 2/3] Add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b40d5f3cff..09097cdd32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file. ### Added +- Two new [templating](https://docs.sourcegraph.com/campaigns/references/batch_spec_templating) variables have been added: `batch_change.name` and `batch_change.description`. + ### Changed - 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) From dcf079a184f6c84b8aeecf418d347f068533f2ba Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Wed, 10 Mar 2021 11:02:33 +0100 Subject: [PATCH 3/3] Add PR number to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09097cdd32..4db14d9e61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ All notable changes to `src-cli` are documented in this file. ### Added -- Two new [templating](https://docs.sourcegraph.com/campaigns/references/batch_spec_templating) variables have been added: `batch_change.name` and `batch_change.description`. +- 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) ### Changed