Search in sources :

Example 1 with ChangeSet

use of com.google.gerrit.server.submit.ChangeSet in project gerrit by GerritCodeReview.

the class SubmitResolvingMergeCommitIT method assertChangeSetMergeable.

private void assertChangeSetMergeable(ChangeData change, boolean expected) throws MissingObjectException, IncorrectObjectTypeException, IOException, PermissionBackendException {
    ChangeSet cs = mergeSuperSet.get().completeChangeSet(change.change(), user(admin), /* includingTopicClosure= */
    false);
    assertThat(submit.unmergeableChanges(cs).isEmpty()).isEqualTo(expected);
}
Also used : ChangeSet(com.google.gerrit.server.submit.ChangeSet)

Example 2 with ChangeSet

use of com.google.gerrit.server.submit.ChangeSet in project gerrit by GerritCodeReview.

the class Submit method unmergeableChanges.

public Collection<ChangeData> unmergeableChanges(ChangeSet cs) throws IOException {
    Set<ChangeData> mergeabilityMap = new HashSet<>();
    Set<ObjectId> outDatedPatchsets = new HashSet<>();
    for (ChangeData change : cs.changes()) {
        mergeabilityMap.add(change);
        // Add all the patchsets commit ids except the current patchset.
        outDatedPatchsets.addAll(change.notes().getPatchSets().values().stream().map(p -> p.commitId()).collect(Collectors.toSet()));
        outDatedPatchsets.remove(change.currentPatchSet().commitId());
    }
    ListMultimap<BranchNameKey, ChangeData> cbb = cs.changesByBranch();
    for (BranchNameKey branch : cbb.keySet()) {
        Collection<ChangeData> targetBranch = cbb.get(branch);
        HashMap<Change.Id, RevCommit> commits = findCommits(targetBranch, branch.project());
        Set<ObjectId> allParents = Sets.newHashSetWithExpectedSize(cs.size());
        for (RevCommit commit : commits.values()) {
            for (RevCommit parent : commit.getParents()) {
                allParents.add(parent.getId());
            }
        }
        for (ChangeData change : targetBranch) {
            RevCommit commit = commits.get(change.getId());
            boolean isMergeCommit = commit.getParentCount() > 1;
            boolean isLastInChain = !allParents.contains(commit.getId());
            if (Arrays.stream(commit.getParents()).anyMatch(c -> outDatedPatchsets.contains(c.getId())) && !isCherryPickSubmit(change)) {
                // cherry-pick.
                continue;
            }
            // Recheck mergeability rather than using value stored in the index,
            // which may be stale.
            // TODO(dborowitz): This is ugly; consider providing a way to not read
            // stored fields from the index in the first place.
            change.setMergeable(null);
            Boolean mergeable = change.isMergeable();
            if (mergeable == null) {
                // Skip whole check, cannot determine if mergeable
                return null;
            }
            if (mergeable) {
                mergeabilityMap.remove(change);
            }
            if (isLastInChain && isMergeCommit && mergeable) {
                for (ChangeData c : targetBranch) {
                    mergeabilityMap.remove(c);
                }
                break;
            }
        }
    }
    return mergeabilityMap;
}
Also used : Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) ProjectCache(com.google.gerrit.server.project.ProjectCache) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) UpdateException(com.google.gerrit.server.update.UpdateException) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) UiAction(com.google.gerrit.extensions.webui.UiAction) EnumSet(java.util.EnumSet) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) SubmitType(com.google.gerrit.extensions.client.SubmitType) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) BranchNameKey(com.google.gerrit.entities.BranchNameKey) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) ChangeData(com.google.gerrit.server.query.change.ChangeData) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) ChangeJson(com.google.gerrit.server.change.ChangeJson) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ChangePermission(com.google.gerrit.server.permissions.ChangePermission) HashMap(java.util.HashMap) Response(com.google.gerrit.extensions.restapi.Response) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) MergeOp(com.google.gerrit.server.submit.MergeOp) ChangeResource(com.google.gerrit.server.change.ChangeResource) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) UsedAt(com.google.gerrit.common.UsedAt) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) CurrentUser(com.google.gerrit.server.CurrentUser) MergeSuperSet(com.google.gerrit.server.submit.MergeSuperSet) AccountResolver(com.google.gerrit.server.account.AccountResolver) ObjectIds.abbreviateName(com.google.gerrit.git.ObjectIds.abbreviateName) ChangeSet(com.google.gerrit.server.submit.ChangeSet) StorageException(com.google.gerrit.exceptions.StorageException) ProjectState(com.google.gerrit.server.project.ProjectState) MoreObjects(com.google.common.base.MoreObjects) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) RevisionResource(com.google.gerrit.server.change.RevisionResource) ProjectUtil(com.google.gerrit.server.ProjectUtil) Project(com.google.gerrit.entities.Project) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeData(com.google.gerrit.server.query.change.ChangeData) BranchNameKey(com.google.gerrit.entities.BranchNameKey) ObjectId(org.eclipse.jgit.lib.ObjectId) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with ChangeSet

use of com.google.gerrit.server.submit.ChangeSet in project gerrit by GerritCodeReview.

the class SubmittedTogether method applyInfo.

