Search in sources :

Example 1 with DuplicateKeyException

use of com.google.gerrit.exceptions.DuplicateKeyException in project gerrit by GerritCodeReview.

the class PutName method renameGroup.

private void renameGroup(GroupDescription.Internal group, String newName) throws ResourceConflictException, ResourceNotFoundException, IOException, ConfigInvalidException {
    AccountGroup.UUID groupUuid = group.getGroupUUID();
    GroupDelta groupDelta = GroupDelta.builder().setName(AccountGroup.nameKey(newName)).build();
    try {
        groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta);
    } catch (NoSuchGroupException e) {
        throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
    } catch (DuplicateKeyException e) {
        throw new ResourceConflictException("group with name " + newName + " already exists", e);
    }
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupDelta(com.google.gerrit.server.group.db.GroupDelta) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException)

Example 2 with DuplicateKeyException

use of com.google.gerrit.exceptions.DuplicateKeyException in project gerrit by GerritCodeReview.

the class GroupNameNotesTest method groupCannotBeRenamedToNameOfAnotherGroup.

@Test
public void groupCannotBeRenamedToNameOfAnotherGroup() throws Exception {
    createGroup(groupUuid, groupName);
    AccountGroup.UUID anotherGroupUuid = AccountGroup.uuid("admins-ABC");
    AccountGroup.NameKey anotherGroupName = AccountGroup.nameKey("admins");
    createGroup(anotherGroupUuid, anotherGroupName);
    DuplicateKeyException thrown = assertThrows(DuplicateKeyException.class, () -> GroupNameNotes.forRename(allUsersName, repo, groupUuid, groupName, anotherGroupName));
    assertThat(thrown).hasMessageThat().contains(anotherGroupName.get());
}
Also used : AccountGroup(com.google.gerrit.entities.AccountGroup) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException) Test(org.junit.Test)

Example 3 with DuplicateKeyException

use of com.google.gerrit.exceptions.DuplicateKeyException in project gerrit by GerritCodeReview.

the class GroupNameNotesTest method newGroupMustNotReuseNameOfAnotherGroup.

@Test
public void newGroupMustNotReuseNameOfAnotherGroup() throws Exception {
    createGroup(groupUuid, groupName);
    AccountGroup.UUID anotherGroupUuid = AccountGroup.uuid("AnotherGroup");
    DuplicateKeyException thrown = assertThrows(DuplicateKeyException.class, () -> GroupNameNotes.forNewGroup(allUsersName, repo, anotherGroupUuid, groupName));
    assertThat(thrown).hasMessageThat().contains(groupName.get());
}
Also used : AccountGroup(com.google.gerrit.entities.AccountGroup) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException) Test(org.junit.Test)

Example 4 with DuplicateKeyException

use of com.google.gerrit.exceptions.DuplicateKeyException in project gerrit by GerritCodeReview.

the class JdbcAccountPatchReviewStore method markReviewed.

@Override
public void markReviewed(PatchSet.Id psId, Account.Id accountId, Collection<String> paths) {
    if (paths == null || paths.isEmpty()) {
        return;
    }
    try (TraceTimer ignored = TraceContext.newTimer("Mark files as reviewed", Metadata.builder().patchSetId(psId.get()).accountId(accountId.get()).resourceCount(paths.size()).build());
        Connection con = ds.getConnection();
        PreparedStatement stmt = con.prepareStatement("INSERT INTO account_patch_reviews " + "(account_id, change_id, patch_set_id, file_name) VALUES " + "(?, ?, ?, ?)")) {
        for (String path : paths) {
            stmt.setInt(1, accountId.get());
            stmt.setInt(2, psId.changeId().get());
            stmt.setInt(3, psId.get());
            stmt.setString(4, path);
            stmt.addBatch();
        }
        stmt.executeBatch();
    } catch (SQLException e) {
        StorageException ormException = convertError("insert", e);
        if (ormException instanceof DuplicateKeyException) {
            return;
        }
        throw ormException;
    }
}
Also used : SQLException(java.sql.SQLException) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StorageException(com.google.gerrit.exceptions.StorageException) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException)

Example 5 with DuplicateKeyException

use of com.google.gerrit.exceptions.DuplicateKeyException in project gerrit by GerritCodeReview.

the class AccountConfig method getNewAccount.

/**
 * Creates a new account.
 *
 * @return the new account
 * @throws DuplicateKeyException if the user branch already exists
 */
Account getNewAccount(Instant registeredOn) throws DuplicateKeyException {
    checkLoaded();
    if (revision != null) {
        throw new DuplicateKeyException(String.format("account %s already exists", accountId));
    }
    this.loadedAccountProperties = Optional.of(new AccountProperties(accountId, registeredOn, new Config(), null));
    return loadedAccountProperties.map(AccountProperties::getAccount).get();
}
Also used : Config(org.eclipse.jgit.lib.Config) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException)

Aggregations

DuplicateKeyException (com.google.gerrit.exceptions.DuplicateKeyException)8 AccountGroup (com.google.gerrit.entities.AccountGroup)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 StorageException (com.google.gerrit.exceptions.StorageException)2 GroupDelta (com.google.gerrit.server.group.db.GroupDelta)2 TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 Account (com.google.gerrit.entities.Account)1 InternalGroup (com.google.gerrit.entities.InternalGroup)1 NoSuchGroupException (com.google.gerrit.exceptions.NoSuchGroupException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)1 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)1 InternalGroupCreation (com.google.gerrit.server.group.db.InternalGroupCreation)1