From 9b04c04a41603588b5610bc75f7c7259a1d2b323 Mon Sep 17 00:00:00 2001 From: Changcheng Xiao Date: Wed, 28 Dec 2016 12:45:29 +0100 Subject: [PATCH] Add a REST API endpoint to support retrieve the robot comment list of a change. Bug: Issue 5053 Change-Id: I4595ae584a6c427d753c4ad9d9a278803ff3a180 --- Documentation/rest-api-changes.txt | 56 ++++++++++++++++++ .../api/revision/RobotCommentsIT.java | 25 ++++++++ .../extensions/api/changes/ChangeApi.java | 17 ++++++ .../server/api/changes/ChangeApiImpl.java | 15 +++++ .../change/ListChangeRobotComments.java | 58 +++++++++++++++++++ .../google/gerrit/server/change/Module.java | 1 + 6 files changed, 172 insertions(+) create mode 100644 gerrit-server/src/main/java/com/google/gerrit/server/change/ListChangeRobotComments.java diff --git a/Documentation/rest-api-changes.txt b/Documentation/rest-api-changes.txt index 4d79774d68..f0fb4c787f 100644 --- a/Documentation/rest-api-changes.txt +++ b/Documentation/rest-api-changes.txt @@ -1895,6 +1895,62 @@ the `patch_set` and `author` fields set. } ---- +[[list-change-robot-comments]] +=== List Change Robot Comments +-- +'GET /changes/link:#change-id[\{change-id\}]/robotcomments' +-- + +Lists the robot comments of all revisions of the change. + +Return a map that maps the file path to a list of +link:#robot-comment-info[RobotCommentInfo] entries. The entries in the +map are sorted by file path. + +.Request +---- + GET /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/robotcomments/ HTTP/1.0 +---- + +.Response +---- + HTTP/1.1 200 OK + Content-Disposition: attachment + Content-Type: application/json; charset=UTF-8 + + )]}' + { + "gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java": [ + { + "id": "TvcXrmjM", + "line": 23, + "message": "unused import", + "updated": "2016-02-26 15:40:43.986000000", + "author": { + "_account_id": 1000110, + "name": "Code Analyzer", + "email": "code.analyzer@example.com" + }, + "robotId": "importChecker", + "robotRunId": "76b1375aa8626ea7149792831fe2ed85e80d9e04" + }, + { + "id": "TveXwFiA", + "line": 49, + "message": "wrong indention", + "updated": "2016-02-26 15:40:45.328000000", + "author": { + "_account_id": 1000110, + "name": "Code Analyzer", + "email": "code.analyzer@example.com" + }, + "robotId": "styleChecker", + "robotRunId": "5c606c425dd45184484f9d0a2ffd725a7607839b" + } + ] + } +---- + [[list-change-drafts]] === List Change Drafts -- diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/revision/RobotCommentsIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/revision/RobotCommentsIT.java index 0b82a8b3f0..f23667e0a7 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/revision/RobotCommentsIT.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/revision/RobotCommentsIT.java @@ -88,6 +88,31 @@ public class RobotCommentsIT extends AbstractDaemonTest { assertRobotComment(comment, in, false); } + @Test + public void addedRobotCommentsCanBeRetrievedByChange() throws Exception { + assume().that(notesMigration.enabled()).isTrue(); + + RobotCommentInput in = createRobotCommentInput(); + addRobotComment(changeId, in); + + pushFactory.create(db, admin.getIdent(), testRepo, changeId) + .to("refs/for/master"); + + RobotCommentInput in2 = createRobotCommentInput(); + addRobotComment(changeId, in2); + + Map> out = + gApi.changes().id(changeId).robotComments(); + + assertThat(out).hasSize(1); + assertThat(out.get(in.path)).hasSize(2); + + RobotCommentInfo comment1 = out.get(in.path).get(0); + assertRobotComment(comment1, in, false); + RobotCommentInfo comment2 = out.get(in.path).get(1); + assertRobotComment(comment2, in2, false); + } + @Test public void robotCommentsCanBeRetrievedAsList() throws Exception { assume().that(notesMigration.enabled()).isTrue(); diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/changes/ChangeApi.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/changes/ChangeApi.java index a545cad44c..8573c6324b 100644 --- a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/changes/ChangeApi.java +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/changes/ChangeApi.java @@ -20,6 +20,7 @@ import com.google.gerrit.extensions.common.ChangeInfo; import com.google.gerrit.extensions.common.CommentInfo; import com.google.gerrit.extensions.common.EditInfo; import com.google.gerrit.extensions.common.MergePatchSetInput; +import com.google.gerrit.extensions.common.RobotCommentInfo; import com.google.gerrit.extensions.common.SuggestedReviewerInfo; import com.google.gerrit.extensions.restapi.NotImplementedException; import com.google.gerrit.extensions.restapi.RestApiException; @@ -177,6 +178,16 @@ public interface ChangeApi { */ Map> comments() throws RestApiException; + /** + * Get all robot comments on a change. + * + * @return robot comments in a map keyed by path; robot comments have the + * {@code revision} field set to indicate their patch set. + * + * @throws RestApiException + */ + Map> robotComments() throws RestApiException; + /** * Get all draft comments for the current user on a change. * @@ -381,6 +392,12 @@ public interface ChangeApi { throw new NotImplementedException(); } + @Override + public Map> robotComments() + throws RestApiException { + throw new NotImplementedException(); + } + @Override public Map> drafts() throws RestApiException { throw new NotImplementedException(); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/ChangeApiImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/ChangeApiImpl.java index 20cf20d076..6fb5bded72 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/ChangeApiImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/ChangeApiImpl.java @@ -34,6 +34,7 @@ import com.google.gerrit.extensions.common.ChangeInfo; import com.google.gerrit.extensions.common.CommentInfo; import com.google.gerrit.extensions.common.EditInfo; import com.google.gerrit.extensions.common.MergePatchSetInput; +import com.google.gerrit.extensions.common.RobotCommentInfo; import com.google.gerrit.extensions.common.SuggestedReviewerInfo; import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.Response; @@ -53,6 +54,7 @@ import com.google.gerrit.server.change.GetTopic; import com.google.gerrit.server.change.Index; import com.google.gerrit.server.change.ListChangeComments; import com.google.gerrit.server.change.ListChangeDrafts; +import com.google.gerrit.server.change.ListChangeRobotComments; import com.google.gerrit.server.change.Move; import com.google.gerrit.server.change.PostHashtags; import com.google.gerrit.server.change.PostReviewers; @@ -109,6 +111,7 @@ class ChangeApiImpl implements ChangeApi { private final GetPastAssignees getPastAssignees; private final DeleteAssignee deleteAssignee; private final ListChangeComments listComments; + private final ListChangeRobotComments listChangeRobotComments; private final ListChangeDrafts listDrafts; private final Check check; private final Index index; @@ -140,6 +143,7 @@ class ChangeApiImpl implements ChangeApi { GetPastAssignees getPastAssignees, DeleteAssignee deleteAssignee, ListChangeComments listComments, + ListChangeRobotComments listChangeRobotComments, ListChangeDrafts listDrafts, Check check, Index index, @@ -170,6 +174,7 @@ class ChangeApiImpl implements ChangeApi { this.getPastAssignees = getPastAssignees; this.deleteAssignee = deleteAssignee; this.listComments = listComments; + this.listChangeRobotComments = listChangeRobotComments; this.listDrafts = listDrafts; this.check = check; this.index = index; @@ -483,6 +488,16 @@ class ChangeApiImpl implements ChangeApi { } } + @Override + public Map> robotComments() + throws RestApiException { + try { + return listChangeRobotComments.apply(change); + } catch (OrmException e) { + throw new RestApiException("Cannot get robot comments", e); + } + } + @Override public Map> drafts() throws RestApiException { try { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/ListChangeRobotComments.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/ListChangeRobotComments.java new file mode 100644 index 0000000000..abfe869eca --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/ListChangeRobotComments.java @@ -0,0 +1,58 @@ +// Copyright (C) 2016 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.server.change; + +import com.google.gerrit.extensions.common.RobotCommentInfo; +import com.google.gerrit.extensions.restapi.AuthException; +import com.google.gerrit.extensions.restapi.RestReadView; +import com.google.gerrit.reviewdb.server.ReviewDb; +import com.google.gerrit.server.CommentsUtil; +import com.google.gerrit.server.query.change.ChangeData; +import com.google.gwtorm.server.OrmException; +import com.google.inject.Inject; +import com.google.inject.Provider; + +import java.util.List; +import java.util.Map; + +public class ListChangeRobotComments implements RestReadView { + private final Provider db; + private final ChangeData.Factory changeDataFactory; + private final Provider commentJson; + private final CommentsUtil commentsUtil; + + @Inject + ListChangeRobotComments(Provider db, + ChangeData.Factory changeDataFactory, + Provider commentJson, + CommentsUtil commentsUtil) { + this.db = db; + this.changeDataFactory = changeDataFactory; + this.commentJson = commentJson; + this.commentsUtil = commentsUtil; + } + + @Override + public Map> apply( + ChangeResource rsrc) throws AuthException, OrmException { + ChangeData cd = changeDataFactory.create(db.get(), rsrc.getControl()); + return commentJson.get() + .setFillAccounts(true) + .setFillPatchSet(true) + .newRobotCommentFormatter() + .format(commentsUtil.robotCommentsByChange(cd.notes())); + } +} + diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/Module.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/Module.java index 4d32222cba..dc9de35984 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/change/Module.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/Module.java @@ -63,6 +63,7 @@ public class Module extends RestApiModule { delete(CHANGE_KIND, "assignee").to(DeleteAssignee.class); get(CHANGE_KIND, "hashtags").to(GetHashtags.class); get(CHANGE_KIND, "comments").to(ListChangeComments.class); + get(CHANGE_KIND, "robotcomments").to(ListChangeRobotComments.class); get(CHANGE_KIND, "drafts").to(ListChangeDrafts.class); get(CHANGE_KIND, "check").to(Check.class); post(CHANGE_KIND, "check").to(Check.class);