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:
@@ -119,6 +119,11 @@ public class CommentsIT extends AbstractDaemonTest {
|
|||||||
assertThat(result).hasSize(1);
|
assertThat(result).hasSize(1);
|
||||||
CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path));
|
CommentInfo actual = Iterables.getOnlyElement(result.get(comment.path));
|
||||||
assertThat(comment).isEqualTo(infoToDraft(path).apply(actual));
|
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(result).hasSize(1);
|
||||||
assertThat(Lists.transform(result.get(path), infoToDraft(path)))
|
assertThat(Lists.transform(result.get(path), infoToDraft(path)))
|
||||||
.containsExactly(c1, c2, c3, c4);
|
.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(result).isNotEmpty();
|
||||||
assertThat(Lists.transform(result.get(file), infoToInput(file)))
|
assertThat(Lists.transform(result.get(file), infoToInput(file)))
|
||||||
.containsExactly(c1, c2, c3, c4);
|
.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
|
// 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);
|
Map<String, List<CommentInfo>> result = getPublishedComments(changeId, revId);
|
||||||
assertThat(result).isNotEmpty();
|
assertThat(result).isNotEmpty();
|
||||||
assertThat(Lists.transform(result.get(file), infoToInput(file))).containsExactly(c1, c2, c3);
|
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 changeId = r.getChangeId();
|
||||||
String revId = r.getCommit().getName();
|
String revId = r.getCommit().getName();
|
||||||
assertThat(getPublishedComments(changeId, revId)).isEmpty();
|
assertThat(getPublishedComments(changeId, revId)).isEmpty();
|
||||||
|
assertThat(getPublishedCommentsAsList(changeId)).isEmpty();
|
||||||
|
|
||||||
List<CommentInput> expectedComments = new ArrayList<>();
|
List<CommentInput> expectedComments = new ArrayList<>();
|
||||||
for (Integer line : lines) {
|
for (Integer line : lines) {
|
||||||
@@ -301,6 +317,10 @@ public class CommentsIT extends AbstractDaemonTest {
|
|||||||
List<CommentInfo> actualComments = result.get(file);
|
List<CommentInfo> actualComments = result.get(file);
|
||||||
assertThat(Lists.transform(actualComments, infoToInput(file)))
|
assertThat(Lists.transform(actualComments, infoToInput(file)))
|
||||||
.containsExactlyElementsIn(expectedComments);
|
.containsExactlyElementsIn(expectedComments);
|
||||||
|
|
||||||
|
List<CommentInfo> list = getPublishedCommentsAsList(changeId);
|
||||||
|
assertThat(Lists.transform(list, infoToInput(file)))
|
||||||
|
.containsExactlyElementsIn(expectedComments);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -1101,11 +1121,19 @@ public class CommentsIT extends AbstractDaemonTest {
|
|||||||
return gApi.changes().id(changeId).revision(revId).comments();
|
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)
|
private Map<String, List<CommentInfo>> getDraftComments(String changeId, String revId)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
return gApi.changes().id(changeId).revision(revId).drafts();
|
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 {
|
private Map<String, List<CommentInfo>> getPublishedComments(String changeId) throws Exception {
|
||||||
return gApi.changes().id(changeId).comments();
|
return gApi.changes().id(changeId).comments();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -262,6 +262,15 @@ public interface ChangeApi {
|
|||||||
*/
|
*/
|
||||||
Map<String, List<CommentInfo>> comments() throws RestApiException;
|
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.
|
* Get all robot comments on a change.
|
||||||
*
|
*
|
||||||
@@ -280,6 +289,15 @@ public interface ChangeApi {
|
|||||||
*/
|
*/
|
||||||
Map<String, List<CommentInfo>> drafts() throws RestApiException;
|
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() throws RestApiException;
|
||||||
|
|
||||||
ChangeInfo check(FixInput fix) throws RestApiException;
|
ChangeInfo check(FixInput fix) throws RestApiException;
|
||||||
@@ -533,6 +551,11 @@ public interface ChangeApi {
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<CommentInfo> commentsAsList() throws RestApiException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, List<RobotCommentInfo>> robotComments() throws RestApiException {
|
public Map<String, List<RobotCommentInfo>> robotComments() throws RestApiException {
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
@@ -543,6 +566,11 @@ public interface ChangeApi {
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<CommentInfo> draftsAsList() throws RestApiException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChangeInfo check() throws RestApiException {
|
public ChangeInfo check() throws RestApiException {
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
|||||||
@@ -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
|
@Override
|
||||||
public Map<String, List<RobotCommentInfo>> robotComments() throws RestApiException {
|
public Map<String, List<RobotCommentInfo>> robotComments() throws RestApiException {
|
||||||
try {
|
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
|
@Override
|
||||||
public ChangeInfo check() throws RestApiException {
|
public ChangeInfo check() throws RestApiException {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import com.google.gerrit.extensions.restapi.RestReadView;
|
|||||||
import com.google.gerrit.reviewdb.client.Comment;
|
import com.google.gerrit.reviewdb.client.Comment;
|
||||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||||
import com.google.gerrit.server.CommentsUtil;
|
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.gerrit.server.query.change.ChangeData;
|
||||||
import com.google.gwtorm.server.OrmException;
|
import com.google.gwtorm.server.OrmException;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
@@ -66,11 +67,21 @@ public class ListChangeDrafts implements RestReadView<ChangeResource> {
|
|||||||
if (requireAuthentication() && !rsrc.getUser().isIdentifiedUser()) {
|
if (requireAuthentication() && !rsrc.getUser().isIdentifiedUser()) {
|
||||||
throw new AuthException("Authentication required");
|
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
|
return commentJson
|
||||||
.get()
|
.get()
|
||||||
.setFillAccounts(includeAuthorInfo())
|
.setFillAccounts(includeAuthorInfo())
|
||||||
.setFillPatchSet(true)
|
.setFillPatchSet(true)
|
||||||
.newCommentFormatter()
|
.newCommentFormatter();
|
||||||
.format(listComments(rsrc));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user