Search in sources :

Example 6 with BatchUpdateOp

use of com.google.gerrit.server.update.BatchUpdateOp in project gerrit by GerritCodeReview.

the class AbstractSubmit method setChangeStatusToNew.

private void setChangeStatusToNew(PushOneCommit.Result... changes) throws Exception {
    for (PushOneCommit.Result change : changes) {
        try (BatchUpdate bu = batchUpdateFactory.create(db, project, userFactory.create(admin.id), TimeUtil.nowTs())) {
            bu.addOp(change.getChange().getId(), new BatchUpdateOp() {

                @Override
                public boolean updateChange(ChangeContext ctx) throws OrmException {
                    ctx.getChange().setStatus(Change.Status.NEW);
                    ctx.getUpdate(ctx.getChange().currentPatchSetId()).setStatus(Change.Status.NEW);
                    return true;
                }
            });
            bu.execute();
        }
    }
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) OrmException(com.google.gwtorm.server.OrmException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 7 with BatchUpdateOp

use of com.google.gerrit.server.update.BatchUpdateOp in project gerrit by GerritCodeReview.

the class ConsistencyCheckerIT method mergeChange.

private ChangeControl mergeChange(ChangeControl ctl) throws Exception {
    final ObjectId oldId = getDestRef(ctl);
    final ObjectId newId = ObjectId.fromString(psUtil.current(db, ctl.getNotes()).getRevision().get());
    final String dest = ctl.getChange().getDest().get();
    try (BatchUpdate bu = newUpdate(adminId)) {
        bu.addOp(ctl.getId(), new BatchUpdateOp() {

            @Override
            public void updateRepo(RepoContext ctx) throws IOException {
                ctx.addRefUpdate(oldId, newId, dest);
            }

            @Override
            public boolean updateChange(ChangeContext ctx) throws OrmException {
                ctx.getChange().setStatus(Change.Status.MERGED);
                ctx.getUpdate(ctx.getChange().currentPatchSetId()).fixStatus(Change.Status.MERGED);
                return true;
            }
        });
        bu.execute();
    }
    return reload(ctl);
}
Also used : RepoContext(com.google.gerrit.server.update.RepoContext) ChangeContext(com.google.gerrit.server.update.ChangeContext) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 8 with BatchUpdateOp

use of com.google.gerrit.server.update.BatchUpdateOp in project gerrit by GerritCodeReview.

the class ConsistencyCheckerIT method mergedChangeIsNotMerged.

@Test
public void mergedChangeIsNotMerged() throws Exception {
    ChangeControl ctl = insertChange();
    try (BatchUpdate bu = newUpdate(adminId)) {
        bu.addOp(ctl.getId(), new BatchUpdateOp() {

            @Override
            public boolean updateChange(ChangeContext ctx) throws OrmException {
                ctx.getChange().setStatus(Change.Status.MERGED);
                ctx.getUpdate(ctx.getChange().currentPatchSetId()).fixStatus(Change.Status.MERGED);
                return true;
            }
        });
        bu.execute();
    }
    ctl = reload(ctl);
    String rev = psUtil.current(db, ctl.getNotes()).getRevision().get();
    ObjectId tip = getDestRef(ctl);
    assertProblems(ctl, null, problem("Patch set 1 (" + rev + ") is not merged into destination ref" + " refs/heads/master (" + tip.name() + "), but change status is MERGED"));
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) OrmException(com.google.gwtorm.server.OrmException) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeControl(com.google.gerrit.server.project.ChangeControl) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 9 with BatchUpdateOp

use of com.google.gerrit.server.update.BatchUpdateOp in project gerrit by GerritCodeReview.

the class ChangeRebuilderIT method rebuildAutomaticallyWithinBatchUpdate.

@Test
public void rebuildAutomaticallyWithinBatchUpdate() throws Exception {
    setNotesMigration(true, true);
    PushOneCommit.Result r = createChange();
    final Change.Id id = r.getPatchSetId().getParentKey();
    assertChangeUpToDate(true, id);
    // Update ReviewDb and NoteDb, then revert the corresponding NoteDb change
    // to simulate it failing.
    NoteDbChangeState oldState = NoteDbChangeState.parse(getUnwrappedDb().changes().get(id));
    String topic = name("a-topic");
    gApi.changes().id(id.get()).topic(topic);
    try (Repository repo = repoManager.openRepository(project)) {
        new TestRepository<>(repo).update(RefNames.changeMetaRef(id), oldState.getChangeMetaId());
    }
    assertChangeUpToDate(false, id);
    // Next NoteDb read comes inside the transaction started by BatchUpdate. In
    // reality this could be caused by a failed update happening between when
    // the change is parsed by ChangesCollection and when the BatchUpdate
    // executes. We simulate it here by using BatchUpdate directly and not going
    // through an API handler.
    final String msg = "message from BatchUpdate";
    try (BatchUpdate bu = batchUpdateFactory.create(db, project, identifiedUserFactory.create(user.getId()), TimeUtil.nowTs())) {
        bu.addOp(id, new BatchUpdateOp() {

            @Override
            public boolean updateChange(ChangeContext ctx) throws OrmException {
                PatchSet.Id psId = ctx.getChange().currentPatchSetId();
                ChangeMessage cm = new ChangeMessage(new ChangeMessage.Key(id, ChangeUtil.messageUuid()), ctx.getAccountId(), ctx.getWhen(), psId);
                cm.setMessage(msg);
                ctx.getDb().changeMessages().insert(Collections.singleton(cm));
                ctx.getUpdate(psId).setChangeMessage(msg);
                return true;
            }
        });
        try {
            bu.execute();
            fail("expected update to fail");
        } catch (UpdateException e) {
            assertThat(e.getMessage()).contains("cannot copy ChangeNotesState");
        }
    }
// TODO(dborowitz): Re-enable these assertions once we fix auto-rebuilding
// in the BatchUpdate path.
//// As an implementation detail, change wasn't actually rebuilt inside the
//// BatchUpdate transaction, but it was rebuilt during read for the
//// subsequent reindex. Thus it's impossible to actually observe an
//// out-of-date state in the caller.
//assertChangeUpToDate(true, id);
//// Check that the bundles are equal.
//ChangeNotes notes = notesFactory.create(dbProvider.get(), project, id);
//ChangeBundle actual = ChangeBundle.fromNotes(commentsUtil, notes);
//ChangeBundle expected = bundleReader.fromReviewDb(getUnwrappedDb(), id);
//assertThat(actual.differencesFrom(expected)).isEmpty();
//assertThat(
//        Iterables.transform(
//            notes.getChangeMessages(),
//            ChangeMessage::getMessage))
//    .contains(msg);
//assertThat(actual.getChange().getTopic()).isEqualTo(topic);
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) Change(com.google.gerrit.reviewdb.client.Change) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) ObjectId(org.eclipse.jgit.lib.ObjectId) UpdateException(com.google.gerrit.server.update.UpdateException) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 10 with BatchUpdateOp

