org.gitlab4j.api.models.AccessLevel Java Examples

The following examples show how to use org.gitlab4j.api.models.AccessLevel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ForkMergeRequestDiscoveryTrait.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected boolean checkTrusted(@NonNull GitLabSCMSourceRequest request,
    @NonNull MergeRequestSCMHead head) {
    if (!head.getOrigin().equals(SCMHeadOrigin.DEFAULT)) {
        AccessLevel permission = request.getPermission(head.getOriginOwner());
        if (permission != null) {
            switch (permission) {
                case MAINTAINER:
                case DEVELOPER:
                case OWNER:
                    return true;
                default:
                    return false;
            }
        }
    }
    return false;
}
 
Example #2
Source File: TestGroupApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@Test
public void testAllMemberOperations() throws GitLabApiException {

    // Arrange and Act
    Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER);

    // Assert
    assertNotNull(member);
    assertEquals(testUser.getId(), member.getId());
    assertEquals(AccessLevel.DEVELOPER, member.getAccessLevel());

    // Act
    List<Member> members = gitLabApi.getGroupApi().getAllMembers(testGroup);

    // Assert
    assertNotNull(members);
    Boolean found = (members.stream().filter(m -> m.getId().equals(member.getId())).findAny().orElse(null) != null);
    assertTrue(found);

    gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId());
}
 
Example #3
Source File: TestProjectApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@Test
public void testProjectsWithAccessLevelFilter() throws GitLabApiException {

    ProjectFilter filter = new ProjectFilter().withMinAccessLevel(AccessLevel.GUEST);
    List<Project> guestProjects = gitLabApi.getProjectApi().getProjects(filter);
    assertTrue(guestProjects != null);
    assertTrue(guestProjects.size() > 0);

    // Use sudo to impersonate a non-admin user
    try {

        gitLabApi.sudo(TEST_SUDO_AS_USERNAME);
        filter = new ProjectFilter().withMinAccessLevel(AccessLevel.OWNER);
        List<Project> ownedProjects = gitLabApi.getProjectApi().getProjects(filter);
        assertTrue(ownedProjects != null);
        assertTrue(guestProjects.size() > ownedProjects.size());

    } finally {
        gitLabApi.unsudo();
    }
}
 
Example #4
Source File: TestTagsApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@Test
public void  testProtectedTags() throws GitLabApiException {

    Tag testTag = gitLabApi.getTagsApi().createTag(testProject, TEST_PROTECTED_TAG, "master");
    assertNotNull(testTag);
    assertEquals(TEST_PROTECTED_TAG, testTag.getName());

    ProtectedTag protectedTag = gitLabApi.getTagsApi().protectTag(testProject, TEST_PROTECTED_TAG, AccessLevel.DEVELOPER);
    assertEquals(TEST_PROTECTED_TAG, protectedTag.getName());

    List<ProtectedTag> tags = gitLabApi.getTagsApi().getProtectedTags(testProject);
    assertTrue(tags.stream().map(ProtectedTag::getName).anyMatch(s -> TEST_PROTECTED_TAG.equals(s)));

    Optional<ProtectedTag> optionalTag = gitLabApi.getTagsApi().getOptionalProtectedTag(testProject, TEST_PROTECTED_TAG);
    assertTrue(optionalTag.isPresent());
    assertEquals(TEST_PROTECTED_TAG, optionalTag.get().getName());

    gitLabApi.getTagsApi().unprotectTag(testProject, TEST_PROTECTED_TAG);
    assertEquals(TEST_PROTECTED_TAG, protectedTag.getName());

    tags = gitLabApi.getTagsApi().getProtectedTags(testProject);
    assertFalse(tags.stream().map(ProtectedTag::getName).anyMatch(s -> TEST_PROTECTED_TAG.equals(s)));
}
 
Example #5
Source File: GitLabMergeRequestCommentTrigger.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
private boolean isTrustedMember(GitLabSCMSource gitLabSCMSource, boolean check) {
    // Return true if only trusted members can trigger option is not checked
    if(!check) {
        return true;
    }
    AccessLevel permission = gitLabSCMSource.getMembers()
        .get(getPayload().getUser().getUsername());
    if (permission != null) {
        switch (permission) {
            case MAINTAINER:
            case DEVELOPER:
            case OWNER:
                return true;
            default:
                return false;
        }
    }
    return false;
}
 
