Merge "Include label votes in ReviewerResource views"

This commit is contained in:
Edwin Kempin 2013-02-20 08:37:40 +00:00 committed by Gerrit Code Review
commit 32238bb351
7 changed files with 133 additions and 31 deletions

View File

@ -77,7 +77,7 @@ class DeleteReviewer implements RestModifyView<ReviewerResource, Input> {
new Predicate<PatchSetApproval>() {
@Override
public boolean apply(PatchSetApproval input) {
return input.getAccountId().equals(rsrc.getAccount().getId());
return input.getAccountId().equals(rsrc.getUser().getAccountId());
}
});
}

View File

@ -27,7 +27,7 @@ public class GetReviewer implements RestReadView<ReviewerResource> {
}
@Override
public Object apply(ReviewerResource reviewerResource) throws OrmException {
return json.format(reviewerResource);
public Object apply(ReviewerResource rsrc) throws OrmException {
return json.format(rsrc);
}
}

View File

@ -32,20 +32,23 @@ class ListReviewers implements RestReadView<ChangeResource> {
private final AccountCache accountCache;
private final Provider<ReviewDb> dbProvider;
private final ReviewerJson json;
private final ReviewerResource.Factory resourceFactory;
@Inject
ListReviewers(AccountCache accountCache,
Provider<ReviewDb> dbProvider,
ReviewerResource.Factory resourceFactory,
ReviewerJson json) {
this.accountCache = accountCache;
this.dbProvider = dbProvider;
this.resourceFactory = resourceFactory;
this.json = json;
}
@Override
public Object apply(ChangeResource rsrc) throws BadRequestException,
OrmException {
Map<Account.Id, Object> reviewers = Maps.newLinkedHashMap();
Map<Account.Id, ReviewerResource> reviewers = Maps.newLinkedHashMap();
ReviewDb db = dbProvider.get();
Change.Id changeId = rsrc.getChange().getId();
for (PatchSetApproval patchSetApproval
@ -53,10 +56,9 @@ class ListReviewers implements RestReadView<ChangeResource> {
Account.Id accountId = patchSetApproval.getAccountId();
if (!reviewers.containsKey(accountId)) {
Account account = accountCache.get(accountId).getAccount();
reviewers.put(accountId,
json.format(new ReviewerResource(rsrc, account)));
reviewers.put(accountId, resourceFactory.create(rsrc, account));
}
}
return reviewers.values();
return json.format(reviewers.values());
}
}

View File

@ -71,6 +71,7 @@ public class Module extends RestApiModule {
install(new FactoryModule() {
@Override
protected void configure() {
factory(ReviewerResource.Factory.class);
factory(AccountInfo.Loader.Factory.class);
factory(EmailReviewComments.Factory.class);
}

View File

@ -14,25 +14,104 @@
package com.google.gerrit.server.change;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.workflow.CategoryFunction;
import com.google.gerrit.server.workflow.FunctionState;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class ReviewerJson {
ReviewerJson() {
private final Provider<ReviewDb> db;
private final ApprovalTypes approvalTypes;
private final FunctionState.Factory functionState;
private final AccountInfo.Loader.Factory accountLoaderFactory;
@Inject
ReviewerJson(Provider<ReviewDb> db,
ApprovalTypes approvalTypes,
FunctionState.Factory functionState,
AccountInfo.Loader.Factory accountLoaderFactory) {
this.db = db;
this.approvalTypes = approvalTypes;
this.functionState = functionState;
this.accountLoaderFactory = accountLoaderFactory;
}
public ReviewerInfo format(ReviewerResource reviewerResource) {
ReviewerInfo reviewerInfo = new ReviewerInfo();
Account account = reviewerResource.getAccount();
reviewerInfo.id = account.getId().toString();
reviewerInfo.email = account.getPreferredEmail();
reviewerInfo.name = account.getFullName();
return reviewerInfo;
public List<ReviewerInfo> format(Collection<ReviewerResource> rsrcs) throws OrmException {
List<ReviewerInfo> infos = Lists.newArrayListWithCapacity(rsrcs.size());
AccountInfo.Loader loader = accountLoaderFactory.create(true);
for (ReviewerResource rsrc : rsrcs) {
ReviewerInfo info = formatOne(rsrc);
loader.put(info);
infos.add(info);
}
loader.fill();
return infos;
}
public static class ReviewerInfo {
public List<ReviewerInfo> format(ReviewerResource rsrc) throws OrmException {
return format(ImmutableList.<ReviewerResource> of(rsrc));
}
private ReviewerInfo formatOne(ReviewerResource rsrc) throws OrmException {
Account.Id id = rsrc.getUser().getAccountId();
ReviewerInfo out = new ReviewerInfo(id);
Change change = rsrc.getChange();
PatchSet.Id psId = change.currentPatchSetId();
List<PatchSetApproval> approvals = db.get().patchSetApprovals()
.byPatchSetUser(psId, id).toList();
ChangeControl control = rsrc.getControl().forUser(rsrc.getUser());
FunctionState fs = functionState.create(control, psId, approvals);
for (ApprovalType at : approvalTypes.getApprovalTypes()) {
CategoryFunction.forCategory(at.getCategory()).run(at, fs);
}
out.approvals = Maps.newHashMapWithExpectedSize(approvals.size());
for (PatchSetApproval ca : approvals) {
for (PermissionRange pr : control.getLabelRanges()) {
if (pr.getMin() != 0 || pr.getMax() != 0) {
// TODO: Support arbitrary labels.
ApprovalType at = approvalTypes.byId(ca.getCategoryId());
if (at != null) {
out.approvals.put(at.getCategory().getLabelName(),
ApprovalCategoryValue.formatValue(ca.getValue()));
}
}
}
}
if (out.approvals.isEmpty()) {
out.approvals = null;
}
return out;
}
public static class ReviewerInfo extends AccountInfo {
final String kind = "gerritcodereview#reviewer";
String id;
String email;
String name;
Map<String, String> approvals;
protected ReviewerInfo(Account.Id id) {
super(id);
}
}
}

View File

@ -16,20 +16,37 @@ package com.google.gerrit.server.change;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.IdentifiedUser;
import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
public class ReviewerResource extends ChangeResource {
public static final TypeLiteral<RestView<ReviewerResource>> REVIEWER_KIND =
new TypeLiteral<RestView<ReviewerResource>>() {};
private final Account account;
public ReviewerResource(ChangeResource changeResource, Account account) {
super(changeResource);
this.account = account;
static interface Factory {
ReviewerResource create(ChangeResource rsrc, IdentifiedUser user);
ReviewerResource create(ChangeResource rsrc, Account account);
}
public Account getAccount() {
return account;
private final IdentifiedUser user;
@AssistedInject
ReviewerResource(@Assisted ChangeResource rsrc,
@Assisted IdentifiedUser user) {
super(rsrc);
this.user = user;
}
@AssistedInject
ReviewerResource(IdentifiedUser.GenericFactory userFactory,
@Assisted ChangeResource rsrc,
@Assisted Account account) {
this(rsrc, userFactory.create(account.getId()));
}
public IdentifiedUser getUser() {
return user;
}
}

View File

@ -38,15 +38,18 @@ public class Reviewers implements
ChildCollection<ChangeResource, ReviewerResource> {
private final DynamicMap<RestView<ReviewerResource>> views;
private final Provider<ReviewDb> dbProvider;
private final ReviewerResource.Factory resourceFactory;
private final AccountCache accountCache;
private final Provider<ListReviewers> list;
@Inject
Reviewers(Provider<ReviewDb> dbProvider,
DynamicMap<RestView<ReviewerResource>> views,
AccountCache accountCache,
Provider<ListReviewers> list) {
ReviewerResource.Factory resourceFactory,
DynamicMap<RestView<ReviewerResource>> views,
AccountCache accountCache,
Provider<ListReviewers> list) {
this.dbProvider = dbProvider;
this.resourceFactory = resourceFactory;
this.views = views;
this.accountCache = accountCache;
this.list = list;
@ -69,7 +72,7 @@ public class Reviewers implements
if (id.equals("self")) {
CurrentUser user = rsrc.getControl().getCurrentUser();
if (user instanceof IdentifiedUser) {
accountId = ((IdentifiedUser)user).getAccountId();
accountId = ((IdentifiedUser) user).getAccountId();
} else if (user instanceof AnonymousUser) {
throw new AuthException("Authentication required");
} else {
@ -84,7 +87,7 @@ public class Reviewers implements
// See if the id exists as a reviewer for this change
if (fetchAccountIds(rsrc).contains(accountId)) {
Account account = accountCache.get(accountId).getAccount();
return new ReviewerResource(rsrc, account);
return resourceFactory.create(rsrc, account);
}
throw new ResourceNotFoundException(id);
}