Search in sources :

Example 11 with Result

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

the class BitbucketService method forkRepository.

/**
 * Uses the configured Bitbucket account to fork the given repository inside the project.
 *
 * @param baseProjectKey     The project key of the base project.
 * @param baseRepositorySlug The repository slug of the base repository.
 * @param username           The user for whom the repository is being forked.
 * @return The slug of the forked repository (i.e. its identifier).
 */
private Map<String, String> forkRepository(String baseProjectKey, String baseRepositorySlug, String username) throws BitbucketException {
    String forkName = String.format("%s-%s", baseRepositorySlug, username);
    Map<String, Object> body = new HashMap<>();
    body.put("name", forkName);
    body.put("project", new HashMap<>());
    ((Map) body.get("project")).put("key", baseProjectKey);
    HttpHeaders headers = HeaderUtil.createAuthorization(BITBUCKET_USER, BITBUCKET_PASSWORD);
    HttpEntity<?> entity = new HttpEntity<>(body, headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Map> response;
    try {
        response = restTemplate.exchange(BITBUCKET_SERVER_URL + "/rest/api/1.0/projects/" + baseProjectKey + "/repos/" + baseRepositorySlug, HttpMethod.POST, entity, Map.class);
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode().equals(HttpStatus.CONFLICT)) {
            log.info("Repository already exists. Going to recover repository information...");
            Map<String, String> result = new HashMap<>();
            result.put("slug", forkName);
            result.put("cloneUrl", buildCloneUrl(baseProjectKey, forkName, username).toString());
            return result;
        } else {
            throw e;
        }
    } catch (Exception e) {
        log.error("Could not fork base repository for user " + username, e);
        throw new BitbucketException("Error while forking repository");
    }
    if (response != null && response.getStatusCode().equals(HttpStatus.CREATED)) {
        String slug = (String) response.getBody().get("slug");
        String cloneUrl = buildCloneUrl(baseProjectKey, forkName, username).toString();
        Map<String, String> result = new HashMap<>();
        result.put("slug", slug);
        result.put("cloneUrl", cloneUrl);
        return result;
    }
    return null;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap) MalformedURLException(java.net.MalformedURLException) BitbucketException(de.tum.in.www1.artemis.exception.BitbucketException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) BitbucketException(de.tum.in.www1.artemis.exception.BitbucketException) RestTemplate(org.springframework.web.client.RestTemplate) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with Result

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

the class GitService method getOrCheckoutRepository.

/**
 * Get the local repository for a given remote repository URL.
 * If the local repo does not exist yet, it will be checked out.
 *
 * @param repoUrl The remote repository.
 * @return
 * @throws IOException
 * @throws GitAPIException
 */
public Repository getOrCheckoutRepository(URL repoUrl) throws IOException, GitAPIException {
    Path localPath = new java.io.File(REPO_CLONE_PATH + folderNameForRepositoryUrl(repoUrl)).toPath();
    // check if Repository object already created and available in cachedRepositories
    if (cachedRepositories.containsKey(localPath)) {
        return cachedRepositories.get(localPath);
    }
    // Check if the repository is already checked out on the server
    if (!Files.exists(localPath)) {
        // Repository is not yet available on the server
        // We need to check it out from the remote repository
        log.info("Cloning from " + repoUrl + " to " + localPath);
        Git result = Git.cloneRepository().setURI(repoUrl.toString()).setCredentialsProvider(new UsernamePasswordCredentialsProvider(GIT_USER, GIT_PASSWORD)).setDirectory(localPath.toFile()).call();
        result.close();
    } else {
        log.info("Repository at " + localPath + " already exists");
    }
    // Open the repository from the filesystem
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.setGitDir(new java.io.File(localPath + "/.git")).readEnvironment().findGitDir().setup();
    // Create the JGit repository object
    Repository repository = new Repository(builder);
    repository.setLocalPath(localPath);
    // Cache the JGit repository object for later use
    // Avoids the expensive re-opening of local repositories
    cachedRepositories.put(localPath, repository);
    return repository;
}
Also used : Path(java.nio.file.Path) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Repository(de.tum.in.www1.artemis.domain.Repository) Git(org.eclipse.jgit.api.Git) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) File(de.tum.in.www1.artemis.domain.File)

Example 13 with Result

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

the class BambooService method retrieveLatestBuildResultDetails.

/**
 * Performs a request to the Bamboo REST API to retrieve details on the failed tests of the latest build.
 * <p>
 * TODO: This currently just gets the failed tests of the default job!
 *
 * @param planKey the key of the plan for which to retrieve the details
 * @return
 */
