Search in sources :

Example 76 with ChangeNotes

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

the class ReceiveCommits method autoCloseChanges.

private void autoCloseChanges(final ReceiveCommand cmd) {
    logDebug("Starting auto-closing of changes");
    String refName = cmd.getRefName();
    checkState(!MagicBranch.isMagicBranch(refName), "shouldn't be auto-closing changes on magic branch %s", refName);
    // insertChangesAndPatchSets.
    try (BatchUpdate bu = batchUpdateFactory.create(db, projectControl.getProject().getNameKey(), user, TimeUtil.nowTs());
        ObjectInserter ins = repo.newObjectInserter();
        ObjectReader reader = ins.newReader();
        RevWalk rw = new RevWalk(reader)) {
        bu.setRepository(repo, rw, ins).updateChangesInParallel();
        bu.setRequestId(receiveId);
        // TODO(dborowitz): Teach BatchUpdate to ignore missing changes.
        RevCommit newTip = rw.parseCommit(cmd.getNewId());
        Branch.NameKey branch = new Branch.NameKey(project.getNameKey(), refName);
        rw.reset();
        rw.markStart(newTip);
        if (!ObjectId.zeroId().equals(cmd.getOldId())) {
            rw.markUninteresting(rw.parseCommit(cmd.getOldId()));
        }
        ListMultimap<ObjectId, Ref> byCommit = changeRefsById();
        Map<Change.Key, ChangeNotes> byKey = null;
        List<ReplaceRequest> replaceAndClose = new ArrayList<>();
        int existingPatchSets = 0;
        int newPatchSets = 0;
        COMMIT: for (RevCommit c; (c = rw.next()) != null; ) {
            rw.parseBody(c);
            for (Ref ref : byCommit.get(c.copy())) {
                existingPatchSets++;
                PatchSet.Id psId = PatchSet.Id.fromRef(ref.getName());
                bu.addOp(psId.getParentKey(), mergedByPushOpFactory.create(requestScopePropagator, psId, refName));
                continue COMMIT;
            }
            for (String changeId : c.getFooterLines(CHANGE_ID)) {
                if (byKey == null) {
                    byKey = openChangesByBranch(branch);
                }
                ChangeNotes onto = byKey.get(new Change.Key(changeId.trim()));
                if (onto != null) {
                    newPatchSets++;
                    // Hold onto this until we're done with the walk, as the call to
                    // req.validate below calls isMergedInto which resets the walk.
                    ReplaceRequest req = new ReplaceRequest(onto.getChangeId(), c, cmd, false);
                    req.notes = onto;
                    replaceAndClose.add(req);
                    continue COMMIT;
                }
            }
        }
        for (final ReplaceRequest req : replaceAndClose) {
            Change.Id id = req.notes.getChangeId();
            if (!req.validate(true)) {
                logDebug("Not closing {} because validation failed", id);
                continue;
            }
            req.addOps(bu, null);
            bu.addOp(id, mergedByPushOpFactory.create(requestScopePropagator, req.psId, refName).setPatchSetProvider(new Provider<PatchSet>() {

                @Override
                public PatchSet get() {
                    return req.replaceOp.getPatchSet();
                }
            }));
            bu.addOp(id, new ChangeProgressOp(closeProgress));
        }
        logDebug("Auto-closing {} changes with existing patch sets and {} with new patch sets", existingPatchSets, newPatchSets);
        bu.execute();
    } catch (RestApiException e) {
        logError("Can't insert patchset", e);
    } catch (IOException | OrmException | UpdateException | PermissionBackendException e) {
        logError("Can't scan for changes to close", e);
    }
}
Also used : ArrayList(java.util.ArrayList) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) OrmException(com.google.gwtorm.server.OrmException) MagicBranch(com.google.gerrit.server.util.MagicBranch) Branch(com.google.gerrit.reviewdb.client.Branch) ObjectReader(org.eclipse.jgit.lib.ObjectReader) UpdateException(com.google.gerrit.server.update.UpdateException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ObjectId(org.eclipse.jgit.lib.ObjectId) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Provider(com.google.inject.Provider) Ref(org.eclipse.jgit.lib.Ref) RequestId(com.google.gerrit.server.util.RequestId) ObjectId(org.eclipse.jgit.lib.ObjectId) RevId(com.google.gerrit.reviewdb.client.RevId) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 77 with ChangeNotes

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

the class ChangeData method ensureChangeLoaded.

public static void ensureChangeLoaded(Iterable<ChangeData> changes) throws OrmException {
    ChangeData first = Iterables.getFirst(changes, null);
    if (first == null) {
        return;
    } else if (first.notesMigration.readChanges()) {
        for (ChangeData cd : changes) {
            cd.change();
        }
        return;
    }
    Map<Change.Id, ChangeData> missing = new HashMap<>();
    for (ChangeData cd : changes) {
        if (cd.change == null) {
            missing.put(cd.getId(), cd);
        }
    }
    if (missing.isEmpty()) {
        return;
    }
    for (ChangeNotes notes : first.notesFactory.create(first.db, missing.keySet())) {
        missing.get(notes.getChangeId()).change = notes.getChange();
    }
}
Also used : HashMap(java.util.HashMap) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes)

