Skip to content

Commit c0357e3

Browse files
author
Jakub Sarzyński
committed
Add API endpoints related to robot comments
1 parent ded2ca5 commit c0357e3

File tree

4 files changed

+405
-0
lines changed

4 files changed

+405
-0
lines changed

changes.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,15 @@ func (s *ChangesService) ListChangeComments(changeID string) (*map[string][]Comm
673673
return s.getCommentInfoMapResponse(u)
674674
}
675675

676+
// Lists the robot comments of all revisions of the change.
677+
// Return a map that maps the file path to a list of RobotCommentInfo entries. The entries in the map are sorted by file path.
678+
//
679+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-change-robot-comments
680+
func (s *ChangesService) ListChangeRobotComments(changeID string) (map[string][]RobotCommentInfo, *Response, error) {
681+
u := fmt.Sprintf("changes/%s/robotcomments", changeID)
682+
return s.getRobotCommentInfoMapSliceResponse(u)
683+
}
684+
676685
// ListChangeDrafts lLists the draft comments of all revisions of the change that belong to the calling user.
677686
// The entries in the map are sorted by file path, and the comments for each path are sorted by patch set number.
678687
// Each comment has the patch_set field set, and no author.
@@ -741,6 +750,22 @@ func (s *ChangesService) getCommentInfoMapSliceResponse(u string) (*map[string][
741750
return v, resp, err
742751
}
743752

753+
// getRobotCommentInfoMapSliceResponse retrieved a map with a slice of RobotCommentInfo Response for a GET request
754+
func (s *ChangesService) getRobotCommentInfoMapSliceResponse(u string) (map[string][]RobotCommentInfo, *Response, error) {
755+
req, err := s.client.NewRequest("GET", u, nil)
756+
if err != nil {
757+
return nil, nil, err
758+
}
759+
760+
var v map[string][]RobotCommentInfo
761+
resp, err := s.client.Do(req, &v)
762+
if err != nil {
763+
return nil, resp, err
764+
}
765+
766+
return v, resp, err
767+
}
768+
744769
// CreateChange creates a new change.
745770
//
746771
// The change input ChangeInput entity must be provided in the request body.

changes_revision.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,26 @@ func (s *ChangesService) GetComment(changeID, revisionID, commentID string) (*Co
201201
return s.getCommentInfoResponse(u)
202202
}
203203

204+
// GetRobotComment retrieves a robot comment of a revision.
205+
//
206+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-robot-comment
207+
func (s *ChangesService) GetRobotComment(changeID, revisionID, commentID string) (*RobotCommentInfo, *Response, error) {
208+
u := fmt.Sprintf("changes/%s/revisions/%s/robotcomments/%s", changeID, revisionID, commentID)
209+
210+
req, err := s.client.NewRequest("GET", u, nil)
211+
if err != nil {
212+
return nil, nil, err
213+
}
214+
215+
v := new(RobotCommentInfo)
216+
resp, err := s.client.Do(req, v)
217+
if err != nil {
218+
return nil, resp, err
219+
}
220+
221+
return v, resp, err
222+
}
223+
204224
// GetSubmitType gets the method the server will use to submit (merge) the change.
205225
//
206226
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-submit-type
@@ -313,6 +333,11 @@ func (s *ChangesService) ListRevisionComments(changeID, revisionID string) (*map
313333
return s.getCommentInfoMapSliceResponse(u)
314334
}
315335

336+
func (s *ChangesService) ListRevisionRobotComments(changeID, revisionID string) (map[string][]RobotCommentInfo, *Response, error) {
337+
u := fmt.Sprintf("changes/%s/revisions/%s/robotcomments", changeID, revisionID)
338+
return s.getRobotCommentInfoMapSliceResponse(u)
339+
}
340+
316341
// ListFiles lists the files that were modified, added or deleted in a revision.
317342
// As result a map is returned that maps the file path to a list of FileInfo entries.
318343
// The entries in the map are sorted by file path.

changes_revision_test.go

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http/httptest"
77
"reflect"
88
"testing"
9+
"time"
910

1011
"github.com/andygrunwald/go-gerrit"
1112
)
@@ -79,3 +80,235 @@ func TestChangesService_ListFilesReviewed(t *testing.T) {
7980
t.Errorf("client.Changes.ListFilesReviewed:\ngot: %q\nwant: %q", got, want)
8081
}
8182
}
83+
84+
func TestChangesService_ListRevisionRobotComment(t *testing.T) {
85+
listRobotCommentResponse := `)]}'
86+
{
87+
"main.c": [
88+
{
89+
"robot_id": "robot",
90+
"robot_run_id": "1",
91+
"fix_suggestions": [
92+
{
93+
"fix_id": "c3302a6f_1578ee9e",
94+
"description": "suggestion",
95+
"replacements": [
96+
{
97+
"path": "main.c",
98+
"range": {
99+
"start_line": 3,
100+
"start_character": 0,
101+
"end_line": 5,
102+
"end_character": 1
103+
},
104+
"replacement": "int main() { printf(\"Hello world!\"); }"
105+
}
106+
]
107+
}
108+
],
109+
"author": {
110+
"_account_id": 1000000,
111+
"name": "Jhon Smith",
112+
"username": "jhon"
113+
},
114+
"change_message_id": "517e6c92e4bd105f1e611294a3010ea177771551",
115+
"patch_set": 1,
116+
"id": "f7d3d07f_bf17b66e",
117+
"line": 5,
118+
"range": {
119+
"start_line": 3,
120+
"start_character": 0,
121+
"end_line": 5,
122+
"end_character": 1
123+
},
124+
"updated": "2022-07-05 13:44:34.000000000",
125+
"message": "[clang-format] fix suggestion",
126+
"commit_id": "be8ce493368f2ce0fa73b56f5e2bd0dc17ca4359"
127+
}
128+
]
129+
}`
130+
131+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
132+
if r.URL.Path != "/changes/changeID/revisions/revisionID/robotcomments" {
133+
t.Errorf("%s != /changes/changeID/revisions/revisionID/robotcomments", r.URL.Path)
134+
}
135+
if r.Method != "GET" {
136+
t.Error("Method != GET")
137+
}
138+
fmt.Fprint(w, listRobotCommentResponse)
139+
}))
140+
defer ts.Close()
141+
142+
client, err := gerrit.NewClient(ts.URL, nil)
143+
if err != nil {
144+
t.Error(err)
145+
}
146+
147+
got, _, err := client.Changes.ListRevisionRobotComments("changeID", "revisionID")
148+
if err != nil {
149+
t.Errorf("Changes.ListRevisionRobotComments returned error: %v", err)
150+
}
151+
152+
want := map[string][]gerrit.RobotCommentInfo{
153+
"main.c": {
154+
{
155+
CommentInfo: gerrit.CommentInfo{
156+
PatchSet: 1,
157+
ID: "f7d3d07f_bf17b66e",
158+
Line: 5,
159+
Range: &gerrit.CommentRange{
160+
StartLine: 3,
161+
StartCharacter: 0,
162+
EndLine: 5,
163+
EndCharacter: 1,
164+
},
165+
Message: "[clang-format] fix suggestion",
166+
Updated: &gerrit.Timestamp{
167+
Time: time.Date(2022, 7, 5, 13, 44, 34, 0, time.UTC),
168+
},
169+
Author: gerrit.AccountInfo{
170+
AccountID: 1000000,
171+
Name: "Jhon Smith",
172+
Username: "jhon",
173+
},
174+
},
175+
RobotID: "robot",
176+
RobotRunID: "1",
177+
FixSuggestions: []gerrit.FixSuggestionInfo{
178+
{
179+
FixID: "c3302a6f_1578ee9e",
180+
Description: "suggestion",
181+
Replacements: []gerrit.FixReplacementInfo{
182+
{
183+
Path: "main.c",
184+
Range: gerrit.CommentRange{
185+
StartLine: 3,
186+
StartCharacter: 0,
187+
EndLine: 5,
188+
EndCharacter: 1,
189+
},
190+
Replacement: "int main() { printf(\"Hello world!\"); }",
191+
},
192+
},
193+
},
194+
},
195+
},
196+
},
197+
}
198+
199+
if !reflect.DeepEqual(got, want) {
200+
t.Errorf("Change.ListRevisionRobotComments:\ngot: %+v\nwant: %+v", got, want)
201+
}
202+
}
203+
204+
func TestChangesService_GetRobotComment(t *testing.T) {
205+
getRobotCommentResponse := `)]}'
206+
{
207+
"robot_id": "robot",
208+
"robot_run_id": "1",
209+
"fix_suggestions": [
210+
{
211+
"fix_id": "c3302a6f_1578ee9e",
212+
"description": "suggestion",
213+
"replacements": [
214+
{
215+
"path": "main.c",
216+
"range": {
217+
"start_line": 3,
218+
"start_character": 0,
219+
"end_line": 5,
220+
"end_character": 1
221+
},
222+
"replacement": "int main() { printf(\"Hello world!\"); }"
223+
}
224+
]
225+
}
226+
],
227+
"author": {
228+
"_account_id": 1000000,
229+
"name": "Jhon Smith",
230+
"username": "jhon"
231+
},
232+
"change_message_id": "517e6c92e4bd105f1e611294a3010ea177771551",
233+
"patch_set": 1,
234+
"id": "f7d3d07f_bf17b66e",
235+
"line": 5,
236+
"range": {
237+
"start_line": 3,
238+
"start_character": 0,
239+
"end_line": 5,
240+
"end_character": 1
241+
},
242+
"updated": "2022-07-05 13:44:34.000000000",
243+
"message": "[clang-format] fix suggestion",
244+
"commit_id": "be8ce493368f2ce0fa73b56f5e2bd0dc17ca4359"
245+
}`
246+
247+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
248+
if r.URL.Path != "/changes/changeID/revisions/revisionID/robotcomments/commentID" {
249+
t.Errorf("%s != /changes/changeID/revisions/revisionID/robotcomments/commentID", r.URL.Path)
250+
}
251+
if r.Method != "GET" {
252+
t.Error("Method != GET")
253+
}
254+
fmt.Fprint(w, getRobotCommentResponse)
255+
}))
256+
defer ts.Close()
257+
258+
client, err := gerrit.NewClient(ts.URL, nil)
259+
if err != nil {
260+
t.Error(err)
261+
}
262+
263+
got, _, err := client.Changes.GetRobotComment("changeID", "revisionID", "commentID")
264+
if err != nil {
265+
t.Errorf("Changes.GetRobotComment returned error: %v", err)
266+
}
267+
268+
want := &gerrit.RobotCommentInfo{
269+
CommentInfo: gerrit.CommentInfo{
270+
PatchSet: 1,
271+
ID: "f7d3d07f_bf17b66e",
272+
Line: 5,
273+
Range: &gerrit.CommentRange{
274+
StartLine: 3,
275+
StartCharacter: 0,
276+
EndLine: 5,
277+
EndCharacter: 1,
278+
},
279+
Message: "[clang-format] fix suggestion",
280+
Updated: &gerrit.Timestamp{
281+
Time: time.Date(2022, 7, 5, 13, 44, 34, 0, time.UTC),
282+
},
283+
Author: gerrit.AccountInfo{
284+
AccountID: 1000000,
285+
Name: "Jhon Smith",
286+
Username: "jhon",
287+
},
288+
},
289+
RobotID: "robot",
290+
RobotRunID: "1",
291+
FixSuggestions: []gerrit.FixSuggestionInfo{
292+
{
293+
FixID: "c3302a6f_1578ee9e",
294+
Description: "suggestion",
295+
Replacements: []gerrit.FixReplacementInfo{
296+
{
297+
Path: "main.c",
298+
Range: gerrit.CommentRange{
299+
StartLine: 3,
300+
StartCharacter: 0,
301+
EndLine: 5,
302+
EndCharacter: 1,
303+
},
304+
Replacement: "int main() { printf(\"Hello world!\"); }",
305+
},
306+
},
307+
},
308+
},
309+
}
310+
311+
if !reflect.DeepEqual(got, want) {
312+
t.Errorf("Change.GetRobotComment:\ngot: %+v\nwant: %+v", got, want)
313+
}
314+
}

0 commit comments

Comments
 (0)