Search in sources :

Example 46 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class Submit method getDescription.

@Override
public UiAction.Description getDescription(RevisionResource resource) {
    Change change = resource.getChange();
    String topic = change.getTopic();
    ReviewDb db = dbProvider.get();
    ChangeData cd = changeDataFactory.create(db, resource.getControl());
    boolean visible;
    try {
        visible = change.getStatus().isOpen() && resource.isCurrent() && !resource.getPatchSet().isDraft() && resource.permissions().test(ChangePermission.SUBMIT);
        MergeOp.checkSubmitRule(cd);
    } catch (ResourceConflictException e) {
        visible = false;
    } catch (PermissionBackendException e) {
        log.error("Error checking if change is submittable", e);
        throw new OrmRuntimeException("Could not check submit permission", e);
    } catch (OrmException e) {
        log.error("Error checking if change is submittable", e);
        throw new OrmRuntimeException("Could not determine problems for the change", e);
    }
    if (!visible) {
        return new UiAction.Description().setLabel("").setTitle("").setVisible(false);
    }
    ChangeSet cs;
    try {
        cs = mergeSuperSet.get().completeChangeSet(db, cd.change(), resource.getControl().getUser());
    } catch (OrmException | IOException e) {
        throw new OrmRuntimeException("Could not determine complete set of changes to be submitted", e);
    }
    int topicSize = 0;
    if (!Strings.isNullOrEmpty(topic)) {
        topicSize = getChangesByTopic(topic).size();
    }
    boolean treatWithTopic = submitWholeTopic && !Strings.isNullOrEmpty(topic) && topicSize > 1;
    String submitProblems = problemsForSubmittingChangeset(cd, cs, resource.getUser());
    Boolean enabled;
    try {
        // 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.
        enabled = cd.isMergeable();
    } catch (OrmException e) {
        throw new OrmRuntimeException("Could not determine mergeability", e);
    }
    if (submitProblems != null) {
        return new UiAction.Description().setLabel(treatWithTopic ? submitTopicLabel : (cs.size() > 1) ? labelWithParents : label).setTitle(submitProblems).setVisible(true).setEnabled(false);
    }
    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));
    }
    RevId revId = resource.getPatchSet().getRevision();
    Map<String, String> params = ImmutableMap.of("patchSet", String.valueOf(resource.getPatchSet().getPatchSetId()), "branch", change.getDest().getShortName(), "commit", ObjectId.fromString(revId.get()).abbreviate(7).name(), "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 : OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Change(com.google.gerrit.reviewdb.client.Change) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) IOException(java.io.IOException) UiAction(com.google.gerrit.extensions.webui.UiAction) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevId(com.google.gerrit.reviewdb.client.RevId) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) OrmException(com.google.gwtorm.server.OrmException) ChangeSet(com.google.gerrit.server.git.ChangeSet) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 47 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class Submit method mergeChange.

public Change mergeChange(BatchUpdate.Factory updateFactory, RevisionResource rsrc, IdentifiedUser submitter, SubmitInput input) throws OrmException, RestApiException, IOException {
    Change change = rsrc.getChange();
    if (!change.getStatus().isOpen()) {
        throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    } else if (!ProjectUtil.branchExists(repoManager, change.getDest())) {
        throw new ResourceConflictException(String.format("destination branch \"%s\" not found.", change.getDest().get()));
    } else if (!rsrc.getPatchSet().getId().equals(change.currentPatchSetId())) {
        // TODO Allow submitting non-current revision by changing the current.
        throw new ResourceConflictException(String.format("revision %s is not current revision", rsrc.getPatchSet().getRevision().get()));
    }
    try (MergeOp op = mergeOpFactory.create(updateFactory)) {
        ReviewDb db = dbProvider.get();
        op.merge(db, change, submitter, true, input, false);
        try {
            change = changeNotesFactory.createChecked(db, change.getProject(), change.getId()).getChange();
        } catch (NoSuchChangeException e) {
            throw new ResourceConflictException("change is deleted");
        }
    }
    switch(change.getStatus()) {
        case MERGED:
            return change;
        case NEW:
            ChangeMessage msg = getConflictMessage(rsrc);
            if (msg != null) {
                throw new ResourceConflictException(msg.getMessage());
            }
        //$FALL-THROUGH$
        case ABANDONED:
        case DRAFT:
        default:
            throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    }
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) MergeOp(com.google.gerrit.server.git.MergeOp) Change(com.google.gerrit.reviewdb.client.Change) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 48 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class Submit method problemsForSubmittingChangeset.

/**
   * @param cd the change the user is currently looking at
   * @param cs set of changes to be submitted at once
   * @param user the user who is checking to submit
   * @return a reason why any of the changes is not submittable or null
   */
private String problemsForSubmittingChangeset(ChangeData cd, ChangeSet cs, CurrentUser user) {
    try {
        if (cs.furtherHiddenChanges()) {
            return BLOCKED_HIDDEN_SUBMIT_TOOLTIP;
        }
        for (ChangeData c : cs.changes()) {
            Set<ChangePermission> can = permissionBackend.user(user).database(dbProvider).change(c).test(EnumSet.of(ChangePermission.READ, ChangePermission.SUBMIT));
            if (!can.contains(ChangePermission.READ)) {
                return BLOCKED_HIDDEN_SUBMIT_TOOLTIP;
            }
            if (!can.contains(ChangePermission.SUBMIT)) {
                return BLOCKED_SUBMIT_TOOLTIP;
            }
            if (c.change().isWorkInProgress()) {
                return BLOCKED_WORK_IN_PROGRESS;
            }
            MergeOp.checkSubmitRule(c);
        }
        Collection<ChangeData> unmergeable = unmergeableChanges(cs);
        if (unmergeable == null) {
            return CLICK_FAILURE_TOOLTIP;
        } else if (!unmergeable.isEmpty()) {
            for (ChangeData c : unmergeable) {
                if (c.change().getKey().equals(cd.change().getKey())) {
                    return CHANGE_UNMERGEABLE;
                }
            }
            return CHANGES_NOT_MERGEABLE + unmergeable.stream().map(c -> c.getId().toString()).collect(joining(", "));
        }
    } catch (ResourceConflictException e) {
        return BLOCKED_SUBMIT_TOOLTIP;
    } catch (PermissionBackendException | OrmException | IOException e) {
        log.error("Error checking if change is submittable", e);
        throw new OrmRuntimeException("Could not determine problems for the change", e);
    }
    return null;
}
Also used : OrmException(com.google.gwtorm.server.OrmException) ListMultimap(com.google.common.collect.ListMultimap) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) ChangeSet(com.google.gerrit.server.git.ChangeSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) Output(com.google.gerrit.server.change.Submit.Output) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) FluentIterable(com.google.common.collect.FluentIterable) SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) MergeSuperSet(com.google.gerrit.server.git.MergeSuperSet) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) RetryHelper(com.google.gerrit.server.update.RetryHelper) RetryingRestModifyView(com.google.gerrit.server.update.RetryingRestModifyView) UiAction(com.google.gerrit.extensions.webui.UiAction) EnumSet(java.util.EnumSet) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) Collection(java.util.Collection) Set(java.util.Set) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) MergeOp(com.google.gerrit.server.git.MergeOp) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) AccountsCollection(com.google.gerrit.server.account.AccountsCollection) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) Branch(com.google.gerrit.reviewdb.client.Branch) ChangeMessagesUtil(com.google.gerrit.server.ChangeMessagesUtil) Singleton(com.google.inject.Singleton) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Change(com.google.gerrit.reviewdb.client.Change) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ChangePermission(com.google.gerrit.server.permissions.ChangePermission) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) Project(com.google.gerrit.reviewdb.client.Project) CurrentUser(com.google.gerrit.server.CurrentUser) Logger(org.slf4j.Logger) MoreObjects(com.google.common.base.MoreObjects) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) 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) ProjectUtil(com.google.gerrit.server.ProjectUtil) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) RevId(com.google.gerrit.reviewdb.client.RevId) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) OrmException(com.google.gwtorm.server.OrmException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) ChangeData(com.google.gerrit.server.query.change.ChangeData) ChangePermission(com.google.gerrit.server.permissions.ChangePermission)

