Search in sources :

Example 1 with ReviewerResource

use of com.google.gerrit.server.change.ReviewerResource in project gerrit by GerritCodeReview.

the class RevisionReviewers method parse.

@Override
public ReviewerResource parse(RevisionResource rsrc, IdString id) throws ResourceNotFoundException, AuthException, MethodNotAllowedException, IOException, ConfigInvalidException {
    if (!rsrc.isCurrent()) {
        throw new MethodNotAllowedException("Cannot access on non-current patch set");
    }
    Address address = Address.tryParse(id.get());
    Account.Id accountId = null;
    try {
        accountId = accounts.parse(TopLevelResource.INSTANCE, id).getUser().getAccountId();
    } catch (ResourceNotFoundException e) {
        if (address == null) {
            throw e;
        }
    }
    Collection<Account.Id> reviewers = approvalsUtil.getReviewers(rsrc.getNotes()).all();
    // See if the id exists as a reviewer for this change
    if (reviewers.contains(accountId)) {
        return resourceFactory.create(rsrc, accountId);
    }
    // See if the address exists as a reviewer on the change
    if (address != null && rsrc.getNotes().getReviewersByEmail().all().contains(address)) {
        return new ReviewerResource(rsrc, address);
    }
    throw new ResourceNotFoundException(id);
}
Also used : Account(com.google.gerrit.entities.Account) ReviewerResource(com.google.gerrit.server.change.ReviewerResource) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) Address(com.google.gerrit.entities.Address) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 2 with ReviewerResource

use of com.google.gerrit.server.change.ReviewerResource in project gerrit by GerritCodeReview.

the class DeleteVote method apply.

@Override
public Response<Object> apply(VoteResource rsrc, DeleteVoteInput input) throws RestApiException, UpdateException, IOException, ConfigInvalidException {
    if (input == null) {
        input = new DeleteVoteInput();
    }
    if (input.label != null && !rsrc.getLabel().equals(input.label)) {
        throw new BadRequestException("label must match URL");
    }
    if (input.notify == null) {
        input.notify = NotifyHandling.ALL;
    }
    ReviewerResource r = rsrc.getReviewer();
    Change change = r.getChange();
    if (r.getRevisionResource() != null && !r.getRevisionResource().isCurrent()) {
        throw new MethodNotAllowedException("Cannot delete vote on non-current patch set");
    }
    try (BatchUpdate bu = updateFactory.create(change.getProject(), r.getChangeResource().getUser(), TimeUtil.now())) {
        bu.setNotify(notifyResolver.resolve(firstNonNull(input.notify, NotifyHandling.ALL), input.notifyDetails));
        bu.addOp(change.getId(), new Op(projectCache.get(r.getChange().getProject()).orElseThrow(illegalState(r.getChange().getProject())), r.getReviewerUser().state(), rsrc.getLabel(), input));
        if (!input.ignoreAutomaticAttentionSetRules && !r.getReviewerUser().getAccountId().equals(currentUserProvider.get().getAccountId())) {
            bu.addOp(change.getId(), attentionSetOpFactory.create(r.getReviewerUser().getAccountId(), /* reason= */
            "Their vote was deleted", /* notify= */
            false));
        }
        if (input.ignoreAutomaticAttentionSetRules) {
            bu.addOp(change.getId(), new AttentionSetUnchangedOp());
        }
        bu.execute();
    }
    return Response.none();
}
Also used : DeleteVoteInput(com.google.gerrit.extensions.api.changes.DeleteVoteInput) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) AddToAttentionSetOp(com.google.gerrit.server.change.AddToAttentionSetOp) AttentionSetUnchangedOp(com.google.gerrit.server.change.AttentionSetUnchangedOp) ReviewerResource(com.google.gerrit.server.change.ReviewerResource) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AttentionSetUnchangedOp(com.google.gerrit.server.change.AttentionSetUnchangedOp) Change(com.google.gerrit.entities.Change) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 3 with ReviewerResource

use of com.google.gerrit.server.change.ReviewerResource in project gerrit by GerritCodeReview.

the class SetReviewersCommand method modifyOne.

private boolean modifyOne(ChangeResource changeRsrc) throws Exception {
    boolean ok = true;
    // 
    for (Account.Id reviewer : toRemove) {
        ReviewerResource rsrc = reviewerFactory.create(changeRsrc, reviewer);
        String error = null;
        try {
            deleteReviewer.apply(rsrc, new DeleteReviewerInput());
        } catch (ResourceNotFoundException e) {
            error = String.format("could not remove %s: not found", reviewer);
        } catch (Exception e) {
            error = String.format("could not remove %s: %s", reviewer, e.getMessage());
        }
        if (error != null) {
            ok = false;
            writeError("error", error);
        }
    }
    // 
    for (String reviewer : toAdd) {
        ReviewerInput input = new ReviewerInput();
        input.reviewer = reviewer;
        input.confirmed = true;
        String error;
        try {
            error = postReviewers.apply(changeRsrc, input).value().error;
        } catch (Exception e) {
            error = String.format("could not add %s: %s", reviewer, e.getMessage());
        }
        if (error != null) {
            ok = false;
            writeError("error", error);
        }
    }
    return ok;
}
Also used : Account(com.google.gerrit.entities.Account) ReviewerResource(com.google.gerrit.server.change.ReviewerResource) DeleteReviewerInput(com.google.gerrit.extensions.api.changes.DeleteReviewerInput) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) StorageException(com.google.gerrit.exceptions.StorageException) ReviewerInput(com.google.gerrit.extensions.api.changes.ReviewerInput) DeleteReviewerInput(com.google.gerrit.extensions.api.changes.DeleteReviewerInput)

Aggregations

ReviewerResource (com.google.gerrit.server.change.ReviewerResource)3 Account (com.google.gerrit.entities.Account)2 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 Address (com.google.gerrit.entities.Address)1 Change (com.google.gerrit.entities.Change)1 StorageException (com.google.gerrit.exceptions.StorageException)1 DeleteReviewerInput (com.google.gerrit.extensions.api.changes.DeleteReviewerInput)1 DeleteVoteInput (com.google.gerrit.extensions.api.changes.DeleteVoteInput)1 ReviewerInput (com.google.gerrit.extensions.api.changes.ReviewerInput)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 AddToAttentionSetOp (com.google.gerrit.server.change.AddToAttentionSetOp)1 AttentionSetUnchangedOp (com.google.gerrit.server.change.AttentionSetUnchangedOp)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)1 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)1