Search in sources :

Example 6 with File

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

Example 7 with File

use of de.tum.in.www1.artemis.domain.File in project ArTEMiS by ls1intum.

the class GitService method listFiles.

/**
 * List all files in the repository
 *
 * @param repo Local Repository Object.
 * @return Collection of File objects
 */
public Collection<File> listFiles(Repository repo) {
    // Check if list of files is already cached
    if (repo.getFiles() == null) {
        Iterator<java.io.File> itr = FileUtils.iterateFiles(repo.getLocalPath().toFile(), HiddenFileFilter.VISIBLE, HiddenFileFilter.VISIBLE);
        Collection<File> files = new LinkedList<>();
        while (itr.hasNext()) {
            files.add(new File(itr.next(), repo));
        }
        // Cache the list of files
        // Avoid expensive rescanning
        repo.setFiles(files);
    }
    return repo.getFiles();
}
Also used : File(de.tum.in.www1.artemis.domain.File)

Example 8 with File

use of de.tum.in.www1.artemis.domain.File in project ArTEMiS by ls1intum.

the class GitServiceIntTest method testPull.

@Test
public void testPull() throws IOException, GitAPIException {
    Participation participation = new Participation();
    participation.setRepositoryUrl(remoteTestRepo);
    Repository repo = gitService.getOrCheckoutRepository(participation);
    Ref oldHead = repo.findRef("HEAD");
    // commit
    File tempDir = Files.createTempDir();
    Git git = Git.cloneRepository().setURI(remoteTestRepo).setCredentialsProvider(new UsernamePasswordCredentialsProvider(GIT_USER, GIT_PASSWORD)).setDirectory(tempDir).call();
    git.commit().setMessage("a commit").setAllowEmpty(true).call();
    git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(GIT_USER, GIT_PASSWORD)).call();
    // pull
    PullResult pullResult = gitService.pull(repo);
    Ref newHead = repo.findRef("HEAD");
    assertThat(oldHead).isNotEqualTo(newHead);
    RevWalk walk = new RevWalk(repo);
    RevCommit commit = walk.parseCommit(newHead.getObjectId());
    assertThat(commit.getFullMessage()).isEqualTo("a commit");
    FileUtils.deleteDirectory(tempDir);
}
Also used : Participation(de.tum.in.www1.artemis.domain.Participation) Repository(de.tum.in.www1.artemis.domain.Repository) Ref(org.eclipse.jgit.lib.Ref) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) PullResult(org.eclipse.jgit.api.PullResult) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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