Search in sources :

Example 56 with BadRequestException

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

the class SetLabelIT method cannotSetEmptyName.

@Test
public void cannotSetEmptyName() throws Exception {
    LabelDefinitionInput input = new LabelDefinitionInput();
    input.name = "";
    BadRequestException thrown = assertThrows(BadRequestException.class, () -> gApi.projects().name(allProjects.get()).label(LabelId.CODE_REVIEW).update(input));
    assertThat(thrown).hasMessageThat().contains("name cannot be empty");
}
Also used : BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 57 with BadRequestException

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

the class PutOptions method apply.

@Override
public Response<GroupOptionsInfo> apply(GroupResource resource, GroupOptionsInfo input) throws NotInternalGroupException, AuthException, BadRequestException, ResourceNotFoundException, IOException, ConfigInvalidException {
    GroupDescription.Internal internalGroup = resource.asInternalGroup().orElseThrow(NotInternalGroupException::new);
    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;
    }
    if (internalGroup.isVisibleToAll() != input.visibleToAll) {
        AccountGroup.UUID groupUuid = internalGroup.getGroupUUID();
        GroupDelta groupDelta = GroupDelta.builder().setVisibleToAll(input.visibleToAll).build();
        try {
            groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta);
        } catch (NoSuchGroupException e) {
            throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
        }
    }
    GroupOptionsInfo options = new GroupOptionsInfo();
    if (input.visibleToAll) {
        options.visibleToAll = true;
    }
    return Response.ok(options);
}
Also used : GroupDescription(com.google.gerrit.entities.GroupDescription) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupDelta(com.google.gerrit.server.group.db.GroupDelta) 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) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException)

Example 58 with BadRequestException

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

the class QueryGroups method apply.

@Override
public Response<List<GroupInfo>> apply(TopLevelResource resource) throws BadRequestException, MethodNotAllowedException, PermissionBackendException {
    if (Strings.isNullOrEmpty(query)) {
        throw new BadRequestException("missing query field");
    }
    GroupQueryProcessor queryProcessor = queryProcessorProvider.get();
    if (queryProcessor.isDisabled()) {
        throw new MethodNotAllowedException("query disabled");
    }
    if (start != 0) {
        queryProcessor.setStart(start);
    }
    if (limit != 0) {
        queryProcessor.setUserProvidedLimit(limit);
    }
    try {
        QueryResult<InternalGroup> result = queryProcessor.query(queryBuilder.parse(query));
        List<InternalGroup> groups = result.entities();
        ArrayList<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groups.size());
        json.addOptions(options);
        for (InternalGroup group : groups) {
            groupInfos.add(json.format(new InternalGroupDescription(group)));
        }
        if (!groupInfos.isEmpty() && result.more()) {
            groupInfos.get(groupInfos.size() - 1)._moreGroups = true;
        }
        return Response.ok(groupInfos);
    } catch (QueryParseException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) GroupQueryProcessor(com.google.gerrit.server.query.group.GroupQueryProcessor) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) InternalGroup(com.google.gerrit.entities.InternalGroup) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 59 with BadRequestException

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

the class PutName method apply.

@Override
public Response<String> apply(GroupResource rsrc, NameInput input) throws NotInternalGroupException, AuthException, BadRequestException, ResourceConflictException, ResourceNotFoundException, IOException, ConfigInvalidException {
    GroupDescription.Internal internalGroup = rsrc.asInternalGroup().orElseThrow(NotInternalGroupException::new);
    if (!rsrc.getControl().isOwner()) {
        throw new AuthException("Not group owner");
    } else if (input == null || Strings.isNullOrEmpty(input.name)) {
        throw new BadRequestException("name is required");
    }
    String newName = input.name.trim();
    if (newName.isEmpty()) {
        throw new BadRequestException("name is required");
    }
    if (internalGroup.getName().equals(newName)) {
        return Response.ok(newName);
    }
    renameGroup(internalGroup, newName);
    return Response.ok(newName);
}
Also used : GroupDescription(com.google.gerrit.entities.GroupDescription) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 60 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 BadRequestException, PermissionBackendException {
    if (conflictingSuggestParameters()) {
        throw new BadRequestException("You should only have no more than one --project and -n with --suggest");
    }
    List<GroupReference> groupRefs = groupBackend.suggest(suggest, projects.stream().findFirst().orElse(null)).stream().limit(limit <= 0 ? 10 : Math.min(limit, 10)).collect(toList());
    List<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groupRefs.size());
    for (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.entities.GroupDescription) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) GroupReference(com.google.gerrit.entities.GroupReference)

Aggregations

BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)310 Test (org.junit.Test)154 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)146 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)56 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)51 AuthException (com.google.gerrit.extensions.restapi.AuthException)46 Repository (org.eclipse.jgit.lib.Repository)30 IdString (com.google.gerrit.extensions.restapi.IdString)29 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)29 LabelDefinitionInput (com.google.gerrit.extensions.common.LabelDefinitionInput)28 ArrayList (java.util.ArrayList)28 RevCommit (org.eclipse.jgit.revwalk.RevCommit)28 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)27 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)27 IOException (java.io.IOException)25 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)24 RevWalk (org.eclipse.jgit.revwalk.RevWalk)22 ObjectId (org.eclipse.jgit.lib.ObjectId)20 Map (java.util.Map)19 Change (com.google.gerrit.entities.Change)18