Search in sources :

Example 1 with BranchInfo

use of com.google.gerrit.extensions.api.projects.BranchInfo in project gerrit by GerritCodeReview.

the class CreateBranchIT method assertCreateSucceeds.

private void assertCreateSucceeds() throws Exception {
    BranchInfo created = branch().create(new BranchInput()).get();
    assertThat(created.ref).isEqualTo(Constants.R_HEADS + branch.getShortName());
}
Also used : BranchInfo(com.google.gerrit.extensions.api.projects.BranchInfo) BranchInput(com.google.gerrit.extensions.api.projects.BranchInput)

Example 2 with BranchInfo

use of com.google.gerrit.extensions.api.projects.BranchInfo in project gerrit by GerritCodeReview.

the class CreateBranch method apply.

@Override
public BranchInfo apply(ProjectResource rsrc, BranchInput input) throws BadRequestException, AuthException, ResourceConflictException, IOException {
    if (input == null) {
        input = new BranchInput();
    }
    if (input.ref != null && !ref.equals(input.ref)) {
        throw new BadRequestException("ref must match URL");
    }
    if (input.revision == null) {
        input.revision = Constants.HEAD;
    }
    while (ref.startsWith("/")) {
        ref = ref.substring(1);
    }
    ref = RefNames.fullName(ref);
    if (!Repository.isValidRefName(ref)) {
        throw new BadRequestException("invalid branch name \"" + ref + "\"");
    }
    if (MagicBranch.isMagicBranch(ref)) {
        throw new BadRequestException("not allowed to create branches under \"" + MagicBranch.getMagicRefNamePrefix(ref) + "\"");
    }
    final Branch.NameKey name = new Branch.NameKey(rsrc.getNameKey(), ref);
    final RefControl refControl = rsrc.getControl().controlForRef(name);
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        ObjectId revid = RefUtil.parseBaseRevision(repo, rsrc.getNameKey(), input.revision);
        RevWalk rw = RefUtil.verifyConnected(repo, revid);
        RevObject object = rw.parseAny(revid);
        if (ref.startsWith(Constants.R_HEADS)) {
            //
            try {
                object = rw.parseCommit(object);
            } catch (IncorrectObjectTypeException notCommit) {
                throw new BadRequestException("\"" + input.revision + "\" not a commit");
            }
        }
        if (!refControl.canCreate(db.get(), repo, object)) {
            throw new AuthException("Cannot create \"" + ref + "\"");
        }
        try {
            final RefUpdate u = repo.updateRef(ref);
            u.setExpectedOldObjectId(ObjectId.zeroId());
            u.setNewObjectId(object.copy());
            u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
            u.setRefLogMessage("created via REST from " + input.revision, false);
            refCreationValidator.validateRefOperation(rsrc.getName(), identifiedUser.get(), u);
            final RefUpdate.Result result = u.update(rw);
            switch(result) {
                case FAST_FORWARD:
                case NEW:
                case NO_CHANGE:
                    referenceUpdated.fire(name.getParentKey(), u, ReceiveCommand.Type.CREATE, identifiedUser.get().getAccount());
                    break;
                case LOCK_FAILURE:
                    if (repo.getRefDatabase().exactRef(ref) != null) {
                        throw new ResourceConflictException("branch \"" + ref + "\" already exists");
                    }
                    String refPrefix = RefUtil.getRefPrefix(ref);
                    while (!Constants.R_HEADS.equals(refPrefix)) {
                        if (repo.getRefDatabase().exactRef(refPrefix) != null) {
                            throw new ResourceConflictException("Cannot create branch \"" + ref + "\" since it conflicts with branch \"" + refPrefix + "\".");
                        }
                        refPrefix = RefUtil.getRefPrefix(refPrefix);
                    }
                //$FALL-THROUGH$
                case FORCED:
                case IO_FAILURE:
                case NOT_ATTEMPTED:
                case REJECTED:
                case REJECTED_CURRENT_BRANCH:
                case RENAMED:
                default:
                    {
                        throw new IOException(result.name());
                    }
            }
            BranchInfo info = new BranchInfo();
            info.ref = ref;
            info.revision = revid.getName();
            info.canDelete = permissionBackend.user(identifiedUser).ref(name).testOrFalse(RefPermission.DELETE) ? true : null;
            return info;
        } catch (IOException err) {
            log.error("Cannot create branch \"" + name + "\"", err);
            throw err;
        }
    } catch (RefUtil.InvalidRevisionException e) {
        throw new BadRequestException("invalid revision \"" + input.revision + "\"");
    }
}
Also used : BranchInfo(com.google.gerrit.extensions.api.projects.BranchInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) AuthException(com.google.gerrit.extensions.restapi.AuthException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MagicBranch(com.google.gerrit.server.util.MagicBranch) Branch(com.google.gerrit.reviewdb.client.Branch) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) BranchInput(com.google.gerrit.extensions.api.projects.BranchInput) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 3 with BranchInfo

use of com.google.gerrit.extensions.api.projects.BranchInfo in project gerrit by GerritCodeReview.

the class ListBranches method allBranches.

