Search in sources :

Example 1 with ChangeContext

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

the class ChangeRebuilderIT method highestNumberedPatchSetIsNotCurrent.

@Test
public void highestNumberedPatchSetIsNotCurrent() throws Exception {
    PushOneCommit.Result r1 = createChange();
    PatchSet.Id psId1 = r1.getPatchSetId();
    Change.Id id = psId1.getParentKey();
    PushOneCommit.Result r2 = amendChange(r1.getChangeId());
    PatchSet.Id psId2 = r2.getPatchSetId();
    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 PatchSetInfoNotAvailableException {
                ctx.getChange().setCurrentPatchSet(patchSetInfoFactory.get(ctx.getDb(), ctx.getNotes(), psId1));
                return true;
            }
        });
        bu.execute();
    }
    ChangeNotes notes = notesFactory.create(db, project, id);
    assertThat(psUtil.byChangeAsMap(db, notes).keySet()).containsExactly(psId1, psId2);
    assertThat(notes.getChange().currentPatchSetId()).isEqualTo(psId1);
    assertThat(db.changes().get(id).currentPatchSetId()).isEqualTo(psId1);
    checker.rebuildAndCheckChanges(id);
    setNotesMigration(true, true);
    notes = notesFactory.create(db, project, id);
    assertThat(psUtil.byChangeAsMap(db, notes).keySet()).containsExactly(psId1, psId2);
    assertThat(notes.getChange().currentPatchSetId()).isEqualTo(psId1);
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) PatchSetInfoNotAvailableException(com.google.gerrit.server.patch.PatchSetInfoNotAvailableException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) 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 2 with ChangeContext

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

the class MergeOp method abandonAllOpenChangeForDeletedProject.

private void abandonAllOpenChangeForDeletedProject(Project.NameKey destProject) {
    try {
        for (ChangeData cd : internalChangeQuery.byProjectOpen(destProject)) {
            try (BatchUpdate bu = batchUpdateFactory.create(db, destProject, internalUserFactory.create(), ts)) {
                bu.setRequestId(submissionId);
                bu.addOp(cd.getId(), new BatchUpdateOp() {

                    @Override
                    public boolean updateChange(ChangeContext ctx) throws OrmException {
                        Change change = ctx.getChange();
                        if (!change.getStatus().isOpen()) {
                            return false;
                        }
                        change.setStatus(Change.Status.ABANDONED);
                        ChangeMessage msg = ChangeMessagesUtil.newMessage(change.currentPatchSetId(), internalUserFactory.create(), change.getLastUpdatedOn(), ChangeMessagesUtil.TAG_MERGED, "Project was deleted.");
                        cmUtil.addChangeMessage(ctx.getDb(), ctx.getUpdate(change.currentPatchSetId()), msg);
                        return true;
                    }
                });
                try {
                    bu.execute();
                } catch (UpdateException | RestApiException e) {
                    logWarn("Cannot abandon changes for deleted project " + destProject, e);
                }
            }
        }
    } catch (OrmException e) {
        logWarn("Cannot abandon changes for deleted project " + destProject, e);
    }
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) OrmException(com.google.gwtorm.server.OrmException) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) Change(com.google.gerrit.reviewdb.client.Change) UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeData(com.google.gerrit.server.query.change.ChangeData) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 3 with ChangeContext

use of com.google.gerrit.server.update.ChangeContext 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 4 with ChangeContext

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

the class AddReviewersOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws RestApiException, IOException {
    change = ctx.getChange();
    if (!accountIds.isEmpty()) {
        if (state == CC) {
            addedCCs = approvalsUtil.addCcs(ctx.getNotes(), ctx.getUpdate(change.currentPatchSetId()), accountIds, forGroup);
        } else {
            addedReviewers = approvalsUtil.addReviewers(ctx.getNotes(), ctx.getUpdate(change.currentPatchSetId()), projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject())).getLabelTypes(change.getDest()), change, accountIds);
        }
    }
    ReviewerStateInternal internalState = ReviewerStateInternal.fromReviewerState(state);
    // TODO(dborowitz): This behavior should live in ApprovalsUtil or something, like addCcs does.
    ImmutableSet<Address> existing = ctx.getNotes().getReviewersByEmail().byState(internalState);
    ImmutableList<Address> addressesToAdd = addresses.stream().filter(a -> !existing.contains(a)).collect(toImmutableList());
    if (state == CC) {
        addedCCsByEmail = addressesToAdd;
    } else {
        addedReviewersByEmail = addressesToAdd;
    }
    for (Address a : addressesToAdd) {
        ctx.getUpdate(change.currentPatchSetId()).putReviewerByEmail(a, internalState);
    }
    if (addedCCs.isEmpty() && addedReviewers.isEmpty() && addressesToAdd.isEmpty()) {
        return false;
    }
    checkAdded();
    if (patchSet == null) {
        patchSet = requireNonNull(psUtil.current(ctx.getNotes()));
    }
    return true;
}
Also used : AccountCache(com.google.gerrit.server.account.AccountCache) CC(com.google.gerrit.extensions.client.ReviewerState.CC) ProjectCache(com.google.gerrit.server.project.ProjectCache) Inject(com.google.inject.Inject) REVIEWER(com.google.gerrit.extensions.client.ReviewerState.REVIEWER) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) Assisted(com.google.inject.assistedinject.Assisted) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) PostUpdateContext(com.google.gerrit.server.update.PostUpdateContext) Objects.requireNonNull(java.util.Objects.requireNonNull) Change(com.google.gerrit.entities.Change) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Address(com.google.gerrit.entities.Address) ChangeContext(com.google.gerrit.server.update.ChangeContext) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableSet(com.google.common.collect.ImmutableSet) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) ReviewerAdded(com.google.gerrit.server.extensions.events.ReviewerAdded) Account(com.google.gerrit.entities.Account) Set(java.util.Set) IOException(java.io.IOException) Streams(com.google.common.collect.Streams) ApprovalsUtil(com.google.gerrit.server.approval.ApprovalsUtil) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) AccountState(com.google.gerrit.server.account.AccountState) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) Address(com.google.gerrit.entities.Address) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal)

Example 5 with ChangeContext

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

the class AbstractSubmit method setChangeStatusToNew.

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

                @Override
                public boolean updateChange(ChangeContext ctx) {
                    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) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Aggregations

ChangeContext (com.google.gerrit.server.update.ChangeContext)13 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)12 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)11 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 OrmException (com.google.gwtorm.server.OrmException)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 Change (com.google.gerrit.entities.Change)3 Change (com.google.gerrit.reviewdb.client.Change)3 RepoContext (com.google.gerrit.server.update.RepoContext)3 Account (com.google.gerrit.entities.Account)2 SubmissionId (com.google.gerrit.entities.SubmissionId)2 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)2 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)2 ChangeUpdate (com.google.gerrit.server.notedb.ChangeUpdate)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 UpdateException (com.google.gerrit.server.update.UpdateException)2