private Map<String, Object> retrieveLatestBuildResultDetails(String planKey) {
    HttpHeaders headers = HeaderUtil.createAuthorization(BAMBOO_USER, BAMBOO_PASSWORD);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Map> response = null;
    try {
        // https://bamboobruegge.in.tum.de/rest/api/latest/result/EIST16W1-TESTEXERCISEAPP-JOB1/latest.json?expand=testResults.failedTests.testResult.errors
        response = restTemplate.exchange(BAMBOO_SERVER_URL + "/rest/api/latest/result/" + planKey.toUpperCase() + "-JOB1/latest.json?expand=testResults.failedTests.testResult.errors", HttpMethod.GET, entity, Map.class);
    } catch (Exception e) {
        log.error("HttpError while retrieving build result details", e);
    }
    if (response != null) {
        Map<String, Object> result = new HashMap<>();
        List resultDetails = (List) ((Map) ((Map) response.getBody().get("testResults")).get("failedTests")).get("testResult");
        result.put("details", resultDetails);
        return result;
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate) GitException(de.tum.in.www1.artemis.exception.GitException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BambooException(de.tum.in.www1.artemis.exception.BambooException)

Example 14 with Result

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

the class BambooService method retrieveBuildStatus.

/**
 * Retrieves the current build status of the given plan.
 *
 * @param planKey the key of the plan for which to retrieve the status
 * @returna map containing the following data:
 * - isActive: true if the plan is queued or building
 * - isBuilding: true if the plan is building
 */
public Map<String, Boolean> retrieveBuildStatus(String planKey) {
    HttpHeaders headers = HeaderUtil.createAuthorization(BAMBOO_USER, BAMBOO_PASSWORD);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Map> response = null;
    try {
        response = restTemplate.exchange(BAMBOO_SERVER_URL + "/rest/api/latest/plan/" + planKey.toUpperCase() + ".json", HttpMethod.GET, entity, Map.class);
    } catch (Exception e) {
        log.error("Bamboo HttpError '" + e.getMessage() + "' while retrieving build status for plan " + planKey, e);
    }
    if (response != null) {
        Map<String, Boolean> result = new HashMap<>();
        boolean isActive = (boolean) response.getBody().get("isActive");
        boolean isBuilding = (boolean) response.getBody().get("isBuilding");
        result.put("isActive", isActive);
        result.put("isBuilding", isBuilding);
        return result;
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate) GitException(de.tum.in.www1.artemis.exception.GitException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BambooException(de.tum.in.www1.artemis.exception.BambooException)

Example 15 with Result

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

the class BambooService method retrieveLatestBuildLogs.

/**
 * Performs a request to the Bamboo REST API to retrieve the build log of the latest build.
 *
 * @param planKey
 * @return
 */
// TODO: save this in the result class so that ArTEMiS does not need to retrieve it everytime
public List<BuildLogEntry> retrieveLatestBuildLogs(String planKey) {
    HttpHeaders headers = HeaderUtil.createAuthorization(BAMBOO_USER, BAMBOO_PASSWORD);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Map> response = null;
    try {
        response = restTemplate.exchange(BAMBOO_SERVER_URL + "/rest/api/latest/result/" + planKey.toUpperCase() + "-JOB1/latest.json?expand=logEntries", HttpMethod.GET, entity, Map.class);
    } catch (Exception e) {
        log.error("HttpError while retrieving build result logs from Bamboo: " + e.getMessage());
    }
    ArrayList logs = new ArrayList<BuildLogEntry>();
    if (response != null) {
        for (HashMap<String, Object> logEntry : (List<HashMap>) ((Map) response.getBody().get("logEntries")).get("logEntry")) {
            Instant i = Instant.ofEpochMilli((long) logEntry.get("date"));
            ZonedDateTime logDate = ZonedDateTime.ofInstant(i, ZoneId.systemDefault());
            BuildLogEntry log = new BuildLogEntry(logDate, (String) logEntry.get("log"));
            logs.add(log);
        }
    }
    return logs;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Instant(java.time.Instant) GitException(de.tum.in.www1.artemis.exception.GitException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BambooException(de.tum.in.www1.artemis.exception.BambooException) ZonedDateTime(java.time.ZonedDateTime) RestTemplate(org.springframework.web.client.RestTemplate)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)15 URI (java.net.URI)10 Result (de.tum.in.www1.artemis.domain.Result)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 Test (org.junit.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)6 RestTemplate (org.springframework.web.client.RestTemplate)6 MalformedURLException (java.net.MalformedURLException)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Participation (de.tum.in.www1.artemis.domain.Participation)4 BambooException (de.tum.in.www1.artemis.exception.BambooException)4 GitException (de.tum.in.www1.artemis.exception.GitException)4 HeaderUtil (de.tum.in.www1.artemis.web.rest.util.HeaderUtil)4 IOException (java.io.IOException)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 HttpEntity (org.springframework.http.HttpEntity)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ResponseEntity (org.springframework.http.ResponseEntity)4 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)3