use of de.tum.in.www1.artemis.domain.File 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.File in project ArTEMiS by ls1intum.
the class ExerciseService method createZipFile.
private void createZipFile(Path zipFilePath, List<Path> paths) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFilePath))) {
paths.stream().filter(path -> !Files.isDirectory(path)).forEach(path -> {
ZipEntry zipEntry = new ZipEntry(path.toString());
try {
zipOutputStream.putNextEntry(zipEntry);
Files.copy(path, zipOutputStream);
zipOutputStream.closeEntry();
} catch (Exception e) {
log.error("Create zip file error", e);
}
});
}
}
use of de.tum.in.www1.artemis.domain.File in project ArTEMiS by ls1intum.
the class ExerciseService method zipAllRepositories.
public Path zipAllRepositories(List<Repository> repositories, Path zipFilePath) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFilePath))) {
repositories.forEach(repository -> {
Path repoPath = repository.getLocalPath();
Path parentRepoPath = repoPath.getParent();
try {
Files.walk(repoPath).filter(path -> !Files.isDirectory(path)).forEach(path -> {
ZipEntry zipEntry = new ZipEntry(parentRepoPath.relativize(path).toString());
try {
zipOutputStream.putNextEntry(zipEntry);
Files.copy(path, zipOutputStream);
zipOutputStream.closeEntry();
} catch (Exception e) {
log.error("Create zip file error", e);
}
});
} catch (IOException e) {
log.error("Create zip file error", e);
}
});
}
return zipFilePath;
}
use of de.tum.in.www1.artemis.domain.File in project ArTEMiS by ls1intum.
the class GitService method zipRepository.
public Path zipRepository(Repository repo) throws IOException {
String zipRepoName = repo.getParticipation().getExercise().getCourse().getTitle() + "-" + repo.getParticipation().getExercise().getTitle() + "-" + repo.getParticipation().getStudent().getLogin() + ".zip";
Path repoPath = repo.getLocalPath();
Path zipFilePath = Paths.get(REPO_CLONE_PATH, "zippedRepos", zipRepoName);
Files.createDirectories(Paths.get(REPO_CLONE_PATH, "zippedRepos"));
try (ZipOutputStream zs = new ZipOutputStream(Files.newOutputStream(zipFilePath))) {
Files.walk(repoPath).filter(path -> !Files.isDirectory(path)).forEach(path -> {
ZipEntry zipEntry = new ZipEntry(repoPath.relativize(path).toString());
try {
zs.putNextEntry(zipEntry);
Files.copy(path, zs);
zs.closeEntry();
} catch (Exception e) {
log.error("Create zip file error", e);
}
});
}
return zipFilePath;
}
use of de.tum.in.www1.artemis.domain.File in project ArTEMiS by ls1intum.
the class BambooService method retrievArtifactPage.
/**
* Gets the content from a Bamboo artifact link
* Follows links on HTML directory pages, if necessary
*
* @param url
* @return
*/
private ResponseEntity retrievArtifactPage(String url) throws BambooException {
HttpHeaders headers = HeaderUtil.createAuthorization(BAMBOO_USER, BAMBOO_PASSWORD);
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<byte[]> response;
try {
response = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);
} catch (Exception e) {
log.error("HttpError while retrieving build artifact", e);
throw new BambooException("HttpError while retrieving build artifact");
}
if (response.getHeaders().containsKey("Content-Type") && response.getHeaders().get("Content-Type").get(0).equals("text/html")) {
// This is an "Index of" HTML page.
String html = new String(response.getBody(), StandardCharsets.UTF_8);
Pattern p = Pattern.compile("href=\"(.*?)\"", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(html);
if (m.find()) {
url = m.group(1);
// Recursively walk through the responses until we get the actual artifact.
return retrievArtifactPage(BAMBOO_SERVER_URL + url);
} else {
throw new BambooException("No artifact link found on artifact page");
}
} else {
// Actual artifact file
return response;
}
}
Aggregations