Search in sources :

Example 76 with BadRequestException

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

the class GetArchiveIT method formatNotSpecified.

@Test
public void formatNotSpecified() throws Exception {
    BadRequestException ex = assertThrows(BadRequestException.class, () -> gApi.changes().id(changeId).current().getArchive(null));
    assertThat(ex).hasMessageThat().isEqualTo("format is not specified");
}
Also used : BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 77 with BadRequestException

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

the class TestSubmitRule method apply.

@Override
public Response<TestSubmitRuleInfo> apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, PermissionBackendException, BadRequestException {
    if (input == null) {
        input = new TestSubmitRuleInput();
    }
    if (input.rule == null) {
        throw new BadRequestException("rule is required");
    }
    if (!rules.isProjectRulesEnabled()) {
        throw new AuthException("project rules are disabled");
    }
    input.filters = MoreObjects.firstNonNull(input.filters, filters);
    Project.NameKey name = rsrc.getProject();
    Optional<ProjectState> project = projectCache.get(name);
    if (!project.isPresent()) {
        throw new BadRequestException("project not found " + name);
    }
    ChangeData cd = changeDataFactory.create(rsrc.getNotes());
    SubmitRecord record = prologRule.evaluate(cd, PrologOptions.dryRunOptions(input.rule, input.filters == Filters.SKIP));
    AccountLoader accounts = accountInfoFactory.create(true);
    TestSubmitRuleInfo out = newSubmitRuleInfo(record, accounts);
    accounts.fill();
    return Response.ok(out);
}
Also used : Project(com.google.gerrit.entities.Project) SubmitRecord(com.google.gerrit.entities.SubmitRecord) TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) AccountLoader(com.google.gerrit.server.account.AccountLoader) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) ProjectState(com.google.gerrit.server.project.ProjectState) ChangeData(com.google.gerrit.server.query.change.ChangeData) TestSubmitRuleInfo(com.google.gerrit.extensions.common.TestSubmitRuleInfo)

Example 78 with BadRequestException

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

the class PostReview method ensureRangesDoNotOverlap.

private static void ensureRangesDoNotOverlap(String commentPath, List<FixReplacementInfo> fixReplacementInfos) throws BadRequestException {
    List<Range> sortedRanges = fixReplacementInfos.stream().map(fixReplacementInfo -> fixReplacementInfo.range).sorted().collect(toList());
    int previousEndLine = 0;
    int previousOffset = -1;
    for (Range range : sortedRanges) {
        if (range.startLine < previousEndLine || (range.startLine == previousEndLine && range.startCharacter < previousOffset)) {
            throw new BadRequestException(String.format("Replacements overlap for the robot comment on %s", commentPath));
        }
        previousEndLine = range.endLine;
        previousOffset = range.endCharacter;
    }
}
Also used : BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Range(com.google.gerrit.extensions.client.Comment.Range)

Example 79 with BadRequestException

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

the class PostReview method onBehalfOf.

