Search in sources :

Example 21 with ChangeInfo

use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.

the class ChangesImpl method get.

private List<ChangeInfo> get(QueryRequest q) throws RestApiException {
    try (DynamicOptions dynamicOptions = new DynamicOptions(injector, dynamicBeans)) {
        QueryChanges qc = queryProvider.get();
        if (q.getQuery() != null) {
            qc.addQuery(q.getQuery());
        }
        qc.setLimit(q.getLimit());
        qc.setStart(q.getStart());
        qc.setNoLimit(q.getNoLimit());
        for (ListChangesOption option : q.getOptions()) {
            qc.addOption(option);
        }
        dynamicOptionParser.parseDynamicOptions(qc, q.getPluginOptions(), dynamicOptions);
        try {
            List<?> result = qc.apply(TopLevelResource.INSTANCE).value();
            if (result.isEmpty()) {
                return ImmutableList.of();
            }
            // Check type safety of result; the extension API should be safer than the
            // REST API in this case, since it's intended to be used in Java.
            Object first = requireNonNull(result.iterator().next());
            checkState(first instanceof ChangeInfo);
            @SuppressWarnings("unchecked") List<ChangeInfo> infos = (List<ChangeInfo>) result;
            return ImmutableList.copyOf(infos);
        } catch (Exception e) {
            throw asRestApiException("Cannot query changes", e);
        }
    }
}
Also used : DynamicOptions(com.google.gerrit.server.DynamicOptions) ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 22 with ChangeInfo

use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.

the class ChangeKindCreator method cherryPick.

/**
 * Creates a cherry pick of the provided change with the given {@link ChangeKind} and returns the
 * change id.
 */
public String cherryPick(String changeId, ChangeKind changeKind, TestRepository<InMemoryRepository> testRepo, TestAccount user, Project.NameKey project) throws Exception {
    switch(changeKind) {
        case REWORK:
        case TRIVIAL_REBASE:
            break;
        case NO_CODE_CHANGE:
        case NO_CHANGE:
        case MERGE_FIRST_PARENT_UPDATE:
        default:
            assertWithMessage("unexpected change kind: " + changeKind).fail();
    }
    testRepo.reset(projectOperations.project(project).getHead("master"));
    PushOneCommit.Result r = pushFactory.create(user.newIdent(), testRepo, PushOneCommit.SUBJECT, "other.txt", "new content " + System.nanoTime()).to("refs/for/master");
    r.assertOkStatus();
    vote(user, r.getChangeId(), 2, 1);
    merge(r);
    String subject = ChangeKind.TRIVIAL_REBASE.equals(changeKind) ? PushOneCommit.SUBJECT : "Reworked change " + System.nanoTime();
    CherryPickInput in = new CherryPickInput();
    in.destination = "master";
    in.message = String.format("%s\n\nChange-Id: %s", subject, changeId);
    ChangeInfo c = gApi.changes().id(changeId).current().cherryPick(in).get();
    return c.changeId;
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) CherryPickInput(com.google.gerrit.extensions.api.changes.CherryPickInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit)

Example 23 with ChangeInfo

use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.

the class ChangeKindCreator method updateSecondParent.

/**
 * Update the second parent of a merge.
 */
public void updateSecondParent(String changeId, TestRepository<InMemoryRepository> testRepo, TestAccount user) throws Exception {
    ChangeInfo c = detailedChange(changeId);
    List<CommitInfo> parents = c.revisions.get(c.currentRevision).commit.parents;
    String parent1 = parents.get(0).commit;
    String parent2 = parents.get(1).commit;
    RevCommit commitParent1 = testRepo.getRevWalk().parseCommit(ObjectId.fromString(parent1));
    testRepo.reset(parent2);
    PushOneCommit.Result newParent2 = createChange("new parent 2", "p2-2.txt", "content 2-2", testRepo, user);
    PushOneCommit merge = pushFactory.create(user.newIdent(), testRepo, changeId);
    merge.setParents(ImmutableList.of(commitParent1, newParent2.getCommit()));
    PushOneCommit.Result result = merge.to("refs/for/master");
    result.assertOkStatus();
    assertThat(getChangeKind(changeId)).isEqualTo(ChangeKind.REWORK);
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) CommitInfo(com.google.gerrit.extensions.common.CommitInfo) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 24 with ChangeInfo

