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

Expand Repository.MergeMutable to cover more fields #684

Merged
merged 1 commit into from
Mar 14, 2024
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
19 changes: 17 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,6 @@ func (r *Repository) UnmarshalJSON(data []byte) error {
//
// Note: We ignore RawConfig fields which are duplicated into Repository:
// name and id.
//
// Note: URL, *Template fields are ignored. They are not used by Sourcegraph.
func (r *Repository) MergeMutable(x *Repository) (mutated bool, err error) {
if r.ID != x.ID {
// Sourcegraph: strange behaviour may occur if ID changes but names don't.
Expand Down Expand Up @@ -688,6 +686,23 @@ func (r *Repository) MergeMutable(x *Repository) (mutated bool, err error) {
}
}

if r.URL != x.URL {
mutated = true
r.URL = x.URL
}
if r.CommitURLTemplate != x.CommitURLTemplate {
mutated = true
r.CommitURLTemplate = x.CommitURLTemplate
}
if r.FileURLTemplate != x.FileURLTemplate {
mutated = true
r.FileURLTemplate = x.FileURLTemplate
}
if r.LineFragmentTemplate != x.LineFragmentTemplate {
mutated = true
r.LineFragmentTemplate = x.LineFragmentTemplate
}

return mutated, nil
}

Expand Down
131 changes: 131 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,134 @@ func TestSearchOptions_String(t *testing.T) {
}
}
}

func TestRepositoryMergeMutable(t *testing.T) {
a := Repository{
ID: 0,
Name: "name",
Branches: []RepositoryBranch{
{
Name: "branchName",
Version: "branchVersion",
},
},
RawConfig: nil,
URL: "url",
CommitURLTemplate: "commitUrlTemplate",
FileURLTemplate: "fileUrlTemplate",
LineFragmentTemplate: "lineFragmentTemplate",
}

t.Run("different ID", func(t *testing.T) {
b := a
b.ID = 1
mutated, err := a.MergeMutable(&b)
if err == nil {
t.Fatalf("want err, got mutated=%t", mutated)
}
})
t.Run("different Name", func(t *testing.T) {
b := a
b.Name = "otherName"
mutated, err := a.MergeMutable(&b)
if err == nil {
t.Fatalf("want err, got mutated=%t", mutated)
}
})
t.Run("different Branches", func(t *testing.T) {
b := a
b.Branches = []RepositoryBranch{
{
Name: "otherBranchName",
Version: "branchVersion",
},
}
mutated, err := a.MergeMutable(&b)
if err == nil {
t.Fatalf("want err, got mutated=%t", mutated)
}
})
t.Run("different RawConfig", func(t *testing.T) {
b := a
b.RawConfig = map[string]string{"foo": "bar"}
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if !reflect.DeepEqual(a.RawConfig, b.RawConfig) {
t.Fatalf("got different RawConfig, %v vs %v", a.RawConfig, b.RawConfig)
}
})
t.Run("different URL", func(t *testing.T) {
b := a
b.URL = "otherURL"
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if a.URL != b.URL {
t.Fatalf("got different URL, %s vs %s", a.URL, b.URL)
}
})
t.Run("different CommitURLTemplate", func(t *testing.T) {
b := a
b.CommitURLTemplate = "otherCommitUrlTemplate"
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if a.CommitURLTemplate != b.CommitURLTemplate {
t.Fatalf("got different CommitURLTemplate, %s vs %s", a.CommitURLTemplate, b.CommitURLTemplate)
}
})
t.Run("different FileURLTemplate", func(t *testing.T) {
b := a
b.FileURLTemplate = "otherFileUrlTemplate"
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if a.FileURLTemplate != b.FileURLTemplate {
t.Fatalf("got different FileURLTemplate, %s vs %s", a.FileURLTemplate, b.FileURLTemplate)
}
})
t.Run("different LineFragmentTemplate", func(t *testing.T) {
b := a
b.LineFragmentTemplate = "otherLineFragmentTemplate"
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if a.LineFragmentTemplate != b.LineFragmentTemplate {
t.Fatalf("got different LineFragmentTemplate, %s vs %s", a.LineFragmentTemplate, b.LineFragmentTemplate)
}
})
t.Run("all same", func(t *testing.T) {
b := a
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if mutated {
t.Fatalf("want mutated=false, got true")
}
if !reflect.DeepEqual(a, b) {
t.Fatalf("got different Repository, %v vs %v", a, b)
}
})
}
Loading