Search in sources :

Example 1 with GitlabUser

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();
}
Also used : GitCredentials(com.epam.pipeline.entity.git.GitCredentials) GitRepositoryUrl(com.epam.pipeline.entity.git.GitRepositoryUrl) GitlabUser(com.epam.pipeline.entity.git.GitlabUser)

Example 2 with GitlabUser

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);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) PipelineSourceItemErrorVO(com.epam.pipeline.controller.vo.PipelineSourceItemErrorVO) GitlabUser(com.epam.pipeline.entity.git.GitlabUser) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Wither(lombok.experimental.Wither) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) RestTemplate(org.springframework.web.client.RestTemplate) DateFormat(java.text.DateFormat) HttpHeaders(org.springframework.http.HttpHeaders) TimeZone(java.util.TimeZone) GitCredentials(com.epam.pipeline.entity.git.GitCredentials) GitProject(com.epam.pipeline.entity.git.GitProject) GitPushCommitEntry(com.epam.pipeline.entity.git.GitPushCommitEntry) Collectors(java.util.stream.Collectors) GitlabVersion(com.epam.pipeline.entity.git.GitlabVersion) IOUtils(org.apache.commons.io.IOUtils) HttpEntity(org.springframework.http.HttpEntity) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) GitRepositoryUrl(com.epam.pipeline.entity.git.GitRepositoryUrl) Template(com.epam.pipeline.entity.template.Template) LocalDate(java.time.LocalDate) Optional(java.util.Optional) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GitTagEntry(com.epam.pipeline.entity.git.GitTagEntry) GitClientException(com.epam.pipeline.exception.git.GitClientException) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) SimpleDateFormat(java.text.SimpleDateFormat) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry) Logger(org.slf4j.Logger) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitFile(com.epam.pipeline.entity.git.GitFile) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(org.springframework.http.HttpMethod) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) HttpStatus(org.springframework.http.HttpStatus) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) URLEncoder(java.net.URLEncoder) Paths(java.nio.file.Paths) DateTimeFormatter(java.time.format.DateTimeFormatter) ResponseEntity(org.springframework.http.ResponseEntity) GitToken(com.epam.pipeline.entity.git.GitToken) AllArgsConstructor(lombok.AllArgsConstructor) NoArgsConstructor(lombok.NoArgsConstructor) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate) List(java.util.List) Stream(java.util.stream.Stream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream)

Aggregations

GitCredentials (com.epam.pipeline.entity.git.GitCredentials)2 GitRepositoryUrl (com.epam.pipeline.entity.git.GitRepositoryUrl)2 GitlabUser (com.epam.pipeline.entity.git.GitlabUser)2 PipelineSourceItemErrorVO (com.epam.pipeline.controller.vo.PipelineSourceItemErrorVO)1 GitCommitEntry (com.epam.pipeline.entity.git.GitCommitEntry)1 GitFile (com.epam.pipeline.entity.git.GitFile)1 GitProject (com.epam.pipeline.entity.git.GitProject)1 GitPushCommitEntry (com.epam.pipeline.entity.git.GitPushCommitEntry)1 GitRepositoryEntry (com.epam.pipeline.entity.git.GitRepositoryEntry)1 GitTagEntry (com.epam.pipeline.entity.git.GitTagEntry)1 GitToken (com.epam.pipeline.entity.git.GitToken)1 GitlabVersion (com.epam.pipeline.entity.git.GitlabVersion)1 Template (com.epam.pipeline.entity.template.Template)1 GitClientException (com.epam.pipeline.exception.git.GitClientException)1 UnexpectedResponseStatusException (com.epam.pipeline.exception.git.UnexpectedResponseStatusException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1