use of de.tum.in.www1.artemis.domain.Repository 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.Repository in project ArTEMiS by ls1intum.
the class BitbucketService method repositoryUrlIsValid.
/**
* Check if the given repository url is valid and accessible on Bitbucket.
* @param repositoryUrl
* @return
*/
@Override
public Boolean repositoryUrlIsValid(URL repositoryUrl) {
String projectKey = getProjectKeyFromUrl(repositoryUrl);
String repositorySlug = getRepositorySlugFromUrl(repositoryUrl);
HttpHeaders headers = HeaderUtil.createAuthorization(BITBUCKET_USER, BITBUCKET_PASSWORD);
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
try {
restTemplate.exchange(BITBUCKET_SERVER_URL + "/rest/api/1.0/projects/" + projectKey + "/repos/" + repositorySlug, HttpMethod.GET, entity, Map.class);
} catch (Exception e) {
return false;
}
return true;
}
use of de.tum.in.www1.artemis.domain.Repository in project ArTEMiS by ls1intum.
the class BitbucketService method deleteRepositoryImpl.
/**
* Deletes the given repository from Bitbucket.
*
* @param projectKey The project key of the repository's project.
* @param repositorySlug The repository's slug.
*/
private void deleteRepositoryImpl(String projectKey, String repositorySlug) {
String baseUrl = BITBUCKET_SERVER_URL + "/rest/api/1.0/projects/" + projectKey + "/repos/" + repositorySlug;
log.info("Delete repository " + baseUrl);
HttpHeaders headers = HeaderUtil.createAuthorization(BITBUCKET_USER, BITBUCKET_PASSWORD);
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
try {
restTemplate.exchange(baseUrl, HttpMethod.DELETE, entity, Map.class);
} catch (Exception e) {
log.error("Could not delete repository", e);
}
}
use of de.tum.in.www1.artemis.domain.Repository 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.Repository 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();
}
Aggregations