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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`. [#491](https://github.com/sourcegraph/src-cli/pull/491)

### 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)
Expand Down
21 changes: 12 additions & 9 deletions internal/batches/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 23 additions & 3 deletions internal/batches/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -249,6 +253,9 @@ func TestExecutor_Integration(t *testing.T) {
"myOutputName3": Output{
Value: "cool-suffix",
},
"myOutputName4": Output{
Value: "${{ batch_change.name }}",
},
},
},
},
Expand All @@ -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 }}",
Expand All @@ -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",
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions internal/batches/run_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
}
Expand Down
26 changes: 15 additions & 11 deletions internal/batches/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -276,24 +278,26 @@ 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,
})
}
}
}

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,
})
}

Expand Down
23 changes: 23 additions & 0 deletions internal/batches/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
},
}
}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
},
Expand Down
16 changes: 16 additions & 0 deletions internal/batches/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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 }}
Expand All @@ -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]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 }}
Expand All @@ -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]
Expand Down