private RevisionResource onBehalfOf(RevisionResource rev, LabelTypes labelTypes, ReviewInput in) throws BadRequestException, AuthException, UnprocessableEntityException, PermissionBackendException, IOException, ConfigInvalidException {
    logger.atFine().log("request is executed on behalf of %s", in.onBehalfOf);
    if (in.labels == null || in.labels.isEmpty()) {
        throw new AuthException(String.format("label required to post review on behalf of \"%s\"", in.onBehalfOf));
    }
    if (in.drafts != DraftHandling.KEEP) {
        throw new AuthException("not allowed to modify other user's drafts");
    }
    logger.atFine().log("label input: %s", in.labels);
    CurrentUser caller = rev.getUser();
    PermissionBackend.ForChange perm = rev.permissions();
    Iterator<Map.Entry<String, Short>> itr = in.labels.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, Short> ent = itr.next();
        Optional<LabelType> type = labelTypes.byLabel(ent.getKey());
        if (!type.isPresent()) {
            logger.atFine().log("label %s not found", ent.getKey());
            if (strictLabels) {
                throw new BadRequestException(String.format("label \"%s\" is not a configured label", ent.getKey()));
            }
            logger.atFine().log("ignoring input for unknown label %s", ent.getKey());
            itr.remove();
            continue;
        }
        if (caller.isInternalUser()) {
            logger.atFine().log("skipping on behalf of permission check for label %s" + " because caller is an internal user", type.get().getName());
        } else {
            try {
                perm.check(new LabelPermission.WithValue(ON_BEHALF_OF, type.get(), ent.getValue()));
            } catch (AuthException e) {
                throw new AuthException(String.format("not permitted to modify label \"%s\" on behalf of \"%s\"", type.get().getName(), in.onBehalfOf), e);
            }
        }
    }
    if (in.labels.isEmpty()) {
        logger.atFine().log("labels are empty after unknown labels have been removed");
        throw new AuthException(String.format("label required to post review on behalf of \"%s\"", in.onBehalfOf));
    }
    IdentifiedUser reviewer = accountResolver.resolve(in.onBehalfOf).asUniqueUserOnBehalfOf(caller);
    logger.atFine().log("on behalf of user was resolved to %s", reviewer.getLoggableName());
    try {
        permissionBackend.user(reviewer).change(rev.getNotes()).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new UnprocessableEntityException(String.format("on_behalf_of account %s cannot see change", reviewer.getAccountId()), e);
    }
    return new RevisionResource(changeResourceFactory.create(rev.getNotes(), reviewer), rev.getPatchSet());
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) CurrentUser(com.google.gerrit.server.CurrentUser) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) AuthException(com.google.gerrit.extensions.restapi.AuthException) RevisionResource(com.google.gerrit.server.change.RevisionResource) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map) HashMap(java.util.HashMap) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Example 80 with BadRequestException

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

the class RemoveFromAttentionSet method apply.

@Override
public Response<Object> apply(AttentionSetEntryResource attentionResource, AttentionSetInput input) throws RestApiException, PermissionBackendException, IOException, ConfigInvalidException, UpdateException {
    if (input == null) {
        throw new BadRequestException("input may not be null");
    }
    input.reason = Strings.nullToEmpty(input.reason).trim();
    if (input.reason.isEmpty()) {
        throw new BadRequestException("missing field: reason");
    }
    input.user = Strings.nullToEmpty(input.user).trim();
    if (!input.user.isEmpty()) {
        Account.Id attentionUserId = AttentionSetUtil.resolveAccount(accountResolver, attentionResource.getChangeResource().getNotes(), input.user);
        if (attentionUserId.get() != attentionResource.getAccountId().get()) {
            throw new BadRequestException("The field \"user\" must be empty, or must match the user specified in the URL.");
        }
    }
    ChangeResource changeResource = attentionResource.getChangeResource();
    try (BatchUpdate bu = updateFactory.create(changeResource.getProject(), changeResource.getUser(), TimeUtil.now())) {
        RemoveFromAttentionSetOp op = opFactory.create(attentionResource.getAccountId(), input.reason, true);
        bu.addOp(changeResource.getId(), op);
        NotifyHandling notify = input.notify == null ? NotifyHandling.OWNER : input.notify;
        NotifyResolver.Result notifyResult = notifyResolver.resolve(notify, input.notifyDetails);
        bu.setNotify(notifyResult);
        bu.execute();
    }
    return Response.none();
}
Also used : Account(com.google.gerrit.entities.Account) NotifyResolver(com.google.gerrit.server.change.NotifyResolver) ChangeResource(com.google.gerrit.server.change.ChangeResource) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RemoveFromAttentionSetOp(com.google.gerrit.server.change.RemoveFromAttentionSetOp) NotifyHandling(com.google.gerrit.extensions.api.changes.NotifyHandling) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

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