Example #6
Source File: GitLabSCMSourceRequest.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the permissions of the supplied user.
 *
 * @param username the username of MR author
 * @return {@link AccessLevel} the permissions of the supplied user.
 */
public AccessLevel getPermission(String username) {
    if (getMembers() == null) {
        return null;
    }
    if (getMembers().containsKey(username)) {
        return getMembers().get(username);
    }
    return null;
}
 
Example #7
Source File: GitLabSCMSource.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
public HashMap<String, AccessLevel> getMembers() {
    HashMap<String, AccessLevel> members = new HashMap<>();
    try {
        GitLabApi gitLabApi = apiBuilder(serverName);
        for (Member m : gitLabApi.getProjectApi().getAllMembers(projectPath)) {
            members.put(m.getUsername(), m.getAccessLevel());
        }
    } catch (GitLabApiException e) {
        LOGGER.log(Level.WARNING, "Exception while fetching members" + e, e);
        return new HashMap<>();
    }
    return members;
}
 
Example #8
Source File: TestGroupApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testMemberOperations() throws GitLabApiException {

    // Arrange and Act
    Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER);

    // Assert
    assertNotNull(member);
    assertEquals(testUser.getId(), member.getId());
    assertEquals(AccessLevel.DEVELOPER, member.getAccessLevel());

    // Act
    Optional<Member> optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId());

    // Assert
    assertTrue(optionalMember.isPresent());

    // Act
    List<Member> members = gitLabApi.getGroupApi().getMembers(testGroup);

    // Assert
    assertNotNull(members);
    Boolean found = (members.stream().filter(m -> m.getId().equals(member.getId())).findAny().orElse(null) != null);
    assertTrue(found);

    // Act
    gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId());

    // Act
    optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId());

    // Assert
    assertFalse(optionalMember.isPresent());
}
 
Example #9
Source File: TestProjectApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testShareProject() throws GitLabApiException {

    assumeTrue(TEST_GROUP != null && TEST_GROUP_PROJECT != null);
    assumeTrue(TEST_GROUP.trim().length() > 0 && TEST_GROUP_PROJECT.trim().length() > 0);
    assumeNotNull(testProject);

    List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP);
    assertNotNull(groups);

    Group shareGroup = groups.get(0);
    gitLabApi.getProjectApi().shareProject(testProject, shareGroup.getId(), AccessLevel.DEVELOPER, null);
    gitLabApi.getProjectApi().unshareProject(testProject, shareGroup.getId());
}
 
Example #10
Source File: GitLabCauseUtils.java    From gitlab-branch-source-plugin with MIT License 4 votes vote down vote up
public static  String defaultVisibilityString(AccessLevel accessLevel) {
    return accessLevel == null ? "" : accessLevel.toString();
}
 
Example #11
Source File: EventProject.java    From gitlab4j-api with MIT License 4 votes vote down vote up
public void setVisibilityLevel(AccessLevel visibilityLevel) {
    this.visibilityLevel = visibilityLevel;
}
 
Example #12
Source File: EventProject.java    From gitlab4j-api with MIT License 4 votes vote down vote up
public AccessLevel getVisibilityLevel() {
    return visibilityLevel;
}
 
Example #13
Source File: EventRepository.java    From gitlab4j-api with MIT License 4 votes vote down vote up
public void setVisibility_level(AccessLevel visibility_level) {
    this.visibility_level = visibility_level;
}
 
Example #14
Source File: EventRepository.java    From gitlab4j-api with MIT License 4 votes vote down vote up
public AccessLevel getVisibility_level() {
    return visibility_level;
}
 
Example #15
Source File: EventSnippet.java    From gitlab4j-api with MIT License 4 votes vote down vote up
public void setVisibilityLevel(AccessLevel visibilityLevel) {
    this.visibilityLevel = visibilityLevel;
}
 
Example #16
Source File: EventSnippet.java    From gitlab4j-api with MIT License 4 votes vote down vote up
public AccessLevel getVisibilityLevel() {
    return visibilityLevel;
}
 
Example #17
Source File: EventSnippet.java    From choerodon-starters with Apache License 2.0 4 votes vote down vote up
public AccessLevel getVisibilityLevel() {
    return visibilityLevel;
}
 
