Java Code Examples for org.gitlab4j.api.models.User#getId()

The following examples show how to use org.gitlab4j.api.models.User#getId() . 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: GitLabOwner.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
@NonNull
public static GitLabOwner fetchOwner(GitLabApi gitLabApi, String projectOwner) {
    try {
        Group group = gitLabApi.getGroupApi().getGroup(projectOwner);
        return new GitLabGroup(group.getName(), group.getWebUrl(), group.getAvatarUrl(),
            group.getId(), group.getFullName(), group.getDescription());
    } catch (GitLabApiException e) {
        if (e.getHttpStatus() != 404) {
            throw new IllegalStateException("Unable to fetch Group", e);
        }

        try {
            User user = gitLabApi.getUserApi().getUser(projectOwner);
            // If user is not found, null is returned
            if (user == null) {
                throw new IllegalStateException(
                    String.format("Owner '%s' is neither a user/group/subgroup", projectOwner));
            }
            return new GitLabUser(user.getName(), user.getWebUrl(), user.getAvatarUrl(),
                user.getId());
        } catch (GitLabApiException e1) {
            throw new IllegalStateException("Unable to fetch User", e1);
        }
    }
}
 
Example 2
Source File: GitLabApi.java    From choerodon-starters with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up all future calls to the GitLab API to be done as another user specified by sudoAsUsername.
 * To revert back to normal non-sudo operation you must call unsudo(), or pass null as the username.
 *
 * @param sudoAsUsername the username to sudo as, null will turn off sudo
 * @throws GitLabApiException if any exception occurs
 */
public void sudo(String sudoAsUsername) throws GitLabApiException {

    if (sudoAsUsername == null || sudoAsUsername.trim().length() == 0) {
        apiClient.setSudoAsId(null);
        return;
    }

    // Get the User specified by username, if you are not an admin or the username is not found, this will fail
    User user = getUserApi().getUser(sudoAsUsername);
    if (user == null || user.getId() == null) {
        throw new GitLabApiException("the specified username was not found");
    }

    Integer sudoAsId = user.getId();
    apiClient.setSudoAsId(sudoAsId);
}
 
Example 3
Source File: GitLabApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
/**
 * Sets up all future calls to the GitLab API to be done as another user specified by sudoAsUsername.
 * To revert back to normal non-sudo operation you must call unsudo(), or pass null as the username.
 *
 * @param sudoAsUsername the username to sudo as, null will turn off sudo
 * @throws GitLabApiException if any exception occurs
 */
public void sudo(String sudoAsUsername) throws GitLabApiException {

    if (sudoAsUsername == null || sudoAsUsername.trim().length() == 0) {
        apiClient.setSudoAsId(null);
        return;
    }

    // Get the User specified by username, if you are not an admin or the username is not found, this will fail
    User user = getUserApi().getUser(sudoAsUsername);
    if (user == null || user.getId() == null) {
        throw new GitLabApiException("the specified username was not found");
    }

    Integer sudoAsId = user.getId();
    apiClient.setSudoAsId(sudoAsId);
}
 
Example 4
Source File: TestProjectApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testDenyRequestAccess() throws GitLabApiException {

    assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0);

    gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME);
    User user = gitLabApi.getUserApi().getCurrentUser();
    assertNotNull(user);
    final Integer userId = user.getId();

    try {
        try {

            AccessRequest accessRequest = gitLabApi.getProjectApi().requestAccess(testProject);
            assertNotNull(accessRequest);
            assertEquals(userId, accessRequest.getId());

        } finally {
            gitLabApi.unsudo();
        }

        List<AccessRequest> requests = gitLabApi.getProjectApi().getAccessRequests(testProject);
        assertTrue(requests.stream().anyMatch(r -> r.getId() == userId));

        gitLabApi.getProjectApi().denyAccessRequest(testProject, userId);

        requests = gitLabApi.getProjectApi().getAccessRequests(testProject);
        assertFalse(requests.stream().anyMatch(r -> r.getId() == userId));

        user = null;

    } finally {
        try {
            if (user != null) {
                gitLabApi.getProjectApi().denyAccessRequest(testProject, userId);
            }
        } catch (Exception ignore) {
        }
    }
}
 
Example 5
Source File: TestGroupApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testDenyRequestAccess() throws GitLabApiException {

    assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0);

    gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME);
    User user = gitLabApi.getUserApi().getCurrentUser();
    assertNotNull(user);
    final Integer userId = user.getId();

    try {
        try {

            AccessRequest accessRequest = gitLabApi.getGroupApi().requestAccess(testGroup);
            assertNotNull(accessRequest);
            assertEquals(userId, accessRequest.getId());

        } finally {
            gitLabApi.unsudo();
        }

        List<AccessRequest> requests = gitLabApi.getGroupApi().getAccessRequests(testGroup);
        assertTrue(requests.stream().anyMatch(r -> r.getId() == userId));

        gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);

        requests = gitLabApi.getGroupApi().getAccessRequests(testGroup);
        assertFalse(requests.stream().anyMatch(r -> r.getId() == userId));

        user = null;

    } finally {
        try {
            if (user != null) {
                gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
            }
        } catch (Exception ignore) {
        }
    }
}
 
Example 6
Source File: TestUserApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testSudoAsUser() throws GitLabApiException {

    assumeTrue(TEST_SUDO_AS_USERNAME != null && TEST_SUDO_AS_USERNAME.length() > 0);

    try {

        gitLabApi.sudo(TEST_SUDO_AS_USERNAME);
        User user = gitLabApi.getUserApi().getCurrentUser();
        assertNotNull(user);
        assertEquals(TEST_SUDO_AS_USERNAME, user.getUsername());
        Integer sudoAsId = user.getId();

        gitLabApi.sudo(null);
        user = gitLabApi.getUserApi().getCurrentUser();
        assertNotNull(user);
        assertEquals(TEST_USERNAME, user.getUsername());

        gitLabApi.unsudo();
        assertNull(gitLabApi.getSudoAsId());

        gitLabApi.setSudoAsId(sudoAsId);
        user = gitLabApi.getUserApi().getCurrentUser();
        assertNotNull(user);
        assertEquals(sudoAsId, user.getId());
        assertEquals(TEST_SUDO_AS_USERNAME, user.getUsername());

    } finally {
        gitLabApi.unsudo();
    }
}