From ecfb0e5059504736017f5edb1adf51c95229980f Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Wed, 11 Dec 2024 08:34:33 +0100 Subject: [PATCH] Allow building uri from origin, path and service type (#4767) * Allow building uri from origin, path and service type * remove redundant test --- .../main/java/org/openrewrite/GitRemote.java | 30 +++++++++++-------- .../java/org/openrewrite/GitRemoteTest.java | 24 +++++++-------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java index 6816c8e9c8b..371827b21b6 100644 --- a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java +++ b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java @@ -113,51 +113,55 @@ public Parser() { * @return the clone url */ public URI toUri(GitRemote remote, String protocol) { + return buildUri(remote.service, remote.origin, remote.path, protocol); + } + + public URI buildUri(Service service, String origin, String path, String protocol) { if (!ALLOWED_PROTOCOLS.contains(protocol)) { throw new IllegalArgumentException("Invalid protocol: " + protocol + ". Must be one of: " + ALLOWED_PROTOCOLS); } URI selectedBaseUrl; - if (remote.service == Service.Unknown) { - if (PORT_PATTERN.matcher(remote.origin).find()) { - throw new IllegalArgumentException("Unable to determine protocol/port combination for an unregistered origin with a port: " + remote.origin); + if (service == Service.Unknown) { + if (PORT_PATTERN.matcher(origin).find()) { + throw new IllegalArgumentException("Unable to determine protocol/port combination for an unregistered origin with a port: " + origin); } - selectedBaseUrl = URI.create(protocol + "://" + stripProtocol(remote.origin)); + selectedBaseUrl = URI.create(protocol + "://" + stripProtocol(origin)); } else { selectedBaseUrl = servers.stream() .filter(server -> server.allOrigins() .stream() - .anyMatch(origin -> origin.equalsIgnoreCase(stripProtocol(remote.origin))) + .anyMatch(o -> o.equalsIgnoreCase(stripProtocol(origin))) ) .flatMap(server -> server.getUris().stream()) .filter(uri -> uri.getScheme().equals(protocol)) .findFirst() .orElseGet(() -> { - URI normalizedUri = Parser.normalize(remote.origin); + URI normalizedUri = Parser.normalize(origin); if (!normalizedUri.getScheme().equals(protocol)) { - throw new IllegalStateException("No matching server found that supports ssh for origin: " + remote.origin); + throw new IllegalStateException("No matching server found that supports ssh for origin: " + origin); } return normalizedUri; }); } - String path = remote.path.replaceFirst("^/", ""); + path = path.replaceFirst("^/", ""); boolean ssh = protocol.equals("ssh"); - switch (remote.service) { + switch (service) { case Bitbucket: if (!ssh) { - path = "scm/" + remote.path; + path = "scm/" + path; } break; case AzureDevOps: if (ssh) { - path = "v3/" + remote.path; + path = "v3/" + path; } else { - path = remote.path.replaceFirst("([^/]+)/([^/]+)/(.*)", "$1/$2/_git/$3"); + path = path.replaceFirst("([^/]+)/([^/]+)/(.*)", "$1/$2/_git/$3"); } break; } - if (remote.service != Service.AzureDevOps) { + if (service != Service.AzureDevOps) { path += ".git"; } String maybeSlash = selectedBaseUrl.toString().endsWith("/") ? "" : "/"; diff --git a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java index 410eb17df9b..baa12ba993d 100644 --- a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java @@ -265,18 +265,18 @@ void parseOriginCaseInsensitive(String cloneUrl, String expectedOrigin, String e @ParameterizedTest @CsvSource(textBlock = """ - GitHub, GitHub - GITLAB, GitLab - bitbucket, Bitbucket - BitbucketCloud, BitbucketCloud - Bitbucket Cloud, BitbucketCloud - BITBUCKET_CLOUD, BitbucketCloud - AzureDevOps, AzureDevOps - AZURE_DEVOPS, AzureDevOps - Azure DevOps, AzureDevOps - idontknow, Unknown - """) - void findServiceForName(String name, GitRemote.Service service){ + GitHub, GitHub + GITLAB, GitLab + bitbucket, Bitbucket + BitbucketCloud, BitbucketCloud + Bitbucket Cloud, BitbucketCloud + BITBUCKET_CLOUD, BitbucketCloud + AzureDevOps, AzureDevOps + AZURE_DEVOPS, AzureDevOps + Azure DevOps, AzureDevOps + idontknow, Unknown + """) + void findServiceForName(String name, GitRemote.Service service) { assertThat(GitRemote.Service.forName(name)).isEqualTo(service); }