Search in sources :

Example 41 with BadRequestException

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

the class PutOptions method apply.

@Override
public GroupOptionsInfo apply(GroupResource resource, GroupOptionsInfo input) throws MethodNotAllowedException, AuthException, BadRequestException, ResourceNotFoundException, OrmException, IOException {
    if (resource.toAccountGroup() == null) {
        throw new MethodNotAllowedException();
    } else if (!resource.getControl().isOwner()) {
        throw new AuthException("Not group owner");
    }
    if (input == null) {
        throw new BadRequestException("options are required");
    }
    if (input.visibleToAll == null) {
        input.visibleToAll = false;
    }
    AccountGroup group = db.get().accountGroups().get(resource.toAccountGroup().getId());
    if (group == null) {
        throw new ResourceNotFoundException();
    }
    group.setVisibleToAll(input.visibleToAll);
    db.get().accountGroups().update(Collections.singleton(group));
    groupCache.evict(group);
    GroupOptionsInfo options = new GroupOptionsInfo();
    if (group.isVisibleToAll()) {
        options.visibleToAll = true;
    }
    return options;
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) GroupOptionsInfo(com.google.gerrit.extensions.common.GroupOptionsInfo)

Example 42 with BadRequestException

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

the class ListGroups method suggestGroups.

private List<GroupInfo> suggestGroups() throws OrmException, BadRequestException {
    if (conflictingSuggestParameters()) {
        throw new BadRequestException("You should only have no more than one --project and -n with --suggest");
    }
    List<GroupReference> groupRefs = Lists.newArrayList(Iterables.limit(groupBackend.suggest(suggest, Iterables.getFirst(projects, null)), limit <= 0 ? 10 : Math.min(limit, 10)));
    List<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groupRefs.size());
    for (final GroupReference ref : groupRefs) {
        GroupDescription.Basic desc = groupBackend.get(ref.getUUID());
        if (desc != null) {
            groupInfos.add(json.addOptions(options).format(desc));
        }
    }
    return groupInfos;
}
Also used : GroupDescription(com.google.gerrit.common.data.GroupDescription) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) GroupReference(com.google.gerrit.common.data.GroupReference)

Example 43 with BadRequestException

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

the class SetHashtagsOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws AuthException, BadRequestException, MethodNotAllowedException, OrmException, IOException {
    if (!notesMigration.readChanges()) {
        throw new MethodNotAllowedException("Cannot add hashtags; NoteDb is disabled");
    }
    if (input == null || (input.add == null && input.remove == null)) {
        updatedHashtags = ImmutableSortedSet.of();
        return false;
    }
    change = ctx.getChange();
    ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId());
    ChangeNotes notes = update.getNotes().load();
    Set<String> existingHashtags = notes.getHashtags();
    Set<String> updated = new HashSet<>();
    toAdd = new HashSet<>(extractTags(input.add));
    toRemove = new HashSet<>(extractTags(input.remove));
    try {
        for (HashtagValidationListener validator : validationListeners) {
            validator.validateHashtags(update.getChange(), toAdd, toRemove);
        }
    } catch (ValidationException e) {
        throw new BadRequestException(e.getMessage());
    }
    updated.addAll(existingHashtags);
    toAdd.removeAll(existingHashtags);
    toRemove.retainAll(existingHashtags);
    if (updated()) {
        updated.addAll(toAdd);
        updated.removeAll(toRemove);
        update.setHashtags(updated);
        addMessage(ctx, update);
    }
    updatedHashtags = ImmutableSortedSet.copyOf(updated);
    return true;
}
Also used : HashtagValidationListener(com.google.gerrit.server.validators.HashtagValidationListener) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) ValidationException(com.google.gerrit.server.validators.ValidationException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate) HashSet(java.util.HashSet)

Example 44 with BadRequestException

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

the class PreviewSubmit method apply.

@Override
public BinaryResult apply(RevisionResource rsrc) throws UpdateException, RestApiException {
    // to get access to a BatchUpdate.Factory.
    return retryHelper.execute((updateFactory) -> {
        if (Strings.isNullOrEmpty(format)) {
            throw new BadRequestException("format is not specified");
        }
        ArchiveFormat f = allowedFormats.extensions.get("." + format);
        if (f == null && format.equals("tgz")) {
            // Always allow tgz, even when the allowedFormats doesn't contain it.
            // Then we allow at least one format even if the list of allowed
            // formats is empty.
            f = ArchiveFormat.TGZ;
        }
        if (f == null) {
            throw new BadRequestException("unknown archive format");
        }
        Change change = rsrc.getChange();
        if (!change.getStatus().isOpen()) {
            throw new PreconditionFailedException("change is " + ChangeUtil.status(change));
        }
        ChangeControl control = rsrc.getControl();
        if (!control.getUser().isIdentifiedUser()) {
            throw new MethodNotAllowedException("Anonymous users cannot submit");
        }
        return getBundles(updateFactory, rsrc, f);
    });
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) ChangeControl(com.google.gerrit.server.project.ChangeControl) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PreconditionFailedException(com.google.gerrit.extensions.restapi.PreconditionFailedException) Change(com.google.gerrit.reviewdb.client.Change)

Example 45 with BadRequestException

use of com.google.gerrit.extensions.restapi.BadRequestException 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)

Aggregations

BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)65 AuthException (com.google.gerrit.extensions.restapi.AuthException)22 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)21 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)15 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)13 Repository (org.eclipse.jgit.lib.Repository)13 Project (com.google.gerrit.reviewdb.client.Project)12 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)11 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)10 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 Account (com.google.gerrit.reviewdb.client.Account)9 Change (com.google.gerrit.reviewdb.client.Change)8 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)8 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)8 Map (java.util.Map)8 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)7 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)7 RevWalk (org.eclipse.jgit.revwalk.RevWalk)7 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)6