Search in sources :

Example 51 with UnprocessableEntityException

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

the class RevisionIT method cherryPickToNonVisibleChangeFails.

@Test
public void cherryPickToNonVisibleChangeFails() throws Exception {
    createBranch(BranchNameKey.create(project, "foo"));
    PushOneCommit.Result dstChange = createChange(testRepo, "foo", SUBJECT, "b.txt", "b", "t");
    dstChange.assertOkStatus();
    gApi.changes().id(dstChange.getChangeId()).setPrivate(true, null);
    PushOneCommit.Result srcChange = createChange();
    CherryPickInput input = new CherryPickInput();
    input.destination = "foo";
    input.base = dstChange.getCommit().name();
    input.message = srcChange.getCommit().getFullMessage();
    requestScopeOperations.setApiUser(user.id());
    UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.changes().id(srcChange.getChangeId()).current().cherryPick(input).get());
    assertThat(thrown).hasMessageThat().contains(String.format("Commit %s does not exist on branch refs/heads/foo", input.base));
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) CherryPickInput(com.google.gerrit.extensions.api.changes.CherryPickInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 52 with UnprocessableEntityException

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

the class RevisionIT method cherryPickToNonExistingBaseCommit.

@Test
public void cherryPickToNonExistingBaseCommit() throws Exception {
    createBranch(BranchNameKey.create(project, "foo"));
    PushOneCommit.Result result = createChange();
    CherryPickInput input = new CherryPickInput();
    input.message = "foo bar";
    input.destination = "foo";
    input.base = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
    UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.changes().id(result.getChangeId()).current().cherryPick(input));
    assertThat(thrown).hasMessageThat().isEqualTo(String.format("Base %s doesn't exist", input.base));
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) CherryPickInput(com.google.gerrit.extensions.api.changes.CherryPickInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 53 with UnprocessableEntityException

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

the class CreateAccount method apply.

public Response<AccountInfo> apply(IdString id, AccountInput input) throws BadRequestException, ResourceConflictException, UnprocessableEntityException, IOException, ConfigInvalidException, PermissionBackendException {
    String username = applyCaseOfUsername(id.get());
    if (input.username != null && !username.equals(applyCaseOfUsername(input.username))) {
        throw new BadRequestException("username must match URL");
    }
    if (!ExternalId.isValidUsername(username)) {
        throw new BadRequestException("Invalid username '" + username + "'");
    }
    if (input.name == null) {
        input.name = input.username;
    }
    Set<AccountGroup.UUID> groups = parseGroups(input.groups);
    Account.Id accountId = Account.id(seq.nextAccountId());
    List<ExternalId> extIds = new ArrayList<>();
    if (input.email != null) {
        if (!validator.isValid(input.email)) {
            throw new BadRequestException("invalid email address");
        }
        extIds.add(externalIdFactory.createEmail(accountId, input.email));
    }
    extIds.add(externalIdFactory.createUsername(username, accountId, input.httpPassword));
    externalIdCreators.runEach(c -> extIds.addAll(c.create(accountId, username, input.email)));
    try {
        accountsUpdateProvider.get().insert("Create Account via API", accountId, u -> u.setFullName(input.name).setPreferredEmail(input.email).addExternalIds(extIds));
    } catch (DuplicateExternalIdKeyException e) {
        if (e.getDuplicateKey().isScheme(SCHEME_USERNAME)) {
            throw new ResourceConflictException("username '" + e.getDuplicateKey().id() + "' already exists");
        } else if (e.getDuplicateKey().isScheme(SCHEME_MAILTO)) {
            throw new UnprocessableEntityException("email '" + e.getDuplicateKey().id() + "' already exists");
        } else {
            // AccountExternalIdCreator returned an external ID that already exists
            throw e;
        }
    }
    for (AccountGroup.UUID groupUuid : groups) {
        try {
            addGroupMember(groupUuid, accountId);
        } catch (NoSuchGroupException e) {
            throw new UnprocessableEntityException(String.format("Group %s not found", groupUuid), e);
        }
    }
    if (input.sshKey != null) {
        try {
            authorizedKeys.addKey(accountId, input.sshKey);
            sshKeyCache.evict(username);
        } catch (InvalidSshKeyException e) {
            throw new BadRequestException(e.getMessage());
        }
    }
    AccountLoader loader = infoLoader.create(true);
    AccountInfo info = loader.get(accountId);
    loader.fill();
    return Response.created(info);
}
Also used : Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) DuplicateExternalIdKeyException(com.google.gerrit.server.account.externalids.DuplicateExternalIdKeyException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) IdString(com.google.gerrit.extensions.restapi.IdString) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) InvalidSshKeyException(com.google.gerrit.exceptions.InvalidSshKeyException) AccountGroup(com.google.gerrit.entities.AccountGroup) AccountLoader(com.google.gerrit.server.account.AccountLoader) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 54 with UnprocessableEntityException

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

