Skip to content

Commit b192404

Browse files
mrnuggetLawnGnome
andauthored
Allow addressing changesets by branch (#461)
* Allow addressing changesets by branch * Use fork instead of local copy * Add tests to make sure addressing by branch works * Add changelog entry * Update CHANGELOG.md Co-authored-by: Adam Harvey <[email protected]> * Update to merged campaignutils version Co-authored-by: Adam Harvey <[email protected]>
1 parent 7ab009f commit b192404

File tree

5 files changed

+152
-4
lines changed

5 files changed

+152
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to `src-cli` are documented in this file.
1414
### Added
1515

1616
- Experimental: [`workspaces` in campaign specs](https://docs.sourcegraph.com/campaigns/references/campaign_spec_yaml_reference#workspaces) is now available to allow users to define multiple workspaces in a single repository. [#442](https://github.com/sourcegraph/src-cli/pull/442)
17+
- The `changesetTemplate.published` field can now also be used to address a specific changeset in a repository by adding `@branch-of-changeset` at the end of the pattern. See [#461](https://github.com/sourcegraph/src-cli/pull/461) for an example and details.
1718

1819
### Changed
1920

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/olekukonko/tablewriter v0.0.4 // indirect
2121
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
2222
github.com/pkg/errors v0.9.1
23-
github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2
23+
github.com/sourcegraph/campaignutils v0.0.0-20210209103213-27e58c3ce8d3
2424
github.com/sourcegraph/codeintelutils v0.0.0-20210118231003-6698e102a8a1
2525
github.com/sourcegraph/go-diff v0.6.1
2626
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
5050
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5151
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
5252
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
53-
github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2 h1:MJu/6WzWdPegzYnZLb04IS0u4VyUpPIAHQyWT5i2vR8=
54-
github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2/go.mod h1:xm6i78Mk2t4DBLQDqEFc/3x6IPf7yYZCgbNaTQGhJHA=
53+
github.com/sourcegraph/campaignutils v0.0.0-20210209103213-27e58c3ce8d3 h1:yXTWFqaMCMgjG3zVRGXjvuaUDfw2XnI6JD5Rh4V04e4=
54+
github.com/sourcegraph/campaignutils v0.0.0-20210209103213-27e58c3ce8d3/go.mod h1:xm6i78Mk2t4DBLQDqEFc/3x6IPf7yYZCgbNaTQGhJHA=
5555
github.com/sourcegraph/codeintelutils v0.0.0-20210118231003-6698e102a8a1 h1:IPWruUo+BwPJqCHBVgjKxK6zTxMkOhwCSYpQ/jZHG/w=
5656
github.com/sourcegraph/codeintelutils v0.0.0-20210118231003-6698e102a8a1/go.mod h1:HplI8gRslTrTUUsSYwu28hSOderix7m5dHNca7xBzeo=
5757
github.com/sourcegraph/go-diff v0.6.1 h1:hmA1LzxW0n1c3Q4YbrFgg4P99GSnebYa3x8gr0HZqLQ=

internal/campaigns/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ func createChangesetSpecs(task *Task, result executionResult, features featureFl
518518
Diff: diff,
519519
},
520520
},
521-
Published: task.Template.Published.Value(repo),
521+
Published: task.Template.Published.ValueWithSuffix(repo, branch),
522522
},
523523
}
524524
}

internal/campaigns/executor_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/zip"
55
"bytes"
66
"context"
7+
"encoding/json"
78
"fmt"
89
"io/ioutil"
910
"log"
@@ -19,6 +20,7 @@ import (
1920
"time"
2021

2122
"github.com/google/go-cmp/cmp"
23+
"github.com/sourcegraph/campaignutils/overridable"
2224
"github.com/sourcegraph/go-diff/diff"
2325
"github.com/sourcegraph/src-cli/internal/api"
2426
"github.com/sourcegraph/src-cli/internal/campaigns/graphql"
@@ -687,6 +689,151 @@ index 0000000..1bd79fb
687689
}
688690
}
689691

