Search in sources :

Example 1 with OrmRuntimeException

use of com.google.gwtorm.server.OrmRuntimeException in project gerrit by GerritCodeReview.

the class ChangeRebuilderIT method rebuilderRespectsReadOnlyInNoteDbChangeState.

@Test
public void rebuilderRespectsReadOnlyInNoteDbChangeState() throws Exception {
    TestTimeUtil.resetWithClockStep(1, SECONDS);
    PushOneCommit.Result r = createChange();
    PatchSet.Id psId1 = r.getPatchSetId();
    Change.Id id = psId1.getParentKey();
    checker.rebuildAndCheckChanges(id);
    setNotesMigration(true, true);
    ReviewDb db = getUnwrappedDb();
    Change c = db.changes().get(id);
    NoteDbChangeState state = NoteDbChangeState.parse(c);
    Timestamp until = new Timestamp(TimeUtil.nowMs() + MILLISECONDS.convert(1, DAYS));
    state = state.withReadOnlyUntil(until);
    c.setNoteDbState(state.toString());
    db.changes().update(Collections.singleton(c));
    try {
        rebuilderWrapper.rebuild(db, id);
        assert_().fail("expected rebuild to fail");
    } catch (OrmRuntimeException e) {
        assertThat(e.getMessage()).contains("read-only until");
    }
    TestTimeUtil.setClock(new Timestamp(until.getTime() + MILLISECONDS.convert(1, SECONDS)));
    rebuilderWrapper.rebuild(db, id);
}
Also used : OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) Timestamp(java.sql.Timestamp) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 2 with OrmRuntimeException

use of com.google.gwtorm.server.OrmRuntimeException in project gerrit by GerritCodeReview.

the class PrimaryStorageMigrator method setPrimaryStorageReviewDb.

private void setPrimaryStorageReviewDb(Change.Id id, ObjectId newMetaId) throws OrmException, IOException {
    ImmutableMap.Builder<Account.Id, ObjectId> draftIds = ImmutableMap.builder();
    try (Repository repo = repoManager.openRepository(allUsers)) {
        for (Ref draftRef : repo.getRefDatabase().getRefs(RefNames.refsDraftCommentsPrefix(id)).values()) {
            Account.Id accountId = Account.Id.fromRef(draftRef.getName());
            if (accountId != null) {
                draftIds.put(accountId, draftRef.getObjectId().copy());
            }
        }
    }
    NoteDbChangeState newState = new NoteDbChangeState(id, PrimaryStorage.REVIEW_DB, Optional.of(RefState.create(newMetaId, draftIds.build())), Optional.empty());
    db().changes().atomicUpdate(id, new AtomicUpdate<Change>() {

        @Override
        public Change update(Change change) {
            if (PrimaryStorage.of(change) != PrimaryStorage.NOTE_DB) {
                throw new OrmRuntimeException("change " + id + " is not NoteDb primary: " + change.getNoteDbState());
            }
            change.setNoteDbState(newState.toString());
            return change;
        }
    });
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) ImmutableMap(com.google.common.collect.ImmutableMap) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId)

Example 3 with OrmRuntimeException

use of com.google.gwtorm.server.OrmRuntimeException 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 4 with OrmRuntimeException

use of com.google.gwtorm.server.OrmRuntimeException in project gerrit by GerritCodeReview.

the class GetRevisionActions method getETag.

