Search in sources :

Example 1 with File

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());
}
Also used : Path(java.nio.file.Path) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ExerciseRepository(de.tum.in.www1.artemis.repository.ExerciseRepository) BambooException(de.tum.in.www1.artemis.exception.BambooException) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with File

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);
            }
        });
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) java.util(java.util) ScheduledFuture(java.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) ExerciseRepository(de.tum.in.www1.artemis.repository.ExerciseRepository) Service(org.springframework.stereotype.Service) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Pageable(org.springframework.data.domain.Pageable) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) Logger(org.slf4j.Logger) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Files(java.nio.file.Files) IOException(java.io.IOException) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BambooException(de.tum.in.www1.artemis.exception.BambooException) TimeUnit(java.util.concurrent.TimeUnit) ParticipationState(de.tum.in.www1.artemis.domain.enumeration.ParticipationState) de.tum.in.www1.artemis.domain(de.tum.in.www1.artemis.domain) Principal(java.security.Principal) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) PageImpl(org.springframework.data.domain.PageImpl) Transactional(org.springframework.transaction.annotation.Transactional) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) BambooException(de.tum.in.www1.artemis.exception.BambooException)

Example 3 with File

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;
}
Also used : Path(java.nio.file.Path) ZipOutputStream(java.util.zip.ZipOutputStream) java.util(java.util) ScheduledFuture(java.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) ExerciseRepository(de.tum.in.www1.artemis.repository.ExerciseRepository) Service(org.springframework.stereotype.Service) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Pageable(org.springframework.data.domain.Pageable) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) Logger(org.slf4j.Logger) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Files(java.nio.file.Files) IOException(java.io.IOException) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BambooException(de.tum.in.www1.artemis.exception.BambooException) TimeUnit(java.util.concurrent.TimeUnit) ParticipationState(de.tum.in.www1.artemis.domain.enumeration.ParticipationState) de.tum.in.www1.artemis.domain(de.tum.in.www1.artemis.domain) Principal(java.security.Principal) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) PageImpl(org.springframework.data.domain.PageImpl) Transactional(org.springframework.transaction.annotation.Transactional) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) BambooException(de.tum.in.www1.artemis.exception.BambooException)

Example 4 with File

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;
}
Also used : Path(java.nio.file.Path) ZipOutputStream(java.util.zip.ZipOutputStream) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) java.util(java.util) Logger(org.slf4j.Logger) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Status(org.eclipse.jgit.api.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Files(java.nio.file.Files) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) Participation(de.tum.in.www1.artemis.domain.Participation) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) HiddenFileFilter(org.apache.commons.io.filefilter.HiddenFileFilter) Value(org.springframework.beans.factory.annotation.Value) Paths(java.nio.file.Paths) Service(org.springframework.stereotype.Service) Repository(de.tum.in.www1.artemis.domain.Repository) PullResult(org.eclipse.jgit.api.PullResult) Git(org.eclipse.jgit.api.Git) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) File(de.tum.in.www1.artemis.domain.File) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException)

Example 5 with File

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;
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Pattern(java.util.regex.Pattern) BambooException(de.tum.in.www1.artemis.exception.BambooException) HttpEntity(org.springframework.http.HttpEntity) Matcher(java.util.regex.Matcher) RestTemplate(org.springframework.web.client.RestTemplate) GitException(de.tum.in.www1.artemis.exception.GitException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BambooException(de.tum.in.www1.artemis.exception.BambooException)

Aggregations

IOException (java.io.IOException)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6 Path (java.nio.file.Path)5 BambooException (de.tum.in.www1.artemis.exception.BambooException)4 ExerciseRepository (de.tum.in.www1.artemis.repository.ExerciseRepository)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Files (java.nio.file.Files)3 Paths (java.nio.file.Paths)3 java.util (java.util)3 ZipEntry (java.util.zip.ZipEntry)3 ZipOutputStream (java.util.zip.ZipOutputStream)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Service (org.springframework.stereotype.Service)3 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)2 File (de.tum.in.www1.artemis.domain.File)2 Participation (de.tum.in.www1.artemis.domain.Participation)2 Repository (de.tum.in.www1.artemis.domain.Repository)2 ParticipationState (de.tum.in.www1.artemis.domain.enumeration.ParticipationState)2 Principal (java.security.Principal)2