Search in sources :

Example 1 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)

Example 2 with JenkinsException

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

the class JenkinsService method getLatestBuildLogs.

@Override
public List<BuildLogEntry> getLatestBuildLogs(ProgrammingSubmission programmingSubmission) {
    ProgrammingExerciseParticipation programmingExerciseParticipation = (ProgrammingExerciseParticipation) programmingSubmission.getParticipation();
    String projectKey = programmingExerciseParticipation.getProgrammingExercise().getProjectKey();
    String buildPlanId = programmingExerciseParticipation.getBuildPlanId();
    ProgrammingLanguage programmingLanguage = programmingExerciseParticipation.getProgrammingExercise().getProgrammingLanguage();
    try {
        final var build = jenkinsJobService.getJobInFolder(projectKey, buildPlanId).getLastBuild();
        List<BuildLogEntry> buildLogEntries;
        // Attempt to parse pipeline logs
        final String pipelineLogs = build.details().getConsoleOutputText();
        if (pipelineLogs != null && pipelineLogs.contains("[Pipeline] Start of Pipeline")) {
            buildLogEntries = JenkinsBuildLogParseUtils.parseBuildLogsFromJenkinsLogs(List.of(pipelineLogs.split("\n")));
        } else {
            // Fallback to legacy logs
            final var logHtml = Jsoup.parse(build.details().getConsoleOutputHtml()).body();
            buildLogEntries = JenkinsBuildLogParseUtils.parseLogsLegacy(logHtml);
        }
        // Filter and save build logs
        buildLogEntries = filterUnnecessaryLogs(buildLogEntries, programmingLanguage);
        buildLogEntries = buildLogService.saveBuildLogs(buildLogEntries, programmingSubmission);
        programmingSubmission.setBuildLogEntries(buildLogEntries);
        programmingSubmissionRepository.save(programmingSubmission);
        return buildLogEntries;
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new JenkinsException(e.getMessage(), e);
    }
}
Also used : ProgrammingExerciseParticipation(de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseParticipation) JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) ProgrammingLanguage(de.tum.in.www1.artemis.domain.enumeration.ProgrammingLanguage) IOException(java.io.IOException)

Example 3 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 4 with JenkinsException

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

the class JenkinsUserManagementService method deleteUser.

@Override
public void deleteUser(User user) throws ContinuousIntegrationException {
    var userLogin = user.getLogin();
    if (userLogin.equals(jenkinsAdminUsername)) {
        log.debug("Jenkins deleteUser: Skipping jenkinsAdminUser: {}.", jenkinsAdminUsername);
        return;
    }
    // Only delete a user if it exists.
    var jenkinsUser = getUser(userLogin);
    if (jenkinsUser == null) {
        return;
    }
    try {
        var uri = UriComponentsBuilder.fromHttpUrl(jenkinsServerUrl.toString()).pathSegment("user", userLogin, "doDelete").build().toUri();
        restTemplate.exchange(uri, HttpMethod.POST, null, Void.class);
        removeUserFromGroups(userLogin, getUserWithGroups(user).getGroups());
    } catch (RestClientException e) {
        throw new JenkinsException("Cannot delete user: " + userLogin, e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) RestClientException(org.springframework.web.client.RestClientException)

Example 5 with JenkinsException

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

the class JenkinsUserManagementService method addUserToGroups.

/**
 * Adds the Artemis user to a group in Jenkins. Jenkins does not support
 * groups so this function fetches all programming exercises belonging to
 * the groups and assigns the user permissions to them.
 *
 * @param userLogin The user login to add to the group
 * @param groups    The groups to add the user to
 */
@Override
public void addUserToGroups(String userLogin, Set<String> groups) throws ContinuousIntegrationException {
    var exercises = programmingExerciseRepository.findAllByInstructorOrEditorOrTAGroupNameIn(groups);
    log.info("Update Jenkins permissions for programming exercises: " + exercises.stream().map(ProgrammingExercise::getProjectKey).toList());
    // TODO: in case we update a tutor group / role here, the tutor should NOT get access to exam exercises before the exam has finished
    exercises.forEach(exercise -> {
        // The exercise's project key is also the name of the Jenkins job that groups all build plans
        // for students, solution, and template.
        var jobName = exercise.getProjectKey();
        var course = exercise.getCourseViaExerciseGroupOrCourseMember();
        if (groups.contains(course.getInstructorGroupName())) {
            try {
                // We are assigning instructor permissions since the exercise's course instructor group is the same as the one that is specified.
                jenkinsJobPermissionsService.addPermissionsForUserToFolder(userLogin, jobName, JenkinsJobPermission.getInstructorPermissions());
            } catch (IOException e) {
                throw new JenkinsException("Cannot assign instructor permissions to user: " + userLogin, e);
            }
        } else if (groups.contains(course.getEditorGroupName())) {
            try {
                // We are assigning editor permissions since the exercise's course editor group is the same as the one that is specified.
                jenkinsJobPermissionsService.addPermissionsForUserToFolder(userLogin, jobName, JenkinsJobPermission.getEditorPermissions());
            } catch (IOException e) {
                throw new JenkinsException("Cannot assign editor permissions to user: " + userLogin, e);
            }
        } else if (groups.contains(course.getTeachingAssistantGroupName())) {
            try {
                // We are assigning teaching assistant permissions since the exercise's course teaching assistant group is the same as the one that is specified.
                jenkinsJobPermissionsService.addTeachingAssistantPermissionsToUserForFolder(userLogin, jobName);
            } catch (IOException e) {
                throw new JenkinsException("Cannot assign teaching assistant permissions to user: " + userLogin, e);
            }
        }
    });
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) IOException(java.io.IOException)

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