Skip to content

Commit

Permalink
Strip origin in findRemoteServer (#4771)
Browse files Browse the repository at this point in the history
* Strip origin in findRemoteServer

* fix test
  • Loading branch information
pstreef authored Dec 11, 2024
1 parent d789bcb commit ecc2d62
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
Expand Down
10 changes: 10 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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://[email protected]"));
}

@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);
Expand Down

0 comments on commit ecc2d62

Please sign in to comment.