-
Notifications
You must be signed in to change notification settings - Fork 75
Introduce executor.Coordinator and split up responsibilities between Coordinator/Service/executor #536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Introduce executor.Coordinator and split up responsibilities between Coordinator/Service/executor #536
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
de92cd5
Broken build with TaskStatusHubThing
mrnugget d444fe9
Introduce Coordinator
mrnugget 72e0af7
Replace TaskStatusHubThing with TaskStatusCollection
mrnugget 74981bc
Clean up the executor
mrnugget a312a7f
Clean up handling of ExecutionOpts
mrnugget 8422db8
Fix missing rename
mrnugget ba60e8c
Make executor property and status a runtime arg
mrnugget 1c7c383
Create separate tests for Coordinator
mrnugget 6688d4a
Fix tests for Coordinator
mrnugget e23a2ab
More test cleanup
mrnugget b9b4de1
Re-add missing test
mrnugget 3a3145d
Re-add transfromGroup test but its passing, what
mrnugget 7677f1a
Add test for transform group
mrnugget 83e9f28
Add test helpers
mrnugget 5a92db3
Backport fix from #539
mrnugget 5e2b8f5
Update internal/batches/executor/coordinator.go
mrnugget File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,313 @@ | ||
| package executor | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing changed in this file. I only moved the test here from |
||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/sourcegraph/batch-change-utils/overridable" | ||
| "github.com/sourcegraph/src-cli/internal/batches" | ||
| "github.com/sourcegraph/src-cli/internal/batches/git" | ||
| "github.com/sourcegraph/src-cli/internal/batches/graphql" | ||
| ) | ||
|
|
||
| func TestCreateChangesetSpecs(t *testing.T) { | ||
| srcCLI := &graphql.Repository{ | ||
| ID: "src-cli", | ||
| Name: "github.com/sourcegraph/src-cli", | ||
| DefaultBranch: &graphql.Branch{Name: "main", Target: graphql.Target{OID: "d34db33f"}}, | ||
| } | ||
|
|
||
| defaultChangesetSpec := &batches.ChangesetSpec{ | ||
| BaseRepository: srcCLI.ID, | ||
| CreatedChangeset: &batches.CreatedChangeset{ | ||
| BaseRef: srcCLI.DefaultBranch.Name, | ||
| BaseRev: srcCLI.DefaultBranch.Target.OID, | ||
| HeadRepository: srcCLI.ID, | ||
| HeadRef: "refs/heads/my-branch", | ||
| Title: "The title", | ||
| Body: "The body", | ||
| Commits: []batches.GitCommitDescription{ | ||
| { | ||
| Message: "git commit message", | ||
| Diff: "cool diff", | ||
| AuthorName: "Sourcegraph", | ||
| AuthorEmail: "batch-changes@sourcegraph.com", | ||
| }, | ||
| }, | ||
| Published: false, | ||
| }, | ||
| } | ||
|
|
||
| specWith := func(s *batches.ChangesetSpec, f func(s *batches.ChangesetSpec)) *batches.ChangesetSpec { | ||
| f(s) | ||
| return s | ||
| } | ||
|
|
||
| defaultTask := &Task{ | ||
| BatchChangeAttributes: &BatchChangeAttributes{ | ||
| Name: "the name", | ||
| Description: "The description", | ||
| }, | ||
| Template: &batches.ChangesetTemplate{ | ||
| Title: "The title", | ||
| Body: "The body", | ||
| Branch: "my-branch", | ||
| Commit: batches.ExpandedGitCommitDescription{ | ||
| Message: "git commit message", | ||
| }, | ||
| Published: parsePublishedFieldString(t, "false"), | ||
| }, | ||
| Repository: srcCLI, | ||
| } | ||
|
|
||
| taskWith := func(t *Task, f func(t *Task)) *Task { | ||
| f(t) | ||
| return t | ||
| } | ||
|
|
||
| defaultResult := executionResult{ | ||
| Diff: "cool diff", | ||
| ChangedFiles: &git.Changes{ | ||
| Modified: []string{"README.md"}, | ||
| }, | ||
| Outputs: map[string]interface{}{}, | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| task *Task | ||
| result executionResult | ||
|
|
||
| want []*batches.ChangesetSpec | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "success", | ||
| task: defaultTask, | ||
| result: defaultResult, | ||
| want: []*batches.ChangesetSpec{ | ||
| defaultChangesetSpec, | ||
| }, | ||
| wantErr: "", | ||
| }, | ||
| { | ||
| name: "publish by branch", | ||
| task: taskWith(defaultTask, func(task *Task) { | ||
| published := `[{"github.com/sourcegraph/*@my-branch": true}]` | ||
| task.Template.Published = parsePublishedFieldString(t, published) | ||
| }), | ||
| result: defaultResult, | ||
| want: []*batches.ChangesetSpec{ | ||
| specWith(defaultChangesetSpec, func(s *batches.ChangesetSpec) { | ||
| s.Published = true | ||
| }), | ||
| }, | ||
| wantErr: "", | ||
| }, | ||
| { | ||
| name: "publish by branch not matching", | ||
| task: taskWith(defaultTask, func(task *Task) { | ||
| published := `[{"github.com/sourcegraph/*@another-branch-name": true}]` | ||
| task.Template.Published = parsePublishedFieldString(t, published) | ||
| }), | ||
| result: defaultResult, | ||
| want: []*batches.ChangesetSpec{ | ||
| specWith(defaultChangesetSpec, func(s *batches.ChangesetSpec) { | ||
| s.Published = false | ||
| }), | ||
| }, | ||
| wantErr: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| have, err := createChangesetSpecs(tt.task, tt.result, true) | ||
| if err != nil { | ||
| if tt.wantErr != "" { | ||
| if err.Error() != tt.wantErr { | ||
| t.Fatalf("wrong error. want=%q, got=%q", tt.wantErr, err.Error()) | ||
| } | ||
| return | ||
| } else { | ||
| t.Fatalf("unexpected error: %s", err) | ||
| } | ||
| } | ||
|
|
||
| if !cmp.Equal(tt.want, have) { | ||
| t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(tt.want, have)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGroupFileDiffs(t *testing.T) { | ||
| diff1 := `diff --git 1/1.txt 1/1.txt | ||
| new file mode 100644 | ||
| index 0000000..19d6416 | ||
| --- /dev/null | ||
| +++ 1/1.txt | ||
| @@ -0,0 +1,1 @@ | ||
| +this is 1 | ||
| ` | ||
| diff2 := `diff --git 1/2/2.txt 1/2/2.txt | ||
| new file mode 100644 | ||
| index 0000000..c825d65 | ||
| --- /dev/null | ||
| +++ 1/2/2.txt | ||
| @@ -0,0 +1,1 @@ | ||
| +this is 2 | ||
| ` | ||
| diff3 := `diff --git 1/2/3/3.txt 1/2/3/3.txt | ||
| new file mode 100644 | ||
| index 0000000..1bd79fb | ||
| --- /dev/null | ||
| +++ 1/2/3/3.txt | ||
| @@ -0,0 +1,1 @@ | ||
| +this is 3 | ||
| ` | ||
|
|
||
| defaultBranch := "my-default-branch" | ||
| allDiffs := diff1 + diff2 + diff3 | ||
|
|
||
| tests := []struct { | ||
| diff string | ||
| defaultBranch string | ||
| groups []batches.Group | ||
| want map[string]string | ||
| }{ | ||
| { | ||
| diff: allDiffs, | ||
| groups: []batches.Group{ | ||
| {Directory: "1/2/3", Branch: "everything-in-3"}, | ||
| }, | ||
| want: map[string]string{ | ||
| "my-default-branch": diff1 + diff2, | ||
| "everything-in-3": diff3, | ||
| }, | ||
| }, | ||
| { | ||
| diff: allDiffs, | ||
| groups: []batches.Group{ | ||
| {Directory: "1/2", Branch: "everything-in-2-and-3"}, | ||
| }, | ||
| want: map[string]string{ | ||
| "my-default-branch": diff1, | ||
| "everything-in-2-and-3": diff2 + diff3, | ||
| }, | ||
| }, | ||
| { | ||
| diff: allDiffs, | ||
| groups: []batches.Group{ | ||
| {Directory: "1", Branch: "everything-in-1-and-2-and-3"}, | ||
| }, | ||
| want: map[string]string{ | ||
| "my-default-branch": "", | ||
| "everything-in-1-and-2-and-3": diff1 + diff2 + diff3, | ||
| }, | ||
| }, | ||
| { | ||
| diff: allDiffs, | ||
| groups: []batches.Group{ | ||
| // Each diff is matched against each directory, last match wins | ||
| {Directory: "1", Branch: "only-in-1"}, | ||
| {Directory: "1/2", Branch: "only-in-2"}, | ||
| {Directory: "1/2/3", Branch: "only-in-3"}, | ||
| }, | ||
| want: map[string]string{ | ||
| "my-default-branch": "", | ||
| "only-in-3": diff3, | ||
| "only-in-2": diff2, | ||
| "only-in-1": diff1, | ||
| }, | ||
| }, | ||
| { | ||
| diff: allDiffs, | ||
| groups: []batches.Group{ | ||
| // Last one wins here, because it matches every diff | ||
| {Directory: "1/2/3", Branch: "only-in-3"}, | ||
| {Directory: "1/2", Branch: "only-in-2"}, | ||
| {Directory: "1", Branch: "only-in-1"}, | ||
| }, | ||
| want: map[string]string{ | ||
| "my-default-branch": "", | ||
| "only-in-1": diff1 + diff2 + diff3, | ||
| }, | ||
| }, | ||
| { | ||
| diff: allDiffs, | ||
| groups: []batches.Group{ | ||
| {Directory: "", Branch: "everything"}, | ||
| }, | ||
| want: map[string]string{ | ||
| "my-default-branch": diff1 + diff2 + diff3, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| have, err := groupFileDiffs(tc.diff, defaultBranch, tc.groups) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %s", err) | ||
| } | ||
| if !cmp.Equal(tc.want, have) { | ||
| t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(tc.want, have)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestValidateGroups(t *testing.T) { | ||
| repoName := "github.com/sourcegraph/src-cli" | ||
| defaultBranch := "my-batch-change" | ||
|
|
||
| tests := []struct { | ||
| defaultBranch string | ||
| groups []batches.Group | ||
| wantErr string | ||
| }{ | ||
| { | ||
| groups: []batches.Group{ | ||
| {Directory: "a", Branch: "my-batch-change-a"}, | ||
| {Directory: "b", Branch: "my-batch-change-b"}, | ||
| }, | ||
| wantErr: "", | ||
| }, | ||
| { | ||
| groups: []batches.Group{ | ||
| {Directory: "a", Branch: "my-batch-change-SAME"}, | ||
| {Directory: "b", Branch: "my-batch-change-SAME"}, | ||
| }, | ||
| wantErr: "transformChanges would lead to multiple changesets in repository github.com/sourcegraph/src-cli to have the same branch \"my-batch-change-SAME\"", | ||
| }, | ||
| { | ||
| groups: []batches.Group{ | ||
| {Directory: "a", Branch: "my-batch-change-SAME"}, | ||
| {Directory: "b", Branch: defaultBranch}, | ||
| }, | ||
| wantErr: "transformChanges group branch for repository github.com/sourcegraph/src-cli is the same as branch \"my-batch-change\" in changesetTemplate", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| err := validateGroups(repoName, defaultBranch, tc.groups) | ||
| var haveErr string | ||
| if err != nil { | ||
| haveErr = err.Error() | ||
| } | ||
|
|
||
| if haveErr != tc.wantErr { | ||
| t.Fatalf("wrong error:\nwant=%q\nhave=%q", tc.wantErr, haveErr) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func parsePublishedFieldString(t *testing.T, input string) overridable.BoolOrString { | ||
| t.Helper() | ||
|
|
||
| var result overridable.BoolOrString | ||
| if err := json.Unmarshal([]byte(input), &result); err != nil { | ||
| t.Fatalf("failed to parse %q as overridable.BoolOrString: %s", input, err) | ||
| } | ||
| return result | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't change this, but as a note to myself, I have to update #538 since it was relying on the features being drilled through to this function. 😬