Search in sources :

Example 6 with BitbucketException

use of de.tum.in.www1.artemis.exception.BitbucketException in project ArTEMiS by ls1intum.

the class BitbucketService method addUserToGroups.

/**
 * Adds an Bitbucket user to (multiple) Bitbucket groups
 *
 * @param username The Bitbucket username
 * @param groups   Names of Bitbucket groups
 * @throws BitbucketException
 */
public void addUserToGroups(String username, List<String> groups) throws BitbucketException {
    HttpHeaders headers = HeaderUtil.createAuthorization(BITBUCKET_USER, BITBUCKET_PASSWORD);
    Map<String, Object> body = new HashMap<>();
    body.put("user", username);
    body.put("groups", groups);
    HttpEntity<?> entity = new HttpEntity<>(body, headers);
    RestTemplate restTemplate = new RestTemplate();
    log.debug("Adding Bitbucket user {} to groups {}", username, groups);
    try {
        restTemplate.exchange(BITBUCKET_SERVER_URL + "/rest/api/1.0/admin/users/add-groups", HttpMethod.POST, entity, Map.class);
    } catch (HttpClientErrorException e) {
        log.error("Could not add Bitbucket user " + username + " to groups" + groups, e);
        throw new BitbucketException("Error while adding Bitbucket user to groups");
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) BitbucketException(de.tum.in.www1.artemis.exception.BitbucketException) HashMap(java.util.HashMap) 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