Skip to content

Commit 9cd8431

Browse files
authored
Merge pull request #41 from andygrunwald/add-missing-endpoints
Add missing AbandonChange, RebaseChange, RestoreChange and RevertChange
2 parents a2e6113 + 6c23053 commit 9cd8431

File tree

3 files changed

+257
-17
lines changed

3 files changed

+257
-17
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ first. For more complete details see
77

88
## Versions
99

10+
### 0.5.1
11+
12+
* Added the `AbandonChange`, `RebaseChange`, `RestoreChange` and
13+
`RevertChange` functions.
14+
1015
### 0.5.0
1116

1217
**WARNING**: This release includes breaking changes.

changes.go

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ type GitPersonInfo struct {
3131
TZ int `json:"tz"`
3232
}
3333

34+
// NotifyInfo entity contains detailed information about who should be
35+
// notified about an update
36+
type NotifyInfo struct {
37+
Accounts []AccountInfo `json:"accounts"`
38+
}
39+
3440
// AbandonInput entity contains information for abandoning a change.
3541
type AbandonInput struct {
36-
Message string `json:"message,omitempty"`
42+
Message string `json:"message,omitempty"`
43+
Notify string `json:"notify"`
44+
NotifyDetails []NotifyInfo `json:"notify_details"`
3745
}
3846

3947
// ApprovalInfo entity contains information about an approval from a user for a label on a change.
@@ -683,21 +691,16 @@ func (s *ChangesService) FixChange(changeID string, input *FixInput) (*ChangeInf
683691
return v, resp, err
684692
}
685693

