Skip to content
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

Allow addressing changesets by branch #461

Merged
merged 6 commits into from
Feb 9, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to `src-cli` are documented in this file.
### Added

- 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)
- 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.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/olekukonko/tablewriter v0.0.4 // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/pkg/errors v0.9.1
github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2
github.com/sourcegraph/campaignutils v0.0.0-20210209103213-27e58c3ce8d3
github.com/sourcegraph/codeintelutils v0.0.0-20210118231003-6698e102a8a1
github.com/sourcegraph/go-diff v0.6.1
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2 h1:MJu/6WzWdPegzYnZLb04IS0u4VyUpPIAHQyWT5i2vR8=
github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2/go.mod h1:xm6i78Mk2t4DBLQDqEFc/3x6IPf7yYZCgbNaTQGhJHA=
github.com/sourcegraph/campaignutils v0.0.0-20210209103213-27e58c3ce8d3 h1:yXTWFqaMCMgjG3zVRGXjvuaUDfw2XnI6JD5Rh4V04e4=
github.com/sourcegraph/campaignutils v0.0.0-20210209103213-27e58c3ce8d3/go.mod h1:xm6i78Mk2t4DBLQDqEFc/3x6IPf7yYZCgbNaTQGhJHA=
github.com/sourcegraph/codeintelutils v0.0.0-20210118231003-6698e102a8a1 h1:IPWruUo+BwPJqCHBVgjKxK6zTxMkOhwCSYpQ/jZHG/w=
github.com/sourcegraph/codeintelutils v0.0.0-20210118231003-6698e102a8a1/go.mod h1:HplI8gRslTrTUUsSYwu28hSOderix7m5dHNca7xBzeo=
github.com/sourcegraph/go-diff v0.6.1 h1:hmA1LzxW0n1c3Q4YbrFgg4P99GSnebYa3x8gr0HZqLQ=
Expand Down
2 changes: 1 addition & 1 deletion internal/campaigns/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func createChangesetSpecs(task *Task, result executionResult, features featureFl
Diff: diff,
},
},
Published: task.Template.Published.Value(repo),
Published: task.Template.Published.ValueWithSuffix(repo, branch),
},
}
}
Expand Down
147 changes: 147 additions & 0 deletions internal/campaigns/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
Expand All @@ -19,6 +20,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/sourcegraph/campaignutils/overridable"
"github.com/sourcegraph/go-diff/diff"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/campaigns/graphql"
Expand Down Expand Up @@ -687,6 +689,151 @@ index 0000000..1bd79fb
}
}

func TestCreateChangesetSpecs(t *testing.T) {
allFeatures := featureFlags{
allowArrayEnvironments: true,
includeAutoAuthorDetails: true,
useGzipCompression: true,
allowtransformChanges: true,
allowWorkspaces: true,
}

srcCLI := &graphql.Repository{
ID: "src-cli",
Name: "github.com/sourcegraph/src-cli",
DefaultBranch: &graphql.Branch{Name: "main", Target: graphql.Target{OID: "d34db33f"}},
}

defaultChangesetSpec := &ChangesetSpec{
BaseRepository: srcCLI.ID,
CreatedChangeset: &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: []GitCommitDescription{
{
Message: "git commit message",
Diff: "cool diff",
AuthorName: "Sourcegraph",
AuthorEmail: "campaigns@sourcegraph.com",
},
},
Published: false,
},
}

specWith := func(s *ChangesetSpec, f func(s *ChangesetSpec)) *ChangesetSpec {
f(s)
return s
}

defaultTask := &Task{
Template: &ChangesetTemplate{
Title: "The title",
Body: "The body",
Branch: "my-branch",
Commit: 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: &StepChanges{
Modified: []string{"README.md"},
},
Outputs: map[string]interface{}{},
}

tests := []struct {
name string
task *Task
result executionResult

want []*ChangesetSpec
wantErr string
}{
{
name: "success",
task: defaultTask,
result: defaultResult,
want: []*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: []*ChangesetSpec{
specWith(defaultChangesetSpec, func(s *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: []*ChangesetSpec{
specWith(defaultChangesetSpec, func(s *ChangesetSpec) {
s.Published = false
}),
},
wantErr: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
have, err := createChangesetSpecs(tt.task, tt.result, allFeatures)
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 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
}

func addToPath(t *testing.T, relPath string) {
t.Helper()

Expand Down