RevisionApi: Offer get{Comments,Drafts}AsList() methods

Current revision api only exposes getter methods for comments and drafts
that return Map<String, List<CommentInfo>> with unset path attribute in
CommentInfo container.

While this is fine for the need of change screen, that leads to
obfuscated client code when only list of comments is needed:

  1. call gApi.changes().id(changeId).revision(rev).comments()
  2. iterate over the result map
  2.1. retrieve the key
  2.2. set path attribute in the CommentInfo container
  3. create, fill and return result list of CommentInfo instances with
     re-filled path attribute.

This change offers missing methods that return list of comment infos
with filled path attributes.

Change-Id: Ie154c772f27dc601049cdddffb0c35a20f3f03e9
This commit is contained in:
David Ostrovsky
2015-05-05 21:05:02 +02:00
parent 0f243ef2a7
commit 20a5470650
5 changed files with 74 additions and 0 deletions

View File

@@ -505,6 +505,19 @@ public class RevisionIT extends AbstractDaemonTest {
CommentInfo comment = Iterables.getOnlyElement(out.get(FILE_NAME));
assertThat(comment.message).isEqualTo(in.message);
assertThat(comment.author.email).isEqualTo(admin.email);
assertThat(comment.path).isNull();
List<CommentInfo> list = gApi.changes()
.id(r.getChangeId())
.revision(r.getCommit().name())
.commentsAsList();
assertThat(list).hasSize(1);
CommentInfo comment2 = list.get(0);
assertThat(comment2.path).isEqualTo(FILE_NAME);
assertThat(comment2.line).isEqualTo(comment.line);
assertThat(comment2.message).isEqualTo(comment.message);
assertThat(comment2.author.email).isEqualTo(comment.author.email);
assertThat(gApi.changes()
.id(r.getChangeId())

View File

@@ -50,6 +50,9 @@ public interface RevisionApi {
Map<String, List<CommentInfo>> comments() throws RestApiException;
Map<String, List<CommentInfo>> drafts() throws RestApiException;
List<CommentInfo> commentsAsList() throws RestApiException;
List<CommentInfo> draftsAsList() throws RestApiException;
DraftApi createDraft(DraftInput in) throws RestApiException;
DraftApi draft(String id) throws RestApiException;
@@ -147,6 +150,16 @@ public interface RevisionApi {
throw new NotImplementedException();
}
@Override
public List<CommentInfo> commentsAsList() throws RestApiException {
throw new NotImplementedException();
}
@Override
public List<CommentInfo> draftsAsList() throws RestApiException {
throw new NotImplementedException();
}
@Override
public Map<String, List<CommentInfo>> drafts() throws RestApiException {
throw new NotImplementedException();

View File

@@ -297,6 +297,15 @@ class RevisionApiImpl implements RevisionApi {
}
}
@Override
public List<CommentInfo> commentsAsList() throws RestApiException {
try {
return listComments.getComments(revision);
} catch (OrmException e) {
throw new RestApiException("Cannot retrieve comments", e);
}
}
@Override
public Map<String, List<CommentInfo>> drafts() throws RestApiException {
try {
@@ -306,6 +315,15 @@ class RevisionApiImpl implements RevisionApi {
}
}
@Override
public List<CommentInfo> draftsAsList() throws RestApiException {
try {
return listDrafts.getComments(revision);
} catch (OrmException e) {
throw new RestApiException("Cannot retrieve drafts", e);
}
}
@Override
public DraftApi draft(String id) throws RestApiException {
try {

View File

@@ -16,7 +16,9 @@ package com.google.gerrit.server.change;
import static com.google.gerrit.server.PatchLineCommentsUtil.COMMENT_INFO_ORDER;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.gerrit.extensions.client.Comment.Range;
import com.google.gerrit.extensions.client.Side;
import com.google.gerrit.extensions.common.CommentInfo;
@@ -96,6 +98,27 @@ class CommentJson {
return out;
}
List<CommentInfo> formatAsList(Iterable<PatchLineComment> l)
throws OrmException {
final AccountLoader accountLoader = fillAccounts
? accountLoaderFactory.create(true)
: null;
List<CommentInfo> out = FluentIterable
.from(l)
.transform(new Function<PatchLineComment, CommentInfo>() {
@Override
public CommentInfo apply(PatchLineComment c) {
return toCommentInfo(c, accountLoader);
}
}).toSortedList(COMMENT_INFO_ORDER);
if (accountLoader != null) {
accountLoader.fill();
}
return out;
}
private CommentInfo toCommentInfo(PatchLineComment c, AccountLoader loader) {
CommentInfo r = new CommentInfo();
if (fillPatchSet) {

View File

@@ -59,4 +59,11 @@ public class ListRevisionDrafts implements RestReadView<RevisionResource> {
.setFillAccounts(includeAuthorInfo())
.format(listComments(rsrc));
}
public List<CommentInfo> getComments(RevisionResource rsrc)
throws OrmException {
return commentJson.get()
.setFillAccounts(includeAuthorInfo())
.formatAsList(listComments(rsrc));
}
}