Skip to content

Commit b548298

Browse files
authored
Merge pull request #43 from andygrunwald/votes-endpoints
Implement ListVotes(), DeleteVotes() and add missing tests
2 parents 3a74595 + 21c1dc2 commit b548298

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

changes_reviewer.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ type AddReviewerResult struct {
2525
Confirm bool `json:"confirm,omitempty"`
2626
}
2727

28+
// DeleteVoteInput entity contains options for the deletion of a vote.
29+
//
30+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#delete-vote-input
31+
type DeleteVoteInput struct {
32+
Label string `json:"label,omitempty"`
33+
Notify string `json:"notify,omitempty"`
34+
NotifyDetails map[string]NotifyInfo `json:"notify_details"`
35+
}
36+
2837
// ListReviewers lists the reviewers of a change.
2938
//
3039
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-reviewers
@@ -124,3 +133,31 @@ func (s *ChangesService) DeleteReviewer(changeID, accountID string) (*Response,
124133
u := fmt.Sprintf("changes/%s/reviewers/%s", changeID, accountID)
125134
return s.client.DeleteRequest(u, nil)
126135
}
136+
137+
// ListVotes lists the votes for a specific reviewer of the change.
138+
//
139+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-votes
140+
func (s *ChangesService) ListVotes(changeID string, accountID string) (map[string]int, *Response, error) {
141+
u := fmt.Sprintf("changes/%s/reviewers/%s/votes/", changeID, accountID)
142+
req, err := s.client.NewRequest("GET", u, nil)
143+
if err != nil {
144+
return nil, nil, err
145+
}
146+
147+
var v map[string]int
148+
resp, err := s.client.Do(req, &v)
149+
if err != nil {
150+
return nil, resp, err
151+
}
152+
return v, resp, err
153+
}
154+
155+
// DeleteVote deletes a single vote from a change. Note, that even when the
156+
// last vote of a reviewer is removed the reviewer itself is still listed on
157+
// the change.
158+
//
159+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#delete-vote
160+
func (s *ChangesService) DeleteVote(changeID string, accountID string, label string, input *DeleteVoteInput) (*Response, error) {
161+
u := fmt.Sprintf("changes/%s/reviewers/%s/votes/%s", changeID, accountID, label)
162+
return s.client.DeleteRequest(u, input)
163+
}