use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.

the class ChangeJson method format.

private ChangeInfo format(ChangeData cd, Optional<PatchSet.Id> limitToPsId, boolean fillAccountLoader, List<PluginDefinedInfo> pluginInfosForChange) {
    try {
        if (fillAccountLoader) {
            accountLoader = accountLoaderFactory.create(has(DETAILED_ACCOUNTS));
            ChangeInfo res = toChangeInfo(cd, limitToPsId, pluginInfosForChange);
            accountLoader.fill();
            return res;
        }
        return toChangeInfo(cd, limitToPsId, pluginInfosForChange);
    } catch (PatchListNotAvailableException | GpgException | IOException | PermissionBackendException | RuntimeException e) {
        if (!has(CHECK)) {
            Throwables.throwIfInstanceOf(e, StorageException.class);
            throw new StorageException(e);
        }
        return checkOnly(cd);
    }
}
Also used : GpgException(com.google.gerrit.server.GpgException) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) PatchListNotAvailableException(com.google.gerrit.server.patch.PatchListNotAvailableException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 25 with ChangeInfo

use of com.google.gerrit.extensions.common.ChangeInfo in project gerrit by GerritCodeReview.

the class ChangeJson method toChangeInfos.

private List<ChangeInfo> toChangeInfos(List<ChangeData> changes, Map<Change.Id, ChangeInfo> cache, ImmutableListMultimap<Change.Id, PluginDefinedInfo> pluginInfosByChange) {
    try (Timer0.Context ignored = metrics.toChangeInfosLatency.start()) {
        List<ChangeInfo> changeInfos = new ArrayList<>(changes.size());
        for (int i = 0; i < changes.size(); i++) {
            // We can only cache and re-use an entity if it's not the last in the list. The last entity
            // may later get _moreChanges set. If it was cached or re-used, that setting would propagate
            // to the original entity yielding wrong results.
            // This problem has two sides where 'last in the list' has to be respected:
            // (1) Caching
            // (2) Reusing
            boolean isCacheable = i != changes.size() - 1;
            ChangeData cd = changes.get(i);
            ChangeInfo info = cache.get(cd.getId());
            if (info != null && isCacheable) {
                changeInfos.add(info);
                continue;
            }
            // Compute and cache if possible
            try {
                ensureLoaded(Collections.singleton(cd));
                info = format(cd, Optional.empty(), false, pluginInfosByChange.get(cd.getId()));
                changeInfos.add(info);
                if (isCacheable) {
                    cache.put(Change.id(info._number), info);
                }
            } catch (RuntimeException e) {
                Optional<RequestCancelledException> requestCancelledException = RequestCancelledException.getFromCausalChain(e);
                if (requestCancelledException.isPresent()) {
                    throw e;
                }
                logger.atWarning().withCause(e).log("Omitting corrupt change %s from results", cd.getId());
            }
        }
        return changeInfos;
    }
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Optional(java.util.Optional) ArrayList(java.util.ArrayList) Timer0(com.google.gerrit.metrics.Timer0) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Aggregations

ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)504 Test (org.junit.Test)434 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)393 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)276 Change (com.google.gerrit.entities.Change)73 RevCommit (org.eclipse.jgit.revwalk.RevCommit)67 ChangeMessageInfo (com.google.gerrit.extensions.common.ChangeMessageInfo)52 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)50 Project (com.google.gerrit.entities.Project)45 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)45 ChangeInput (com.google.gerrit.extensions.common.ChangeInput)36 RevisionInfo (com.google.gerrit.extensions.common.RevisionInfo)36 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)35 Repository (org.eclipse.jgit.lib.Repository)35 CommitInfo (com.google.gerrit.extensions.common.CommitInfo)32 LabelInfo (com.google.gerrit.extensions.common.LabelInfo)32 TestRepository (org.eclipse.jgit.junit.TestRepository)32 ObjectId (org.eclipse.jgit.lib.ObjectId)30 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)29 List (java.util.List)29