Search in sources :

Example 61 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class RelatedChangesSorter method sort.

public List<PatchSetData> sort(List<ChangeData> in, PatchSet startPs) throws IOException, PermissionBackendException {
    checkArgument(!in.isEmpty(), "Input may not be empty");
    // Map of all patch sets, keyed by commit SHA-1.
    Map<ObjectId, PatchSetData> byId = collectById(in);
    PatchSetData start = byId.get(startPs.commitId());
    requireNonNull(start, () -> String.format("commit %s of patch set %s not found in %s", startPs.commitId().name(), startPs.id(), byId.entrySet().stream().collect(toMap(e -> e.getKey().name(), e -> e.getValue().patchSet().id()))));
    // Map of patch set -> immediate parent.
    ListMultimap<PatchSetData, PatchSetData> parents = MultimapBuilder.hashKeys(in.size()).arrayListValues(3).build();
    // Map of patch set -> immediate children.
    ListMultimap<PatchSetData, PatchSetData> children = MultimapBuilder.hashKeys(in.size()).arrayListValues(3).build();
    // All other patch sets of the same change as startPs.
    List<PatchSetData> otherPatchSetsOfStart = new ArrayList<>();
    for (ChangeData cd : in) {
        for (PatchSet ps : cd.patchSets()) {
            PatchSetData thisPsd = requireNonNull(byId.get(ps.commitId()));
            if (cd.getId().equals(start.id()) && !ps.id().equals(start.psId())) {
                otherPatchSetsOfStart.add(thisPsd);
            }
            for (RevCommit p : thisPsd.commit().getParents()) {
                PatchSetData parentPsd = byId.get(p);
                if (parentPsd != null) {
                    parents.put(thisPsd, parentPsd);
                    children.put(parentPsd, thisPsd);
                }
            }
        }
    }
    Collection<PatchSetData> ancestors = walkAncestors(parents, start);
    List<PatchSetData> descendants = walkDescendants(children, start, otherPatchSetsOfStart, ancestors);
    List<PatchSetData> result = new ArrayList<>(ancestors.size() + descendants.size() - 1);
    result.addAll(Lists.reverse(descendants));
    result.addAll(ancestors);
    return result;
}
Also used : PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ListMultimap(com.google.common.collect.ListMultimap) ProjectCache(com.google.gerrit.server.project.ProjectCache) MultimapBuilder(com.google.common.collect.MultimapBuilder) Inject(com.google.inject.Inject) ChangePermission(com.google.gerrit.server.permissions.ChangePermission) HashMap(java.util.HashMap) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) Deque(java.util.Deque) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) Collectors.toMap(java.util.stream.Collectors.toMap) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) LinkedHashSet(java.util.LinkedHashSet) Memoized(com.google.auto.value.extension.memoized.Memoized) Collection(java.util.Collection) ProjectState(com.google.gerrit.server.project.ProjectState) Set(java.util.Set) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) ObjectId(org.eclipse.jgit.lib.ObjectId) Objects(java.util.Objects) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) AutoValue(com.google.auto.value.AutoValue) Project(com.google.gerrit.entities.Project) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ArrayDeque(java.util.ArrayDeque) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.entities.PatchSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 62 with PatchSet

use of com.google.gerrit.entities.PatchSet 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 63 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ReviewerJson method format.

public ReviewerInfo format(ReviewerInfo out, Account.Id reviewerAccountId, ChangeData cd, Iterable<PatchSetApproval> approvals) throws PermissionBackendException {
    LabelTypes labelTypes = cd.getLabelTypes();
    out.approvals = new TreeMap<>(labelTypes.nameComparator());
    for (PatchSetApproval ca : approvals) {
        Optional<LabelType> at = labelTypes.byLabel(ca.labelId());
        at.ifPresent(lt -> out.approvals.put(lt.getName(), formatValue(ca.value())));
    }
    // Add dummy approvals for all permitted labels for the user even if they
    // do not exist in the DB.
    PatchSet ps = cd.currentPatchSet();
    if (ps != null) {
        PermissionBackend.ForChange perm = permissionBackend.absentUser(reviewerAccountId).change(cd);
        for (SubmitRecord rec : cd.submitRecords(SubmitRuleOptions.defaults())) {
            if (rec.labels == null) {
                continue;
            }
            for (SubmitRecord.Label label : rec.labels) {
                String name = label.label;
                Optional<LabelType> type = labelTypes.byLabel(name);
                if (out.approvals.containsKey(name) || !type.isPresent()) {
                    continue;
                }
                if (perm.test(new LabelPermission(type.get()))) {
                    out.approvals.put(name, formatValue((short) 0));
                }
            }
        }
    }
    if (out.approvals.isEmpty()) {
        out.approvals = null;
    }
    return out;
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) LabelTypes(com.google.gerrit.entities.LabelTypes) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) LabelType(com.google.gerrit.entities.LabelType) PatchSet(com.google.gerrit.entities.PatchSet) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Example 64 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class RevisionJson method getRevisions.

/**
 * Returns multiple {@link RevisionInfo}s for a single change. Uses the provided {@link
 * AccountLoader} to lazily populate accounts. Callers have to call {@link AccountLoader#fill()}
 * afterwards to populate all accounts in the returned {@link RevisionInfo}s.
 */