Example #18
Source File: EventSnippet.java    From choerodon-starters with Apache License 2.0 4 votes vote down vote up
public void setVisibilityLevel(AccessLevel visibilityLevel) {
    this.visibilityLevel = visibilityLevel;
}
 
Example #19
Source File: EventProject.java    From choerodon-starters with Apache License 2.0 4 votes vote down vote up
public void setVisibilityLevel(AccessLevel visibilityLevel) {
    this.visibilityLevel = visibilityLevel;
}
 
Example #20
Source File: EventProject.java    From choerodon-starters with Apache License 2.0 4 votes vote down vote up
public AccessLevel getVisibilityLevel() {
    return visibilityLevel;
}
 
Example #21
Source File: EventRepository.java    From choerodon-starters with Apache License 2.0 4 votes vote down vote up
public void setVisibility_level(AccessLevel visibility_level) {
    this.visibility_level = visibility_level;
}
 
Example #22
Source File: EventRepository.java    From choerodon-starters with Apache License 2.0 4 votes vote down vote up
public AccessLevel getVisibility_level() {
    return visibility_level;
}
 
Example #23
Source File: ProtectedBranchesApi.java    From choerodon-starters with Apache License 2.0 3 votes vote down vote up
/**
 * Protects a single repository branch or several project repository branches using a wildcard protected branch.
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/protected_branches</code></pre>
 *
 * @param projectIdOrPath           the project in the form of an Integer(ID), String(path), or Project instance
 * @param branchName                the name of the branch to protect, can be a wildcard
 * @param pushAccessLevel           access levels allowed to push (defaults: 40, maintainer access level)
 * @param mergeAccessLevel          access levels allowed to merge (defaults: 40, maintainer access level)
 * @param unprotectAccessLevel      access levels allowed to unprotect (defaults: 40, maintainer access level)
 * @param codeOwnerApprovalRequired prevent pushes to this branch if it matches an item in the CODEOWNERS file. (defaults: false)
 * @return the branch info for the protected branch
 * @throws GitLabApiException if any exception occurs
 */
public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName,
                                     AccessLevel pushAccessLevel, AccessLevel mergeAccessLevel, AccessLevel unprotectAccessLevel,
                                     Boolean codeOwnerApprovalRequired) throws GitLabApiException {
    Form formData = new GitLabApiForm()
            .withParam("name", branchName, true)
            .withParam("push_access_level", pushAccessLevel)
            .withParam("merge_access_level", mergeAccessLevel)
            .withParam("unprotect_access_level", unprotectAccessLevel)
            .withParam("code_owner_approval_required", codeOwnerApprovalRequired);
    Response response = post(Response.Status.CREATED, formData.asMap(),
            "projects", getProjectIdOrPath(projectIdOrPath), "protected_branches");
    return (response.readEntity(ProtectedBranch.class));
}
 
Example #24
Source File: GroupApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Adds an LDAP group link.
 *
 * <pre><code>GitLab Endpoint: POST /groups/:id/ldap_group_links</code></pre>
 *
 * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
 * @param cn the CN of a LDAP group
 * @param groupAccess the minimum access level for members of the LDAP group
 * @param provider the LDAP provider for the LDAP group
 * @throws GitLabApiException if any exception occurs
 */
public void addLdapGroupLink(Object groupIdOrPath, String cn, AccessLevel groupAccess, String provider) throws GitLabApiException {

    if (groupAccess == null) {
        throw new RuntimeException("groupAccess cannot be null or empty");
    }

    addLdapGroupLink(groupIdOrPath, cn, groupAccess.toValue(), provider);
}
 
Example #25
Source File: ProjectApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Approve access for the specified user to the specified project.
 *
 * <pre><code>GitLab Endpoint: PUT /projects/:id/access_requests/:user_id/approve</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
 * @param userId the user ID to approve access for
 * @param accessLevel the access level the user is approved for, if null will be developer (30)
 * @return the approved AccessRequest instance
 * @throws GitLabApiException if any exception occurs
 */
public AccessRequest approveAccessRequest(Object projectIdOrPath, Integer userId, AccessLevel accessLevel) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("access_level", accessLevel);
    Response response = this.putWithFormData(Response.Status.CREATED, formData,
            "projects", getProjectIdOrPath(projectIdOrPath), "access_requests", userId, "approve");
    return (response.readEntity(AccessRequest.class));
}
 
