Search in sources :

Example 21 with UnprocessableEntityException

use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.

the class PostReviewers method addByAccountId.

@Nullable
private Addition addByAccountId(String reviewer, ChangeResource rsrc, ReviewerState state, NotifyHandling notify, ListMultimap<RecipientType, Account.Id> accountsToNotify, boolean allowGroup, boolean allowByEmail) throws OrmException, PermissionBackendException {
    Account.Id accountId = null;
    try {
        accountId = accounts.parse(reviewer).getAccountId();
    } catch (UnprocessableEntityException | AuthException e) {
        // AuthException won't occur since the user is authenticated at this point.
        if (!allowGroup && !allowByEmail) {
            // Only return failure if we aren't going to try other interpretations.
            return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerNotFoundUser, reviewer));
        }
        return null;
    }
    ReviewerResource rrsrc = reviewerFactory.create(rsrc, accountId);
    Account member = rrsrc.getReviewerUser().getAccount();
    PermissionBackend.ForRef perm = permissionBackend.user(rrsrc.getReviewerUser()).ref(rrsrc.getChange().getDest());
    if (isValidReviewer(member, perm)) {
        return new Addition(reviewer, rsrc, ImmutableSet.of(member.getId()), null, state, notify, accountsToNotify);
    }
    if (!member.isActive()) {
        if (allowByEmail && state == CC) {
            return null;
        }
        return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerInactive, reviewer));
    }
    return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerCantSeeChange, reviewer));
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) AuthException(com.google.gerrit.extensions.restapi.AuthException) Nullable(com.google.gerrit.common.Nullable)

Example 22 with UnprocessableEntityException

use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.

the class PutAssignee method applyImpl.

@Override
protected AccountInfo applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, AssigneeInput input) throws RestApiException, UpdateException, OrmException, IOException, PermissionBackendException {
    rsrc.permissions().check(ChangePermission.EDIT_ASSIGNEE);
    input.assignee = Strings.nullToEmpty(input.assignee).trim();
    if (input.assignee.isEmpty()) {
        throw new BadRequestException("missing assignee field");
    }
    IdentifiedUser assignee = accounts.parse(input.assignee);
    if (!assignee.getAccount().isActive()) {
        throw new UnprocessableEntityException(input.assignee + " is not active");
    }
    try {
        rsrc.permissions().database(db).user(assignee).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new AuthException("read not permitted for " + input.assignee);
    }
    try (BatchUpdate bu = updateFactory.create(db.get(), rsrc.getChange().getProject(), rsrc.getControl().getUser(), TimeUtil.nowTs())) {
        SetAssigneeOp op = assigneeFactory.create(assignee);
        bu.addOp(rsrc.getId(), op);
        PostReviewers.Addition reviewersAddition = addAssigneeAsCC(rsrc, input.assignee);
        bu.addOp(rsrc.getId(), reviewersAddition.op);
        bu.execute();
        return accountLoaderFactory.create(true).fillOne(assignee.getAccountId());
    }
}
Also used : Addition(com.google.gerrit.server.change.PostReviewers.Addition) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 23 with UnprocessableEntityException

use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.

the class BanCommit method apply.

@Override
public BanResultInfo apply(ProjectResource rsrc, Input input) throws UnprocessableEntityException, AuthException, ResourceConflictException, IOException {
    BanResultInfo r = new BanResultInfo();
    if (input != null && input.commits != null && !input.commits.isEmpty()) {
        List<ObjectId> commitsToBan = new ArrayList<>(input.commits.size());
        for (String c : input.commits) {
            try {
                commitsToBan.add(ObjectId.fromString(c));
            } catch (IllegalArgumentException e) {
                throw new UnprocessableEntityException(e.getMessage());
            }
        }
        try {
            BanCommitResult result = banCommit.ban(rsrc.getControl(), commitsToBan, input.reason);
            r.newlyBanned = transformCommits(result.getNewlyBannedCommits());
            r.alreadyBanned = transformCommits(result.getAlreadyBannedCommits());
            r.ignored = transformCommits(result.getIgnoredObjectIds());
        } catch (PermissionDeniedException e) {
            throw new AuthException(e.getMessage());
        } catch (ConcurrentRefUpdateException e) {
            throw new ResourceConflictException(e.getMessage(), e);
        }
    }
    return r;
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionDeniedException(com.google.gerrit.common.errors.PermissionDeniedException) BanCommitResult(com.google.gerrit.server.git.BanCommitResult) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)

Aggregations

UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)23 AuthException (com.google.gerrit.extensions.restapi.AuthException)16 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)10 Account (com.google.gerrit.reviewdb.client.Account)7 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)5 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)5 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)5 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)4 CurrentUser (com.google.gerrit.server.CurrentUser)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AccessSection (com.google.gerrit.common.data.AccessSection)3 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)3 RestModifyView (com.google.gerrit.extensions.restapi.RestModifyView)3 Ref (org.eclipse.jgit.lib.Ref)3 Nullable (com.google.gerrit.common.Nullable)2 Capable (com.google.gerrit.common.data.Capable)2 GroupDescription (com.google.gerrit.common.data.GroupDescription)2 Permission (com.google.gerrit.common.data.Permission)2