Search in sources :

Example 21 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class MergeOp method getRevisions.

private SetMultimap<ObjectId, PatchSet.Id> getRevisions(OpenRepo or, Collection<ChangeData> cds) throws IntegrationException {
    try {
        List<String> refNames = new ArrayList<>(cds.size());
        for (ChangeData cd : cds) {
            Change c = cd.change();
            if (c != null) {
                refNames.add(c.currentPatchSetId().toRefName());
            }
        }
        SetMultimap<ObjectId, PatchSet.Id> revisions = MultimapBuilder.hashKeys(cds.size()).hashSetValues(1).build();
        for (Map.Entry<String, Ref> e : or.repo.getRefDatabase().exactRef(refNames.toArray(new String[refNames.size()])).entrySet()) {
            revisions.put(e.getValue().getObjectId(), PatchSet.Id.fromRef(e.getKey()));
        }
        return revisions;
    } catch (IOException | OrmException e) {
        throw new IntegrationException("Failed to validate changes", e);
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) ChangeData(com.google.gerrit.server.query.change.ChangeData) Ref(org.eclipse.jgit.lib.Ref) OrmException(com.google.gwtorm.server.OrmException) RequestId(com.google.gerrit.server.util.RequestId) ObjectId(org.eclipse.jgit.lib.ObjectId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 22 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class MergeOp method validateChangeList.

private BranchBatch validateChangeList(OpenRepo or, Collection<ChangeData> submitted) throws IntegrationException {
    logDebug("Validating {} changes", submitted.size());
    Set<CodeReviewCommit> toSubmit = new LinkedHashSet<>(submitted.size());
    SetMultimap<ObjectId, PatchSet.Id> revisions = getRevisions(or, submitted);
    SubmitType submitType = null;
    ChangeData choseSubmitTypeFrom = null;
    for (ChangeData cd : submitted) {
        Change.Id changeId = cd.getId();
        ChangeControl ctl;
        Change chg;
        try {
            ctl = cd.changeControl();
            chg = cd.change();
        } catch (OrmException e) {
            commitStatus.logProblem(changeId, e);
            continue;
        }
        SubmitType st = getSubmitType(cd);
        if (st == null) {
            commitStatus.logProblem(changeId, "No submit type for change");
            continue;
        }
        if (submitType == null) {
            submitType = st;
            choseSubmitTypeFrom = cd;
        } else if (st != submitType) {
            commitStatus.problem(changeId, String.format("Change has submit type %s, but previously chose submit type %s " + "from change %s in the same batch", st, submitType, choseSubmitTypeFrom.getId()));
            continue;
        }
        if (chg.currentPatchSetId() == null) {
            String msg = "Missing current patch set on change";
            logError(msg + " " + changeId);
            commitStatus.problem(changeId, msg);
            continue;
        }
        PatchSet ps;
        Branch.NameKey destBranch = chg.getDest();
        try {
            ps = cd.currentPatchSet();
        } catch (OrmException e) {
            commitStatus.logProblem(changeId, e);
            continue;
        }
        if (ps == null || ps.getRevision() == null || ps.getRevision().get() == null) {
            commitStatus.logProblem(changeId, "Missing patch set or revision on change");
            continue;
        }
        String idstr = ps.getRevision().get();
        ObjectId id;
        try {
            id = ObjectId.fromString(idstr);
        } catch (IllegalArgumentException e) {
            commitStatus.logProblem(changeId, e);
            continue;
        }
        if (!revisions.containsEntry(id, ps.getId())) {
            // TODO this is actually an error, the branch is gone but we
            // want to merge the issue. We can't safely do that if the
            // tip is not reachable.
            //
            commitStatus.logProblem(changeId, "Revision " + idstr + " of patch set " + ps.getPatchSetId() + " does not match " + ps.getId().toRefName() + " for change");
            continue;
        }
        CodeReviewCommit commit;
        try {
            commit = or.rw.parseCommit(id);
        } catch (IOException e) {
            commitStatus.logProblem(changeId, e);
            continue;
        }
        // TODO(dborowitz): Consider putting ChangeData in CodeReviewCommit.
        commit.setControl(ctl);
        commit.setPatchsetId(ps.getId());
        commitStatus.put(commit);
        MergeValidators mergeValidators = mergeValidatorsFactory.create();
        try {
            mergeValidators.validatePreMerge(or.repo, commit, or.project, destBranch, ps.getId(), caller);
        } catch (MergeValidationException mve) {
            commitStatus.problem(changeId, mve.getMessage());
            continue;
        }
        commit.add(or.canMergeFlag);
        toSubmit.add(commit);
    }
    logDebug("Submitting on this run: {}", toSubmit);
    return new AutoValue_MergeOp_BranchBatch(submitType, toSubmit);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) ChangeData(com.google.gerrit.server.query.change.ChangeData) MergeValidationException(com.google.gerrit.server.git.validators.MergeValidationException) OrmException(com.google.gwtorm.server.OrmException) ChangeControl(com.google.gerrit.server.project.ChangeControl) Branch(com.google.gerrit.reviewdb.client.Branch) OpenBranch(com.google.gerrit.server.git.MergeOpRepoManager.OpenBranch) SubmitType(com.google.gerrit.extensions.client.SubmitType) RequestId(com.google.gerrit.server.util.RequestId) ObjectId(org.eclipse.jgit.lib.ObjectId) MergeValidators(com.google.gerrit.server.git.validators.MergeValidators)

Example 23 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData 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 24 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class MergeSuperSet method byCommitsOnBranchNotMerged.

private List<ChangeData> byCommitsOnBranchNotMerged(OpenRepo or, ReviewDb db, CurrentUser user, Branch.NameKey branch, Set<String> hashes) throws OrmException, IOException {
    if (hashes.isEmpty()) {
        return ImmutableList.of();
    }
    QueryKey k = QueryKey.create(branch, hashes);
    List<ChangeData> cached = queryCache.get(k);
    if (cached != null) {
        return cached;
    }
    List<ChangeData> result = new ArrayList<>();
    Iterable<ChangeData> destChanges = query().byCommitsOnBranchNotMerged(or.repo, db, branch, hashes);
    for (ChangeData chd : destChanges) {
        chd.changeControl(user);
        result.add(chd);
    }
    queryCache.put(k, result);
    return result;
}
Also used : ArrayList(java.util.ArrayList) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 25 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class MergeSuperSet method topicClosure.

/**
   * Completes {@code cs} with any additional changes from its topics
   *
   * <p>{@link #completeChangeSetIncludingTopics} calls this repeatedly, alternating with {@link
   * #completeChangeSetWithoutTopic}, to discover what additional changes should be submitted with a
   * change until the set stops growing.
   *
   * <p>{@code topicsSeen} and {@code visibleTopicsSeen} keep track of topics already explored to
   * avoid wasted work.
   *
   * @return the resulting larger {@link ChangeSet}
   */
private ChangeSet topicClosure(ReviewDb db, ChangeSet cs, CurrentUser user, Set<String> topicsSeen, Set<String> visibleTopicsSeen) throws OrmException {
    List<ChangeData> visibleChanges = new ArrayList<>();
    List<ChangeData> nonVisibleChanges = new ArrayList<>();
    for (ChangeData cd : cs.changes()) {
        visibleChanges.add(cd);
        String topic = cd.change().getTopic();
        if (Strings.isNullOrEmpty(topic) || visibleTopicsSeen.contains(topic)) {
            continue;
        }
        for (ChangeData topicCd : query().byTopicOpen(topic)) {
            try {
                topicCd.changeControl(user);
                if (topicCd.changeControl().isVisible(db, topicCd)) {
                    visibleChanges.add(topicCd);
                } else {
                    nonVisibleChanges.add(topicCd);
                }
            } catch (OrmException e) {
                if (e.getCause() instanceof NoSuchChangeException) {
                // Ignore and skip this change
                } else {
                    throw e;
                }
            }
        }
        topicsSeen.add(topic);
        visibleTopicsSeen.add(topic);
    }
    for (ChangeData cd : cs.nonVisibleChanges()) {
        nonVisibleChanges.add(cd);
        String topic = cd.change().getTopic();
        if (Strings.isNullOrEmpty(topic) || topicsSeen.contains(topic)) {
            continue;
        }
        for (ChangeData topicCd : query().byTopicOpen(topic)) {
            topicCd.changeControl(user);
            nonVisibleChanges.add(topicCd);
        }
        topicsSeen.add(topic);
    }
    return new ChangeSet(visibleChanges, nonVisibleChanges);
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) ArrayList(java.util.ArrayList) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Aggregations

ChangeData (com.google.gerrit.server.query.change.ChangeData)99 Test (org.junit.Test)32 RevCommit (org.eclipse.jgit.revwalk.RevCommit)31 Change (com.google.gerrit.reviewdb.client.Change)26 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)25 OrmException (com.google.gwtorm.server.OrmException)24 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)21 ObjectId (org.eclipse.jgit.lib.ObjectId)18 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)16 ArrayList (java.util.ArrayList)16 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)12 IOException (java.io.IOException)12 Repo (com.google.gerrit.testutil.InMemoryRepositoryManager.Repo)11 Repository (org.eclipse.jgit.lib.Repository)11 Project (com.google.gerrit.reviewdb.client.Project)10 RevWalk (org.eclipse.jgit.revwalk.RevWalk)10 Ref (org.eclipse.jgit.lib.Ref)9 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)8 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)8 RevId (com.google.gerrit.reviewdb.client.RevId)8