Search in sources :

Example 1 with TagInput

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

the class CommitIncludedInIT method includedInMergedChange.

@Test
public void includedInMergedChange() throws Exception {
    Result result = createChange();
    gApi.changes().id(result.getChangeId()).revision(result.getCommit().name()).review(ReviewInput.approve());
    gApi.changes().id(result.getChangeId()).revision(result.getCommit().name()).submit();
    assertThat(getIncludedIn(result.getCommit().getId()).branches).containsExactly("master");
    assertThat(getIncludedIn(result.getCommit().getId()).tags).isEmpty();
    grantTagPermissions();
    gApi.projects().name(result.getChange().project().get()).tag("test-tag").create(new TagInput());
    assertThat(getIncludedIn(result.getCommit().getId()).tags).containsExactly("test-tag");
    createBranch(new Branch.NameKey(project.get(), "test-branch"));
    assertThat(getIncludedIn(result.getCommit().getId()).branches).containsExactly("master", "test-branch");
}
Also used : Branch(com.google.gerrit.reviewdb.client.Branch) TagInput(com.google.gerrit.extensions.api.projects.TagInput) Result(com.google.gerrit.acceptance.PushOneCommit.Result) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 2 with TagInput

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

the class CreateTag method apply.

@Override
public TagInfo apply(ProjectResource resource, TagInput input) throws RestApiException, IOException, PermissionBackendException {
    if (input == null) {
        input = new TagInput();
    }
    if (input.ref != null && !ref.equals(input.ref)) {
        throw new BadRequestException("ref must match URL");
    }
    if (input.revision == null) {
        input.revision = Constants.HEAD;
    }
    ref = RefUtil.normalizeTagRef(ref);
    RefControl refControl = resource.getControl().controlForRef(ref);
    PermissionBackend.ForRef perm = permissionBackend.user(identifiedUser).project(resource.getNameKey()).ref(ref);
    try (Repository repo = repoManager.openRepository(resource.getNameKey())) {
        ObjectId revid = RefUtil.parseBaseRevision(repo, resource.getNameKey(), input.revision);
        RevWalk rw = RefUtil.verifyConnected(repo, revid);
        RevObject object = rw.parseAny(revid);
        rw.reset();
        boolean isAnnotated = Strings.emptyToNull(input.message) != null;
        boolean isSigned = isAnnotated && input.message.contains("-----BEGIN PGP SIGNATURE-----\n");
        if (isSigned) {
            throw new MethodNotAllowedException("Cannot create signed tag \"" + ref + "\"");
        } else if (isAnnotated && !refControl.canPerform(Permission.CREATE_TAG)) {
            throw new AuthException("Cannot create annotated tag \"" + ref + "\"");
        } else {
            perm.check(RefPermission.CREATE);
        }
        if (repo.getRefDatabase().exactRef(ref) != null) {
            throw new ResourceConflictException("tag \"" + ref + "\" already exists");
        }
        try (Git git = new Git(repo)) {
            TagCommand tag = git.tag().setObjectId(object).setName(ref.substring(R_TAGS.length())).setAnnotated(isAnnotated).setSigned(isSigned);
            if (isAnnotated) {
                tag.setMessage(input.message).setTagger(identifiedUser.get().newCommitterIdent(TimeUtil.nowTs(), TimeZone.getDefault()));
            }
            Ref result = tag.call();
            tagCache.updateFastForward(resource.getNameKey(), ref, ObjectId.zeroId(), result.getObjectId());
            referenceUpdated.fire(resource.getNameKey(), ref, ObjectId.zeroId(), result.getObjectId(), identifiedUser.get().getAccount());
            try (RevWalk w = new RevWalk(repo)) {
                return ListTags.createTagInfo(perm, result, w);
            }
        }
    } catch (InvalidRevisionException e) {
        throw new BadRequestException("Invalid base revision");
    } catch (GitAPIException e) {
        log.error("Cannot create tag \"" + ref + "\"", e);
        throw new IOException(e);
    }
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) AuthException(com.google.gerrit.extensions.restapi.AuthException) TagInput(com.google.gerrit.extensions.api.projects.TagInput) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TagCommand(org.eclipse.jgit.api.TagCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) InvalidRevisionException(com.google.gerrit.server.project.RefUtil.InvalidRevisionException)

Example 3 with TagInput

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

the class CreateTag method apply.