private List<BranchInfo> allBranches(ProjectResource rsrc) throws IOException, ResourceNotFoundException {
    List<Ref> refs;
    try (Repository db = repoManager.openRepository(rsrc.getNameKey())) {
        Collection<Ref> heads = db.getRefDatabase().getRefs(Constants.R_HEADS).values();
        refs = new ArrayList<>(heads.size() + 3);
        refs.addAll(heads);
        refs.addAll(db.getRefDatabase().exactRef(Constants.HEAD, RefNames.REFS_CONFIG, RefNames.REFS_USERS_DEFAULT).values());
    } catch (RepositoryNotFoundException noGitRepository) {
        throw new ResourceNotFoundException();
    }
    Set<String> targets = Sets.newHashSetWithExpectedSize(1);
    for (Ref ref : refs) {
        if (ref.isSymbolic()) {
            targets.add(ref.getTarget().getName());
        }
    }
    ProjectControl pctl = rsrc.getControl();
    PermissionBackend.ForProject perm = permissionBackend.user(user).project(rsrc.getNameKey());
    List<BranchInfo> branches = new ArrayList<>(refs.size());
    for (Ref ref : refs) {
        if (ref.isSymbolic()) {
            // A symbolic reference to another branch, instead of
            // showing the resolved value, show the name it references.
            //
            String target = ref.getTarget().getName();
            RefControl targetRefControl = pctl.controlForRef(target);
            if (!targetRefControl.isVisible()) {
                continue;
            }
            if (target.startsWith(Constants.R_HEADS)) {
                target = target.substring(Constants.R_HEADS.length());
            }
            BranchInfo b = new BranchInfo();
            b.ref = ref.getName();
            b.revision = target;
            branches.add(b);
            if (!Constants.HEAD.equals(ref.getName())) {
                b.canDelete = perm.ref(ref.getName()).testOrFalse(RefPermission.DELETE) ? true : null;
            }
            continue;
        }
        if (pctl.controlForRef(ref.getName()).isVisible()) {
            branches.add(createBranchInfo(perm.ref(ref.getName()), ref, pctl, targets));
        }
    }
    Collections.sort(branches, new BranchComparator());
    return branches;
}
Also used : BranchInfo(com.google.gerrit.extensions.api.projects.BranchInfo) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) ArrayList(java.util.ArrayList) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Ref(org.eclipse.jgit.lib.Ref) Repository(org.eclipse.jgit.lib.Repository) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 4 with BranchInfo

use of com.google.gerrit.extensions.api.projects.BranchInfo in project gerrit by GerritCodeReview.

the class ListBranches method createBranchInfo.

private BranchInfo createBranchInfo(PermissionBackend.ForRef perm, Ref ref, ProjectControl pctl, Set<String> targets) {
    BranchInfo info = new BranchInfo();
    info.ref = ref.getName();
    info.revision = ref.getObjectId() != null ? ref.getObjectId().name() : null;
    info.canDelete = !targets.contains(ref.getName()) && perm.testOrFalse(RefPermission.DELETE) ? true : null;
    BranchResource rsrc = new BranchResource(pctl, info);
    for (UiAction.Description d : uiActions.from(branchViews, rsrc)) {
        if (info.actions == null) {
            info.actions = new TreeMap<>();
        }
        info.actions.put(d.getId(), new ActionInfo(d));
    }
    List<WebLinkInfo> links = webLinks.getBranchLinks(pctl.getProject().getName(), ref.getName());
    info.webLinks = links.isEmpty() ? null : links;
    return info;
}
Also used : BranchInfo(com.google.gerrit.extensions.api.projects.BranchInfo) WebLinkInfo(com.google.gerrit.extensions.common.WebLinkInfo) ActionInfo(com.google.gerrit.extensions.common.ActionInfo) UiAction(com.google.gerrit.extensions.webui.UiAction)

Example 5 with BranchInfo

use of com.google.gerrit.extensions.api.projects.BranchInfo in project gerrit by GerritCodeReview.

the class AgreementsIT method cherrypickChangeWithoutCLA.

@Test
public void cherrypickChangeWithoutCLA() throws Exception {
    assume().that(isContributorAgreementsEnabled()).isTrue();
    // Create a new branch
    setApiUser(admin);
    BranchInfo dest = gApi.projects().name(project.get()).branch("cherry-pick-to").create(new BranchInput()).get();
    // Create a change succeeds when agreement is not required
    setUseContributorAgreements(InheritableBoolean.FALSE);
    ChangeInfo change = gApi.changes().create(newChangeInput()).get();
    // Approve and submit it
    gApi.changes().id(change.changeId).current().review(ReviewInput.approve());
    gApi.changes().id(change.changeId).current().submit(new SubmitInput());
    // Cherry-pick is not allowed when CLA is required but not signed
    setApiUser(user);
    setUseContributorAgreements(InheritableBoolean.TRUE);
    CherryPickInput in = new CherryPickInput();
    in.destination = dest.ref;
    in.message = change.subject;
    exception.expect(AuthException.class);
    exception.expectMessage("A Contributor Agreement must be completed");
    gApi.changes().id(change.changeId).current().cherryPick(in);
}
Also used : SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) BranchInfo(com.google.gerrit.extensions.api.projects.BranchInfo) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) CherryPickInput(com.google.gerrit.extensions.api.changes.CherryPickInput) BranchInput(com.google.gerrit.extensions.api.projects.BranchInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

BranchInfo (com.google.gerrit.extensions.api.projects.BranchInfo)7 BranchInput (com.google.gerrit.extensions.api.projects.BranchInput)3 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 Repository (org.eclipse.jgit.lib.Repository)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 CherryPickInput (com.google.gerrit.extensions.api.changes.CherryPickInput)1 SubmitInput (com.google.gerrit.extensions.api.changes.SubmitInput)1 ActionInfo (com.google.gerrit.extensions.common.ActionInfo)1 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)1 WebLinkInfo (com.google.gerrit.extensions.common.WebLinkInfo)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 UiAction (com.google.gerrit.extensions.webui.UiAction)1 Branch (com.google.gerrit.reviewdb.client.Branch)1 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)1 MagicBranch (com.google.gerrit.server.util.MagicBranch)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1