692+
func TestCreateChangesetSpecs(t *testing.T) {
693+
allFeatures := featureFlags{
694+
allowArrayEnvironments: true,
695+
includeAutoAuthorDetails: true,
696+
useGzipCompression: true,
697+
allowtransformChanges: true,
698+
allowWorkspaces: true,
699+
}
700+
701+
srcCLI := &graphql.Repository{
702+
ID: "src-cli",
703+
Name: "github.com/sourcegraph/src-cli",
704+
DefaultBranch: &graphql.Branch{Name: "main", Target: graphql.Target{OID: "d34db33f"}},
705+
}
706+
707+
defaultChangesetSpec := &ChangesetSpec{
708+
BaseRepository: srcCLI.ID,
709+
CreatedChangeset: &CreatedChangeset{
710+
BaseRef: srcCLI.DefaultBranch.Name,
711+
BaseRev: srcCLI.DefaultBranch.Target.OID,
712+
HeadRepository: srcCLI.ID,
713+
HeadRef: "refs/heads/my-branch",
714+
Title: "The title",
715+
Body: "The body",
716+
Commits: []GitCommitDescription{
717+
{
718+
Message: "git commit message",
719+
Diff: "cool diff",
720+
AuthorName: "Sourcegraph",
721+
AuthorEmail: "[email protected]",
722+
},
723+
},
724+
Published: false,
725+
},
726+
}
727+
728+
specWith := func(s *ChangesetSpec, f func(s *ChangesetSpec)) *ChangesetSpec {
729+
f(s)
730+
return s
731+
}
732+
733+
defaultTask := &Task{
734+
Template: &ChangesetTemplate{
735+
Title: "The title",
736+
Body: "The body",
737+
Branch: "my-branch",
738+
Commit: ExpandedGitCommitDescription{
739+
Message: "git commit message",
740+
},
741+
Published: parsePublishedFieldString(t, "false"),
742+
},
743+
Repository: srcCLI,
744+
}
745+
746+
taskWith := func(t *Task, f func(t *Task)) *Task {
747+
f(t)
748+
return t
749+
}
750+
751+
defaultResult := executionResult{
752+
Diff: "cool diff",
753+
ChangedFiles: &StepChanges{
754+
Modified: []string{"README.md"},
755+
},
756+
Outputs: map[string]interface{}{},
757+
}
758+
759+
tests := []struct {
760+
name string
761+
task *Task
762+
result executionResult
763+
764+
want []*ChangesetSpec
765+
wantErr string
766+
}{
767+
{
768+
name: "success",
769+
task: defaultTask,
770+
result: defaultResult,
771+
want: []*ChangesetSpec{
772+
defaultChangesetSpec,
773+
},
774+
wantErr: "",
775+
},
776+
{
777+
name: "publish by branch",
778+
task: taskWith(defaultTask, func(task *Task) {
779+
published := `[{"github.com/sourcegraph/*@my-branch": true}]`
780+
task.Template.Published = parsePublishedFieldString(t, published)
781+
}),
782+
result: defaultResult,
783+
want: []*ChangesetSpec{
784+
specWith(defaultChangesetSpec, func(s *ChangesetSpec) {
785+
s.Published = true
786+
}),
787+
},
788+
wantErr: "",
789+
},
790+
{
791+
name: "publish by branch not matching",
792+
task: taskWith(defaultTask, func(task *Task) {
793+
published := `[{"github.com/sourcegraph/*@another-branch-name": true}]`
794+
task.Template.Published = parsePublishedFieldString(t, published)
795+
}),
796+
result: defaultResult,
797+
want: []*ChangesetSpec{
798+
specWith(defaultChangesetSpec, func(s *ChangesetSpec) {
799+
s.Published = false
800+
}),
801+
},
802+
wantErr: "",
803+
},
804+
}
805+
806+
for _, tt := range tests {
807+
t.Run(tt.name, func(t *testing.T) {
808+
have, err := createChangesetSpecs(tt.task, tt.result, allFeatures)
809+
if err != nil {
810+
if tt.wantErr != "" {
811+
if err.Error() != tt.wantErr {
812+
t.Fatalf("wrong error. want=%q, got=%q", tt.wantErr, err.Error())
813+
}
814+
return
815+
} else {
816+
t.Fatalf("unexpected error: %s", err)
817+
}
818+
}
819+
820+
if !cmp.Equal(tt.want, have) {
821+
t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(tt.want, have))
822+
}
823+
})
824+
}
825+
}
826+
827+
func parsePublishedFieldString(t *testing.T, input string) overridable.BoolOrString {
828+
t.Helper()
829+
830+
var result overridable.BoolOrString
831+
if err := json.Unmarshal([]byte(input), &result); err != nil {
832+
t.Fatalf("failed to parse %q as overridable.BoolOrString: %s", input, err)
833+
}
834+
return result
835+
}
836+
690837
func addToPath(t *testing.T, relPath string) {
691838
t.Helper()
692839

0 commit comments

Comments
 (0)