use of de.tum.in.www1.artemis.domain.ProgrammingExercise in project ArTEMiS by ls1intum.
the class ExerciseService method cleanup.
/**
* Delete build plans (except BASE) and optionally repositores of all exercise participations.
*
* @param id id of the exercise for which build plans in respective participations are deleted
*/
@Transactional
public java.io.File cleanup(Long id, boolean deleteRepositories) throws java.io.IOException {
Exercise exercise = findOneLoadParticipations(id);
log.info("Request to cleanup all participations for Exercise : {}", exercise.getTitle());
List<Repository> studentRepositories = new ArrayList<>();
Path finalZipFilePath = null;
if (Optional.ofNullable(exercise).isPresent() && exercise instanceof ProgrammingExercise) {
exercise.getParticipations().forEach(participation -> {
if (participation.getBuildPlanId() != null) {
// ignore participations without build plan id
try {
continuousIntegrationService.get().deleteBuildPlan(participation.getBuildPlanId());
} catch (BambooException ex) {
log.error(ex.getMessage());
if (ex.getCause() != null) {
log.error(ex.getCause().getMessage());
}
}
participation.setInitializationState(ParticipationState.INACTIVE);
participation.setBuildPlanId(null);
participationService.save(participation);
}
if (deleteRepositories == true && participation.getRepositoryUrl() != null) {
// ignore participations without repository URL
try {
// 1. clone the repository
Repository repo = gitService.get().getOrCheckoutRepository(participation);
// 2. collect the repo file
studentRepositories.add(repo);
} catch (GitAPIException | IOException ex) {
log.error("Archiving and deleting the repository " + participation.getRepositoryUrlAsUrl() + " did not work as expected", ex);
}
}
});
if (deleteRepositories == false) {
// in this case, we are done
return null;
}
if (studentRepositories.isEmpty()) {
log.info("No student repositories have been found.");
return null;
}
// from here on, deleteRepositories is true and does not need to be evaluated again
log.info("Create zip file for all repositories");
Files.createDirectories(Paths.get("zippedRepos"));
finalZipFilePath = Paths.get("zippedRepos", exercise.getCourse().getTitle() + " " + exercise.getTitle() + " Student Repositories.zip");
zipAllRepositories(studentRepositories, finalZipFilePath);
exercise.getParticipations().forEach(participation -> {
if (participation.getRepositoryUrl() != null) {
// ignore participations without repository URL
try {
// 3. delete the locally cloned repo again
gitService.get().deleteLocalRepository(participation);
} catch (IOException e) {
log.error("Archiving and deleting the repository " + participation.getRepositoryUrlAsUrl() + " did not work as expected", e);
}
// 4. finally delete the repository on the VC Server
versionControlService.get().deleteRepository(participation.getRepositoryUrlAsUrl());
participation.setRepositoryUrl(null);
participation.setInitializationState(ParticipationState.FINISHED);
participationService.save(participation);
}
});
scheduleForDeletion(finalZipFilePath, 300);
} else {
log.info("Exercise with id {} is not an instance of ProgrammingExercise. Ignoring the request to cleanup repositories and build plan", id);
return null;
}
return new java.io.File(finalZipFilePath.toString());
}
use of de.tum.in.www1.artemis.domain.ProgrammingExercise in project ArTEMiS by ls1intum.
the class ExerciseService method archive.
// does not delete anything
@Transactional
public java.io.File archive(Long id) {
Exercise exercise = findOneLoadParticipations(id);
log.info("Request to archive all participations repositories for Exercise : {}", exercise.getTitle());
List<Path> zippedRepoFiles = new ArrayList<>();
Path finalZipFilePath = null;
if (Optional.ofNullable(exercise).isPresent() && exercise instanceof ProgrammingExercise) {
exercise.getParticipations().forEach(participation -> {
try {
if (participation.getRepositoryUrl() != null) {
// ignore participations without repository URL
// 1. clone the repository
Repository repo = gitService.get().getOrCheckoutRepository(participation);
// 2. zip repository and collect the zip file
log.info("Create temporary zip file for repository " + repo.getLocalPath().toString());
Path zippedRepoFile = gitService.get().zipRepository(repo);
zippedRepoFiles.add(zippedRepoFile);
// 3. delete the locally cloned repo again
gitService.get().deleteLocalRepository(participation);
}
} catch (IOException | GitAPIException ex) {
log.error("Archiving and deleting the repository " + participation.getRepositoryUrlAsUrl() + " did not work as expected");
}
});
if (!exercise.getParticipations().isEmpty() && !zippedRepoFiles.isEmpty()) {
try {
// create a large zip file with all zipped repos and provide it for download
log.info("Create zip file for all repositories");
finalZipFilePath = Paths.get(zippedRepoFiles.get(0).getParent().toString(), exercise.getCourse().getTitle() + " " + exercise.getTitle() + " Student Repositories.zip");
createZipFile(finalZipFilePath, zippedRepoFiles);
scheduleForDeletion(finalZipFilePath, 300);
log.info("Delete all temporary zip repo files");
// delete the temporary zipped repo files
for (Path zippedRepoFile : zippedRepoFiles) {
Files.delete(zippedRepoFile);
}
} catch (IOException ex) {
log.error("Archiving and deleting the local repositories did not work as expected");
}
} else {
log.info("The zip file could not be created. Ignoring the request to archive repositories", id);
return null;
}
} else {
log.info("Exercise with id {} is not an instance of ProgrammingExercise. Ignoring the request to archive repositories", id);
return null;
}
return new java.io.File(finalZipFilePath.toString());
}
use of de.tum.in.www1.artemis.domain.ProgrammingExercise in project ArTEMiS by ls1intum.
the class ProgrammingExerciseResource method getProgrammingExercisesForCourse.
/**
* GET /courses/:courseId/exercises : get all the exercises.
*
* @return the ResponseEntity with status 200 (OK) and the list of programmingExercises in body
*/
@GetMapping(value = "/courses/{courseId}/programming-exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
@Transactional(readOnly = true)
public ResponseEntity<List<ProgrammingExercise>> getProgrammingExercisesForCourse(@PathVariable Long courseId) {
log.debug("REST request to get all ProgrammingExercises for the course with id : {}", courseId);
Course course = courseService.findOne(courseId);
User user = userService.getUserWithGroupsAndAuthorities();
if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
List<ProgrammingExercise> exercises = programmingExerciseRepository.findByCourseId(courseId);
return ResponseEntity.ok().body(exercises);
}
use of de.tum.in.www1.artemis.domain.ProgrammingExercise in project ArTEMiS by ls1intum.
the class ProgrammingExerciseResource method createProgrammingExercise.
/**
* POST /programming-exercises : Create a new programmingExercise.
*
* @param programmingExercise the programmingExercise to create
* @return the ResponseEntity with status 201 (Created) and with body the new programmingExercise, or with status 400 (Bad Request) if the programmingExercise has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/programming-exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public ResponseEntity<ProgrammingExercise> createProgrammingExercise(@RequestBody ProgrammingExercise programmingExercise) throws URISyntaxException {
log.debug("REST request to save ProgrammingExercise : {}", programmingExercise);
if (programmingExercise.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new programmingExercise cannot already have an ID")).body(null);
}
// fetch course from database to make sure client didn't change groups
Course course = courseService.findOne(programmingExercise.getCourse().getId());
if (course == null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "courseNotFound", "The course belonging to this programming exercise does not exist")).body(null);
}
User user = userService.getUserWithGroupsAndAuthorities();
if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
ResponseEntity<ProgrammingExercise> errorResponse = checkProgrammingExerciseForError(programmingExercise);
if (errorResponse != null) {
return errorResponse;
}
ProgrammingExercise result = programmingExerciseRepository.save(programmingExercise);
return ResponseEntity.created(new URI("/api/programming-exercises/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
use of de.tum.in.www1.artemis.domain.ProgrammingExercise in project ArTEMiS by ls1intum.
the class ExerciseResource method createExercise.
/**
* POST /exercises : Create a new exercise.
*
* @param exercise the exercise to create
* @return the ResponseEntity with status 201 (Created) and with body the new exercise, or with status 400 (Bad Request) if the exercise has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public // TODO: test if it still works with abstract entity in body
ResponseEntity<Exercise> createExercise(@RequestBody Exercise exercise) throws URISyntaxException {
log.debug("REST request to save Exercise : {}", exercise);
Course course = exercise.getCourse();
User user = userService.getUserWithGroupsAndAuthorities();
if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (exercise.getId() != null) {
throw new BadRequestAlertException("A new exercise cannot already have an ID", ENTITY_NAME, "idexists");
}
if (exercise instanceof ProgrammingExercise) {
ResponseEntity<Exercise> errorResponse = checkProgrammingExerciseForError((ProgrammingExercise) exercise);
if (errorResponse != null) {
return errorResponse;
}
}
Exercise result = exerciseRepository.save(exercise);
return ResponseEntity.created(new URI("/api/exercises/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Aggregations