public SubmittedTogetherInfo applyInfo(ChangeResource resource) throws AuthException, IOException, PermissionBackendException {
    Change c = resource.getChange();
    try {
        List<ChangeData> cds;
        int hidden;
        if (c.isNew()) {
            ChangeSet cs = mergeSuperSet.get().completeChangeSet(c, resource.getUser(), options.contains(TOPIC_CLOSURE));
            cds = ensureRequiredDataIsLoaded(cs.changes().asList());
            hidden = cs.nonVisibleChanges().size();
        } else if (c.isMerged()) {
            cds = queryProvider.get().bySubmissionId(c.getSubmissionId());
            hidden = 0;
        } else {
            cds = Collections.emptyList();
            hidden = 0;
        }
        if (hidden != 0 && !options.contains(NON_VISIBLE_CHANGES)) {
            throw new AuthException("change would be submitted with a change that you cannot see");
        }
        cds = sort(cds, hidden);
        SubmittedTogetherInfo info = new SubmittedTogetherInfo();
        info.changes = json.create(jsonOpt).format(cds);
        info.nonVisibleChanges = hidden;
        return info;
    } catch (StorageException | IOException e) {
        logger.atSevere().withCause(e).log("Error on getting a ChangeSet");
        throw e;
    }
}
Also used : AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.entities.Change) SubmittedTogetherInfo(com.google.gerrit.extensions.api.changes.SubmittedTogetherInfo) IOException(java.io.IOException) ChangeData(com.google.gerrit.server.query.change.ChangeData) ChangeSet(com.google.gerrit.server.submit.ChangeSet) StorageException(com.google.gerrit.exceptions.StorageException)

Example 4 with ChangeSet

use of com.google.gerrit.server.submit.ChangeSet in project gerrit by GerritCodeReview.

the class Submit method getDescription.

@Override
public UiAction.Description getDescription(RevisionResource resource) throws IOException, PermissionBackendException {
    Change change = resource.getChange();
    if (!change.isNew() || !resource.isCurrent()) {
        // submit not visible
        return null;
    }
    if (!projectCache.get(resource.getProject()).map(ProjectState::statePermitsWrite).orElse(false)) {
        // submit not visible
        return null;
    }
    ChangeData cd = resource.getChangeResource().getChangeData();
    try {
        MergeOp.checkSubmitRequirements(cd);
    } catch (ResourceConflictException e) {
        // submit not visible
        return null;
    }
    ChangeSet cs = mergeSuperSet.get().completeChangeSet(cd.change(), resource.getUser(), /*includingTopicClosure= */
    false);
    String topic = change.getTopic();
    int topicSize = 0;
    if (!Strings.isNullOrEmpty(topic)) {
        topicSize = queryProvider.get().noFields().byTopicOpen(topic).size();
    }
    boolean treatWithTopic = submitWholeTopic && !Strings.isNullOrEmpty(topic) && topicSize > 1;
    String submitProblems = problemsForSubmittingChangeset(cd, cs, resource.getUser());
    if (submitProblems != null) {
        return new UiAction.Description().setLabel(treatWithTopic ? submitTopicLabel : (cs.size() > 1) ? labelWithParents : label).setTitle(submitProblems).setVisible(true).setEnabled(false);
    }
    // Recheck mergeability rather than using value stored in the index, which may be stale.
    // TODO(dborowitz): This is ugly; consider providing a way to not read stored fields from the
    // index in the first place.
    // cd.setMergeable(null);
    // That was done in unmergeableChanges which was called by problemsForSubmittingChangeset, so
    // now it is safe to read from the cache, as it yields the same result.
    Boolean enabled = cd.isMergeable();
    if (treatWithTopic) {
        Map<String, String> params = ImmutableMap.of("topicSize", String.valueOf(topicSize), "submitSize", String.valueOf(cs.size()));
        return new UiAction.Description().setLabel(submitTopicLabel).setTitle(Strings.emptyToNull(submitTopicTooltip.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
    }
    Map<String, String> params = ImmutableMap.of("patchSet", String.valueOf(resource.getPatchSet().number()), "branch", change.getDest().shortName(), "commit", abbreviateName(resource.getPatchSet().commitId()), "submitSize", String.valueOf(cs.size()));
    ParameterizedString tp = cs.size() > 1 ? titlePatternWithAncestors : titlePattern;
    return new UiAction.Description().setLabel(cs.size() > 1 ? labelWithParents : label).setTitle(Strings.emptyToNull(tp.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.entities.Change) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) ChangeData(com.google.gerrit.server.query.change.ChangeData) ChangeSet(com.google.gerrit.server.submit.ChangeSet) ParameterizedString(com.google.gerrit.common.data.ParameterizedString)

Aggregations

ChangeSet (com.google.gerrit.server.submit.ChangeSet)4 Change (com.google.gerrit.entities.Change)3 ChangeData (com.google.gerrit.server.query.change.ChangeData)3 ParameterizedString (com.google.gerrit.common.data.ParameterizedString)2 StorageException (com.google.gerrit.exceptions.StorageException)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 IOException (java.io.IOException)2 MoreObjects (com.google.common.base.MoreObjects)1 Strings (com.google.common.base.Strings)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ListMultimap (com.google.common.collect.ListMultimap)1 Sets (com.google.common.collect.Sets)1 FluentLogger (com.google.common.flogger.FluentLogger)1 UsedAt (com.google.gerrit.common.UsedAt)1 BranchNameKey (com.google.gerrit.entities.BranchNameKey)1 PatchSet (com.google.gerrit.entities.PatchSet)1 Project (com.google.gerrit.entities.Project)1 SubmitTypeRecord (com.google.gerrit.entities.SubmitTypeRecord)1 SubmitInput (com.google.gerrit.extensions.api.changes.SubmitInput)1