diff --git a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java index dd85812bd7a..22bc3c63743 100644 --- a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java +++ b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java @@ -80,7 +80,10 @@ public URI toUri(GitRemote remote, String protocol) { selectedBaseUrl = URI.create(protocol + "://" + stripProtocol(remote.origin)); } else { selectedBaseUrl = servers.stream() - .filter(server -> server.allOrigins().contains(stripProtocol(remote.origin))) + .filter(server -> server.allOrigins() + .stream() + .anyMatch(origin -> origin.equalsIgnoreCase(stripProtocol(remote.origin))) + ) .flatMap(server -> server.getUris().stream()) .filter(uri -> uri.getScheme().equals(protocol)) .findFirst() @@ -156,7 +159,7 @@ public Parser registerRemote(Service service, String origin) { } public RemoteServer findRemoteServer(String origin) { - return servers.stream().filter(server -> server.origin.equals(origin)) + return servers.stream().filter(server -> server.origin.equalsIgnoreCase(origin)) .findFirst() .orElseGet(() -> { URI normalizedUri = normalize(origin); @@ -166,7 +169,7 @@ public RemoteServer findRemoteServer(String origin) { } private void add(RemoteServer server) { - if (server.service != Service.Unknown || servers.stream().noneMatch(s -> s.origin.equals(server.origin))) { + if (server.service != Service.Unknown || servers.stream().noneMatch(s -> s.origin.equalsIgnoreCase(server.origin))) { servers.add(server); } } @@ -179,15 +182,15 @@ public GitRemote parse(String url) { switch (match.service) { case AzureDevOps: - if (match.matchedUri.getHost().equals("ssh.dev.azure.com")) { - repositoryPath = repositoryPath.replaceFirst("v3/", ""); + if (match.matchedUri.getHost().equalsIgnoreCase("ssh.dev.azure.com")) { + repositoryPath = repositoryPath.replaceFirst("(?i)v3/", ""); } else { - repositoryPath = repositoryPath.replaceFirst("/_git/", "/"); + repositoryPath = repositoryPath.replaceFirst("(?i)/_git/", "/"); } break; case Bitbucket: if (url.startsWith("http")) { - repositoryPath = repositoryPath.replaceFirst("scm/", ""); + repositoryPath = repositoryPath.replaceFirst("(?i)scm/", ""); } break; } @@ -222,7 +225,9 @@ private String repositoryPath(RemoteServerMatch match, URI normalizedUri) { String uri = normalizedUri.toString(); String contextPath = origin.getPath(); String path = normalizedUri.getPath(); - if (!normalizedUri.getHost().equals(origin.getHost()) || normalizedUri.getPort() != origin.getPort() || !path.startsWith(contextPath)) { + if (!normalizedUri.getHost().equalsIgnoreCase(origin.getHost()) || + normalizedUri.getPort() != origin.getPort() || + !path.toLowerCase(Locale.ENGLISH).startsWith(contextPath.toLowerCase(Locale.ENGLISH))) { throw new IllegalArgumentException("Origin: " + origin + " does not match the clone url: " + uri); } return path.substring(contextPath.length()) @@ -264,7 +269,7 @@ static URI normalize(String url) { String maybePort = maybePort(uri.getPort(), scheme); String path = uri.getPath().replaceFirst("/$", "") - .replaceFirst("\\.git$", "") + .replaceFirst("(?i)\\.git$", "") .replaceFirst("^/", ""); return URI.create((scheme + "://" + host + maybePort + "/" + path).replaceFirst("/$", "")); } catch (URISyntaxException e) { @@ -310,8 +315,10 @@ public RemoteServer(Service service, String origin, Collection uris) { } private GitRemote.Parser.@Nullable RemoteServerMatch match(URI normalizedUri) { + String lowerCaseNormalizedUri = normalizedUri.toString().toLowerCase(Locale.ENGLISH); for (URI uri : uris) { - if (normalizedUri.toString().startsWith(Parser.normalize(uri.toString()).toString())) { + String normalizedServerUri = Parser.normalize(uri.toString()).toString().toLowerCase(Locale.ENGLISH); + if (lowerCaseNormalizedUri.startsWith(normalizedServerUri)) { return new Parser.RemoteServerMatch(service, origin, uri); } } diff --git a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java index 2fb0991b2b1..8247c3d89a9 100644 --- a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java @@ -240,4 +240,21 @@ void findRemote() { assertThat(parser.findRemoteServer("scm.unregistered.com").getOrigin()).isEqualTo("scm.unregistered.com"); assertThat(parser.findRemoteServer("https://scm.unregistered.com").getOrigin()).isEqualTo("scm.unregistered.com"); } + + @ParameterizedTest + @CsvSource(textBlock = """ + https://github.com/org/repo, github.com, org/repo, org, repo + https://GITHUB.COM/ORG/REPO, github.com, ORG/REPO, ORG, REPO + ssh://GITHUB.COM/ORG/REPO.GIT, github.com, ORG/REPO, ORG, REPO + https://DEV.AZURE.COM/ORG/PROJECT/_GIT/REPO, dev.azure.com, ORG/PROJECT/REPO, ORG/PROJECT, REPO + GIT@SSH.DEV.AZURE.COM:V3/ORG/PROJECT/REPO, dev.azure.com, ORG/PROJECT/REPO, ORG/PROJECT, REPO + """) + void parseOriginCaseInsensitive(String cloneUrl, String expectedOrigin, String expectedPath, String expectedOrganization, String expectedRepositoryName) { + GitRemote.Parser parser = new GitRemote.Parser(); + GitRemote remote = parser.parse(cloneUrl); + assertThat(remote.getOrigin()).isEqualTo(expectedOrigin); + assertThat(remote.getPath()).isEqualTo(expectedPath); + assertThat(remote.getOrganization()).isEqualTo(expectedOrganization); + assertThat(remote.getRepositoryName()).isEqualTo(expectedRepositoryName); + } }