Search in sources :

Example 31 with ChangeData

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

the class ChangeJson method ensureLoaded.

private void ensureLoaded(Iterable<ChangeData> all) throws OrmException {
    if (lazyLoad) {
        ChangeData.ensureChangeLoaded(all);
        if (has(ALL_REVISIONS)) {
            ChangeData.ensureAllPatchSetsLoaded(all);
        } else if (has(CURRENT_REVISION) || has(MESSAGES)) {
            ChangeData.ensureCurrentPatchSetLoaded(all);
        }
        if (has(REVIEWED) && userProvider.get().isIdentifiedUser()) {
            ChangeData.ensureReviewedByLoadedForOpenChanges(all);
        }
        ChangeData.ensureCurrentApprovalsLoaded(all);
    } else {
        for (ChangeData cd : all) {
            cd.setLazyLoad(false);
        }
    }
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 32 with ChangeData

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

the class AbandonUtil method getValidChanges.

private Collection<ChangeControl> getValidChanges(Collection<ChangeControl> changeControls, String query) throws OrmException, QueryParseException {
    Collection<ChangeControl> validChanges = new ArrayList<>();
    for (ChangeControl cc : changeControls) {
        String newQuery = query + " change:" + cc.getId();
        List<ChangeData> changesToAbandon = queryProcessor.enforceVisibility(false).query(queryBuilder.parse(newQuery)).entities();
        if (!changesToAbandon.isEmpty()) {
            validChanges.add(cc);
        } else {
            log.debug("Change data with id \"{}\" does not satisfy the query \"{}\"" + " any more, hence skipping it in clean up", cc.getId(), query);
        }
    }
    return validChanges;
}
Also used : ChangeControl(com.google.gerrit.server.project.ChangeControl) ArrayList(java.util.ArrayList) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 33 with ChangeData

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

the class AbandonUtil method abandonInactiveOpenChanges.

public void abandonInactiveOpenChanges(BatchUpdate.Factory updateFactory) {
    if (cfg.getAbandonAfter() <= 0) {
        return;
    }
    try {
        String query = "status:new age:" + TimeUnit.MILLISECONDS.toMinutes(cfg.getAbandonAfter()) + "m";
        if (!cfg.getAbandonIfMergeable()) {
            query += " -is:mergeable";
        }
        List<ChangeData> changesToAbandon = queryProcessor.enforceVisibility(false).query(queryBuilder.parse(query)).entities();
        ImmutableListMultimap.Builder<Project.NameKey, ChangeControl> builder = ImmutableListMultimap.builder();
        for (ChangeData cd : changesToAbandon) {
            ChangeControl control = cd.changeControl(internalUser);
            builder.put(control.getProject().getNameKey(), control);
        }
        int count = 0;
        ListMultimap<Project.NameKey, ChangeControl> abandons = builder.build();
        String message = cfg.getAbandonMessage();
        for (Project.NameKey project : abandons.keySet()) {
            Collection<ChangeControl> changes = getValidChanges(abandons.get(project), query);
            try {
                abandon.batchAbandon(updateFactory, project, internalUser, changes, message);
                count += changes.size();
            } catch (Throwable e) {
                StringBuilder msg = new StringBuilder("Failed to auto-abandon inactive change(s):");
                for (ChangeControl change : changes) {
                    msg.append(" ").append(change.getId().get());
                }
                msg.append(".");
                log.error(msg.toString(), e);
            }
        }
        log.info(String.format("Auto-Abandoned %d of %d changes.", count, changesToAbandon.size()));
    } catch (QueryParseException | OrmException e) {
        log.error("Failed to query inactive open changes for auto-abandoning.", e);
    }
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData) QueryParseException(com.google.gerrit.server.query.QueryParseException) Project(com.google.gerrit.reviewdb.client.Project) OrmException(com.google.gwtorm.server.OrmException) ChangeControl(com.google.gerrit.server.project.ChangeControl) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Example 34 with ChangeData

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

the class ChangeIdHandler method parseArguments.

@Override
public final int parseArguments(final Parameters params) throws CmdLineException {
    final String token = params.getParameter(0);
    final String[] tokens = token.split(",");
    if (tokens.length != 3) {
        throw new CmdLineException(owner, "change should be specified as <project>,<branch>,<change-id>");
    }
    try {
        final Change.Key key = Change.Key.parse(tokens[2]);
        final Project.NameKey project = new Project.NameKey(tokens[0]);
        final Branch.NameKey branch = new Branch.NameKey(project, tokens[1]);
        for (final ChangeData cd : queryProvider.get().byBranchKey(branch, key)) {
            setter.addValue(cd.getId());
            return 1;
        }
    } catch (IllegalArgumentException e) {
        throw new CmdLineException(owner, "Change-Id is not valid");
    } catch (OrmException e) {
        throw new CmdLineException(owner, "Database error: " + e.getMessage());
    }
    throw new CmdLineException(owner, "\"" + token + "\": change not found");
}
Also used : Change(com.google.gerrit.reviewdb.client.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) Project(com.google.gerrit.reviewdb.client.Project) OrmException(com.google.gwtorm.server.OrmException) Branch(com.google.gerrit.reviewdb.client.Branch) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 35 with ChangeData

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

the class WalkSorterTest method subsetOfSeriesOfChangesWithReverseTimestamps.

@Test
public void subsetOfSeriesOfChangesWithReverseTimestamps() throws Exception {
    TestRepository<Repo> p = newRepo("p");
    RevCommit c0 = p.commit().tick(-1).create();
    RevCommit c1 = p.commit().tick(-1).parent(c0).create();
    RevCommit c2 = p.commit().tick(-1).parent(c1).create();
    RevCommit c3 = p.commit().tick(-1).parent(c2).create();
    RevCommit c4 = p.commit().tick(-1).parent(c3).create();
    RevWalk rw = p.getRevWalk();
    rw.parseCommit(c1);
    assertThat(rw.parseCommit(c2).getCommitTime()).isLessThan(c1.getCommitTime());
    assertThat(rw.parseCommit(c3).getCommitTime()).isLessThan(c2.getCommitTime());
    assertThat(rw.parseCommit(c4).getCommitTime()).isLessThan(c3.getCommitTime());
    ChangeData cd1 = newChange(p, c1);
    ChangeData cd2 = newChange(p, c2);
    ChangeData cd4 = newChange(p, c4);
    List<ChangeData> changes = ImmutableList.of(cd1, cd2, cd4);
    WalkSorter sorter = new WalkSorter(repoManager);
    List<PatchSetData> expected = ImmutableList.of(patchSetData(cd4, c4), patchSetData(cd2, c2), patchSetData(cd1, c1));
    for (List<ChangeData> list : permutations(changes)) {
        // Not inOrder(); since child of c2 is missing, partial topo sort isn't
        // guaranteed to work.
        assertThat(sorter.sort(list)).containsExactlyElementsIn(expected);
    }
}
Also used : PatchSetData(com.google.gerrit.server.change.WalkSorter.PatchSetData) Repo(com.google.gerrit.testutil.InMemoryRepositoryManager.Repo) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

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