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