Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.change; 16 : 17 : import com.google.gerrit.entities.Account; 18 : import com.google.gerrit.entities.Address; 19 : import com.google.gerrit.extensions.registration.DynamicMap; 20 : import com.google.gerrit.extensions.restapi.AuthException; 21 : import com.google.gerrit.extensions.restapi.ChildCollection; 22 : import com.google.gerrit.extensions.restapi.IdString; 23 : import com.google.gerrit.extensions.restapi.MethodNotAllowedException; 24 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 25 : import com.google.gerrit.extensions.restapi.RestView; 26 : import com.google.gerrit.extensions.restapi.TopLevelResource; 27 : import com.google.gerrit.server.approval.ApprovalsUtil; 28 : import com.google.gerrit.server.change.ReviewerResource; 29 : import com.google.gerrit.server.change.RevisionResource; 30 : import com.google.gerrit.server.restapi.account.AccountsCollection; 31 : import com.google.inject.Inject; 32 : import com.google.inject.Singleton; 33 : import java.io.IOException; 34 : import java.util.Collection; 35 : import org.eclipse.jgit.errors.ConfigInvalidException; 36 : 37 : @Singleton 38 : public class RevisionReviewers implements ChildCollection<RevisionResource, ReviewerResource> { 39 : private final DynamicMap<RestView<ReviewerResource>> views; 40 : private final ApprovalsUtil approvalsUtil; 41 : private final AccountsCollection accounts; 42 : private final ReviewerResource.Factory resourceFactory; 43 : private final ListRevisionReviewers list; 44 : 45 : @Inject 46 : RevisionReviewers( 47 : ApprovalsUtil approvalsUtil, 48 : AccountsCollection accounts, 49 : ReviewerResource.Factory resourceFactory, 50 : DynamicMap<RestView<ReviewerResource>> views, 51 145 : ListRevisionReviewers list) { 52 145 : this.approvalsUtil = approvalsUtil; 53 145 : this.accounts = accounts; 54 145 : this.resourceFactory = resourceFactory; 55 145 : this.views = views; 56 145 : this.list = list; 57 145 : } 58 : 59 : @Override 60 : public DynamicMap<RestView<ReviewerResource>> views() { 61 2 : return views; 62 : } 63 : 64 : @Override 65 : public RestView<RevisionResource> list() { 66 1 : return list; 67 : } 68 : 69 : @Override 70 : public ReviewerResource parse(RevisionResource rsrc, IdString id) 71 : throws ResourceNotFoundException, AuthException, MethodNotAllowedException, IOException, 72 : ConfigInvalidException { 73 5 : if (!rsrc.isCurrent()) { 74 1 : throw new MethodNotAllowedException("Cannot access on non-current patch set"); 75 : } 76 5 : Address address = Address.tryParse(id.get()); 77 : 78 5 : Account.Id accountId = null; 79 : try { 80 5 : accountId = accounts.parse(TopLevelResource.INSTANCE, id).getUser().getAccountId(); 81 0 : } catch (ResourceNotFoundException e) { 82 0 : if (address == null) { 83 0 : throw e; 84 : } 85 5 : } 86 5 : Collection<Account.Id> reviewers = approvalsUtil.getReviewers(rsrc.getNotes()).all(); 87 : // See if the id exists as a reviewer for this change 88 5 : if (reviewers.contains(accountId)) { 89 5 : return resourceFactory.create(rsrc, accountId); 90 : } 91 : 92 : // See if the address exists as a reviewer on the change 93 1 : if (address != null && rsrc.getNotes().getReviewersByEmail().all().contains(address)) { 94 0 : return new ReviewerResource(rsrc, address); 95 : } 96 : 97 1 : throw new ResourceNotFoundException(id); 98 : } 99 : }