@Override
public String getETag(RevisionResource rsrc) {
    Hasher h = Hashing.md5().newHasher();
    CurrentUser user = rsrc.getControl().getUser();
    try {
        rsrc.getChangeResource().prepareETag(h, user);
        h.putBoolean(Submit.wholeTopicEnabled(config));
        ReviewDb db = dbProvider.get();
        ChangeSet cs = mergeSuperSet.get().completeChangeSet(db, rsrc.getChange(), user);
        for (ChangeData cd : cs.changes()) {
            changeResourceFactory.create(cd.changeControl()).prepareETag(h, user);
        }
        h.putBoolean(cs.furtherHiddenChanges());
    } catch (IOException | OrmException e) {
        throw new OrmRuntimeException(e);
    }
    return h.hash().toString();
}
Also used : Hasher(com.google.common.hash.Hasher) OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) CurrentUser(com.google.gerrit.server.CurrentUser) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) ChangeSet(com.google.gerrit.server.git.ChangeSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 5 with OrmRuntimeException

use of com.google.gwtorm.server.OrmRuntimeException in project gerrit by GerritCodeReview.

the class Submit method getDescription.

@Override
public UiAction.Description getDescription(RevisionResource resource) {
    Change change = resource.getChange();
    String topic = change.getTopic();
    ReviewDb db = dbProvider.get();
    ChangeData cd = changeDataFactory.create(db, resource.getControl());
    boolean visible;
    try {
        visible = change.getStatus().isOpen() && resource.isCurrent() && !resource.getPatchSet().isDraft() && resource.permissions().test(ChangePermission.SUBMIT);
        MergeOp.checkSubmitRule(cd);
    } catch (ResourceConflictException e) {
        visible = false;
    } catch (PermissionBackendException e) {
        log.error("Error checking if change is submittable", e);
        throw new OrmRuntimeException("Could not check submit permission", e);
    } catch (OrmException e) {
        log.error("Error checking if change is submittable", e);
        throw new OrmRuntimeException("Could not determine problems for the change", e);
    }
    if (!visible) {
        return new UiAction.Description().setLabel("").setTitle("").setVisible(false);
    }
    ChangeSet cs;
    try {
        cs = mergeSuperSet.get().completeChangeSet(db, cd.change(), resource.getControl().getUser());
    } catch (OrmException | IOException e) {
        throw new OrmRuntimeException("Could not determine complete set of changes to be submitted", e);
    }
    int topicSize = 0;
    if (!Strings.isNullOrEmpty(topic)) {
        topicSize = getChangesByTopic(topic).size();
    }
    boolean treatWithTopic = submitWholeTopic && !Strings.isNullOrEmpty(topic) && topicSize > 1;
    String submitProblems = problemsForSubmittingChangeset(cd, cs, resource.getUser());
    Boolean enabled;
    try {
        // Recheck mergeability rather than using value stored in the index,
        // which may be stale.
        // TODO(dborowitz): This is ugly; consider providing a way to not read
        // stored fields from the index in the first place.
        // cd.setMergeable(null);
        // That was done in unmergeableChanges which was called by
        // problemsForSubmittingChangeset, so now it is safe to read from
        // the cache, as it yields the same result.
        enabled = cd.isMergeable();
    } catch (OrmException e) {
        throw new OrmRuntimeException("Could not determine mergeability", e);
    }
    if (submitProblems != null) {
        return new UiAction.Description().setLabel(treatWithTopic ? submitTopicLabel : (cs.size() > 1) ? labelWithParents : label).setTitle(submitProblems).setVisible(true).setEnabled(false);
    }
    if (treatWithTopic) {
        Map<String, String> params = ImmutableMap.of("topicSize", String.valueOf(topicSize), "submitSize", String.valueOf(cs.size()));
        return new UiAction.Description().setLabel(submitTopicLabel).setTitle(Strings.emptyToNull(submitTopicTooltip.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
    }
    RevId revId = resource.getPatchSet().getRevision();
    Map<String, String> params = ImmutableMap.of("patchSet", String.valueOf(resource.getPatchSet().getPatchSetId()), "branch", change.getDest().getShortName(), "commit", ObjectId.fromString(revId.get()).abbreviate(7).name(), "submitSize", String.valueOf(cs.size()));
    ParameterizedString tp = cs.size() > 1 ? titlePatternWithAncestors : titlePattern;
    return new UiAction.Description().setLabel(cs.size() > 1 ? labelWithParents : label).setTitle(Strings.emptyToNull(tp.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
}
Also used : OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Change(com.google.gerrit.reviewdb.client.Change) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) IOException(java.io.IOException) UiAction(com.google.gerrit.extensions.webui.UiAction) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevId(com.google.gerrit.reviewdb.client.RevId) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) OrmException(com.google.gwtorm.server.OrmException) ChangeSet(com.google.gerrit.server.git.ChangeSet) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Aggregations

OrmRuntimeException (com.google.gwtorm.server.OrmRuntimeException)7 Change (com.google.gerrit.reviewdb.client.Change)6 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)4 ChangeSet (com.google.gerrit.server.git.ChangeSet)3 ChangeData (com.google.gerrit.server.query.change.ChangeData)3 OrmException (com.google.gwtorm.server.OrmException)3 IOException (java.io.IOException)3 Timestamp (java.sql.Timestamp)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ParameterizedString (com.google.gerrit.common.data.ParameterizedString)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)2 UiAction (com.google.gerrit.extensions.webui.UiAction)2 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)2 RevId (com.google.gerrit.reviewdb.client.RevId)2 CurrentUser (com.google.gerrit.server.CurrentUser)2 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MoreObjects (com.google.common.base.MoreObjects)1 Strings (com.google.common.base.Strings)1