Example 78 with ChangeNotes

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

the class RebaseUtil method parseBase.

public Base parseBase(RevisionResource rsrc, String base) {
    // Try parsing the base as a ref string.
    PatchSet.Id basePatchSetId = PatchSet.Id.fromRef(base);
    if (basePatchSetId != null) {
        Change.Id baseChangeId = basePatchSetId.changeId();
        ChangeNotes baseNotes = notesFor(rsrc, baseChangeId);
        if (baseNotes != null) {
            return Base.create(notesFor(rsrc, basePatchSetId.changeId()), psUtil.get(baseNotes, basePatchSetId));
        }
    }
    // Try parsing base as a change number (assume current patch set).
    Integer baseChangeId = Ints.tryParse(base);
    if (baseChangeId != null) {
        ChangeNotes baseNotes = notesFor(rsrc, Change.id(baseChangeId));
        if (baseNotes != null) {
            return Base.create(baseNotes, psUtil.current(baseNotes));
        }
    }
    // Try parsing as SHA-1.
    Base ret = null;
    for (ChangeData cd : queryProvider.get().byProjectCommit(rsrc.getProject(), base)) {
        for (PatchSet ps : cd.patchSets()) {
            if (!ObjectIds.matchesAbbreviation(ps.commitId(), base)) {
                continue;
            }
            if (ret == null || ret.patchSet().id().get() < ps.id().get()) {
                ret = Base.create(cd.notes(), ps);
            }
        }
    }
    return ret;
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 79 with ChangeNotes

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

the class SetPrivateOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws ResourceConflictException, BadRequestException {
    change = ctx.getChange();
    if (ctx.getChange().isPrivate() == isPrivate) {
        // No-op
        isNoOp = true;
        return false;
    }
    if (isPrivate && !change.isNew()) {
        throw new BadRequestException(String.format("cannot set %s change to private", ChangeUtil.status(change)));
    }
    ChangeNotes notes = ctx.getNotes();
    ps = psUtil.get(notes, change.currentPatchSetId());
    ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId());
    change.setPrivate(isPrivate);
    change.setLastUpdatedOn(ctx.getWhen());
    update.setPrivate(isPrivate);
    addMessage(ctx);
    return true;
}
Also used : BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 80 with ChangeNotes

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

the class IndexChanges method apply.

@Override
public Response<String> apply(ConfigResource resource, Input input) {
    if (input == null || input.changes == null) {
        return Response.ok("Nothing to index");
    }
    for (String id : input.changes) {
        for (ChangeNotes n : changeFinder.find(id)) {
            indexer.index(changeDataFactory.create(n));
            logger.atFine().log("Indexed change %s", id);
        }
    }
    return Response.ok("Indexed changes " + input.changes);
}
Also used : ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes)

Aggregations

ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)134 Test (org.junit.Test)54 Change (com.google.gerrit.entities.Change)47 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)43 PatchSet (com.google.gerrit.entities.PatchSet)42 ObjectId (org.eclipse.jgit.lib.ObjectId)33 StorageException (com.google.gerrit.exceptions.StorageException)22 Change (com.google.gerrit.reviewdb.client.Change)21 Project (com.google.gerrit.entities.Project)17 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)16 FixInput (com.google.gerrit.extensions.api.changes.FixInput)16 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)14 HumanComment (com.google.gerrit.entities.HumanComment)13 TestChanges.newPatchSet (com.google.gerrit.testing.TestChanges.newPatchSet)12 IOException (java.io.IOException)12 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)10 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)10 AuthException (com.google.gerrit.extensions.restapi.AuthException)9