use of com.epam.pipeline.entity.git.GitlabUser in project cloud-pipeline by epam.
the class GitlabClient method buildCloneCredentials.
public GitCredentials buildCloneCredentials(boolean useEnvVars, boolean issueToken, Long duration) {
final String gitUrl = StringUtils.isNotBlank(fullUrl) ? fullUrl : gitHost;
Assert.state(StringUtils.isNotBlank(gitUrl), "Gitlab URL is required to issue credentials.");
final GitCredentials.GitCredentialsBuilder credentialsBuilder = GitCredentials.builder();
if (StringUtils.isEmpty(token)) {
return credentialsBuilder.url(gitUrl).build();
}
final String cloneToken;
final String userName;
final String email;
if (issueToken && !externalHost) {
final GitlabUser user = findUser(this.userName).orElseGet(() -> GitlabUser.builder().username(adminName).id(adminId).build());
userName = user.getUsername();
cloneToken = createImpersonationToken(projectName, user.getId(), duration);
email = user.getEmail();
} else {
userName = externalHost ? this.userName.replaceAll("@.*$", "") : adminName;
cloneToken = token;
email = null;
}
GitRepositoryUrl repositoryUrl = GitRepositoryUrl.from(gitUrl);
repositoryUrl = useEnvVars ? repositoryUrl.withUsername("${GIT_USER}").withPassword("${GIT_TOKEN}") : repositoryUrl.withUsername(userName).withPassword(cloneToken);
LOGGER.debug("Ready url for user {} with token {}", userName, cloneToken);
return credentialsBuilder.url(repositoryUrl.asString()).userName(userName).token(cloneToken).email(email).build();
}
use of com.epam.pipeline.entity.git.GitlabUser in project cloud-pipeline by epam.
the class GitlabClient method findUser.
private Optional<GitlabUser> findUser(String userName) {
final RestTemplate template = new RestTemplate();
final HttpEntity headers = getAuthHeaders();
final String searchUri = UriComponentsBuilder.fromHttpUrl(String.format(GITLAB_API_USERS, gitHost)).queryParam("search", userName).build().toUriString();
ResponseEntity<List<GitlabUser>> response = template.exchange(searchUri, HttpMethod.GET, headers, new ParameterizedTypeReference<List<GitlabUser>>() {
});
return Optional.of(response).filter(r -> r.getStatusCode() == HttpStatus.OK).map(ResponseEntity::getBody).map(List::stream).flatMap(Stream::findFirst);
}
Aggregations