diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/change/CommentsIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/change/CommentsIT.java index 6fcb893747..1c79340f1b 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/change/CommentsIT.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/change/CommentsIT.java @@ -119,6 +119,11 @@ public class CommentsIT extends AbstractDaemonTest { assertThat(result).hasSize(1); CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path)); assertThat(comment).isEqualTo(infoToDraft(path).apply(actual)); + + List list = getDraftCommentsAsList(changeId); + assertThat(list).hasSize(1); + actual = list.get(0); + assertThat(comment).isEqualTo(infoToDraft(path).apply(actual)); } } @@ -141,6 +146,10 @@ public class CommentsIT extends AbstractDaemonTest { assertThat(result).hasSize(1); assertThat(Lists.transform(result.get(path), infoToDraft(path))) .containsExactly(c1, c2, c3, c4); + + List list = getDraftCommentsAsList(changeId); + assertThat(list).hasSize(4); + assertThat(Lists.transform(list, infoToDraft(path))).containsExactly(c1, c2, c3, c4); } } @@ -243,6 +252,9 @@ public class CommentsIT extends AbstractDaemonTest { assertThat(result).isNotEmpty(); assertThat(Lists.transform(result.get(file), infoToInput(file))) .containsExactly(c1, c2, c3, c4); + + List list = getPublishedCommentsAsList(changeId); + assertThat(Lists.transform(list, infoToInput(file))).containsExactly(c1, c2, c3, c4); } // for the commit message comments on the auto-merge are not possible @@ -261,6 +273,9 @@ public class CommentsIT extends AbstractDaemonTest { Map> result = getPublishedComments(changeId, revId); assertThat(result).isNotEmpty(); assertThat(Lists.transform(result.get(file), infoToInput(file))).containsExactly(c1, c2, c3); + + List list = getPublishedCommentsAsList(changeId); + assertThat(Lists.transform(list, infoToInput(file))).containsExactly(c1, c2, c3); } } @@ -285,6 +300,7 @@ public class CommentsIT extends AbstractDaemonTest { String changeId = r.getChangeId(); String revId = r.getCommit().getName(); assertThat(getPublishedComments(changeId, revId)).isEmpty(); + assertThat(getPublishedCommentsAsList(changeId)).isEmpty(); List expectedComments = new ArrayList<>(); for (Integer line : lines) { @@ -301,6 +317,10 @@ public class CommentsIT extends AbstractDaemonTest { List actualComments = result.get(file); assertThat(Lists.transform(actualComments, infoToInput(file))) .containsExactlyElementsIn(expectedComments); + + List list = getPublishedCommentsAsList(changeId); + assertThat(Lists.transform(list, infoToInput(file))) + .containsExactlyElementsIn(expectedComments); } @Test @@ -1101,11 +1121,19 @@ public class CommentsIT extends AbstractDaemonTest { return gApi.changes().id(changeId).revision(revId).comments(); } + private List getPublishedCommentsAsList(String changeId) throws Exception { + return gApi.changes().id(changeId).commentsAsList(); + } + private Map> getDraftComments(String changeId, String revId) throws Exception { return gApi.changes().id(changeId).revision(revId).drafts(); } + private List getDraftCommentsAsList(String changeId) throws Exception { + return gApi.changes().id(changeId).draftsAsList(); + } + private Map> getPublishedComments(String changeId) throws Exception { return gApi.changes().id(changeId).comments(); } 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 5183a89e38..202a67d47d 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 @@ -262,6 +262,15 @@ public interface ChangeApi { */ Map> comments() throws RestApiException; + /** + * Get all published comments on a change as a list. + * + * @return comments as a list; comments have the {@code revision} field set to indicate their + * patch set. + * @throws RestApiException + */ + List commentsAsList() throws RestApiException; + /** * Get all robot comments on a change. * @@ -280,6 +289,15 @@ public interface ChangeApi { */ Map> drafts() throws RestApiException; + /** + * Get all draft comments for the current user on a change as a list. + * + * @return drafts as a list; comments have the {@code revision} field set to indicate their patch + * set. + * @throws RestApiException + */ + List draftsAsList() throws RestApiException; + ChangeInfo check() throws RestApiException; ChangeInfo check(FixInput fix) throws RestApiException; @@ -533,6 +551,11 @@ public interface ChangeApi { throw new NotImplementedException(); } + @Override + public List commentsAsList() throws RestApiException { + throw new NotImplementedException(); + } + @Override public Map> robotComments() throws RestApiException { throw new NotImplementedException(); @@ -543,6 +566,11 @@ public interface ChangeApi { throw new NotImplementedException(); } + @Override + public List draftsAsList() throws RestApiException { + throw new NotImplementedException(); + } + @Override public ChangeInfo check() 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 447fdf4f1b..373a4253c9 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 @@ -619,6 +619,15 @@ class ChangeApiImpl implements ChangeApi { } } + @Override + public List commentsAsList() throws RestApiException { + try { + return listComments.getComments(change); + } catch (Exception e) { + throw asRestApiException("Cannot get comments", e); + } + } + @Override public Map> robotComments() throws RestApiException { try { @@ -637,6 +646,15 @@ class ChangeApiImpl implements ChangeApi { } } + @Override + public List draftsAsList() throws RestApiException { + try { + return listDrafts.getComments(change); + } catch (Exception e) { + throw asRestApiException("Cannot get drafts", e); + } + } + @Override public ChangeInfo check() throws RestApiException { try { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/ListChangeDrafts.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/ListChangeDrafts.java index f0b693383c..22fc5ea171 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/change/ListChangeDrafts.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/ListChangeDrafts.java @@ -20,6 +20,7 @@ import com.google.gerrit.extensions.restapi.RestReadView; import com.google.gerrit.reviewdb.client.Comment; import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.server.CommentsUtil; +import com.google.gerrit.server.change.CommentJson.CommentFormatter; import com.google.gerrit.server.query.change.ChangeData; import com.google.gwtorm.server.OrmException; import com.google.inject.Inject; @@ -66,11 +67,21 @@ public class ListChangeDrafts implements RestReadView { if (requireAuthentication() && !rsrc.getUser().isIdentifiedUser()) { throw new AuthException("Authentication required"); } + return getCommentFormatter().format(listComments(rsrc)); + } + + public List getComments(ChangeResource rsrc) throws AuthException, OrmException { + if (requireAuthentication() && !rsrc.getUser().isIdentifiedUser()) { + throw new AuthException("Authentication required"); + } + return getCommentFormatter().formatAsList(listComments(rsrc)); + } + + private CommentFormatter getCommentFormatter() { return commentJson .get() .setFillAccounts(includeAuthorInfo()) .setFillPatchSet(true) - .newCommentFormatter() - .format(listComments(rsrc)); + .newCommentFormatter(); } }