Search in sources :

Example 1 with User

use of org.gitlab4j.api.models.User in project octane-gitlab-service by MicroFocus.

the class GitlabServices method init.

@PostConstruct
private void init() throws MalformedURLException {
    // Adding webHooks
    initWebHookListenerURL();
    gitLabApi = gitLabApiWrapper.getGitLabApi();
    try {
        List<Project> projects = isCurrentUserAdmin() ? gitLabApi.getProjectApi().getProjects() : gitLabApi.getProjectApi().getMemberProjects();
        User currentUser = gitLabApi.getUserApi().getCurrentUser();
        if (cleanupOnly) {
            log.info("start with cleanup process");
            for (Project project : projects) {
                if (gitLabApiWrapper.isUserHasPermissionForProject(project, currentUser)) {
                    deleteWebHooks(project.getId());
                }
            }
        } else {
            for (Project project : projects) {
                if (gitLabApiWrapper.isUserHasPermissionForProject(project, currentUser)) {
                    addWebHookToProject(project.getId(), true);
                }
            }
        }
    } catch (GitLabApiException e) {
        log.warn("Failed to create GitLab web hooks", e);
        throw new RuntimeException(e);
    }
    // start cleanUp thread
    executor = Executors.newSingleThreadScheduledExecutor();
    executor.scheduleAtFixedRate(new TestResultsCleanUpRunnable(applicationSettings.getConfig().getTestResultsOutputFolderPath()), TestResultsCleanUpRunnable.INTERVAL, TestResultsCleanUpRunnable.INTERVAL, TimeUnit.MINUTES);
}
Also used : Project(org.gitlab4j.api.models.Project) User(org.gitlab4j.api.models.User) GitLabApiException(org.gitlab4j.api.GitLabApiException) TestResultsCleanUpRunnable(com.microfocus.octane.gitlab.testresults.TestResultsCleanUpRunnable) PostConstruct(javax.annotation.PostConstruct)

Example 2 with User

use of org.gitlab4j.api.models.User in project legend-sdlc by finos.

the class AbstractGitLabApiTest method prepareGitLabUser.

/**
 * Create the proper users for authenticating the GitLab operations.
 */
protected static void prepareGitLabUser() throws LegendSDLCServerException {
    try {
        GitLabApi rootGitLabApi = GitLabApi.oauth2Login(TEST_HOST_URL, TEST_ADMIN_USERNAME, TEST_ADMIN_PASSWORD, null, null, true);
        Optional<User> testUser = rootGitLabApi.getUserApi().getOptionalUser(TEST_OWNER_USERNAME);
        if (!testUser.isPresent()) {
            User userSettings = new User().withUsername(TEST_OWNER_USERNAME).withEmail(TEST_OWNER_USERNAME + "@testUser.org").withName("Owner User").withSkipConfirmation(true).withIsAdmin(true);
            rootGitLabApi.getUserApi().createUser(userSettings, TEST_OWNER_PASSWORD, false);
            LOGGER.info("Created user with name {} and username {}", userSettings.getName(), userSettings.getUsername());
        }
        Optional<User> testMember = rootGitLabApi.getUserApi().getOptionalUser(TEST_MEMBER_USERNAME);
        if (!testMember.isPresent()) {
            User userSettings = new User().withUsername(TEST_MEMBER_USERNAME).withEmail(TEST_MEMBER_PASSWORD + "@testUser.org").withName("Member User").withSkipConfirmation(true).withIsAdmin(true);
            rootGitLabApi.getUserApi().createUser(userSettings, TEST_MEMBER_PASSWORD, false);
            LOGGER.info("Created user with name {} and username {}", userSettings.getName(), userSettings.getUsername());
        }
    } catch (GitLabApiException e) {
        StringBuilder builder = new StringBuilder("Error creating user for authentication; response status: ").append(e.getHttpStatus());
        String eMessage = e.getMessage();
        if (eMessage != null) {
            builder.append("; error message: ").append(eMessage);
        }
        if (e.hasValidationErrors()) {
            builder.append("; validation error(s): ").append(e.getValidationErrors());
        }
        throw new LegendSDLCServerException(builder.toString(), e);
    }
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) User(org.gitlab4j.api.models.User) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 3 with User

