From 338c5421069a894fe830efa4f1e3404965d1233a Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Wed, 11 Dec 2024 10:51:55 +0100
Subject: [PATCH 1/2] Strip origin in findRemoteServer
---
.../main/java/org/openrewrite/GitRemote.java | 18 ++++++++++++++++--
.../java/org/openrewrite/GitRemoteTest.java | 10 ++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java
index 371827b21b6..85f3063024c 100644
--- a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java
+++ b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java
@@ -116,6 +116,14 @@ public URI toUri(GitRemote remote, String protocol) {
return buildUri(remote.service, remote.origin, remote.path, protocol);
}
+ /**
+ * Build a {@link URI} clone url from components, if that protocol is supported (configured) by the matched server
+ * @param service the type of SCM service
+ * @param origin the origin of the SCM service, any protocol will be stripped (and not used for matching)
+ * @param path the path to the repository
+ * @param protocol the protocol to use. Supported protocols: ssh, http, https
+ * @return
+ */
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);
@@ -207,11 +215,17 @@ public Parser registerRemote(Service service, String origin) {
return this;
}
+ /**
+ * Find a registered remote server by an origin.
+ * @param origin the origin of the server. Any protocol will be stripped (and not used to match)
+ * @return The server if found, or an unknown type server with a normalized url/origin if not found.
+ */
public RemoteServer findRemoteServer(String origin) {
- return servers.stream().filter(server -> server.origin.equalsIgnoreCase(origin))
+ String strippedOrigin = stripProtocol(origin);
+ return servers.stream().filter(server -> server.origin.equalsIgnoreCase(strippedOrigin))
.findFirst()
.orElseGet(() -> {
- URI normalizedUri = normalize(origin);
+ URI normalizedUri = normalize(strippedOrigin);
String normalizedOrigin = normalizedUri.getHost() + maybePort(normalizedUri.getPort(), normalizedUri.getScheme());
return new RemoteServer(Service.Unknown, normalizedOrigin, normalizedUri);
});
diff --git a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
index baa12ba993d..74335ae5705 100644
--- a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
+++ b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
@@ -232,11 +232,21 @@ void shouldNotStripJgit() {
assertThat(remote.getPath()).isEqualTo("openrewrite/jgit");
}
+ @Test
+ void shouldNotReplaceExistingWellKnownServer(){
+ GitRemote.Parser parser = new GitRemote.Parser()
+ .registerRemote(GitRemote.Service.GitHub, URI.create("https://github.com"), List.of(URI.create("ssh://notgithub.com")));
+
+ assertThat(parser.findRemoteServer("github.com").getUris())
+ .containsExactlyInAnyOrder(URI.create("https://github.com"), URI.create("ssh://github.com"));
+ }
+
@Test
void findRemote() {
GitRemote.Parser parser = new GitRemote.Parser()
.registerRemote(GitRemote.Service.Bitbucket, URI.create("scm.company.com/stash"), Collections.emptyList());
assertThat(parser.findRemoteServer("github.com").getService()).isEqualTo(GitRemote.Service.GitHub);
+ assertThat(parser.findRemoteServer("https://github.com").getService()).isEqualTo(GitRemote.Service.GitHub);
assertThat(parser.findRemoteServer("gitlab.com").getService()).isEqualTo(GitRemote.Service.GitLab);
assertThat(parser.findRemoteServer("bitbucket.org").getService()).isEqualTo(GitRemote.Service.BitbucketCloud);
assertThat(parser.findRemoteServer("dev.azure.com").getService()).isEqualTo(GitRemote.Service.AzureDevOps);
From c912a44510a34db0b1a0ae5e50404e3432838c5b Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Wed, 11 Dec 2024 13:53:33 +0100
Subject: [PATCH 2/2] fix test
---
rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
index 74335ae5705..37581c2f934 100644
--- a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
+++ b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
@@ -238,7 +238,7 @@ void shouldNotReplaceExistingWellKnownServer(){
.registerRemote(GitRemote.Service.GitHub, URI.create("https://github.com"), List.of(URI.create("ssh://notgithub.com")));
assertThat(parser.findRemoteServer("github.com").getUris())
- .containsExactlyInAnyOrder(URI.create("https://github.com"), URI.create("ssh://github.com"));
+ .containsExactlyInAnyOrder(URI.create("https://github.com"), URI.create("ssh://git@github.com"));
}
@Test