the class DeleteExternalIds method apply.

@Override
public Response<?> apply(AccountResource resource, List<String> extIds) throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
    if (!self.get().hasSameAccountId(resource.getUser())) {
        permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
    }
    if (extIds == null || extIds.isEmpty()) {
        throw new BadRequestException("external IDs are required");
    }
    Map<ExternalId.Key, ExternalId> externalIdMap = externalIds.byAccount(resource.getUser().getAccountId()).stream().collect(toMap(ExternalId::key, Function.identity()));
    List<ExternalId> toDelete = new ArrayList<>();
    Optional<ExternalId.Key> last = resource.getUser().getLastLoginExternalIdKey();
    for (String externalIdStr : extIds) {
        ExternalId id = externalIdMap.get(externalIdKeyFactory.parse(externalIdStr));
        if (id == null) {
            throw new UnprocessableEntityException(String.format("External id %s does not exist", externalIdStr));
        }
        if (!last.isPresent() || !last.get().equals(id.key())) {
            if (id.isScheme(SCHEME_USERNAME)) {
                if (self.get().hasSameAccountId(resource.getUser())) {
                    throw new AuthException("User cannot delete its own externalId in 'username:' scheme");
                }
                permissionBackend.currentUser().checkAny(ImmutableSet.of(GlobalPermission.ADMINISTRATE_SERVER, GlobalPermission.MAINTAIN_SERVER));
            }
            toDelete.add(id);
        } else {
            throw new ResourceConflictException(String.format("External id %s cannot be deleted", externalIdStr));
        }
    }
    try {
        accountManager.unlink(resource.getUser().getAccountId(), toDelete.stream().map(ExternalId::key).collect(toSet()));
    } catch (AccountException e) {
        throw new ResourceConflictException(e.getMessage());
    }
    return Response.none();
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountException(com.google.gerrit.server.account.AccountException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException)

Example 55 with UnprocessableEntityException

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

the class ImpersonationIT method submitOnBehalfOfFailsWhenUserCannotSeeDestinationRef.

@Test
public void submitOnBehalfOfFailsWhenUserCannotSeeDestinationRef() throws Exception {
    blockRead(newGroup);
    allowSubmitOnBehalfOf();
    PushOneCommit.Result r = createChange();
    String changeId = project.get() + "~master~" + r.getChangeId();
    gApi.changes().id(changeId).current().review(ReviewInput.approve());
    SubmitInput in = new SubmitInput();
    in.onBehalfOf = user.email();
    UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.changes().id(changeId).current().submit(in));
    assertThat(thrown).hasMessageThat().contains("on_behalf_of account " + user.id() + " cannot see change");
}
Also used : SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)78 AuthException (com.google.gerrit.extensions.restapi.AuthException)31 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)27 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)25 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)23 Test (org.junit.Test)23 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 Account (com.google.gerrit.entities.Account)12 Ref (org.eclipse.jgit.lib.Ref)10 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)9 CurrentUser (com.google.gerrit.server.CurrentUser)9 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)9 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)9 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)9 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)8 Map (java.util.Map)8 ObjectId (org.eclipse.jgit.lib.ObjectId)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 Nullable (com.google.gerrit.common.Nullable)7