Search in sources :

Example 21 with JenkinsException

use of de.tum.in.www1.artemis.exception.JenkinsException in project Artemis by ls1intum.

the class JenkinsUserManagementService method createUser.

/**
 * Creates a user in Jenkins. Note that the user login acts as
 * a unique identifier in Jenkins.
 *
 * @param user     The user to create
 * @param password The user's password
 */
@Override
public void createUser(User user, String password) throws ContinuousIntegrationException {
    if (user.getLogin().equals(jenkinsAdminUsername)) {
        log.debug("Jenkins createUser: Skipping jenkinsAdminUser: {}.", jenkinsAdminUsername);
        return;
    }
    // Only create a user if it doesn't already exist.
    if (getUser(user.getLogin()) != null) {
        throw new JenkinsException("Cannot create user: " + user.getLogin() + " because the login already exists");
    }
    // Make sure the user login contains legal characters.
    if (!isUserLoginLegal(user)) {
        throw new JenkinsException("Cannot create user: " + user.getLogin() + " because the login contains illegal characters");
    }
    try {
        // Create the Jenkins user
        var uri = UriComponentsBuilder.fromHttpUrl(jenkinsServerUrl.toString()).pathSegment("securityRealm", "createAccountByAdmin").build().toUri();
        restTemplate.exchange(uri, HttpMethod.POST, getCreateUserFormHttpEntity(user, password), Void.class);
        // Adds the user to groups of existing programming exercises
        addUserToGroups(user.getLogin(), getUserWithGroups(user).getGroups());
    } catch (RestClientException e) {
        throw new JenkinsException("Cannot create user: " + user.getLogin(), e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) RestClientException(org.springframework.web.client.RestClientException)

Example 22 with JenkinsException

use of de.tum.in.www1.artemis.exception.JenkinsException in project ArTEMiS by ls1intum.

the class JenkinsUserManagementService method createUser.

/**
 * Creates a user in Jenkins. Note that the user login acts as
 * a unique identifier in Jenkins.
 *
 * @param user The user to create
 */
@Override
public void createUser(User user) throws ContinuousIntegrationException {
    if (user.getLogin().equals(jenkinsAdminUsername)) {
        log.debug("Jenkins createUser: Skipping jenkinsAdminUser: {}.", jenkinsAdminUsername);
        return;
    }
    // Only create a user if it doesn't already exist.
    if (getUser(user.getLogin()) != null) {
        throw new JenkinsException("Cannot create user: " + user.getLogin() + " because the login already exists");
    }
    // Make sure the user login contains legal characters.
    if (!isUserLoginLegal(user)) {
        throw new JenkinsException("Cannot create user: " + user.getLogin() + " because the login contains illegal characters");
    }
    try {
        // Create the Jenkins user
        var uri = UriComponentsBuilder.fromHttpUrl(jenkinsServerUrl.toString()).pathSegment("securityRealm", "createAccountByAdmin").build().toUri();
        restTemplate.exchange(uri, HttpMethod.POST, getCreateUserFormHttpEntity(user), Void.class);
        // Adds the user to groups of existing programming exercises
        addUserToGroups(user.getLogin(), getUserWithGroups(user).getGroups());
    } catch (RestClientException e) {
        throw new JenkinsException("Cannot create user: " + user.getLogin(), e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) RestClientException(org.springframework.web.client.RestClientException)

Aggregations

JenkinsException (de.tum.in.www1.artemis.exception.JenkinsException)22 IOException (java.io.IOException)16 RestClientException (org.springframework.web.client.RestClientException)6 ProgrammingLanguage (de.tum.in.www1.artemis.domain.enumeration.ProgrammingLanguage)2 ProgrammingExerciseParticipation (de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseParticipation)2 TransformerException (javax.xml.transform.TransformerException)2