changes_reviewer_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package gerrit_test
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/andygrunwald/go-gerrit"
10+
)
11+
12+
func newClient(t *testing.T, server *httptest.Server) *gerrit.Client {
13+
client, err := gerrit.NewClient(server.URL, nil)
14+
if err != nil {
15+
t.Error(err)
16+
}
17+
return client
18+
}
19+
20+
func TestChangesService_ListReviewers(t *testing.T) {
21+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
expected := "/changes/123/reviewers/"
23+
if r.URL.Path != expected {
24+
t.Errorf("%s != %s", r.URL.Path, expected)
25+
}
26+
27+
fmt.Fprint(w, `[{"_account_id": 1}]`)
28+
}))
29+
defer ts.Close()
30+
31+
client := newClient(t, ts)
32+
data, _, err := client.Changes.ListReviewers("123")
33+
if err != nil {
34+
t.Error(err)
35+
}
36+
37+
if len(*data) != 1 {
38+
t.Error("Length of data !=1 ")
39+
}
40+
41+
if (*data)[0].AccountID != 1 {
42+
t.Error("AccountID != 1")
43+
}
44+
}
45+
46+
func TestChangesService_SuggestReviewers(t *testing.T) {
47+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48+
expected := "/changes/123/suggest_reviewers"
49+
if r.URL.Path != expected {
50+
t.Errorf("%s != %s", r.URL.Path, expected)
51+
}
52+
53+
fmt.Fprint(w, `[{"account": {"_account_id": 1}}]`)
54+
}))
55+
defer ts.Close()
56+
57+
client := newClient(t, ts)
58+
data, _, err := client.Changes.SuggestReviewers("123", nil)
59+
if err != nil {
60+
t.Error(err)
61+
}
62+
63+
if len(*data) != 1 {
64+
t.Error("Length of data !=1 ")
65+
}
66+
67+
if (*data)[0].Account.AccountID != 1 {
68+
t.Error("AccountID != 1")
69+
}
70+
}
71+
72+
func TestChangesService_GetReviewer(t *testing.T) {
73+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
74+
expected := "/changes/123/reviewers/1"
75+
if r.URL.Path != expected {
76+
t.Errorf("%s != %s", r.URL.Path, expected)
77+
}
78+
79+
fmt.Fprint(w, `{"_account_id": 1}`)
80+
}))
81+
defer ts.Close()
82+
83+
client := newClient(t, ts)
84+
data, _, err := client.Changes.GetReviewer("123", "1")
85+
if err != nil {
86+
t.Error(err)
87+
}
88+
if data.AccountID != 1 {
89+
t.Error("AccountID != 1")
90+
}
91+
}
92+
93+
func TestChangesService_AddReviewer(t *testing.T) {
94+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
95+
expected := "/changes/123/reviewers"
96+
if r.URL.Path != expected {
97+
t.Errorf("%s != %s", r.URL.Path, expected)
98+
}
99+
if r.Method != "POST" {
100+
t.Error("Method != POST")
101+
}
102+
103+
fmt.Fprint(w, `{"confirm": true}`)
104+
}))
105+
defer ts.Close()
106+
107+
client := newClient(t, ts)
108+
data, _, err := client.Changes.AddReviewer("123", &gerrit.ReviewerInput{})
109+
if err != nil {
110+
t.Error(err)
111+
}
112+
if !data.Confirm {
113+
t.Error("Confirm != true")
114+
}
115+
}
116+
117+
func TestChangesService_DeleteReviewer(t *testing.T) {
118+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
119+
expected := "/changes/123/reviewers/1"
120+
if r.URL.Path != expected {
121+
t.Errorf("%s != %s", r.URL.Path, expected)
122+
}
123+
if r.Method != "DELETE" {
124+
t.Error("Method != DELETE")
125+
}
126+
w.WriteHeader(http.StatusOK)
127+
}))
128+
defer ts.Close()
129+
130+
client := newClient(t, ts)
131+
_, err := client.Changes.DeleteReviewer("123", "1")
132+
if err != nil {
133+
t.Error(err)
134+
}
135+
}
136+
137+
func TestChangesService_ListVotes(t *testing.T) {
138+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
139+
expected := "/changes/123/reviewers/1/votes/"
140+
if r.URL.Path != expected {
141+
t.Errorf("%s != %s", r.URL.Path, expected)
142+
}
143+
fmt.Fprint(w, `{"Code-Review": 2, "Verified": 1}`)
144+
}))
145+
defer ts.Close()
146+
147+
client := newClient(t, ts)
148+
votes, _, err := client.Changes.ListVotes("123", "1")
149+
if err != nil {
150+
t.Error(err)
151+
}
152+
if votes["Code-Review"] != 2 {
153+
t.Error("Code-Review != 2")
154+
}
155+
if votes["Verified"] != 1 {
156+
t.Error("Verified != 1")
157+
}
158+
}
159+
160+
func TestChangesService_DeleteVote(t *testing.T) {
161+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
162+
expected := "/changes/123/reviewers/1/votes/Code-Review"
163+
if r.URL.Path != expected {
164+
t.Errorf("%s != %s", r.URL.Path, expected)
165+
}
166+
167+
if r.Method != "DELETE" {
168+
t.Error("Method != DELETE")
169+
}
170+
w.WriteHeader(http.StatusOK)
171+
}))
172+
defer ts.Close()
173+
174+
client := newClient(t, ts)
175+
_, err := client.Changes.DeleteVote("123", "1", "Code-Review", nil)
176+
if err != nil {
177+
t.Error(err)
178+
}
179+
}

0 commit comments

Comments
 (0)