Search in sources :

Example 6 with Capable

use of com.google.gerrit.common.data.Capable in project gerrit by GerritCodeReview.

the class CherryPick method applyImpl.

@Override
protected ChangeInfo applyImpl(BatchUpdate.Factory updateFactory, RevisionResource revision, CherryPickInput input) throws OrmException, IOException, UpdateException, RestApiException {
    final ChangeControl control = revision.getControl();
    input.parent = input.parent == null ? 1 : input.parent;
    if (input.message == null || input.message.trim().isEmpty()) {
        throw new BadRequestException("message must be non-empty");
    } else if (input.destination == null || input.destination.trim().isEmpty()) {
        throw new BadRequestException("destination must be non-empty");
    }
    if (!control.isVisible(dbProvider.get())) {
        throw new AuthException("Cherry pick not permitted");
    }
    ProjectControl projectControl = control.getProjectControl();
    Capable capable = projectControl.canPushToAtLeastOneRef();
    if (capable != Capable.OK) {
        throw new AuthException(capable.getMessage());
    }
    String refName = RefNames.fullName(input.destination);
    RefControl refControl = projectControl.controlForRef(refName);
    if (!refControl.canUpload()) {
        throw new AuthException("Not allowed to cherry pick " + revision.getChange().getId().toString() + " to " + input.destination);
    }
    try {
        Change.Id cherryPickedChangeId = cherryPickChange.cherryPick(updateFactory, revision.getChange(), revision.getPatchSet(), input, refName, refControl);
        return json.noOptions().format(revision.getProject(), cherryPickedChangeId);
    } catch (InvalidChangeOperationException e) {
        throw new BadRequestException(e.getMessage());
    } catch (IntegrationException | NoSuchChangeException e) {
        throw new ResourceConflictException(e.getMessage());
    }
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) IntegrationException(com.google.gerrit.server.git.IntegrationException) 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) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Capable(com.google.gerrit.common.data.Capable) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) ChangeControl(com.google.gerrit.server.project.ChangeControl) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 7 with Capable

use of com.google.gerrit.common.data.Capable in project gerrit by GerritCodeReview.

the class MagicBranch method checkMagicBranchRefs.

/**
   * Checks if a (magic branch)/branch_name reference exists in the destination repository and only
   * returns Capable.OK if it does not match any.
   *
   * <p>These block the client from being able to even send us a pack file, as it is very unlikely
   * the user passed the --force flag and the new commit is probably not going to fast-forward the
   * branch.
   */
public static Capable checkMagicBranchRefs(Repository repo, Project project) {
    Capable result = checkMagicBranchRef(NEW_CHANGE, repo, project);
    if (result != Capable.OK) {
        return result;
    }
    result = checkMagicBranchRef(NEW_DRAFT_CHANGE, repo, project);
    if (result != Capable.OK) {
        return result;
    }
    result = checkMagicBranchRef(NEW_PUBLISH_CHANGE, repo, project);
    if (result != Capable.OK) {
        return result;
    }
    return Capable.OK;
}
Also used : Capable(com.google.gerrit.common.data.Capable)

Example 8 with Capable

use of com.google.gerrit.common.data.Capable in project gerrit by GerritCodeReview.

the class Receive method runImpl.

