Search in sources :

Example 6 with JenkinsException

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

the class JenkinsJobService method createJobInFolder.

/**
 * Creates a job inside a folder
 * @param jobConfig the config of the job to create
 * @param folderName the name of the folder
 * @param jobName the name of the job
 */
public void createJobInFolder(Document jobConfig, String folderName, String jobName) {
    try {
        var folder = getFolderJob(folderName);
        if (folder == null) {
            throw new JenkinsException("Cannot create job " + jobName + " because the folder " + folderName + " does not exist.");
        }
        var existingJob = jenkinsServer.getJob(folder, jobName);
        if (existingJob != null) {
            log.info("Build Plan {} already exists. Skipping creation of job.", jobName);
            return;
        }
        String configString = XmlFileUtils.writeToString(jobConfig);
        jenkinsServer.createJob(folder, jobName, configString, useCrumb);
    } catch (IOException | TransformerException e) {
        log.error(e.getMessage(), e);
        throw new JenkinsException(e.getMessage(), e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Example 7 with JenkinsException

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

the class JenkinsJobService method getJobConfigForJobInFolder.

/**
 * Gets the xml config of the job that is inside a folder and replaces the old reference to the master and main branch by a reference to the default branch
 * @param folderName the name of the folder
 * @param jobName the name of the job
 * @return the xml document
 */
public Document getJobConfigForJobInFolder(String folderName, String jobName) {
    try {
        var folder = getFolderJob(folderName);
        if (folder == null) {
            throw new JenkinsException("The folder " + folderName + "does not exist.");
        }
        String xmlString = jenkinsServer.getJobXml(folder, jobName);
        // Replace the old reference to the master and main branch by a reference to the default branch
        xmlString = xmlString.replace("*/master", "**");
        xmlString = xmlString.replace("*/main", "**");
        return XmlFileUtils.readFromString(xmlString);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new JenkinsException(e.getMessage(), e);
    }
}
Also used : JenkinsException(de.tum.in.www1.artemis.exception.JenkinsException) IOException(java.io.IOException)

Example 8 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 9 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 10 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)

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