Search in sources :

Example 11 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class WorkInProgressOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws OrmException {
    Change change = ctx.getChange();
    ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId());
    change.setWorkInProgress(workInProgress);
    change.setLastUpdatedOn(ctx.getWhen());
    update.setWorkInProgress(workInProgress);
    addMessage(ctx, update);
    return true;
}
Also used : Change(com.google.gerrit.reviewdb.client.Change) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 12 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class TestChanges method newUpdate.

public static ChangeUpdate newUpdate(Injector injector, Change c, final CurrentUser user) throws Exception {
    injector = injector.createChildInjector(new FactoryModule() {

        @Override
        public void configure() {
            bind(CurrentUser.class).toInstance(user);
        }
    });
    ChangeUpdate update = injector.getInstance(ChangeUpdate.Factory.class).create(stubChangeControl(injector.getInstance(AbstractChangeNotes.Args.class), c, user), TimeUtil.nowTs(), Ordering.<String>natural());
    ChangeNotes notes = update.getNotes();
    boolean hasPatchSets = notes.getPatchSets() != null && !notes.getPatchSets().isEmpty();
    NotesMigration migration = injector.getInstance(NotesMigration.class);
    if (hasPatchSets || !migration.readChanges()) {
        return update;
    }
    // Change doesn't exist yet. NoteDb requires that there be a commit for the
    // first patch set, so create one.
    GitRepositoryManager repoManager = injector.getInstance(GitRepositoryManager.class);
    try (Repository repo = repoManager.openRepository(c.getProject())) {
        TestRepository<Repository> tr = new TestRepository<>(repo);
        PersonIdent ident = user.asIdentifiedUser().newCommitterIdent(update.getWhen(), TimeZone.getDefault());
        TestRepository<Repository>.CommitBuilder<Repository> cb = tr.commit().author(ident).committer(ident).message(firstNonNull(c.getSubject(), "Test change"));
        Ref parent = repo.exactRef(c.getDest().get());
        if (parent != null) {
            cb.parent(tr.getRevWalk().parseCommit(parent.getObjectId()));
        }
        update.setBranch(c.getDest().get());
        update.setChangeId(c.getKey().get());
        update.setCommit(tr.getRevWalk(), cb.create());
        return update;
    }
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) CurrentUser(com.google.gerrit.server.CurrentUser) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) AbstractChangeNotes(com.google.gerrit.server.notedb.AbstractChangeNotes) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) AbstractChangeNotes(com.google.gerrit.server.notedb.AbstractChangeNotes) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate) NotesMigration(com.google.gerrit.server.notedb.NotesMigration) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) PersonIdent(org.eclipse.jgit.lib.PersonIdent)