686-
// SubmitChange submits a change.
687-
//
688-
// The request body only needs to include a SubmitInput entity if submitting on behalf of another user.
689-
//
690-
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#submit-change
691-
func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*ChangeInfo, *Response, error) {
692-
u := fmt.Sprintf("changes/%s/submit", changeID)
693-
694+
// change is an internal function to consolidate code used by SubmitChange,
695+
// AbandonChange and other similar functions.
696+
func (s *ChangesService) change(tail string, changeID string, input interface{}) (*ChangeInfo, *Response, error) {
697+
u := fmt.Sprintf("changes/%s/%s", changeID, tail)
694698
req, err := s.client.NewRequest("POST", u, input)
695699
if err != nil {
696700
return nil, nil, err
697701
}
698702

699703
v := new(ChangeInfo)
700-
701704
resp, err := s.client.Do(req, v)
702705
if resp.StatusCode == http.StatusConflict {
703706
body, _ := ioutil.ReadAll(resp.Body)
@@ -706,10 +709,51 @@ func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*Cha
706709
return v, resp, err
707710
}
708711

709-
/*
710-
Missing Change Endpoints
711-
Abandon Change
712-
Restore Change
713-
Rebase Change
714-
Revert Change
715-
*/
712+
// SubmitChange submits a change.
713+
//
714+
// The request body only needs to include a SubmitInput entity if submitting on behalf of another user.
715+
//
716+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#submit-change
717+
func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*ChangeInfo, *Response, error) {
718+
return s.change("submit", changeID, input)
719+
}
720+
721+
// AbandonChange abandons a change.
722+
//
723+
// The request body does not need to include a AbandonInput entity if no review
724+
// comment is added.
725+
//
726+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#abandon-change
727+
func (s *ChangesService) AbandonChange(changeID string, input *AbandonInput) (*ChangeInfo, *Response, error) {
728+
return s.change("abandon", changeID, input)
729+
}
730+
731+
// RebaseChange rebases a change.
732+
//
733+
// Optionally, the parent revision can be changed to another patch set through
734+
// the RebaseInput entity.
735+
//
736+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#rebase-change
737+
func (s *ChangesService) RebaseChange(changeID string, input *RebaseInput) (*ChangeInfo, *Response, error) {
738+
return s.change("rebase", changeID, input)
739+
}
740+
741+
// RestoreChange restores a change.
742+
//
743+
// The request body does not need to include a RestoreInput entity if no review
744+
// comment is added.
745+
//
746+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#restore-change
747+
func (s *ChangesService) RestoreChange(changeID string, input *RestoreInput) (*ChangeInfo, *Response, error) {
748+
return s.change("restore", changeID, input)
749+
}
750+
751+
// RevertChange reverts a change.
752+
//
753+
// The request body does not need to include a RevertInput entity if no
754+
// review comment is added.
755+
//
756+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#revert-change
757+
func (s *ChangesService) RevertChange(changeID string, input *RevertInput) (*ChangeInfo, *Response, error) {
758+
return s.change("revert", changeID, input)
759+
}

changes_test.go

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"net/http/httptest"
7+
"testing"
78

89
"github.com/andygrunwald/go-gerrit"
910
)
@@ -77,3 +78,193 @@ func ExampleChangesService_PublishChangeEdit() {
7778
panic(err)
7879
}
7980
}
81+
82+
func TestChangesService_SubmitChange(t *testing.T) {
83+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84+
if r.URL.Path != "/changes/123/submit" {
85+
t.Errorf("%s != /changes/123/submit", r.URL.Path)
86+
}
87+
fmt.Fprint(w, `{"id": "123"}`)
88+
}))
89+
defer ts.Close()
90+
91+
client, err := gerrit.NewClient(ts.URL, nil)
92+
if err != nil {
93+
t.Error(err)
94+
}
95+
info, _, err := client.Changes.SubmitChange("123", nil)
96+
if err != nil {
97+
t.Error(err)
98+
}
99+
if info.ID != "123" {
100+
t.Error("Invalid id")
101+
}
102+
}
103+
104+
func TestChangesService_SubmitChange_Conflict(t *testing.T) {
105+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
106+
w.WriteHeader(http.StatusConflict)
107+
}))
108+
defer ts.Close()
109+
110+
client, err := gerrit.NewClient(ts.URL, nil)
111+
if err != nil {
112+
t.Error(err)
113+
}
114+
_, response, _ := client.Changes.SubmitChange("123", nil)
115+
if response.StatusCode != http.StatusConflict {
116+
t.Error("Expected 409 code")
117+
}
118+
}
119+
120+
func TestChangesService_AbandonChange(t *testing.T) {
121+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
122+
if r.URL.Path != "/changes/123/abandon" {
123+
t.Errorf("%s != /changes/123/abandon", r.URL.Path)
124+
}
125+
fmt.Fprint(w, `{"id": "123"}`)
126+
}))
127+
defer ts.Close()
128+
129+
client, err := gerrit.NewClient(ts.URL, nil)
130+
if err != nil {
131+
t.Error(err)
132+
}
133+
info, _, err := client.Changes.AbandonChange("123", nil)
134+
if err != nil {
135+
t.Error(err)
136+
}
137+
if info.ID != "123" {
138+
t.Error("Invalid id")
139+
}
140+
}
141+
142+
func TestChangesService_AbandonChange_Conflict(t *testing.T) {
143+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
144+
w.WriteHeader(http.StatusConflict)
145+
}))
146+
defer ts.Close()
147+
148+
client, err := gerrit.NewClient(ts.URL, nil)
149+
if err != nil {
150+
t.Error(err)
151+
}
152+
_, response, _ := client.Changes.AbandonChange("123", nil)
153+
if response.StatusCode != http.StatusConflict {
154+
t.Error("Expected 409 code")
155+
}
156+
}
157+
158+
func TestChangesService_RebaseChange(t *testing.T) {
159+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
160+
if r.URL.Path != "/changes/123/rebase" {
161+
t.Errorf("%s != /changes/123/rebase", r.URL.Path)
162+
}
163+
fmt.Fprint(w, `{"id": "123"}`)
164+
}))
165+
defer ts.Close()
166+
167+
client, err := gerrit.NewClient(ts.URL, nil)
168+
if err != nil {
169+
t.Error(err)
170+
}
171+
info, _, err := client.Changes.RebaseChange("123", nil)
172+
if err != nil {
173+
t.Error(err)
174+
}
175+
if info.ID != "123" {
176+
t.Error("Invalid id")
177+
}
178+
}
179+
180+
func TestChangesService_RebaseChange_Conflict(t *testing.T) {
181+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
182+
w.WriteHeader(http.StatusConflict)
183+
}))
184+
defer ts.Close()
185+
186+
client, err := gerrit.NewClient(ts.URL, nil)
187+
if err != nil {
188+
t.Error(err)
189+
}
190+
_, response, _ := client.Changes.RebaseChange("123", nil)
191+
if response.StatusCode != http.StatusConflict {
192+
t.Error("Expected 409 code")
193+
}
194+
}
195+
196+
func TestChangesService_RestoreChange(t *testing.T) {
197+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
198+
if r.URL.Path != "/changes/123/restore" {
199+
t.Errorf("%s != /changes/123/restore", r.URL.Path)
200+
}
201+
fmt.Fprint(w, `{"id": "123"}`)
202+
}))
203+
defer ts.Close()
204+
205+
client, err := gerrit.NewClient(ts.URL, nil)
206+
if err != nil {
207+
t.Error(err)
208+
}
209+
info, _, err := client.Changes.RestoreChange("123", nil)
210+
if err != nil {
211+
t.Error(err)
212+
}
213+
if info.ID != "123" {
214+
t.Error("Invalid id")
215+
}
216+
}
217+
218+
func TestChangesService_RestoreChange_Conflict(t *testing.T) {
219+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
220+
w.WriteHeader(http.StatusConflict)
221+
}))
222+
defer ts.Close()
223+
224+
client, err := gerrit.NewClient(ts.URL, nil)
225+
if err != nil {
226+
t.Error(err)
227+
}
228+
_, response, _ := client.Changes.RestoreChange("123", nil)
229+
if response.StatusCode != http.StatusConflict {
230+
t.Error("Expected 409 code")
231+
}
232+
}
233+
234+
func TestChangesService_RevertChange(t *testing.T) {
235+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
236+
if r.URL.Path != "/changes/123/revert" {
237+
t.Errorf("%s != /changes/123/revert", r.URL.Path)
238+
}
239+
fmt.Fprint(w, `{"id": "123"}`)
240+
}))
241+
defer ts.Close()
242+
243+
client, err := gerrit.NewClient(ts.URL, nil)
244+
if err != nil {
245+
t.Error(err)
246+
}
247+
info, _, err := client.Changes.RevertChange("123", nil)
248+
if err != nil {
249+
t.Error(err)
250+
}
251+
if info.ID != "123" {
252+
t.Error("Invalid id")
253+
}
254+
}
255+
256+
func TestChangesService_RevertChange_Conflict(t *testing.T) {
257+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
258+
w.WriteHeader(http.StatusConflict)
259+
}))
260+
defer ts.Close()
261+
262+
client, err := gerrit.NewClient(ts.URL, nil)
263+
if err != nil {
264+
t.Error(err)
265+
}
266+
_, response, _ := client.Changes.RevertChange("123", nil)
267+
if response.StatusCode != http.StatusConflict {
268+
t.Error("Expected 409 code")
269+
}
270+
}

0 commit comments

Comments
 (0)