use of de.tum.in.www1.artemis.exception.BitbucketException in project ArTEMiS by ls1intum.
the class BitbucketService method createUser.
/**
* Creates an user on Bitbucket
*
* @param username The wanted Bitbucket username
* @param password The wanted passowrd in clear text
* @param emailAddress The eMail address for the user
* @param displayName The display name (full name)
* @throws BitbucketException
*/
public void createUser(String username, String password, String emailAddress, String displayName) throws BitbucketException {
HttpHeaders headers = HeaderUtil.createAuthorization(BITBUCKET_USER, BITBUCKET_PASSWORD);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(BITBUCKET_SERVER_URL + "/rest/api/1.0/admin/users").queryParam("name", username).queryParam("email", emailAddress).queryParam("emailAddress", emailAddress).queryParam("password", password).queryParam("displayName", displayName).queryParam("addToDefaultGroup", "true").queryParam("notify", "false");
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
log.debug("Creating Bitbucket user {} ({})", username, emailAddress);
try {
restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entity, Map.class);
} catch (HttpClientErrorException e) {
log.error("Could not create Bitbucket user " + username, e);
throw new BitbucketException("Error while creating user");
}
}
use of de.tum.in.www1.artemis.exception.BitbucketException in project ArTEMiS by ls1intum.
the class BitbucketService method giveWritePermission.
/**
* Gives user write permissions for a repository.
*
* @param projectKey The project key of the repository's project.
* @param repositorySlug The repository's slug.
* @param username The user whom to give write permissions.
*/
private void giveWritePermission(String projectKey, String repositorySlug, String username) throws BitbucketException {
// NAME&PERMISSION
String baseUrl = BITBUCKET_SERVER_URL + "/rest/api/1.0/projects/" + projectKey + "/repos/" + repositorySlug + "/permissions/users?name=";
HttpHeaders headers = HeaderUtil.createAuthorization(BITBUCKET_USER, BITBUCKET_PASSWORD);
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
try {
restTemplate.exchange(baseUrl + username + "&permission=REPO_WRITE", HttpMethod.PUT, entity, Map.class);
} catch (Exception e) {
log.error("Could not give write permission", e);
throw new BitbucketException("Error while giving repository permissions");
}
}
use of de.tum.in.www1.artemis.exception.BitbucketException in project ArTEMiS by ls1intum.
the class BitbucketService method configureRepository.
@Override
public void configureRepository(URL repositoryUrl, String username) {
if (username.startsWith(USER_PREFIX)) {
// It is an automatically created user
User user = userService.getUserByLogin(username).get();
if (!userExists(username)) {
log.debug("Bitbucket user {} does not exist yet", username);
String displayName = (user.getFirstName() + " " + user.getLastName()).trim();
createUser(username, userService.decryptPasswordByLogin(username).get(), user.getEmail(), displayName);
try {
addUserToGroups(username, user.getGroups());
} catch (BitbucketException e) {
/*
This might throw exceptions, for example if the group does not exist on Bitbucket.
We can safely ignore them.
*/
}
} else {
log.debug("Bitbucket user {} already exists", username);
}
}
giveWritePermission(getProjectKeyFromUrl(repositoryUrl), getRepositorySlugFromUrl(repositoryUrl), username);
}
use of de.tum.in.www1.artemis.exception.BitbucketException 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.exception.BitbucketException in project ArTEMiS by ls1intum.
the class BitbucketService method userExists.
/**
* Checks if an username exists on Bitbucket
*
* @param username the Bitbucket username to check
* @return true if it exists
* @throws BitbucketException
*/
private Boolean userExists(String username) throws BitbucketException {
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/users/" + username, HttpMethod.GET, entity, Map.class);
} catch (HttpClientErrorException e) {
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
return false;
}
log.error("Could not check if user " + username + " exists.", e);
throw new BitbucketException("Could not check if user exists");
}
return true;
}
Aggregations