Example 49 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class Revert method applyImpl.

@Override
protected ChangeInfo applyImpl(BatchUpdate.Factory updateFactory, ChangeResource req, RevertInput input) throws IOException, OrmException, RestApiException, UpdateException, NoSuchChangeException {
    RefControl refControl = req.getControl().getRefControl();
    ProjectControl projectControl = req.getControl().getProjectControl();
    Capable capable = projectControl.canPushToAtLeastOneRef();
    if (capable != Capable.OK) {
        throw new AuthException(capable.getMessage());
    }
    Change change = req.getChange();
    if (!refControl.canUpload()) {
        throw new AuthException("revert not permitted");
    } else if (change.getStatus() != Status.MERGED) {
        throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    }
    Change.Id revertedChangeId = revert(updateFactory, req.getControl(), Strings.emptyToNull(input.message));
    return json.noOptions().format(req.getProject(), revertedChangeId);
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Capable(com.google.gerrit.common.data.Capable) RefControl(com.google.gerrit.server.project.RefControl) AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.reviewdb.client.Change) ProjectControl(com.google.gerrit.server.project.ProjectControl)

Example 50 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class Rebase method findBaseRev.

private ObjectId findBaseRev(Repository repo, RevWalk rw, RevisionResource rsrc, RebaseInput input) throws RestApiException, OrmException, IOException, NoSuchChangeException {
    Branch.NameKey destRefKey = rsrc.getChange().getDest();
    if (input == null || input.base == null) {
        return rebaseUtil.findBaseRevision(rsrc.getPatchSet(), destRefKey, repo, rw);
    }
    Change change = rsrc.getChange();
    String str = input.base.trim();
    if (str.equals("")) {
        // Remove existing dependency to other patch set.
        Ref destRef = repo.exactRef(destRefKey.get());
        if (destRef == null) {
            throw new ResourceConflictException("can't rebase onto tip of branch " + destRefKey.get() + "; branch doesn't exist");
        }
        return destRef.getObjectId();
    }
    @SuppressWarnings("resource") ReviewDb db = dbProvider.get();
    Base base = rebaseUtil.parseBase(rsrc, str);
    if (base == null) {
        throw new ResourceConflictException("base revision is missing: " + str);
    }
    PatchSet.Id baseId = base.patchSet().getId();
    if (!base.control().isPatchVisible(base.patchSet(), db)) {
        throw new AuthException("base revision not accessible: " + str);
    } else if (change.getId().equals(baseId.getParentKey())) {
        throw new ResourceConflictException("cannot rebase change onto itself");
    }
    Change baseChange = base.control().getChange();
    if (!baseChange.getProject().equals(change.getProject())) {
        throw new ResourceConflictException("base change is in wrong project: " + baseChange.getProject());
    } else if (!baseChange.getDest().equals(change.getDest())) {
        throw new ResourceConflictException("base change is targeting wrong branch: " + baseChange.getDest());
    } else if (baseChange.getStatus() == Status.ABANDONED) {
        throw new ResourceConflictException("base change is abandoned: " + baseChange.getKey());
    } else if (isMergedInto(rw, rsrc.getPatchSet(), base.patchSet())) {
        throw new ResourceConflictException("base change " + baseChange.getKey() + " is a descendant of the current change - recursion not allowed");
    }
    return ObjectId.fromString(base.patchSet().getRevision().get());
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Branch(com.google.gerrit.reviewdb.client.Branch) AuthException(com.google.gerrit.extensions.restapi.AuthException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) Base(com.google.gerrit.server.change.RebaseUtil.Base)

Aggregations

ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)75 AuthException (com.google.gerrit.extensions.restapi.AuthException)25 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)21 Change (com.google.gerrit.reviewdb.client.Change)19 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)18 Project (com.google.gerrit.reviewdb.client.Project)17 IOException (java.io.IOException)17 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)14 Repository (org.eclipse.jgit.lib.Repository)14 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)13 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)12 OrmException (com.google.gwtorm.server.OrmException)12 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)12 ObjectId (org.eclipse.jgit.lib.ObjectId)12 RevWalk (org.eclipse.jgit.revwalk.RevWalk)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)10 ArrayList (java.util.ArrayList)10 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)10 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)9