Search in sources :

Example 1 with GitToken

use of com.epam.pipeline.entity.git.GitToken in project cloud-pipeline by epam.

the class GitlabClient method createImpersonationToken.

private String createImpersonationToken(String tokenName, Long userId, LocalDate expires) {
    if (adminId == null) {
        throw new IllegalArgumentException("Token may be issued only for local Gitlab.");
    }
    HttpHeaders headers = new HttpHeaders();
    headers.add(TOKEN_HEADER, token);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("name", tokenName);
    parameters.put("expires_at", gitDateFormat.format(expires));
    parameters.put("scopes[]", "api");
    HttpEntity entity = new HttpEntity<>(headers);
    try {
        String url = addUrlParameters(String.format(GIT_ISSUE_TOKEN, gitHost, userId), parameters);
        URI uri = new URI(url);
        ResponseEntity<GitToken> response = new RestTemplate().exchange(uri, HttpMethod.POST, entity, new ParameterizedTypeReference<GitToken>() {
        });
        if (response.getStatusCode() == HttpStatus.CREATED) {
            return response.getBody().getToken();
        } else {
            throw new IllegalArgumentException("Failed to issue Gitlab token");
        }
    } catch (URISyntaxException | UnsupportedEncodingException | HttpClientErrorException e) {
        throw new IllegalArgumentException("Failed to issue Gitlab token");
    }
}
Also used : GitToken(com.epam.pipeline.entity.git.GitToken) HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RestTemplate(org.springframework.web.client.RestTemplate)

Aggregations

GitToken (com.epam.pipeline.entity.git.GitToken)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 RestTemplate (org.springframework.web.client.RestTemplate)1