Search in sources :

Example 1 with SSHUserPrivateKey

use of com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey in project gitea-plugin by jenkinsci.

the class GiteaSCMBuilder method checkoutUriTemplate.

/**
 * Returns a {@link UriTemplate} for checkout according to credentials configuration.
 * Expects the parameters {@code owner} and {@code repository} to be populated before expansion.
 *
 * @param context       the context within which to resolve the credentials.
 * @param serverUrl     the server url
 * @param sshRemote     any valid SSH remote URL for the server.
 * @param credentialsId the credentials.
 * @return a {@link UriTemplate}
 */
public static UriTemplate checkoutUriTemplate(@CheckForNull Item context, @NonNull String serverUrl, @CheckForNull String sshRemote, @CheckForNull String credentialsId) {
    if (credentialsId != null && sshRemote != null) {
        URIRequirementBuilder builder = URIRequirementBuilder.create();
        URI serverUri = URI.create(serverUrl);
        if (serverUri.getHost() != null) {
            builder.withHostname(serverUri.getHost());
        }
        StandardCredentials credentials = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(StandardCredentials.class, context, context instanceof Queue.Task ? Tasks.getDefaultAuthenticationOf((Queue.Task) context) : ACL.SYSTEM, builder.build()), CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId), CredentialsMatchers.instanceOf(StandardCredentials.class)));
        if (credentials instanceof SSHUserPrivateKey) {
            int atIndex = sshRemote.indexOf('@');
            int colonIndex = sshRemote.indexOf(':');
            if (atIndex != -1 && colonIndex != -1 && atIndex < colonIndex) {
                // this is an scp style url, we will translate to ssh style
                return UriTemplate.buildFromTemplate("ssh://" + sshRemote.substring(0, colonIndex)).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).literal(".git").build();
            }
            URI sshUri = URI.create(sshRemote);
            return UriTemplate.buildFromTemplate("ssh://git@" + sshUri.getHost() + (sshUri.getPort() != 22 ? ":" + sshUri.getPort() : "")).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).literal(".git").build();
        }
        if (credentials instanceof PersonalAccessToken) {
            try {
                // TODO is there a way we can get git plugin to redact the secret?
                URI tokenUri = new URI(serverUri.getScheme(), ((PersonalAccessToken) credentials).getToken().getPlainText(), serverUri.getHost(), serverUri.getPort(), serverUri.getPath(), serverUri.getQuery(), serverUri.getFragment());
                return UriTemplate.buildFromTemplate(tokenUri.toASCIIString()).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).literal(".git").build();
            } catch (URISyntaxException e) {
            // ok we are at the end of the road
            }
        }
    }
    return UriTemplate.buildFromTemplate(serverUrl).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).literal(".git").build();
}
Also used : PersonalAccessToken(org.jenkinsci.plugin.gitea.credentials.PersonalAccessToken) URIRequirementBuilder(com.cloudbees.plugins.credentials.domains.URIRequirementBuilder) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Queue(hudson.model.Queue) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) SSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey)

Aggregations

SSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey)1 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 URIRequirementBuilder (com.cloudbees.plugins.credentials.domains.URIRequirementBuilder)1 Queue (hudson.model.Queue)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 PersonalAccessToken (org.jenkinsci.plugin.gitea.credentials.PersonalAccessToken)1