use of com.google.gerrit.server.update.BatchUpdateOp in project gerrit by GerritCodeReview.

the class ConsistencyChecker method insertMergedPatchSet.

private void insertMergedPatchSet(final RevCommit commit, @Nullable final PatchSet.Id psIdToDelete, boolean reuseOldPsId) {
    ProblemInfo notFound = problem("No patch set found for merged commit " + commit.name());
    if (!user.get().isIdentifiedUser()) {
        notFound.status = Status.FIX_FAILED;
        notFound.outcome = "Must be called by an identified user to insert new patch set";
        return;
    }
    ProblemInfo insertPatchSetProblem;
    ProblemInfo deleteOldPatchSetProblem;
    if (psIdToDelete == null) {
        insertPatchSetProblem = problem(String.format("Expected merged commit %s has no associated patch set", commit.name()));
        deleteOldPatchSetProblem = null;
    } else {
        String msg = String.format("Expected merge commit %s corresponds to patch set %s," + " not the current patch set %s", commit.name(), psIdToDelete.get(), change().currentPatchSetId().get());
        // Maybe an identical problem, but different fix.
        deleteOldPatchSetProblem = reuseOldPsId ? null : problem(msg);
        insertPatchSetProblem = problem(msg);
    }
    List<ProblemInfo> currProblems = new ArrayList<>(3);
    currProblems.add(notFound);
    if (deleteOldPatchSetProblem != null) {
        currProblems.add(insertPatchSetProblem);
    }
    currProblems.add(insertPatchSetProblem);
    try {
        PatchSet.Id psId = (psIdToDelete != null && reuseOldPsId) ? psIdToDelete : ChangeUtil.nextPatchSetId(repo, change().currentPatchSetId());
        PatchSetInserter inserter = patchSetInserterFactory.create(ctl, psId, commit);
        try (BatchUpdate bu = newBatchUpdate()) {
            bu.setRepository(repo, rw, oi);
            if (psIdToDelete != null) {
                // Delete the given patch set ref. If reuseOldPsId is true,
                // PatchSetInserter will reinsert the same ref, making it a no-op.
                bu.addOp(ctl.getId(), new BatchUpdateOp() {

                    @Override
                    public void updateRepo(RepoContext ctx) throws IOException {
                        ctx.addRefUpdate(commit, ObjectId.zeroId(), psIdToDelete.toRefName());
                    }
                });
                if (!reuseOldPsId) {
                    bu.addOp(ctl.getId(), new DeletePatchSetFromDbOp(checkNotNull(deleteOldPatchSetProblem), psIdToDelete));
                }
            }
            bu.addOp(ctl.getId(), inserter.setValidate(false).setFireRevisionCreated(false).setNotify(NotifyHandling.NONE).setAllowClosed(true).setMessage("Patch set for merged commit inserted by consistency checker"));
            bu.addOp(ctl.getId(), new FixMergedOp(notFound));
            bu.execute();
        }
        ctl = changeControlFactory.controlFor(db.get(), inserter.getChange(), ctl.getUser());
        insertPatchSetProblem.status = Status.FIXED;
        insertPatchSetProblem.outcome = "Inserted as patch set " + psId.get();
    } catch (OrmException | IOException | UpdateException | RestApiException e) {
        warn(e);
        for (ProblemInfo pi : currProblems) {
            pi.status = Status.FIX_FAILED;
            pi.outcome = "Error inserting merged patch set";
        }
        return;
    }
}
Also used : RepoContext(com.google.gerrit.server.update.RepoContext) ProblemInfo(com.google.gerrit.extensions.common.ProblemInfo) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) IOException(java.io.IOException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) OrmException(com.google.gwtorm.server.OrmException) UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Aggregations

BatchUpdate (com.google.gerrit.server.update.BatchUpdate)12 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)12 ChangeContext (com.google.gerrit.server.update.ChangeContext)9 OrmException (com.google.gwtorm.server.OrmException)9 Change (com.google.gerrit.reviewdb.client.Change)5 UpdateException (com.google.gerrit.server.update.UpdateException)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)4 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)4 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)4 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)4 RepoContext (com.google.gerrit.server.update.RepoContext)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)2 Repository (org.eclipse.jgit.lib.Repository)2 DeleteReviewerInput (com.google.gerrit.extensions.api.changes.DeleteReviewerInput)1 ChangeKind (com.google.gerrit.extensions.client.ChangeKind)1 ProblemInfo (com.google.gerrit.extensions.common.ProblemInfo)1