Search in sources :

Example 11 with JenkinsException

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

the class JenkinsUserManagementService method removeUserFromGroups.

/**
 * Removes the Artemis user from the specified groups. Jenkins doesn't support groups so this function fetches
 * all programming exercises belonging to the groups, and revokes the user's permissions from them.
 *
 * @param userLogin The login of the Artemis user to remove from the group
 * @param groups    The groups to remove the user from
 */
@Override
public void removeUserFromGroups(String userLogin, Set<String> groups) throws ContinuousIntegrationException {
    // Remove all permissions assigned to the user for each exercise that belongs to the specified groups.
    var exercises = programmingExerciseRepository.findAllByInstructorOrEditorOrTAGroupNameIn(groups);
    log.info("Update Jenkins permissions for programming exercises: " + exercises.stream().map(ProgrammingExercise::getProjectKey).toList());
    exercises.forEach(exercise -> {
        try {
            // The exercise's projectkey is also the name of the Jenkins folder job which groups the student's, solution,
            // and template build plans together
            var jobName = exercise.getProjectKey();
            jenkinsJobPermissionsService.removePermissionsFromUserOfFolder(userLogin, jobName, Set.of(JenkinsJobPermission.values()));
        } catch (IOException e) {
            throw new JenkinsException("Cannot revoke permissions from user: " + userLogin, e);
        }
    });
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) IOException(java.io.IOException)

Example 12 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 13 with JenkinsException

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

the class JenkinsUserManagementService method assignPermissionsToInstructorAndEditorAndTAsForCourse.

/**
 * Assigns teaching assistant and/or editor and/or instructor permissions to each user belonging to the teaching assistant/editor/instructor groups of the course.
 *
 * @param course               the course
 * @param programmingExercises list of programmingExercises for which the permissions should be changed
 */
private void assignPermissionsToInstructorAndEditorAndTAsForCourse(Course course, List<ProgrammingExercise> programmingExercises) {
    var instructors = userRepository.findAllInGroupWithAuthorities(course.getInstructorGroupName()).stream().map(User::getLogin).collect(Collectors.toSet());
    var editors = userRepository.findAllInGroupWithAuthorities(course.getEditorGroupName()).stream().map(User::getLogin).collect(Collectors.toSet());
    var teachingAssistants = userRepository.findAllInGroupWithAuthorities(course.getTeachingAssistantGroupName()).stream().map(User::getLogin).collect(Collectors.toSet());
    // Courses can have the same groups. We do not want to add/remove users from exercises of other courses belonging to the same group
    programmingExercises.forEach(exercise -> {
        var job = exercise.getProjectKey();
        try {
            jenkinsJobPermissionsService.addInstructorAndEditorAndTAPermissionsToUsersForFolder(teachingAssistants, editors, instructors, job);
        } catch (IOException e) {
            throw new JenkinsException("Cannot assign teaching assistant and editor and instructor permissions for job: " + job, e);
        }
    });
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) IOException(java.io.IOException)

Example 14 with JenkinsException

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

the class JenkinsUserManagementService method removePermissionsFromInstructorsAndEditorsAndTAsForCourse.

/**
 * Removes all permissions assigned to instructors and editors and teaching assistants for the specified course. The function
 * fetches all exercises that belong to the course and removes all permissions assigned to all instructors and editors and
 * teaching assistants belonging to the groups.
 *
 * @param instructorGroup        the group of instructors
 * @param editorGroup            the group of editors
 * @param teachingAssistantGroup the group of teaching assistants
 * @param course                 the course
 * @param programmingExercises   list of programmingExercises for which the permissions should be changed
 */
private void removePermissionsFromInstructorsAndEditorsAndTAsForCourse(String instructorGroup, String editorGroup, String teachingAssistantGroup, Course course, List<ProgrammingExercise> programmingExercises) {
    // Fetch all instructors and editors and teaching assistants belonging to the group that was removed from the course.
    var oldInstructors = userRepository.findAllInGroupWithAuthorities(instructorGroup);
    var oldEditors = userRepository.findAllInGroupWithAuthorities(editorGroup);
    var oldTeachingAssistants = userRepository.findAllInGroupWithAuthorities(teachingAssistantGroup);
    var usersFromOldGroup = Stream.concat(oldInstructors.stream(), Stream.concat(oldEditors.stream(), oldTeachingAssistants.stream())).map(User::getLogin).collect(Collectors.toSet());
    // Revoke all permissions.
    programmingExercises.forEach(exercise -> {
        try {
            jenkinsJobPermissionsService.removePermissionsFromUsersForFolder(usersFromOldGroup, exercise.getProjectKey(), Set.of(JenkinsJobPermission.values()));
        } catch (IOException e) {
            throw new JenkinsException("Cannot remove permissions from all users for job: " + exercise.getProjectKey(), e);
        }
    });
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) IOException(java.io.IOException)

Example 15 with JenkinsException

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

the class JenkinsUserManagementService method removeUserFromGroups.

/**
 * Removes the Artemis user from the specified groups. Jenkins doesn't support groups so this function fetches
 * all programming exercises belonging to the groups, and revokes the user's permissions from them.
 *
 * @param userLogin The login of the Artemis user to remove from the group
 * @param groups    The groups to remove the user from
 */
@Override
public void removeUserFromGroups(String userLogin, Set<String> groups) throws ContinuousIntegrationException {
    // Remove all permissions assigned to the user for each exercise that belongs to the specified groups.
    var exercises = programmingExerciseRepository.findAllByInstructorOrEditorOrTAGroupNameIn(groups);
    log.info("Update Jenkins permissions for programming exercises: " + exercises.stream().map(ProgrammingExercise::getProjectKey).toList());
    exercises.forEach(exercise -> {
        try {
            // The exercise's projectkey is also the name of the Jenkins folder job which groups the student's, solution,
            // and template build plans together
            var jobName = exercise.getProjectKey();
            jenkinsJobPermissionsService.removePermissionsFromUserOfFolder(userLogin, jobName, Set.of(JenkinsJobPermission.values()));
        } catch (IOException e) {
            throw new JenkinsException("Cannot revoke permissions from 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