Example 13 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class DeleteReviewerOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws AuthException, ResourceNotFoundException, OrmException {
    Account.Id reviewerId = reviewer.getId();
    if (!approvalsUtil.getReviewers(ctx.getDb(), ctx.getNotes()).all().contains(reviewerId)) {
        throw new ResourceNotFoundException();
    }
    currChange = ctx.getChange();
    currPs = psUtil.current(ctx.getDb(), ctx.getNotes());
    LabelTypes labelTypes = ctx.getControl().getLabelTypes();
    // removing a reviewer will remove all her votes
    for (LabelType lt : labelTypes.getLabelTypes()) {
        newApprovals.put(lt.getName(), (short) 0);
    }
    StringBuilder msg = new StringBuilder();
    msg.append("Removed reviewer " + reviewer.getFullName());
    StringBuilder removedVotesMsg = new StringBuilder();
    removedVotesMsg.append(" with the following votes:\n\n");
    List<PatchSetApproval> del = new ArrayList<>();
    boolean votesRemoved = false;
    for (PatchSetApproval a : approvals(ctx, reviewerId)) {
        if (ctx.getControl().canRemoveReviewer(a)) {
            del.add(a);
            if (a.getPatchSetId().equals(currPs.getId()) && a.getValue() != 0) {
                oldApprovals.put(a.getLabel(), a.getValue());
                removedVotesMsg.append("* ").append(a.getLabel()).append(formatLabelValue(a.getValue())).append(" by ").append(userFactory.create(a.getAccountId()).getNameEmail()).append("\n");
                votesRemoved = true;
            }
        } else {
            throw new AuthException("delete reviewer not permitted");
        }
    }
    if (votesRemoved) {
        msg.append(removedVotesMsg);
    } else {
        msg.append(".");
    }
    ctx.getDb().patchSetApprovals().delete(del);
    ChangeUpdate update = ctx.getUpdate(currPs.getId());
    update.removeReviewer(reviewerId);
    changeMessage = ChangeMessagesUtil.newMessage(ctx, msg.toString(), ChangeMessagesUtil.TAG_DELETE_REVIEWER);
    cmUtil.addChangeMessage(ctx.getDb(), update, changeMessage);
    return true;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) LabelTypes(com.google.gerrit.common.data.LabelTypes) LabelType(com.google.gerrit.common.data.LabelType) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 14 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class PatchSetInserter method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws ResourceConflictException, OrmException, IOException {
    ReviewDb db = ctx.getDb();
    ChangeControl ctl = ctx.getControl();
    change = ctx.getChange();
    ChangeUpdate update = ctx.getUpdate(psId);
    update.setSubjectForCommit("Create patch set " + psId.get());
    if (!change.getStatus().isOpen() && !allowClosed) {
        throw new ResourceConflictException(String.format("Cannot create new patch set of change %s because it is %s", change.getId(), ChangeUtil.status(change)));
    }
    List<String> newGroups = groups;
    if (newGroups.isEmpty()) {
        PatchSet prevPs = psUtil.current(db, ctx.getNotes());
        if (prevPs != null) {
            newGroups = prevPs.getGroups();
        }
    }
    patchSet = psUtil.insert(db, ctx.getRevWalk(), ctx.getUpdate(psId), psId, commitId, draft, newGroups, null, description);
    if (notify != NotifyHandling.NONE) {
        oldReviewers = approvalsUtil.getReviewers(db, ctl.getNotes());
    }
    if (message != null) {
        changeMessage = ChangeMessagesUtil.newMessage(patchSet.getId(), ctx.getUser(), ctx.getWhen(), message, ChangeMessagesUtil.TAG_UPLOADED_PATCH_SET);
        changeMessage.setMessage(message);
    }
    patchSetInfo = patchSetInfoFactory.get(ctx.getRevWalk(), ctx.getRevWalk().parseCommit(commitId), psId);
    if (change.getStatus() != Change.Status.DRAFT && !allowClosed) {
        change.setStatus(Change.Status.NEW);
    }
    change.setCurrentPatchSet(patchSetInfo);
    if (copyApprovals) {
        approvalCopier.copy(db, ctl, patchSet);
    }
    if (changeMessage != null) {
        cmUtil.addChangeMessage(db, update, changeMessage);
    }
    return true;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ChangeControl(com.google.gerrit.server.project.ChangeControl) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 15 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class MergedByPushOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws OrmException, IOException {
    change = ctx.getChange();
    correctBranch = refName.equals(change.getDest().get());
    if (!correctBranch) {
        return false;
    }
    if (patchSetProvider != null) {
        // Caller might have also arranged for construction of a new patch set
        // that is not present in the old notes so we can't use PatchSetUtil.
        patchSet = patchSetProvider.get();
    } else {
        patchSet = checkNotNull(psUtil.get(ctx.getDb(), ctx.getNotes(), psId), "patch set %s not found", psId);
    }
    info = getPatchSetInfo(ctx);
    ChangeUpdate update = ctx.getUpdate(psId);
    Change.Status status = change.getStatus();
    if (status == Change.Status.MERGED) {
        return true;
    }
    if (status.isOpen()) {
        change.setCurrentPatchSet(info);
        change.setStatus(Change.Status.MERGED);
        // we cannot reconstruct the submit records for when this change was
        // submitted, this is why we must fix the status
        update.fixStatus(Change.Status.MERGED);
        update.setCurrentPatchSet();
    }
    StringBuilder msgBuf = new StringBuilder();
    msgBuf.append("Change has been successfully pushed");
    if (!refName.equals(change.getDest().get())) {
        msgBuf.append(" into ");
        if (refName.startsWith(Constants.R_HEADS)) {
            msgBuf.append("branch ");
            msgBuf.append(Repository.shortenRefName(refName));
        } else {
            msgBuf.append(refName);
        }
    }
    msgBuf.append(".");
    ChangeMessage msg = ChangeMessagesUtil.newMessage(psId, ctx.getUser(), ctx.getWhen(), msgBuf.toString(), ChangeMessagesUtil.TAG_MERGED);
    cmUtil.addChangeMessage(ctx.getDb(), update, msg);
    PatchSetApproval submitter = ApprovalsUtil.newApproval(change.currentPatchSetId(), ctx.getUser(), LabelId.legacySubmit(), 1, ctx.getWhen());
    update.putApproval(submitter.getLabel(), submitter.getValue());
    ctx.getDb().patchSetApprovals().upsert(Collections.singleton(submitter));
    return true;
}
Also used : ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) Change(com.google.gerrit.reviewdb.client.Change) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Aggregations

ChangeUpdate (com.google.gerrit.server.notedb.ChangeUpdate)15 Change (com.google.gerrit.reviewdb.client.Change)7 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)3 Repository (org.eclipse.jgit.lib.Repository)3 LabelTypes (com.google.gerrit.common.data.LabelTypes)2 Account (com.google.gerrit.reviewdb.client.Account)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)2 ChangeControl (com.google.gerrit.server.project.ChangeControl)2 RequestId (com.google.gerrit.server.util.RequestId)2 ValidationException (com.google.gerrit.server.validators.ValidationException)2 Collection (java.util.Collection)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 LabelType (com.google.gerrit.common.data.LabelType)1 SubmitRecord (com.google.gerrit.common.data.SubmitRecord)1 FactoryModule (com.google.gerrit.extensions.config.FactoryModule)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1