Skip to content

Commit 67e5874

Browse files
authored
changes: fill in the rest of Changes.ChangeInput (#97)
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>
1 parent 58f949a commit 67e5874

File tree

2 files changed

+86
-25
lines changed

2 files changed

+86
-25
lines changed

changes.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,21 @@ type DiffIntralineInfo [][2]int
371371
//
372372
// Docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-input
373373
type ChangeInput struct {
374-
Project string `json:"project"`
375-
Branch string `json:"branch"`
376-
Subject string `json:"subject"`
377-
Topic string `json:"topic,omitempty"`
378-
Status string `json:"status,omitempty"`
379-
380-
// Attention: Fields are missing.
381-
// TODO Add the full list of attributes from https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-input
374+
Project string `json:"project"`
375+
Branch string `json:"branch"`
376+
Subject string `json:"subject"`
377+
Topic string `json:"topic,omitempty"`
378+
Status string `json:"status,omitempty"`
379+
IsPrivate bool `json:"is_private,omitempty"`
380+
WorkInProgress bool `json:"work_in_progress,omitempty"`
381+
BaseChange string `json:"base_change,omitempty"`
382+
BaseCommit string `json:"base_commit,omitempty"`
383+
NewBranch bool `json:"new_branch,omitempty"`
384+
ValidationOptions map[string]interface{} `json:"validation_options,omitempty"`
385+
Merge *MergeInput `json:"merge,omitempty"`
386+
Author *AccountInput `json:"author,omitempty"`
387+
Notify string `json:"notify,omitempty"`
388+
NotifyDetails string `json:"notify_details,omitempty"`
382389
}
383390

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

460+
// The MergeInput entity contains information about the merge
461+
//
462+
// Docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#merge-input
463+
type MergeInput struct {
464+
Source string `json:"source"`
465+
SourceBranch string `json:"source_branch,omitempty"`
466+
Strategy string `json:"strategy,omitempty"`
467+
AllowConflicts bool `json:"allow_conflicts,omitempty"`
468+
}
469+
453470
// RevisionInfo entity contains information about a patch set.
454471
type RevisionInfo struct {
455472
Draft bool `json:"draft,omitempty"`

changes_test.go

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ func ExampleChangesService_PublishChangeEdit() {
106106
}
107107
}
108108

109+
func disallowEmptyFields(t *testing.T, payload map[string]interface{}, path string) {
110+
for field, generic := range payload {
111+
curPath := field
112+
if len(path) > 0 {
113+
curPath = path + "." + field
114+
}
115+
switch value := generic.(type) {
116+
case string:
117+
if len(value) == 0 {
118+
t.Errorf("Empty value for field %q", curPath)
119+
}
120+
case map[string]interface{}:
121+
if len(value) == 0 {
122+
t.Errorf("Empty value for field %q", curPath)
123+
}
124+
disallowEmptyFields(t, value, curPath)
125+
}
126+
}
127+
}
128+
109129
func TestChangesService_CreateChange(t *testing.T) {
110130
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111131
decoder := json.NewDecoder(r.Body)
@@ -131,16 +151,14 @@ func TestChangesService_CreateChange(t *testing.T) {
131151
project := required("project")
132152
branch := required("branch")
133153
subject := required("subject")
134-
135-
for field, generic := range payload {
136-
switch value := generic.(type) {
137-
case string:
138-
if len(value) == 0 {
139-
t.Errorf("Empty value for field %q", field)
140-
}
154+
if merge, ok := payload["merge"]; ok {
155+
if _, ok := merge.(map[string]interface{})["source"]; !ok {
156+
t.Error(`Missing required field "merge.source"`)
141157
}
142158
}
143159

160+
disallowEmptyFields(t, payload, "")
161+
144162
if r.URL.Path != "/changes/" {
145163
t.Errorf("%s != /changes/", r.URL.Path)
146164
}
@@ -155,16 +173,42 @@ func TestChangesService_CreateChange(t *testing.T) {
155173
if err != nil {
156174
t.Error(err)
157175
}
158-
info, _, err := client.Changes.CreateChange(&gerrit.ChangeInput{
159-
Project: "myProject",
160-
Branch: "main",
161-
Subject: "test change",
162-
})
163-
if err != nil {
164-
t.Error(err)
165-
}
166-
if info.ID != "abc1234" {
167-
t.Error("Invalid id")
176+
177+
cases := map[string]gerrit.ChangeInput{
178+
"RequiredOnly": {
179+
Project: "myProject",
180+
Branch: "main",
181+
Subject: "test change",
182+
},
183+
"WithMerge": {
184+
Project: "myProject",
185+
Branch: "main",
186+
Subject: "test change",
187+
Merge: &gerrit.MergeInput{
188+
Source: "45/3/1",
189+
},
190+
},
191+
"WithAppend": {
192+
Project: "myProject",
193+
Branch: "main",
194+
Subject: "test change",
195+
Author: &gerrit.AccountInput{
196+
Username: "roboto",
197+
Name: "Rob Oto",
198+
},
199+
},
200+
}
201+
for name, input := range cases {
202+
t.Run(name, func(t *testing.T) {
203+
info, _, err := client.Changes.CreateChange(&input)
204+
if err != nil {
205+
t.Error(err)
206+
}
207+
208+
if info.ID != "abc1234" {
209+
t.Error("Invalid id")
210+
}
211+
})
168212
}
169213
}
170214

0 commit comments

Comments
 (0)