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