use of org.gitlab4j.api.models.User in project choerodon-starters by open-hand.

the class UserApi method createUser.

/**
 * Creates a new user. Note only administrators can create new users.
 * <p>
 * POST /users
 * <p>
 * email (required) - Email
 * password (required) - Password
 * username (required) - Username
 * name (required) - Name
 * skype (optional) - Skype ID
 * linkedin (optional) - Linkedin
 * twitter (optional) - Twitter account
 * website_url (optional) - Website url
 * projects_limit (optional) - Number of projects user can create
 * extern_uid (optional) - External UID
 * provider (optional) - External provider name
 * bio (optional) - User's bio
 * admin (optional) - User is admin - true or false (default)
 * can_create_group (optional) - User can create groups - true or false
 *
 * @param user          the User instance with the user info to create
 * @param password      the password for the new user
 * @param projectsLimit the maximum number of project
 * @return created User instance
 * @throws GitLabApiException if any exception occurs
 */
public User createUser(User user, String password, Integer projectsLimit) throws GitLabApiException {
    Form formData = userToForm(user, projectsLimit, password, true);
    Response response = post(Response.Status.CREATED, formData, "users");
    return (response.readEntity(User.class));
}
Also used : Response(javax.ws.rs.core.Response) User(org.gitlab4j.api.models.User) Form(javax.ws.rs.core.Form)

Example 4 with User

use of org.gitlab4j.api.models.User in project choerodon-starters by open-hand.

the class UserApi method getActiveUsers.

/**
 * Get a list of active users using the specified page and per page settings.
 * <p>
 * GET /users?active=true
 *
 * @param page    the page to get
 * @param perPage the number of users per page
 * @return the list of active Users in the specified range
 * @throws GitLabApiException if any exception occurs
 */
public List<User> getActiveUsers(int page, int perPage) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("active", true).withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage);
    Response response = get(Response.Status.OK, formData.asMap(), "users");
    return (response.readEntity(new GenericType<List<User>>() {
    }));
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) User(org.gitlab4j.api.models.User)

Example 5 with User

use of org.gitlab4j.api.models.User in project choerodon-starters by open-hand.

the class UserApi method getUser.

/**
 * Lookup a user by username.
 * <p>
 * NOTE: This is for admin users only.
 * <p>
 * GET /users?username=:username
 *
 * @param username the username of the user to get
 * @return the User instance for the specified username
 * @throws GitLabApiException if any exception occurs
 */
public User getUser(String username) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("username", username, true);
    Response response = get(Response.Status.OK, formData.asMap(), "users");
    List<User> users = response.readEntity(new GenericType<List<User>>() {
    });
    return (users.isEmpty() ? null : users.get(0));
}
Also used : Response(javax.ws.rs.core.Response) User(org.gitlab4j.api.models.User) List(java.util.List)

Aggregations

User (org.gitlab4j.api.models.User)29 Response (javax.ws.rs.core.Response)9 GitLabApiException (org.gitlab4j.api.GitLabApiException)8 GenericType (javax.ws.rs.core.GenericType)6 UserApi (org.gitlab4j.api.UserApi)6 Test (org.junit.Test)6 GitUser (de.catma.repository.git.GitUser)5 File (java.io.File)5 Project (org.gitlab4j.api.models.Project)4 IOException (java.io.IOException)3 CustomAttribute (org.gitlab4j.api.models.CustomAttribute)3 PersonalAccessToken (org.gitlab4j.api.models.PersonalAccessToken)3 ILocalGitRepositoryManager (de.catma.repository.git.interfaces.ILocalGitRepositoryManager)2 IRemoteGitServerManager (de.catma.repository.git.interfaces.IRemoteGitServerManager)2 GitLabServerManagerTest (de.catma.repository.git.managers.GitLabServerManagerTest)2 PersonalAccessToken (de.catma.repository.git.managers.gitlab4j_api_custom.models.PersonalAccessToken)2 Pair (de.catma.util.Pair)2 SecureRandom (java.security.SecureRandom)2 List (java.util.List)2 Form (javax.ws.rs.core.Form)2