@Override
protected void runImpl() throws IOException, Failure {
    if (!projectControl.canRunReceivePack()) {
        throw new Failure(1, "fatal: receive-pack not permitted on this server");
    }
    final ReceiveCommits receive = factory.create(projectControl, repo).getReceiveCommits();
    Capable r = receive.canUpload();
    if (r != Capable.OK) {
        throw die(r.getMessage());
    }
    receive.init();
    receive.addReviewers(reviewerId);
    receive.addExtraCC(ccId);
    ReceivePack rp = receive.getReceivePack();
    try {
        rp.receive(in, out, err);
        session.setPeerAgent(rp.getPeerUserAgent());
    } catch (UnpackException badStream) {
        // we want to present this error to the user
        if (badStream.getCause() instanceof TooLargeObjectInPackException) {
            StringBuilder msg = new StringBuilder();
            msg.append("Receive error on project \"").append(projectControl.getProject().getName()).append("\"");
            msg.append(" (user ");
            msg.append(currentUser.getAccount().getUserName());
            msg.append(" account ");
            msg.append(currentUser.getAccountId());
            msg.append("): ");
            msg.append(badStream.getCause().getMessage());
            log.info(msg.toString());
            throw new UnloggedFailure(128, "error: " + badStream.getCause().getMessage());
        }
        // This may have been triggered by branch level access controls.
        // Log what the heck is going on, as detailed as we can.
        //
        StringBuilder msg = new StringBuilder();
        msg.append("Unpack error on project \"").append(projectControl.getProject().getName()).append("\":\n");
        msg.append("  AdvertiseRefsHook: ").append(rp.getAdvertiseRefsHook());
        if (rp.getAdvertiseRefsHook() == AdvertiseRefsHook.DEFAULT) {
            msg.append("DEFAULT");
        } else if (rp.getAdvertiseRefsHook() instanceof VisibleRefFilter) {
            msg.append("VisibleRefFilter");
        } else {
            msg.append(rp.getAdvertiseRefsHook().getClass());
        }
        msg.append("\n");
        if (rp.getAdvertiseRefsHook() instanceof VisibleRefFilter) {
            Map<String, Ref> adv = rp.getAdvertisedRefs();
            msg.append("  Visible references (").append(adv.size()).append("):\n");
            for (Ref ref : adv.values()) {
                msg.append("  - ").append(ref.getObjectId().abbreviate(8).name()).append(" ").append(ref.getName()).append("\n");
            }
            Map<String, Ref> allRefs = rp.getRepository().getRefDatabase().getRefs(RefDatabase.ALL);
            List<Ref> hidden = new ArrayList<>();
            for (Ref ref : allRefs.values()) {
                if (!adv.containsKey(ref.getName())) {
                    hidden.add(ref);
                }
            }
            msg.append("  Hidden references (").append(hidden.size()).append("):\n");
            for (Ref ref : hidden) {
                msg.append("  - ").append(ref.getObjectId().abbreviate(8).name()).append(" ").append(ref.getName()).append("\n");
            }
        }
        IOException detail = new IOException(msg.toString(), badStream);
        throw new Failure(128, "fatal: Unpack error, check server log", detail);
    }
}
Also used : TooLargeObjectInPackException(org.eclipse.jgit.errors.TooLargeObjectInPackException) IOException(java.io.IOException) AsyncReceiveCommits(com.google.gerrit.server.git.AsyncReceiveCommits) ReceiveCommits(com.google.gerrit.server.git.ReceiveCommits) Ref(org.eclipse.jgit.lib.Ref) Capable(com.google.gerrit.common.data.Capable) UnpackException(org.eclipse.jgit.errors.UnpackException) VisibleRefFilter(com.google.gerrit.server.git.VisibleRefFilter) ArrayList(java.util.ArrayList) List(java.util.List) ReceivePack(org.eclipse.jgit.transport.ReceivePack) Map(java.util.Map)

Example 9 with Capable

use of com.google.gerrit.common.data.Capable 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)

Aggregations

Capable (com.google.gerrit.common.data.Capable)9 AuthException (com.google.gerrit.extensions.restapi.AuthException)5 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 Change (com.google.gerrit.reviewdb.client.Change)4 ProjectControl (com.google.gerrit.server.project.ProjectControl)4 RefControl (com.google.gerrit.server.project.RefControl)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 Ref (org.eclipse.jgit.lib.Ref)3 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)2 Project (com.google.gerrit.reviewdb.client.Project)2 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)2 IntegrationException (com.google.gerrit.server.git.IntegrationException)2 ChangeControl (com.google.gerrit.server.project.ChangeControl)2 InvalidChangeOperationException (com.google.gerrit.server.project.InvalidChangeOperationException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 AccessSection (com.google.gerrit.common.data.AccessSection)1 ContributorAgreement (com.google.gerrit.common.data.ContributorAgreement)1 PermissionRule (com.google.gerrit.common.data.PermissionRule)1