Search in sources :

Example 1 with GitRepositoryUrl

use of com.epam.pipeline.entity.git.GitRepositoryUrl 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 GitRepositoryUrl

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

the class GitlabClient method initializeGitlabClientFromRepositoryAndToken.

public static GitlabClient initializeGitlabClientFromRepositoryAndToken(String user, String repository, String token, Long adminId, String adminName, boolean externalHost) {
    final GitRepositoryUrl gitRepositoryUrl = GitRepositoryUrl.from(repository);
    final String host = gitRepositoryUrl.getProtocol() + gitRepositoryUrl.getHost();
    final String namespace = gitRepositoryUrl.getNamespace().orElseThrow(() -> new IllegalArgumentException("Invalid repository URL format"));
    final String project = gitRepositoryUrl.getProject().orElseThrow(() -> new IllegalArgumentException("Invalid repository URL format"));
    final String userOrNamespace = externalHost ? gitRepositoryUrl.getUsername().orElse(namespace) : user;
    LOGGER.trace("Created Git client for repository {}", repository);
    return new GitlabClient(host, namespace, userOrNamespace, token, project, repository, adminId, adminName, externalHost);
}
Also used : GitRepositoryUrl(com.epam.pipeline.entity.git.GitRepositoryUrl)

Aggregations

GitRepositoryUrl (com.epam.pipeline.entity.git.GitRepositoryUrl)2 GitCredentials (com.epam.pipeline.entity.git.GitCredentials)1 GitlabUser (com.epam.pipeline.entity.git.GitlabUser)1