Search in sources :

Example 31 with Change

use of com.google.gerrit.reviewdb.client.Change 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 32 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class StreamEventsApiListener method onDraftPublished.

@Override
public void onDraftPublished(DraftPublishedListener.Event ev) {
    try {
        ChangeNotes notes = getNotes(ev.getChange());
        Change change = notes.getChange();
        PatchSet ps = getPatchSet(notes, ev.getRevision());
        DraftPublishedEvent event = new DraftPublishedEvent(change);
        event.change = changeAttributeSupplier(change);
        event.patchSet = patchSetAttributeSupplier(change, ps);
        event.uploader = accountAttributeSupplier(ev.getWho());
        dispatcher.get().postEvent(change, event);
    } catch (OrmException e) {
        log.error("Failed to dispatch event", e);
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.reviewdb.client.Change)

Example 33 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class LabelNormalizer method normalize.

/**
   * @param ctl change control containing the given approvals.
   * @param approvals list of approvals.
   * @return copies of approvals normalized to the defined ranges for the label type and permissions
   *     for the user. Approvals for unknown labels are not included in the output, nor are
   *     approvals where the user has no permissions for that label.
   */
public Result normalize(ChangeControl ctl, Collection<PatchSetApproval> approvals) {
    List<PatchSetApproval> unchanged = Lists.newArrayListWithCapacity(approvals.size());
    List<PatchSetApproval> updated = Lists.newArrayListWithCapacity(approvals.size());
    List<PatchSetApproval> deleted = Lists.newArrayListWithCapacity(approvals.size());
    LabelTypes labelTypes = ctl.getLabelTypes();
    for (PatchSetApproval psa : approvals) {
        Change.Id changeId = psa.getKey().getParentKey().getParentKey();
        checkArgument(changeId.equals(ctl.getId()), "Approval %s does not match change %s", psa.getKey(), ctl.getChange().getKey());
        if (psa.isLegacySubmit()) {
            unchanged.add(psa);
            continue;
        }
        LabelType label = labelTypes.byLabel(psa.getLabelId());
        if (label == null) {
            deleted.add(psa);
            continue;
        }
        PatchSetApproval copy = copy(psa);
        applyTypeFloor(label, copy);
        if (!applyRightFloor(ctl, label, copy)) {
            deleted.add(psa);
        } else if (copy.getValue() != psa.getValue()) {
            updated.add(copy);
        } else {
            unchanged.add(psa);
        }
    }
    return Result.create(unchanged, updated, deleted);
}
Also used : LabelTypes(com.google.gerrit.common.data.LabelTypes) LabelType(com.google.gerrit.common.data.LabelType) Change(com.google.gerrit.reviewdb.client.Change) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval)

Example 34 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method rebuild.

@Override
public Result rebuild(NoteDbUpdateManager manager, ChangeBundle bundle) throws NoSuchChangeException, IOException, OrmException {
    Change change = new Change(bundle.getChange());
    buildUpdates(manager, bundle);
    return manager.stageAndApplyDelta(change);
}
Also used : Change(com.google.gerrit.reviewdb.client.Change)

Example 35 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method execute.

public Result execute(ReviewDb db, Change.Id changeId, NoteDbUpdateManager manager, boolean checkReadOnly) throws OrmException, IOException {
    db = ReviewDbUtil.unwrapDb(db);
    Change change = checkNoteDbState(ChangeNotes.readOneReviewDbChange(db, changeId));
    if (change == null) {
        throw new NoSuchChangeException(changeId);
    }
    final String oldNoteDbState = change.getNoteDbState();
    Result r = manager.stageAndApplyDelta(change);
    final String newNoteDbState = change.getNoteDbState();
    try {
        db.changes().atomicUpdate(changeId, new AtomicUpdate<Change>() {

            @Override
            public Change update(Change change) {
                if (checkReadOnly) {
                    NoteDbChangeState.checkNotReadOnly(change, skewMs);
                }
                String currNoteDbState = change.getNoteDbState();
                if (Objects.equals(currNoteDbState, newNoteDbState)) {
                    // Another thread completed the same rebuild we were about to.
                    throw new AbortUpdateException();
                } else if (!Objects.equals(oldNoteDbState, currNoteDbState)) {
                    // Another thread updated the state to something else.
                    throw new ConflictingUpdateException(change, oldNoteDbState);
                }
                change.setNoteDbState(newNoteDbState);
                return change;
            }
        });
    } catch (ConflictingUpdateException e) {
        // the other thread.
        throw new OrmException(e.getMessage());
    } catch (AbortUpdateException e) {
        if (NoteDbChangeState.parse(changeId, newNoteDbState).isUpToDate(manager.getChangeRepo().cmds.getRepoRefCache(), manager.getAllUsersRepo().cmds.getRepoRefCache())) {
            // Result was flushed to the repo by whatever thread won the race.
            return r;
        }
    // If the state doesn't match, that means another thread attempted this
    // rebuild, but failed. Fall through and try to update the ref again.
    }
    if (migration.failChangeWrites()) {
        // results instead of reading from the repo.
        throw new OrmException(NoteDbUpdateManager.CHANGES_READ_ONLY);
    }
    manager.execute();
    return r;
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) Result(com.google.gerrit.server.notedb.NoteDbUpdateManager.Result)

Aggregations

Change (com.google.gerrit.reviewdb.client.Change)191 Test (org.junit.Test)96 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)53 ObjectId (org.eclipse.jgit.lib.ObjectId)32 Timestamp (java.sql.Timestamp)31 Account (com.google.gerrit.reviewdb.client.Account)30 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)28 OrmException (com.google.gwtorm.server.OrmException)28 Project (com.google.gerrit.reviewdb.client.Project)27 Repository (org.eclipse.jgit.lib.Repository)27 RevWalk (org.eclipse.jgit.revwalk.RevWalk)24 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)23 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)22 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)21 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)20 RevCommit (org.eclipse.jgit.revwalk.RevCommit)19 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)17 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)17 ChangeData (com.google.gerrit.server.query.change.ChangeData)16 RevId (com.google.gerrit.reviewdb.client.RevId)15