Implement reviewer access via REST

This change enables users to request details about a reviewer given a
change and a reviewer id.  The returned result is a map including the
reviewers:
* kind (gerritcodereview#reviewer)
* id
* email
* name (if provided)

Example:
curl -s localhost:8080/
    changes/blah~master~I2933f24c1fc0d7f44a34bb1dc7a3242fdfd29028/
    reviewers/1000000

{
  "kind": "gerritcodereview#reviewer",
  "id": "1000000",
  "email": "cco3%40android.com"
}

Change-Id: I3e164527d5e4cc5707577b31a8618a7ae9f7b098
This commit is contained in:
Conley Owens
2012-11-19 14:56:58 -08:00
parent 07600e253d
commit d26313ae45
4 changed files with 95 additions and 15 deletions

View File

@@ -14,13 +14,20 @@
package com.google.gerrit.server.change;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
public class GetReviewer implements RestReadView<ReviewerResource> {
private final ReviewerJson json;
@Inject
GetReviewer(ReviewerJson json) {
this.json = json;
}
@Override
public Object apply(ReviewerResource resource)
throws BadRequestException, Exception {
throw new BadRequestException("Not yet implemented");
public Object apply(ReviewerResource reviewerResource) throws OrmException {
return json.format(reviewerResource);
}
}

View File

@@ -0,0 +1,38 @@
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.change;
import com.google.gerrit.reviewdb.client.Account;
public class ReviewerJson {
ReviewerJson() {
}
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 static class ReviewerInfo {
final String kind = "gerritcodereview#reviewer";
String id;
String email;
String name;
}
}

View File

@@ -22,14 +22,14 @@ public class ReviewerResource extends ChangeResource {
public static final TypeLiteral<RestView<ReviewerResource>> REVIEWER_KIND =
new TypeLiteral<RestView<ReviewerResource>>() {};
private final Account.Id id;
private final Account account;
public ReviewerResource(ChangeResource rsrc, Account.Id id) {
super(rsrc);
this.id = id;
public ReviewerResource(ChangeResource changeResource, Account account) {
super(changeResource);
this.account = account;
}
public Account.Id getAccountId() {
return id;
public Account getAccount() {
return account;
}
}

View File

@@ -19,15 +19,30 @@ import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.AccountCache;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.HashSet;
import java.util.Set;
public class Reviewers implements
ChildCollection<ChangeResource, ReviewerResource> {
private final DynamicMap<RestView<ReviewerResource>> views;
private final Provider<ReviewDb> dbProvider;
private final AccountCache accountCache;
@Inject
Reviewers(DynamicMap<RestView<ReviewerResource>> views) {
Reviewers(Provider<ReviewDb> dbProvider,
DynamicMap<RestView<ReviewerResource>> views,
AccountCache accountCache) {
this.dbProvider = dbProvider;
this.views = views;
this.accountCache = accountCache;
}
@Override
@@ -41,11 +56,31 @@ public class Reviewers implements
}
@Override
public ReviewerResource parse(ChangeResource change, String id)
throws ResourceNotFoundException, Exception {
if (id.matches("^[0-9]+$")) {
return new ReviewerResource(change, Account.Id.parse(id));
public ReviewerResource parse(ChangeResource changeResource, String id)
throws OrmException, ResourceNotFoundException {
// Get the account id
if (!id.matches("^[0-9]+$")) {
throw new ResourceNotFoundException(id);
}
Account.Id accountId = Account.Id.parse(id);
// See if the id exists as a reviewer for this change
if (fetchAccountIds(changeResource).contains(accountId)) {
Account account = accountCache.get(accountId).getAccount();
return new ReviewerResource(changeResource, account);
}
throw new ResourceNotFoundException(id);
}
private Set<Account.Id> fetchAccountIds(ChangeResource changeResource)
throws OrmException {
ReviewDb db = dbProvider.get();
Change.Id changeId = changeResource.getChange().getId();
Set<Account.Id> accountIds = new HashSet<Account.Id>();
for (PatchSetApproval patchSetApproval
: db.patchSetApprovals().byChange(changeId)) {
accountIds.add(patchSetApproval.getAccountId());
}
return accountIds;
}
}