Search in sources :

Example 11 with Repository

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;
}
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 12 with Repository

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;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) MalformedURLException(java.net.MalformedURLException) BitbucketException(de.tum.in.www1.artemis.exception.BitbucketException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException)

Example 13 with Repository

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);
    }
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) MalformedURLException(java.net.MalformedURLException) BitbucketException(de.tum.in.www1.artemis.exception.BitbucketException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException)

Example 14 with Repository

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());
}
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 15 with Repository

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();
}
Also used : File(de.tum.in.www1.artemis.domain.File)

Aggregations

Repository (de.tum.in.www1.artemis.domain.Repository)9 Participation (de.tum.in.www1.artemis.domain.Participation)7 Test (org.junit.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 Path (java.nio.file.Path)5 BitbucketException (de.tum.in.www1.artemis.exception.BitbucketException)4 IOException (java.io.IOException)4 MalformedURLException (java.net.MalformedURLException)4 Git (org.eclipse.jgit.api.Git)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)4 RestTemplate (org.springframework.web.client.RestTemplate)4 File (de.tum.in.www1.artemis.domain.File)3 BambooException (de.tum.in.www1.artemis.exception.BambooException)3 ExerciseRepository (de.tum.in.www1.artemis.repository.ExerciseRepository)3 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)3 Transactional (org.springframework.transaction.annotation.Transactional)3 File (java.io.File)2 URL (java.net.URL)2 Files (java.nio.file.Files)2