Example #26
Source File: ProjectApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Share a project with the specified group.
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/share</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
 * @param groupId the ID of the group to share with, required
 * @param accessLevel the permissions level to grant the group, required
 * @param expiresAt the share expiration date, optional
 * @throws GitLabApiException if any exception occurs
 */
public void shareProject(Object projectIdOrPath, Integer groupId, AccessLevel accessLevel, Date expiresAt)
        throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm()
        .withParam("group_id", groupId, true)
        .withParam("group_access", accessLevel, true)
        .withParam("expires_at", expiresAt);
    post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "share");
}
 
Example #27
Source File: GroupApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Approve access for the specified user to the specified group.
 *
 * <pre><code>PUT /groups/:id/access_requests/:user_id/approve</code></pre>
 *
 * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
 * @param userId the user ID to approve access for
 * @param accessLevel the access level the user is approved for, if null will be developer (30)
 * @return the approved AccessRequest instance
 * @throws GitLabApiException if any exception occurs
 */
public AccessRequest approveAccessRequest(Object groupIdOrPath, Integer userId, AccessLevel accessLevel) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("access_level", accessLevel);
    Response response = this.putWithFormData(Response.Status.CREATED, formData,
            "groups", getGroupIdOrPath(groupIdOrPath), "access_requests", userId, "approve");
    return (response.readEntity(AccessRequest.class));
}
 
Example #28
Source File: ProtectedBranchesApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Protects a single repository branch or several project repository branches using a wildcard protected branch.
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/protected_branches</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
 * @param branchName the name of the branch to protect, can be a wildcard
 * @param pushAccessLevel access levels allowed to push (defaults: 40, maintainer access level)
 * @param mergeAccessLevel access levels allowed to merge (defaults: 40, maintainer access level)
 * @param unprotectAccessLevel access levels allowed to unprotect (defaults: 40, maintainer access level)
 * @param codeOwnerApprovalRequired prevent pushes to this branch if it matches an item in the CODEOWNERS file. (defaults: false)
 * @return the branch info for the protected branch
 * @throws GitLabApiException if any exception occurs
 */
public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName,
 AccessLevel pushAccessLevel, AccessLevel mergeAccessLevel, AccessLevel unprotectAccessLevel,
 Boolean codeOwnerApprovalRequired) throws GitLabApiException {
    Form formData = new GitLabApiForm()
            .withParam("name", branchName, true)
            .withParam("push_access_level", pushAccessLevel)
            .withParam("merge_access_level", mergeAccessLevel)
            .withParam("unprotect_access_level", unprotectAccessLevel)
            .withParam("code_owner_approval_required", codeOwnerApprovalRequired);
    Response response = post(Response.Status.CREATED, formData.asMap(),
            "projects", getProjectIdOrPath(projectIdOrPath), "protected_branches");
    return (response.readEntity(ProtectedBranch.class));
}
 
Example #29
Source File: ProtectedBranchesApi.java    From gitlab4j-api with MIT License 2 votes vote down vote up
/**
 * Protects a single repository branch or several project repository branches using a wildcard protected branch.
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/protected_branches</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
 * @param branchName the name of the branch to protect, can be a wildcard
 * @param pushAccessLevel Access levels allowed to push (defaults: 40, maintainer access level)
 * @param mergeAccessLevel Access levels allowed to merge (defaults: 40, maintainer access level)
 * @return the branch info for the protected branch
 * @throws GitLabApiException if any exception occurs
 */
public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, AccessLevel pushAccessLevel, AccessLevel mergeAccessLevel) throws GitLabApiException {
    return (protectBranch(projectIdOrPath, branchName, pushAccessLevel, mergeAccessLevel, null, null));
}
 
Example #30
Source File: ProtectedBranchesApi.java    From gitlab4j-api with MIT License 2 votes vote down vote up
/**
 * Protects a single repository branch or several project repository branches
 * using a wildcard protected branch.
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/protected_branches</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
 * @param branchName the name of the branch to protect, can be a wildcard
 * @return the branch info for the protected branch
 * @throws GitLabApiException if any exception occurs
 */
public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName) throws GitLabApiException {
    return protectBranch(projectIdOrPath, branchName, AccessLevel.MAINTAINER, AccessLevel.MAINTAINER);
}