ChangeApi: Add methods to get change comments/drafts as list

The current API allows to get all change comments and drafts, but as a
map of path names to lists of comment infos; the paths are not set in
the comment infos. This is not convenient for callers that just want a
list of all comments on the change; they have to transform the map to
a list, setting the path field on each element.

Add new methods that return the comments as a plain list, with paths
already set. This reduces the amount of code clients need to write.

Note that such methods already exist on the revision API.

Change-Id: Ic3fd48347d195ebbe361d32167dbaacb572f066d
This commit is contained in:
David Pursehouse
2019-08-05 22:32:08 +09:00
parent 8a093ffd8c
commit f841099db5
4 changed files with 87 additions and 2 deletions

View File

@@ -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<CommentInfo> 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<CommentInfo> 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<CommentInfo> 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<String, List<CommentInfo>> result = getPublishedComments(changeId, revId);
assertThat(result).isNotEmpty();
assertThat(Lists.transform(result.get(file), infoToInput(file))).containsExactly(c1, c2, c3);
List<CommentInfo> 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<CommentInput> expectedComments = new ArrayList<>();
for (Integer line : lines) {
@@ -301,6 +317,10 @@ public class CommentsIT extends AbstractDaemonTest {
List<CommentInfo> actualComments = result.get(file);
assertThat(Lists.transform(actualComments, infoToInput(file)))
.containsExactlyElementsIn(expectedComments);
List<CommentInfo> 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<CommentInfo> getPublishedCommentsAsList(String changeId) throws Exception {
return gApi.changes().id(changeId).commentsAsList();
}
private Map<String, List<CommentInfo>> getDraftComments(String changeId, String revId)
throws Exception {
return gApi.changes().id(changeId).revision(revId).drafts();
}
private List<CommentInfo> getDraftCommentsAsList(String changeId) throws Exception {
return gApi.changes().id(changeId).draftsAsList();
}
private Map<String, List<CommentInfo>> getPublishedComments(String changeId) throws Exception {
return gApi.changes().id(changeId).comments();
}

View File

@@ -262,6 +262,15 @@ public interface ChangeApi {
*/
Map<String, List<CommentInfo>> 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<CommentInfo> commentsAsList() throws RestApiException;
/**
* Get all robot comments on a change.
*
@@ -280,6 +289,15 @@ public interface ChangeApi {
*/
Map<String, List<CommentInfo>> 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<CommentInfo> 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<CommentInfo> commentsAsList() throws RestApiException {
throw new NotImplementedException();
}
@Override
public Map<String, List<RobotCommentInfo>> robotComments() throws RestApiException {
throw new NotImplementedException();
@@ -543,6 +566,11 @@ public interface ChangeApi {
throw new NotImplementedException();
}
@Override
public List<CommentInfo> draftsAsList() throws RestApiException {
throw new NotImplementedException();
}
@Override
public ChangeInfo check() throws RestApiException {
throw new NotImplementedException();

View File

@@ -619,6 +619,15 @@ class ChangeApiImpl implements ChangeApi {
}
}
@Override
public List<CommentInfo> commentsAsList() throws RestApiException {
try {
return listComments.getComments(change);
} catch (Exception e) {
throw asRestApiException("Cannot get comments", e);
}
}
@Override
public Map<String, List<RobotCommentInfo>> robotComments() throws RestApiException {
try {
@@ -637,6 +646,15 @@ class ChangeApiImpl implements ChangeApi {
}
}
@Override
public List<CommentInfo> draftsAsList() throws RestApiException {
try {
return listDrafts.getComments(change);
} catch (Exception e) {
throw asRestApiException("Cannot get drafts", e);
}
}
@Override
public ChangeInfo check() throws RestApiException {
try {

View File

@@ -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<ChangeResource> {
if (requireAuthentication() && !rsrc.getUser().isIdentifiedUser()) {
throw new AuthException("Authentication required");
}
return getCommentFormatter().format(listComments(rsrc));
}
public List<CommentInfo> 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();
}
}