diff --git a/go.mod b/go.mod index 58705db20d..86517eed9e 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/sourcegraph/batch-change-utils v0.0.0-20210708162152-c9f35b905d94 github.com/sourcegraph/go-diff v0.6.1 github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf - github.com/sourcegraph/sourcegraph/lib v0.0.0-20210831122102-a95181f49527 + github.com/sourcegraph/sourcegraph/lib v0.0.0-20210901132614-5780c9c4b466 github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect golang.org/x/net v0.0.0-20210614182718-04defd469f4e diff --git a/go.sum b/go.sum index 086bf13b26..735b720c57 100644 --- a/go.sum +++ b/go.sum @@ -263,8 +263,8 @@ github.com/sourcegraph/go-diff v0.6.1 h1:hmA1LzxW0n1c3Q4YbrFgg4P99GSnebYa3x8gr0H github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs= github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf h1:oAdWFqhStsWiiMP/vkkHiMXqFXzl1XfUNOdxKJbd6bI= github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf/go.mod h1:ppFaPm6kpcHnZGqQTFhUIAQRIEhdQDWP1PCv4/ON354= -github.com/sourcegraph/sourcegraph/lib v0.0.0-20210831122102-a95181f49527 h1:Bj2XOghFkSnxE0EcpmtmjIFbqDkJ0wBNTvM6cguKaKs= -github.com/sourcegraph/sourcegraph/lib v0.0.0-20210831122102-a95181f49527/go.mod h1:aH/URdfJ92i5Ec/5u2GSlliMZtKzsD91oTJNwZpH8Hc= +github.com/sourcegraph/sourcegraph/lib v0.0.0-20210901132614-5780c9c4b466 h1:hyo3Vr63aieMyWqrfy0skzZHT3ACjajrd3ksVxmMl7k= +github.com/sourcegraph/sourcegraph/lib v0.0.0-20210901132614-5780c9c4b466/go.mod h1:lSNpzAxCBx40MpkM/DbMH7ZUMURcpAstABzhbc4O5V8= github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152 h1:z/MpntplPaW6QW95pzcAR/72Z5TWDyDnSo0EOcyij9o= github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= diff --git a/internal/batches/executor/changeset_specs.go b/internal/batches/executor/changeset_specs.go index 44844bdd44..a2a56cd701 100644 --- a/internal/batches/executor/changeset_specs.go +++ b/internal/batches/executor/changeset_specs.go @@ -9,6 +9,7 @@ import ( batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" "github.com/sourcegraph/src-cli/internal/batches" "github.com/sourcegraph/src-cli/internal/batches/template" + "github.com/sourcegraph/src-cli/internal/batches/util" ) 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.`) @@ -23,7 +24,7 @@ func createChangesetSpecs(task *Task, result executionResult, features batches.F Path: result.Path, }, Outputs: result.Outputs, - Repository: *task.Repository, + Repository: util.GraphQLRepoToTemplatingRepo(task.Repository), } var authorName string diff --git a/internal/batches/executor/changeset_specs_test.go b/internal/batches/executor/changeset_specs_test.go index 79bed582bf..6e626fa4a4 100644 --- a/internal/batches/executor/changeset_specs_test.go +++ b/internal/batches/executor/changeset_specs_test.go @@ -8,8 +8,8 @@ import ( "github.com/mitchellh/copystructure" "github.com/sourcegraph/batch-change-utils/overridable" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "github.com/sourcegraph/src-cli/internal/batches" - "github.com/sourcegraph/src-cli/internal/batches/git" "github.com/sourcegraph/src-cli/internal/batches/template" ) diff --git a/internal/batches/executor/coordinator_test.go b/internal/batches/executor/coordinator_test.go index b158a0bc47..ec95e6876e 100644 --- a/internal/batches/executor/coordinator_test.go +++ b/internal/batches/executor/coordinator_test.go @@ -11,7 +11,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/sourcegraph/batch-change-utils/overridable" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" - "github.com/sourcegraph/src-cli/internal/batches/git" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "github.com/sourcegraph/src-cli/internal/batches/graphql" "github.com/sourcegraph/src-cli/internal/batches/mock" "github.com/sourcegraph/src-cli/internal/batches/template" diff --git a/internal/batches/executor/execution_cache_test.go b/internal/batches/executor/execution_cache_test.go index 5eb5084c49..14ccfcc5e3 100644 --- a/internal/batches/executor/execution_cache_test.go +++ b/internal/batches/executor/execution_cache_test.go @@ -8,7 +8,7 @@ import ( "github.com/google/go-cmp/cmp" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" - "github.com/sourcegraph/src-cli/internal/batches/git" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "gopkg.in/yaml.v3" ) diff --git a/internal/batches/executor/executor_test.go b/internal/batches/executor/executor_test.go index d5af386d4c..78366665ff 100644 --- a/internal/batches/executor/executor_test.go +++ b/internal/batches/executor/executor_test.go @@ -18,10 +18,10 @@ import ( "github.com/pkg/errors" "github.com/sourcegraph/go-diff/diff" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "github.com/sourcegraph/src-cli/internal/api" "github.com/sourcegraph/src-cli/internal/batches" "github.com/sourcegraph/src-cli/internal/batches/docker" - "github.com/sourcegraph/src-cli/internal/batches/git" "github.com/sourcegraph/src-cli/internal/batches/mock" "github.com/sourcegraph/src-cli/internal/batches/template" "github.com/sourcegraph/src-cli/internal/batches/workspace" diff --git a/internal/batches/executor/run_steps.go b/internal/batches/executor/run_steps.go index 43de56c3c0..8cfa43dfaf 100644 --- a/internal/batches/executor/run_steps.go +++ b/internal/batches/executor/run_steps.go @@ -15,9 +15,10 @@ import ( "github.com/hashicorp/go-multierror" "github.com/pkg/errors" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" - "github.com/sourcegraph/src-cli/internal/batches/git" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "github.com/sourcegraph/src-cli/internal/batches/log" "github.com/sourcegraph/src-cli/internal/batches/template" + "github.com/sourcegraph/src-cli/internal/batches/util" "github.com/sourcegraph/src-cli/internal/batches/workspace" yamlv3 "gopkg.in/yaml.v3" @@ -125,7 +126,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult, stepContext := template.StepContext{ BatchChange: *opts.task.BatchChangeAttributes, - Repository: *opts.task.Repository, + Repository: util.GraphQLRepoToTemplatingRepo(opts.task.Repository), Outputs: execResult.Outputs, Steps: template.StepsContext{ Path: execResult.Path, diff --git a/internal/batches/features_test.go b/internal/batches/features_test.go deleted file mode 100644 index 0c23368752..0000000000 --- a/internal/batches/features_test.go +++ /dev/null @@ -1 +0,0 @@ -package batches diff --git a/internal/batches/git/changes.go b/internal/batches/git/changes.go deleted file mode 100644 index ff31286e30..0000000000 --- a/internal/batches/git/changes.go +++ /dev/null @@ -1,73 +0,0 @@ -package git - -import ( - "fmt" - "strings" - - "github.com/sourcegraph/go-diff/diff" -) - -// Changes are the changes made to files in a repository. -type Changes struct { - Modified []string `json:"modified"` - Added []string `json:"added"` - Deleted []string `json:"deleted"` - Renamed []string `json:"renamed"` -} - -// ParseStatus parses the output of `git status` and turns it into Changes. -func ParseGitStatus(out []byte) (Changes, error) { - result := Changes{} - - stripped := strings.TrimSpace(string(out)) - if stripped == "" { - return result, nil - } - - for _, line := range strings.Split(stripped, "\n") { - if len(line) < 4 { - return result, fmt.Errorf("git status line has unrecognized format: %q", line) - } - - file := line[3:] - - switch line[0] { - case 'M': - result.Modified = append(result.Modified, file) - case 'A': - result.Added = append(result.Added, file) - case 'D': - result.Deleted = append(result.Deleted, file) - case 'R': - files := strings.Split(file, " -> ") - newFile := files[len(files)-1] - result.Renamed = append(result.Renamed, newFile) - } - } - - return result, nil -} - -func ChangesInDiff(rawDiff []byte) (Changes, error) { - result := Changes{} - - fileDiffs, err := diff.ParseMultiFileDiff(rawDiff) - if err != nil { - return result, err - } - - for _, fd := range fileDiffs { - switch { - case fd.NewName == "/dev/null": - result.Deleted = append(result.Deleted, fd.OrigName) - case fd.OrigName == "/dev/null": - result.Added = append(result.Added, fd.NewName) - case fd.OrigName == fd.NewName: - result.Modified = append(result.Modified, fd.OrigName) - case fd.OrigName != fd.NewName: - result.Renamed = append(result.Renamed, fd.NewName) - } - } - - return result, nil -} diff --git a/internal/batches/git/changes_test.go b/internal/batches/git/changes_test.go deleted file mode 100644 index c020f708a1..0000000000 --- a/internal/batches/git/changes_test.go +++ /dev/null @@ -1,80 +0,0 @@ -package git - -import ( - "testing" - - "github.com/google/go-cmp/cmp" -) - -func TestParseGitStatus(t *testing.T) { - const input = `M README.md -M another_file.go -A new_file.txt -A barfoo/new_file.txt -D to_be_deleted.txt -R README.md -> README.markdown -` - parsed, err := ParseGitStatus([]byte(input)) - if err != nil { - t.Fatal(err) - } - - want := Changes{ - Modified: []string{"README.md", "another_file.go"}, - Added: []string{"new_file.txt", "barfoo/new_file.txt"}, - Deleted: []string{"to_be_deleted.txt"}, - Renamed: []string{"README.markdown"}, - } - - if !cmp.Equal(want, parsed) { - t.Fatalf("wrong output:\n%s", cmp.Diff(want, parsed)) - } -} - -func TestChangesInDiff(t *testing.T) { - const input = `diff --git README.md README.md -index c9644dd..2552420 100644 ---- README.md -+++ README.md -@@ -1,2 +1,3 @@ - # Welcome to the README - foobar -+barfoo and what else? -diff --git a_new_file_appears.txt a_new_file_appears.txt -new file mode 100644 -index 0000000..09f946b ---- /dev/null -+++ a_new_file_appears.txt -@@ -0,0 +1 @@ -+boom! like magic it appears -diff --git another_file.txt another_cool_file.txt -similarity index 100% -rename from another_file.txt -rename to another_cool_file.txt -diff --git yet_another_file.txt yet_another_file.txt -deleted file mode 100644 -index c27a40c..0000000 ---- yet_another_file.txt -+++ /dev/null -@@ -1,3 +0,0 @@ --this is yet another file --this time though --with tree lines -` - - changes, err := ChangesInDiff([]byte(input)) - if err != nil { - t.Fatal(err) - } - - want := Changes{ - Modified: []string{"README.md"}, - Added: []string{"a_new_file_appears.txt"}, - Deleted: []string{"yet_another_file.txt"}, - Renamed: []string{"another_cool_file.txt"}, - } - - if !cmp.Equal(want, changes) { - t.Fatalf("wrong output:\n%s", cmp.Diff(want, changes)) - } -} diff --git a/internal/batches/graphql/repository.go b/internal/batches/graphql/repository.go index 9a6ddd6184..d08b756fbc 100644 --- a/internal/batches/graphql/repository.go +++ b/internal/batches/graphql/repository.go @@ -3,7 +3,6 @@ package graphql import ( "crypto/sha256" "encoding/base64" - "sort" "strings" ) @@ -93,16 +92,3 @@ func (r *Repository) SlugForPath(path string) string { } return strings.ReplaceAll(name, "/", "-") + "-" + r.Rev() } - -func (r *Repository) SearchResultPaths() (list fileMatchPathList) { - var files []string - for f := range r.FileMatches { - files = append(files, f) - } - sort.Strings(files) - return fileMatchPathList(files) -} - -type fileMatchPathList []string - -func (f fileMatchPathList) String() string { return strings.Join(f, " ") } diff --git a/internal/batches/service/build_tasks.go b/internal/batches/service/build_tasks.go index 9f7f91e538..cb5c6dc90f 100644 --- a/internal/batches/service/build_tasks.go +++ b/internal/batches/service/build_tasks.go @@ -8,6 +8,7 @@ import ( "github.com/sourcegraph/src-cli/internal/batches/executor" "github.com/sourcegraph/src-cli/internal/batches/graphql" "github.com/sourcegraph/src-cli/internal/batches/template" + "github.com/sourcegraph/src-cli/internal/batches/util" ) // buildTasks returns tasks for all the workspaces determined for the given spec. @@ -37,21 +38,23 @@ func buildTasks(ctx context.Context, spec *batcheslib.BatchSpec, repos []*graphq } func buildTask(spec *batcheslib.BatchSpec, r *graphql.Repository, path string, onlyWorkspace bool) (*executor.Task, bool, error) { - stepCtx := &template.StepContext{ - Repository: *r, - BatchChange: template.BatchChangeAttributes{ - Name: spec.Name, - Description: spec.Description, - }, + batchChange := template.BatchChangeAttributes{ + Name: spec.Name, + Description: spec.Description, } - var taskSteps []batcheslib.Step + taskSteps := []batcheslib.Step{} for _, step := range spec.Steps { + // If no if condition is given, just go ahead and add the step to the list. if step.IfCondition() == "" { taskSteps = append(taskSteps, step) continue } + stepCtx := &template.StepContext{ + Repository: util.GraphQLRepoToTemplatingRepo(r), + BatchChange: batchChange, + } static, boolVal, err := template.IsStaticBool(step.IfCondition(), stepCtx) if err != nil { return nil, false, err @@ -82,11 +85,8 @@ func buildTask(spec *batcheslib.BatchSpec, r *graphql.Repository, path string, o Steps: taskSteps, OnlyFetchWorkspace: onlyWorkspace, - TransformChanges: spec.TransformChanges, - Template: spec.ChangesetTemplate, - BatchChangeAttributes: &template.BatchChangeAttributes{ - Name: spec.Name, - Description: spec.Description, - }, + TransformChanges: spec.TransformChanges, + Template: spec.ChangesetTemplate, + BatchChangeAttributes: &batchChange, }, true, nil } diff --git a/internal/batches/template/main_test.go b/internal/batches/template/main_test.go index 38a13e63e9..5113074ad0 100644 --- a/internal/batches/template/main_test.go +++ b/internal/batches/template/main_test.go @@ -1,11 +1,9 @@ package template -import "github.com/sourcegraph/src-cli/internal/batches/graphql" - -var testRepo1 = &graphql.Repository{ +var testRepo1 = &TemplatingRepository{ ID: "src-cli", Name: "github.com/sourcegraph/src-cli", - DefaultBranch: &graphql.Branch{Name: "main", Target: graphql.Target{OID: "d34db33f"}}, + DefaultBranch: TemplatingBranch{Name: "main", TargetOID: "d34db33f"}, FileMatches: map[string]bool{ "README.md": true, "main.go": true, diff --git a/internal/batches/template/partial_eval_test.go b/internal/batches/template/partial_eval_test.go index ec0d0b6f9c..403d7ed4b7 100644 --- a/internal/batches/template/partial_eval_test.go +++ b/internal/batches/template/partial_eval_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/google/go-cmp/cmp" - "github.com/sourcegraph/src-cli/internal/batches/graphql" ) var partialEvalStepCtx = &StepContext{ @@ -13,7 +12,7 @@ var partialEvalStepCtx = &StepContext{ Description: "test-description", }, // Step is not set when evalStepCondition is called - Repository: graphql.Repository{ + Repository: TemplatingRepository{ Name: "github.com/sourcegraph/src-cli", FileMatches: map[string]bool{ "README.md": true, diff --git a/internal/batches/template/templating.go b/internal/batches/template/templating.go index 9fc9ff1226..2fc5aea408 100644 --- a/internal/batches/template/templating.go +++ b/internal/batches/template/templating.go @@ -3,13 +3,13 @@ package template import ( "bytes" "io" + "sort" "strings" "text/template" "github.com/gobwas/glob" "github.com/pkg/errors" - "github.com/sourcegraph/src-cli/internal/batches/git" - "github.com/sourcegraph/src-cli/internal/batches/graphql" + "github.com/sourcegraph/sourcegraph/lib/batches/git" ) const startDelim = "${{" @@ -85,6 +85,31 @@ type BatchChangeAttributes struct { Description string } +type TemplatingBranch struct { + Name string + TargetOID string +} + +type TemplatingRepository struct { + ID string + Name string + DefaultBranch TemplatingBranch + FileMatches map[string]bool +} + +func (r TemplatingRepository) SearchResultPaths() (list fileMatchPathList) { + var files []string + for f := range r.FileMatches { + files = append(files, f) + } + sort.Strings(files) + return fileMatchPathList(files) +} + +type fileMatchPathList []string + +func (f fileMatchPathList) String() string { return strings.Join(f, " ") } + // StepContext represents the contextual information available when rendering a // step's fields, such as "run" or "outputs", as templates. type StepContext struct { @@ -102,7 +127,7 @@ type StepContext struct { // previous step. PreviousStep StepResult // Repository is the Sourcegraph repository in which the steps are executed. - Repository graphql.Repository + Repository TemplatingRepository } // ToFuncMap returns a template.FuncMap to access fields on the StepContext in a @@ -232,7 +257,7 @@ type ChangesetTemplateContext struct { Outputs map[string]interface{} // Repository is the repository in which the steps were executed. - Repository graphql.Repository + Repository TemplatingRepository } // ToFuncMap returns a template.FuncMap to access fields on the StepContext in a diff --git a/internal/batches/template/templating_test.go b/internal/batches/template/templating_test.go index d9e1d62160..b6990f9fc4 100644 --- a/internal/batches/template/templating_test.go +++ b/internal/batches/template/templating_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" - "github.com/sourcegraph/src-cli/internal/batches/git" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "gopkg.in/yaml.v3" ) diff --git a/internal/batches/util/repo.go b/internal/batches/util/repo.go new file mode 100644 index 0000000000..eaea63cfdc --- /dev/null +++ b/internal/batches/util/repo.go @@ -0,0 +1,20 @@ +package util + +import ( + "github.com/sourcegraph/src-cli/internal/batches/graphql" + "github.com/sourcegraph/src-cli/internal/batches/template" +) + +// GraphQLRepoToTemplatingRepo transforms a given *graphql.Repository into a +// template.TemplatingRepository. +func GraphQLRepoToTemplatingRepo(r *graphql.Repository) template.TemplatingRepository { + return template.TemplatingRepository{ + ID: r.ID, + Name: r.Name, + DefaultBranch: template.TemplatingBranch{ + Name: r.DefaultBranch.Name, + TargetOID: r.DefaultBranch.Target.OID, + }, + FileMatches: r.FileMatches, + } +} diff --git a/internal/batches/workspace/bind_workspace.go b/internal/batches/workspace/bind_workspace.go index c9e1ae12e9..02fd95e1f3 100644 --- a/internal/batches/workspace/bind_workspace.go +++ b/internal/batches/workspace/bind_workspace.go @@ -14,8 +14,8 @@ import ( "github.com/hashicorp/go-multierror" "github.com/pkg/errors" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "github.com/sourcegraph/src-cli/internal/batches" - "github.com/sourcegraph/src-cli/internal/batches/git" "github.com/sourcegraph/src-cli/internal/batches/graphql" ) diff --git a/internal/batches/workspace/volume_workspace.go b/internal/batches/workspace/volume_workspace.go index f1c3fc2536..fa8d96d52c 100644 --- a/internal/batches/workspace/volume_workspace.go +++ b/internal/batches/workspace/volume_workspace.go @@ -14,9 +14,9 @@ import ( "github.com/pkg/errors" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "github.com/sourcegraph/src-cli/internal/batches" "github.com/sourcegraph/src-cli/internal/batches/docker" - "github.com/sourcegraph/src-cli/internal/batches/git" "github.com/sourcegraph/src-cli/internal/batches/graphql" "github.com/sourcegraph/src-cli/internal/exec" "github.com/sourcegraph/src-cli/internal/version" diff --git a/internal/batches/workspace/volume_workspace_test.go b/internal/batches/workspace/volume_workspace_test.go index ce0e813593..a2226c2800 100644 --- a/internal/batches/workspace/volume_workspace_test.go +++ b/internal/batches/workspace/volume_workspace_test.go @@ -13,8 +13,8 @@ import ( "github.com/pkg/errors" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "github.com/sourcegraph/src-cli/internal/batches/docker" - "github.com/sourcegraph/src-cli/internal/batches/git" "github.com/sourcegraph/src-cli/internal/batches/graphql" "github.com/sourcegraph/src-cli/internal/batches/mock" "github.com/sourcegraph/src-cli/internal/exec/expect" diff --git a/internal/batches/workspace/workspace.go b/internal/batches/workspace/workspace.go index dad4e40c50..d5c7a67871 100644 --- a/internal/batches/workspace/workspace.go +++ b/internal/batches/workspace/workspace.go @@ -6,9 +6,9 @@ import ( "github.com/pkg/errors" batcheslib "github.com/sourcegraph/sourcegraph/lib/batches" + "github.com/sourcegraph/sourcegraph/lib/batches/git" "github.com/sourcegraph/src-cli/internal/batches" "github.com/sourcegraph/src-cli/internal/batches/docker" - "github.com/sourcegraph/src-cli/internal/batches/git" "github.com/sourcegraph/src-cli/internal/batches/graphql" )