Search in sources :

Example 16 with Repository

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

the class RepositoryResource method getStatus.

/**
 * GET /repository/{id}: Get the "clean" status of the repository. Clean = No uncommitted changes.
 *
 * @param id Participation ID
 * @param request
 * @param authentication
 * @return
 * @throws IOException
 * @throws GitAPIException
 */
@GetMapping(value = "/repository/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<RepositoryStatusDTO> getStatus(@PathVariable Long id, HttpServletRequest request, AbstractAuthenticationToken authentication) throws IOException, GitAPIException {
    log.debug("REST request to get clean status for Repository for Participation : {}", id);
    Participation participation = participationService.findOne(id);
    if (!userHasPermissions(participation))
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    if (!Optional.ofNullable(participation).isPresent()) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    Repository repository = gitService.get().getOrCheckoutRepository(participation);
    RepositoryStatusDTO status = new RepositoryStatusDTO();
    status.isClean = gitService.get().isClean(repository);
    if (status.isClean) {
        gitService.get().pull(repository);
    }
    return new ResponseEntity<>(status, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) RepositoryStatusDTO(de.tum.in.www1.artemis.web.rest.dto.RepositoryStatusDTO)

Example 17 with Repository

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

the class GitServiceIntTest method testCommitAndPush.

@Test
public void testCommitAndPush() throws IOException, GitAPIException {
    Participation participation = new Participation();
    participation.setRepositoryUrl(remoteTestRepo);
    Repository repo = gitService.getOrCheckoutRepository(participation);
    Ref oldHead = repo.findRef("HEAD");
    gitService.commitAndPush(repo, "test commit");
    Ref head = repo.findRef("HEAD");
    assertThat(head).isNotEqualTo(oldHead);
    RevWalk walk = new RevWalk(repo);
    RevCommit commit = walk.parseCommit(head.getObjectId());
    assertThat(commit.getFullMessage()).isEqualTo("test commit");
    // get remote ref
    Git git = new Git(repo);
    Collection<Ref> refs = git.lsRemote().setHeads(true).call();
    Ref remoteHead = refs.iterator().next();
    assertThat(head.getObjectId()).isEqualTo(remoteHead.getObjectId());
    gitService.deleteLocalRepository(repo);
}
Also used : Participation(de.tum.in.www1.artemis.domain.Participation) Repository(de.tum.in.www1.artemis.domain.Repository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with Repository

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

the class GitServiceIntTest method testGetOrCheckoutRepositoryForNewRepo.

@Test
public void testGetOrCheckoutRepositoryForNewRepo() throws IOException, GitAPIException {
    Participation participation = new Participation();
    participation.setRepositoryUrl(remoteTestRepo);
    Repository repo = gitService.getOrCheckoutRepository(participation);
    assertThat(repo.getBranch()).isEqualTo("master");
    assertThat(repo.getDirectory()).exists();
    gitService.deleteLocalRepository(repo);
}
Also used : Participation(de.tum.in.www1.artemis.domain.Participation) Repository(de.tum.in.www1.artemis.domain.Repository) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with Repository

use of de.tum.in.www1.artemis.domain.Repository 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

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