Map<String, RevisionInfo> getRevisions(AccountLoader accountLoader, ChangeData cd, Map<PatchSet.Id, PatchSet> map, Optional<PatchSet.Id> limitToPsId, ChangeInfo changeInfo) throws PatchListNotAvailableException, GpgException, IOException, PermissionBackendException {
    Map<String, RevisionInfo> res = new LinkedHashMap<>();
    try (Repository repo = openRepoIfNecessary(cd.project());
        RevWalk rw = newRevWalk(repo)) {
        for (PatchSet in : map.values()) {
            PatchSet.Id id = in.id();
            boolean want;
            if (has(ALL_REVISIONS)) {
                want = true;
            } else if (limitToPsId.isPresent()) {
                want = id.equals(limitToPsId.get());
            } else {
                want = id.equals(cd.change().currentPatchSetId());
            }
            if (want) {
                res.put(in.commitId().name(), toRevisionInfo(accountLoader, cd, in, repo, rw, false, changeInfo));
            }
        }
        return res;
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) PatchSet(com.google.gerrit.entities.PatchSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) LinkedHashMap(java.util.LinkedHashMap)

Example 65 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ProjectsConsistencyChecker method executeQueryAndAutoCloseChanges.

private ImmutableList<ChangeInfo> executeQueryAndAutoCloseChanges(Predicate<ChangeData> basePredicate, Set<Change.Id> seenChanges, List<Predicate<ChangeData>> predicates, boolean fix, Map<Change.Key, ObjectId> changeIdToMergedSha1, List<ObjectId> mergedSha1s) {
    if (predicates.isEmpty()) {
        return ImmutableList.of();
    }
    try {
        List<ChangeData> queryResult = retryHelper.changeIndexQuery("projectsConsistencyCheckerQueryChanges", q -> q.setRequestedFields(ChangeField.CHANGE, ChangeField.PATCH_SET).query(and(basePredicate, or(predicates)))).call();
        // Result for this query that we want to return to the client.
        ImmutableList.Builder<ChangeInfo> autoCloseableChangesByBranch = ImmutableList.builder();
        for (ChangeData autoCloseableChange : queryResult) {
            // earlier queries.
            if (seenChanges.add(autoCloseableChange.getId())) {
                retryHelper.changeUpdate("projectsConsistencyCheckerAutoCloseChanges", () -> {
                    // Auto-close by change
                    if (changeIdToMergedSha1.containsKey(autoCloseableChange.change().getKey())) {
                        autoCloseableChangesByBranch.add(changeJson(fix, changeIdToMergedSha1.get(autoCloseableChange.change().getKey())).format(autoCloseableChange));
                        return null;
                    }
                    // Auto-close by commit
                    for (ObjectId patchSetSha1 : autoCloseableChange.patchSets().stream().map(PatchSet::commitId).collect(toSet())) {
                        if (mergedSha1s.contains(patchSetSha1)) {
                            autoCloseableChangesByBranch.add(changeJson(fix, patchSetSha1).format(autoCloseableChange));
                            break;
                        }
                    }
                    return null;
                }).call();
            }
        }
        return autoCloseableChangesByBranch.build();
    } catch (Exception e) {
        Throwables.throwIfUnchecked(e);
        throw new StorageException(e);
    }
}
Also used : DynamicItem(com.google.gerrit.extensions.registration.DynamicItem) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ChangePredicates(com.google.gerrit.server.query.change.ChangePredicates) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CheckProjectResultInfo(com.google.gerrit.extensions.api.projects.CheckProjectResultInfo) Map(java.util.Map) RefNames(com.google.gerrit.entities.RefNames) RetryHelper(com.google.gerrit.server.update.RetryHelper) UrlFormatter(com.google.gerrit.server.config.UrlFormatter) Predicate.or(com.google.gerrit.index.query.Predicate.or) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Ref(org.eclipse.jgit.lib.Ref) ChangeJson(com.google.gerrit.server.change.ChangeJson) Singleton(com.google.inject.Singleton) AutoCloseableChangesCheckResult(com.google.gerrit.extensions.api.projects.CheckProjectResultInfo.AutoCloseableChangesCheckResult) IndexConfig(com.google.gerrit.index.IndexConfig) RevCommit(org.eclipse.jgit.revwalk.RevCommit) FixInput(com.google.gerrit.extensions.api.changes.FixInput) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) AutoCloseableChangesCheckInput(com.google.gerrit.extensions.api.projects.CheckProjectInput.AutoCloseableChangesCheckInput) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) Predicate(com.google.gerrit.index.query.Predicate) ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ChangeStatusPredicate.open(com.google.gerrit.server.query.change.ChangeStatusPredicate.open) StorageException(com.google.gerrit.exceptions.StorageException) Predicate.and(com.google.gerrit.index.query.Predicate.and) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeField(com.google.gerrit.server.index.change.ChangeField) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Project(com.google.gerrit.entities.Project) CheckProjectInput(com.google.gerrit.extensions.api.projects.CheckProjectInput) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Repository(org.eclipse.jgit.lib.Repository) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) ImmutableList(com.google.common.collect.ImmutableList) PatchSet(com.google.gerrit.entities.PatchSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) StorageException(com.google.gerrit.exceptions.StorageException) IOException(java.io.IOException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException)

Aggregations

PatchSet (com.google.gerrit.entities.PatchSet)123 Change (com.google.gerrit.entities.Change)61 Test (org.junit.Test)48 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)41 ObjectId (org.eclipse.jgit.lib.ObjectId)35 RevCommit (org.eclipse.jgit.revwalk.RevCommit)29 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)28 Project (com.google.gerrit.entities.Project)25 StorageException (com.google.gerrit.exceptions.StorageException)25 Repository (org.eclipse.jgit.lib.Repository)22 IOException (java.io.IOException)20 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)19 ChangeData (com.google.gerrit.server.query.change.ChangeData)18 HumanComment (com.google.gerrit.entities.HumanComment)16 RevWalk (org.eclipse.jgit.revwalk.RevWalk)16 Inject (com.google.inject.Inject)14 Map (java.util.Map)14 List (java.util.List)13 ImmutableList (com.google.common.collect.ImmutableList)12 AuthException (com.google.gerrit.extensions.restapi.AuthException)11