@Override
public Response<TagInfo> apply(ProjectResource resource, IdString id, TagInput input) throws RestApiException, IOException, PermissionBackendException, NoSuchProjectException {
    String ref = id.get();
    if (input == null) {
        input = new TagInput();
    }
    if (input.ref != null && !ref.equals(input.ref)) {
        throw new BadRequestException("ref must match URL");
    }
    if (input.revision != null) {
        input.revision = input.revision.trim();
    }
    if (Strings.isNullOrEmpty(input.revision)) {
        input.revision = Constants.HEAD;
    }
    ref = RefUtil.normalizeTagRef(ref);
    PermissionBackend.ForRef perm = permissionBackend.currentUser().project(resource.getNameKey()).ref(ref);
    try (Repository repo = repoManager.openRepository(resource.getNameKey())) {
        ObjectId revid = RefUtil.parseBaseRevision(repo, resource.getNameKey(), input.revision);
        RevWalk rw = RefUtil.verifyConnected(repo, revid);
        // Reachability through tags does not influence a commit's visibility, so no need to check for
        // visibility.
        RevObject object = rw.parseAny(revid);
        rw.reset();
        boolean isAnnotated = Strings.emptyToNull(input.message) != null;
        boolean isSigned = isAnnotated && input.message.contains("-----BEGIN PGP SIGNATURE-----\n");
        if (isSigned) {
            throw new MethodNotAllowedException("Cannot create signed tag \"" + ref + "\"");
        } else if (isAnnotated) {
            if (!check(perm, RefPermission.CREATE_TAG)) {
                throw new AuthException("Cannot create annotated tag \"" + ref + "\"");
            }
        } else {
            perm.check(RefPermission.CREATE);
        }
        if (repo.getRefDatabase().exactRef(ref) != null) {
            throw new ResourceConflictException("tag \"" + ref + "\" already exists");
        }
        try (Git git = new Git(repo)) {
            TagCommand tag = git.tag().setObjectId(object).setName(ref.substring(R_TAGS.length())).setAnnotated(isAnnotated).setSigned(isSigned);
            if (isAnnotated) {
                tag.setMessage(input.message).setTagger(resource.getUser().asIdentifiedUser().newCommitterIdent(TimeUtil.now(), TimeZone.getDefault()));
            }
            Ref result = tag.call();
            tagCache.updateFastForward(resource.getNameKey(), ref, ObjectId.zeroId(), result.getObjectId());
            referenceUpdated.fire(resource.getNameKey(), ref, ObjectId.zeroId(), result.getObjectId(), resource.getUser().asIdentifiedUser().state());
            try (RevWalk w = new RevWalk(repo)) {
                return Response.created(ListTags.createTagInfo(perm, result, w, resource.getProjectState(), links));
            }
        }
    } catch (InvalidRevisionException e) {
        throw new BadRequestException("Invalid base revision", e);
    } catch (GitAPIException e) {
        logger.atSevere().withCause(e).log("Cannot create tag \"%s\"", ref);
        throw new IOException(e);
    }
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) AuthException(com.google.gerrit.extensions.restapi.AuthException) TagInput(com.google.gerrit.extensions.api.projects.TagInput) IdString(com.google.gerrit.extensions.restapi.IdString) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TagCommand(org.eclipse.jgit.api.TagCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) InvalidRevisionException(com.google.gerrit.server.project.RefUtil.InvalidRevisionException)

Example 4 with TagInput

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

the class GetBranchIT method getTagRef.

@Test
public void getTagRef() throws Exception {
    // create a tag
    TagInput input = new TagInput();
    input.message = "My Tag";
    input.revision = projectOperations.project(project).getHead("master").name();
    TagInfo tagInfo = gApi.projects().name(project.get()).tag("my-tag").create(input).get();
    // any user who can see the project, can see the tag
    requestScopeOperations.setApiUser(user.id());
    assertBranchFound(project, tagInfo.ref);
}
Also used : TagInfo(com.google.gerrit.extensions.api.projects.TagInfo) TagInput(com.google.gerrit.extensions.api.projects.TagInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with TagInput

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

the class GetBranchIT method cannotGetTagRefThatPointsToNonVisibleBranch.

@Test
public void cannotGetTagRefThatPointsToNonVisibleBranch() throws Exception {
    // create a tag
    TagInput input = new TagInput();
    input.message = "My Tag";
    input.revision = projectOperations.project(project).getHead("master").name();
    TagInfo tagInfo = gApi.projects().name(project.get()).tag("my-tag").create(input).get();
    // block read access to the branch
    projectOperations.project(project).forUpdate().add(block(Permission.READ).ref(RefNames.fullName("master")).group(ANONYMOUS_USERS)).update();
    // if the user cannot see the project, the tag is not visible
    requestScopeOperations.setApiUser(user.id());
    assertBranchNotFound(project, tagInfo.ref);
}
Also used : TagInfo(com.google.gerrit.extensions.api.projects.TagInfo) TagInput(com.google.gerrit.extensions.api.projects.TagInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

TagInput (com.google.gerrit.extensions.api.projects.TagInput)23 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)20 Test (org.junit.Test)20 TagInfo (com.google.gerrit.extensions.api.projects.TagInfo)10 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)6 AuthException (com.google.gerrit.extensions.restapi.AuthException)4 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)3 Result (com.google.gerrit.acceptance.PushOneCommit.Result)3 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)3 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)2 InvalidRevisionException (com.google.gerrit.server.project.RefUtil.InvalidRevisionException)2 IOException (java.io.IOException)2 Git (org.eclipse.jgit.api.Git)2 TagCommand (org.eclipse.jgit.api.TagCommand)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Ref (org.eclipse.jgit.lib.Ref)2 Repository (org.eclipse.jgit.lib.Repository)2