Search in sources :

Example 16 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 17 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 18 with JenkinsException

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

the class JenkinsJobService method getFolderJob.

/**
 * Gets the folder job or null if it doesn't exist
 * @param folderName the name of the folder job
 * @return the folder job
 */
public FolderJob getFolderJob(String folderName) {
    try {
        final var job = jenkinsServer.getJob(folderName);
        if (job == null) {
            return null;
        }
        final var folderJob = jenkinsServer.getFolderJob(job);
        if (!folderJob.isPresent()) {
            return null;
        }
        return folderJob.get();
    } 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 19 with JenkinsException

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

the class JenkinsJobService method getFolderJob.

/**
 * Gets the folder job or null if it doesn't exist
 * @param folderName the name of the folder job
 * @return the folder job
 */
public FolderJob getFolderJob(String folderName) {
    try {
        final var job = jenkinsServer.getJob(folderName);
        if (job == null) {
            return null;
        }
        final var folderJob = jenkinsServer.getFolderJob(job);
        if (!folderJob.isPresent()) {
            return null;
        }
        return folderJob.get();
    } 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 20 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