Search in sources :

Example 46 with RestApiException

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

the class NoteDbPrimaryIT method testReadOnly.

private void testReadOnly(Change.Id id) throws Exception {
    Timestamp before = TimeUtil.nowTs();
    Timestamp until = new Timestamp(before.getTime() + 1000 * 3600);
    // Set read-only.
    Change c = db.changes().get(id);
    assertThat(c).named("change " + id).isNotNull();
    NoteDbChangeState state = NoteDbChangeState.parse(c);
    state = state.withReadOnlyUntil(until);
    c.setNoteDbState(state.toString());
    db.changes().update(Collections.singleton(c));
    assertThat(gApi.changes().id(id.get()).get().subject).isEqualTo(PushOneCommit.SUBJECT);
    assertThat(gApi.changes().id(id.get()).get().topic).isNull();
    try {
        gApi.changes().id(id.get()).topic("a-topic");
        assert_().fail("expected read-only exception");
    } catch (RestApiException e) {
        Optional<Throwable> oe = Throwables.getCausalChain(e).stream().filter(x -> x instanceof OrmRuntimeException).findFirst();
        assertThat(oe).named("OrmRuntimeException in causal chain of " + e).isPresent();
        assertThat(oe.get().getMessage()).contains("read-only");
    }
    assertThat(gApi.changes().id(id.get()).get().topic).isNull();
    TestTimeUtil.setClock(new Timestamp(until.getTime() + 1000));
    assertThat(gApi.changes().id(id.get()).get().subject).isEqualTo(PushOneCommit.SUBJECT);
    gApi.changes().id(id.get()).topic("a-topic");
    assertThat(gApi.changes().id(id.get()).get().topic).isEqualTo("a-topic");
}
Also used : OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) Optional(java.util.Optional) Change(com.google.gerrit.reviewdb.client.Change) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Timestamp(java.sql.Timestamp) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState)

Example 47 with RestApiException

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

the class AccountApiImpl method starChange.

@Override
public void starChange(String changeId) throws RestApiException {
    try {
        ChangeResource rsrc = changes.parse(TopLevelResource.INSTANCE, IdString.fromUrl(changeId));
        starredChangesCreate.setChange(rsrc);
        starredChangesCreate.apply(account, new StarredChanges.EmptyInput());
    } catch (Exception e) {
        throw asRestApiException("Cannot star change", e);
    }
}
Also used : ChangeResource(com.google.gerrit.server.change.ChangeResource) StarredChanges(com.google.gerrit.server.account.StarredChanges) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 48 with RestApiException

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

the class ProjectApiImpl method create.

@Override
public ProjectApi create(ProjectInput in) throws RestApiException {
    try {
        if (name == null) {
            throw new ResourceConflictException("Project already exists");
        }
        if (in.name != null && !name.equals(in.name)) {
            throw new BadRequestException("name must match input.name");
        }
        CreateProject impl = createProjectFactory.create(name);
        permissionBackend.user(user).checkAny(GlobalPermission.fromAnnotation(impl.getClass()));
        impl.apply(TopLevelResource.INSTANCE, in);
        return projectApi.create(projects.parse(name));
    } catch (Exception e) {
        throw asRestApiException("Cannot create project: " + e.getMessage(), e);
    }
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) CreateProject(com.google.gerrit.server.project.CreateProject) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException)

Example 49 with RestApiException

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

the class UnfusedNoteDbBatchUpdate method executeRefUpdates.

// TODO(dborowitz): Don't execute non-change ref updates separately when fusing phases.
private void executeRefUpdates(boolean dryrun) throws IOException, RestApiException {
    if (getRefUpdates().isEmpty()) {
        logDebug("No ref updates to execute");
        return;
    }
    // May not be opened if the caller added ref updates but no new objects.
    initRepository();
    batchRefUpdate = repoView.getRepository().getRefDatabase().newBatchUpdate();
    batchRefUpdate.setPushCertificate(pushCert);
    batchRefUpdate.setRefLogMessage(refLogMessage, true);
    batchRefUpdate.setAllowNonFastForwards(true);
    repoView.getCommands().addTo(batchRefUpdate);
    logDebug("Executing batch of {} ref updates", batchRefUpdate.getCommands().size());
    if (dryrun) {
        return;
    }
    // that might have access to unflushed objects.
    try (RevWalk updateRw = new RevWalk(repoView.getRepository())) {
        batchRefUpdate.execute(updateRw, NullProgressMonitor.INSTANCE);
    }
    boolean ok = true;
    for (ReceiveCommand cmd : batchRefUpdate.getCommands()) {
        if (cmd.getResult() != ReceiveCommand.Result.OK) {
            ok = false;
            break;
        }
    }
    if (!ok) {
        throw new RestApiException("BatchRefUpdate failed: " + batchRefUpdate);
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 50 with RestApiException

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

the class PrimaryStorageMigrator method releaseReadOnlyLeaseInNoteDb.

private void releaseReadOnlyLeaseInNoteDb(Project.NameKey project, Change.Id id) throws OrmException {
    // (In practice retrying won't happen, since we aren't using fused updates at this point.)
    try {
        retryHelper.execute(updateFactory -> {
            try (BatchUpdate bu = updateFactory.create(db.get(), project, internalUserFactory.create(), TimeUtil.nowTs())) {
                bu.addOp(id, new BatchUpdateOp() {

                    @Override
                    public boolean updateChange(ChangeContext ctx) {
                        ctx.getUpdate(ctx.getChange().currentPatchSetId()).setReadOnlyUntil(new Timestamp(0));
                        return true;
                    }
                });
                bu.execute();
                return null;
            }
        });
    } catch (RestApiException | UpdateException e) {
        throw new OrmException(e);
    }
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) OrmException(com.google.gwtorm.server.OrmException) UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Timestamp(java.sql.Timestamp) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Aggregations

RestApiException (com.google.gerrit.extensions.restapi.RestApiException)93 ApiUtil.asRestApiException (com.google.gerrit.server.api.ApiUtil.asRestApiException)38 IOException (java.io.IOException)31 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)24 UpdateException (com.google.gerrit.server.update.UpdateException)23 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)22 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)20 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)19 AuthException (com.google.gerrit.extensions.restapi.AuthException)18 StorageException (com.google.gerrit.exceptions.StorageException)17 List (java.util.List)17 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)16 Inject (com.google.inject.Inject)15 ArrayList (java.util.ArrayList)15 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)14 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)14 ChangeContext (com.google.gerrit.server.update.ChangeContext)14 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)14 Provider (com.google.inject.Provider)12 Change (com.google.gerrit.entities.Change)11