Skip to content

Commit

Permalink
changes: fill in the rest of Changes.ChangeInput (#97)
Browse files Browse the repository at this point in the history
The ChangeInput type was recently added with just the bare-bones
fields in place. This commit adds the rest of the fields per the
current gerrit documentation.

Updated TestChangesService_CreateChange to have stronger assertions
about empty values (recursively checks nested structures), and added
test cases specific to exercise ChangeInput.Merge and
ChangeInput.Author.

This change is a result of the discussion on PR #96.

Co-authored-by: Wade Carpenter <wwade@users.noreply.github.com>
  • Loading branch information
wwade and wwade authored Sep 18, 2021
1 parent 58f949a commit 67e5874
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 25 deletions.
33 changes: 25 additions & 8 deletions changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,21 @@ type DiffIntralineInfo [][2]int
//
// Docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-input
type ChangeInput struct {
Project string `json:"project"`
Branch string `json:"branch"`
Subject string `json:"subject"`
Topic string `json:"topic,omitempty"`
Status string `json:"status,omitempty"`

// Attention: Fields are missing.
// TODO Add the full list of attributes from https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-input
Project string `json:"project"`
Branch string `json:"branch"`
Subject string `json:"subject"`
Topic string `json:"topic,omitempty"`
Status string `json:"status,omitempty"`
IsPrivate bool `json:"is_private,omitempty"`
WorkInProgress bool `json:"work_in_progress,omitempty"`
BaseChange string `json:"base_change,omitempty"`
BaseCommit string `json:"base_commit,omitempty"`
NewBranch bool `json:"new_branch,omitempty"`
ValidationOptions map[string]interface{} `json:"validation_options,omitempty"`
Merge *MergeInput `json:"merge,omitempty"`
Author *AccountInput `json:"author,omitempty"`
Notify string `json:"notify,omitempty"`
NotifyDetails string `json:"notify_details,omitempty"`
}

// ChangeInfo entity contains information about a change.
Expand Down Expand Up @@ -450,6 +457,16 @@ type LabelInfo struct {
Values map[string]string `json:"values,omitempty"`
}

// The MergeInput entity contains information about the merge
//
// Docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#merge-input
type MergeInput struct {
Source string `json:"source"`
SourceBranch string `json:"source_branch,omitempty"`
Strategy string `json:"strategy,omitempty"`
AllowConflicts bool `json:"allow_conflicts,omitempty"`
}

// RevisionInfo entity contains information about a patch set.
type RevisionInfo struct {
Draft bool `json:"draft,omitempty"`
Expand Down
78 changes: 61 additions & 17 deletions changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ func ExampleChangesService_PublishChangeEdit() {
}
}

func disallowEmptyFields(t *testing.T, payload map[string]interface{}, path string) {
for field, generic := range payload {
curPath := field
if len(path) > 0 {
curPath = path + "." + field
}
switch value := generic.(type) {
case string:
if len(value) == 0 {
t.Errorf("Empty value for field %q", curPath)
}
case map[string]interface{}:
if len(value) == 0 {
t.Errorf("Empty value for field %q", curPath)
}
disallowEmptyFields(t, value, curPath)
}
}
}

func TestChangesService_CreateChange(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
Expand All @@ -131,16 +151,14 @@ func TestChangesService_CreateChange(t *testing.T) {
project := required("project")
branch := required("branch")
subject := required("subject")

for field, generic := range payload {
switch value := generic.(type) {
case string:
if len(value) == 0 {
t.Errorf("Empty value for field %q", field)
}
if merge, ok := payload["merge"]; ok {
if _, ok := merge.(map[string]interface{})["source"]; !ok {
t.Error(`Missing required field "merge.source"`)
}
}

disallowEmptyFields(t, payload, "")

if r.URL.Path != "/changes/" {
t.Errorf("%s != /changes/", r.URL.Path)
}
Expand All @@ -155,16 +173,42 @@ func TestChangesService_CreateChange(t *testing.T) {
if err != nil {
t.Error(err)
}
info, _, err := client.Changes.CreateChange(&gerrit.ChangeInput{
Project: "myProject",
Branch: "main",
Subject: "test change",
})
if err != nil {
t.Error(err)
}
if info.ID != "abc1234" {
t.Error("Invalid id")

cases := map[string]gerrit.ChangeInput{
"RequiredOnly": {
Project: "myProject",
Branch: "main",
Subject: "test change",
},
"WithMerge": {
Project: "myProject",
Branch: "main",
Subject: "test change",
Merge: &gerrit.MergeInput{
Source: "45/3/1",
},
},
"WithAppend": {
Project: "myProject",
Branch: "main",
Subject: "test change",
Author: &gerrit.AccountInput{
Username: "roboto",
Name: "Rob Oto",
},
},
}
for name, input := range cases {
t.Run(name, func(t *testing.T) {
info, _, err := client.Changes.CreateChange(&input)
if err != nil {
t.Error(err)
}

if info.ID != "abc1234" {
t.Error("Invalid id")
}
})
}
}

Expand Down

0 comments on commit 67e5874

Please sign in to comment.