Skip to content

Commit

Permalink
Case insensitive origin matching (#4465)
Browse files Browse the repository at this point in the history
  • Loading branch information
pstreef committed Sep 3, 2024
1 parent 1f4564c commit 25f442f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
27 changes: 17 additions & 10 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -310,8 +315,10 @@ public RemoteServer(Service service, String origin, Collection<URI> 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);
}
}
Expand Down
17 changes: 17 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 25f442f

Please sign in to comment.