Search in sources :

Example 1 with BitbucketException

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

Example 2 with BitbucketException

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");
    }
}
Also used : BitbucketException(de.tum.in.www1.artemis.exception.BitbucketException) 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 3 with BitbucketException

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

Example 4 with BitbucketException

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;
}
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 5 with BitbucketException

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

Aggregations

BitbucketException (de.tum.in.www1.artemis.exception.BitbucketException)6 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)5 RestTemplate (org.springframework.web.client.RestTemplate)5 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2 User (de.tum.in.www1.artemis.domain.User)1 Map (java.util.Map)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1