Skip to content

Commit

Permalink
Switch from branchSuffix to branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnugget committed Dec 2, 2020
1 parent 2fff783 commit b635d03
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 41 deletions.
4 changes: 2 additions & 2 deletions internal/campaigns/campaign_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ type TransformChanges struct {
}

type Group struct {
Directory string `json:"directory,omitempty" yaml:"directory"`
BranchSuffix string `json:"branchSuffix,omitempty" yaml:"branchSuffix"`
Directory string `json:"directory,omitempty" yaml:"directory"`
Branch string `json:"branch,omitempty" yaml:"branch"`
}

func ParseCampaignSpec(data []byte, features featureFlags) (*CampaignSpec, error) {
Expand Down
16 changes: 7 additions & 9 deletions internal/campaigns/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,12 @@ func groupFileDiffs(completeDiff, defaultBranch string, groups []Group) (map[str
}

// Housekeeping: we setup these two datastructures so we can
// - access the branchSuffixes by the directory for which they should be used
// - access the group.Branch by the directory for which they should be used
// - check against the given directories, starting with the longest one.
suffixesByDirectory := make(map[string]string, len(groups))
dirsByLen := make([]string, len(suffixesByDirectory))
branchesByDirectory := make(map[string]string, len(groups))
dirsByLen := make([]string, len(branchesByDirectory))
for _, g := range groups {
suffixesByDirectory[g.Directory] = g.BranchSuffix
branchesByDirectory[g.Directory] = g.Branch
dirsByLen = append(dirsByLen, g.Directory)
}
sort.Slice(dirsByLen, func(i, j int) bool {
Expand Down Expand Up @@ -501,15 +501,13 @@ func groupFileDiffs(completeDiff, defaultBranch string, groups []Group) (map[str
continue
}

// If it *did* match a directory, we look up which suffix we should add
// to the default branch and add it under that:
suffix, ok := suffixesByDirectory[matchingDir]
// If it *did* match a directory, we look up which branch we should use:
branch, ok := branchesByDirectory[matchingDir]
if !ok {
panic("this should not happen: " + matchingDir)
}

b := defaultBranch + suffix
byBranch[b] = append(byBranch[b], f)
byBranch[branch] = append(byBranch[branch], f)
}

finalDiffsByBranch := make(map[string]string, len(byBranch))
Expand Down
52 changes: 26 additions & 26 deletions internal/campaigns/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestExecutor_Integration(t *testing.T) {
},
transform: &TransformChanges{
Group: []Group{
{Directory: "a/b/c", BranchSuffix: "-in-directory-c"},
{Directory: "a/b/c", Branch: "in-directory-c"},
},
},
wantFilesChanged: filesByRepository{
Expand All @@ -174,7 +174,7 @@ func TestExecutor_Integration(t *testing.T) {
"a/a.go",
"a/b/b.go",
},
changesetTemplateBranch + "-in-directory-c": []string{
"in-directory-c": []string{
"a/b/c/c.go",
},
},
Expand Down Expand Up @@ -354,66 +354,66 @@ index 0000000..1bd79fb
{
diff: allDiffs,
groups: []Group{
{Directory: "1/2/3", BranchSuffix: "-everything-in-3"},
{Directory: "1/2/3", Branch: "everything-in-3"},
},
want: map[string]string{
"my-default-branch": diff1 + diff2,
"my-default-branch-everything-in-3": diff3,
"my-default-branch": diff1 + diff2,
"everything-in-3": diff3,
},
},
{
diff: allDiffs,
groups: []Group{
{Directory: "1/2", BranchSuffix: "-everything-in-2-and-3"},
{Directory: "1/2", Branch: "everything-in-2-and-3"},
},
want: map[string]string{
"my-default-branch": diff1,
"my-default-branch-everything-in-2-and-3": diff2 + diff3,
"my-default-branch": diff1,
"everything-in-2-and-3": diff2 + diff3,
},
},
{
diff: allDiffs,
groups: []Group{
{Directory: "1", BranchSuffix: "-everything-in-1-and-2-and-3"},
{Directory: "1", Branch: "everything-in-1-and-2-and-3"},
},
want: map[string]string{
"my-default-branch": "",
"my-default-branch-everything-in-1-and-2-and-3": diff1 + diff2 + diff3,
"my-default-branch": "",
"everything-in-1-and-2-and-3": diff1 + diff2 + diff3,
},
},
{
diff: allDiffs,
groups: []Group{
{Directory: "1/2/3", BranchSuffix: "-only-in-3"},
{Directory: "1/2", BranchSuffix: "-only-in-2"},
{Directory: "1", BranchSuffix: "-only-in-1"},
{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": "",
"my-default-branch-only-in-3": diff3,
"my-default-branch-only-in-2": diff2,
"my-default-branch-only-in-1": diff1,
"my-default-branch": "",
"only-in-3": diff3,
"only-in-2": diff2,
"only-in-1": diff1,
},
},
{
diff: allDiffs,
groups: []Group{
// Different order than above
{Directory: "1", BranchSuffix: "-only-in-1"},
{Directory: "1/2", BranchSuffix: "-only-in-2"},
{Directory: "1/2/3", BranchSuffix: "-only-in-3"},
{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": "",
"my-default-branch-only-in-3": diff3,
"my-default-branch-only-in-2": diff2,
"my-default-branch-only-in-1": diff1,
"my-default-branch": "",
"only-in-3": diff3,
"only-in-2": diff2,
"only-in-1": diff1,
},
},
{
diff: allDiffs,
groups: []Group{
{Directory: "", BranchSuffix: "-everything"},
{Directory: "", Branch: "everything"},
},
want: map[string]string{
"my-default-branch": diff1 + diff2 + diff3,
Expand Down
4 changes: 2 additions & 2 deletions schema/campaign_spec.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@
"type": "string",
"description": "The directory path (relative to the repository root) of the changes to include in this group."
},
"branchSuffix": {
"branch": {
"type": "string",
"description": "The branch suffix to add to the `branch` attribute of the `changesetTemplate` when creating the additonal changeset."
"description": "The branch on the repository to propose changes to. If unset, the repository's default branch is used."
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions schema/campaign_spec_stringdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b635d03

Please sign in to comment.