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());
}
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();
}
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);
}
Aggregations