Java Code Examples for com.cloudbees.plugins.credentials.domains.URIRequirementBuilder#withHostname()

The following examples show how to use com.cloudbees.plugins.credentials.domains.URIRequirementBuilder#withHostname() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitLabSCMBuilder.java    From gitlab-branch-source-plugin with MIT License 4 votes vote down vote up
/**
 * Returns a {@link UriTemplate} for checkout according to credentials configuration.
 *
 * @param context the context within which to resolve the credentials.
 * @param serverUrl the server url
 * @param sshRemote the SSH remote URL for the project.
 * @param httpRemote the HTTPS remote URL for the project.
 * @param credentialsId the credentials.
 * @param projectPath the full path to the project (with namespace).
 * @return a {@link UriTemplate}
 */
public static UriTemplate checkoutUriTemplate(@CheckForNull Item context,
    @NonNull String serverUrl,
    @CheckForNull String httpRemote,
    @CheckForNull String sshRemote,
    @CheckForNull String credentialsId,
    @NonNull String projectPath) {

    if (credentialsId != null && sshRemote != null) {
        URIRequirementBuilder builder = URIRequirementBuilder.create();
        URI serverUri = URI.create(serverUrl);
        if (serverUri.getHost() != null) {
            builder.withHostname(serverUri.getHost());
        }
        StandardUsernameCredentials credentials = CredentialsMatchers.firstOrNull(
            CredentialsProvider.lookupCredentials(
                StandardUsernameCredentials.class,
                context,
                context instanceof Queue.Task
                    ? ((Queue.Task) context).getDefaultAuthentication()
                    : ACL.SYSTEM,
                builder.build()
            ),
            CredentialsMatchers.allOf(
                CredentialsMatchers.withId(credentialsId),
                CredentialsMatchers.instanceOf(StandardUsernameCredentials.class)
            )
        );
        if (credentials instanceof SSHUserPrivateKey) {
            return UriTemplate.buildFromTemplate(sshRemote)
                .build();
        }
    }
    if (httpRemote != null) {
        return UriTemplate.buildFromTemplate(httpRemote)
            .build();
    }
    return UriTemplate.buildFromTemplate(serverUrl + '/' + projectPath)
        .literal(".git")
        .build();
}
 
Example 2
Source File: GiteaSCMBuilder.java    From gitea-plugin with MIT License 4 votes vote down vote up
/**
 * 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
                                ? ((Queue.Task) context).getDefaultAuthentication()
                                : 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() != -1 ? ":" + 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();
}