Skip to content

Commit

Permalink
feat: Add method to find a GitRemote.RemoteServer registered to the…
Browse files Browse the repository at this point in the history
… `GitRemote.Parser` (#4436)
  • Loading branch information
pstreef authored Aug 22, 2024
1 parent 175d5c4 commit bfb045b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 38 deletions.
86 changes: 48 additions & 38 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public Parser() {

/**
* Transform a {@link GitRemote} into a clone url in the form of an {@link URI}
* @param remote the previously parsed GitRemote
*
* @param remote the previously parsed GitRemote
* @param protocol the protocol to use. Supported protocols: ssh, http, https
* @return the clone url
*/
Expand Down Expand Up @@ -154,6 +155,16 @@ public Parser registerRemote(Service service, String origin) {
return this;
}

public RemoteServer findRemoteServer(String origin) {
return servers.stream().filter(server -> server.origin.equals(origin))
.findFirst()
.orElseGet(() -> {
URI normalizedUri = normalize(origin);
String normalizedOrigin = normalizedUri.getHost() + maybePort(normalizedUri.getPort(), normalizedUri.getScheme());
return new RemoteServer(Service.Unknown, normalizedOrigin, normalizedUri);
});
}

private void add(RemoteServer server) {
if (server.service != Service.Unknown || servers.stream().noneMatch(s -> s.origin.equals(server.origin))) {
servers.add(server);
Expand Down Expand Up @@ -261,43 +272,6 @@ static URI normalize(String url) {
}
}

@Value
private static class RemoteServer {
Service service;
String origin;
List<URI> uris = new ArrayList<>();

public RemoteServer(Service service, String origin, URI... uris) {
this(service, origin, Arrays.asList(uris));
}

public RemoteServer(Service service, String origin, Collection<URI> uris) {
this.service = service;
this.origin = origin;
this.uris.addAll(uris);
}

@Nullable
private RemoteServerMatch match(URI normalizedUri) {
for (URI uri : uris) {
if (normalizedUri.toString().startsWith(normalize(uri.toString()).toString())) {
return new RemoteServerMatch(service, origin, uri);
}
}
return null;
}

public Set<String> allOrigins() {
Set<String> origins = new LinkedHashSet<>();
origins.add(origin);
for (URI uri : uris) {
URI normalized = normalize(uri.toString());
origins.add(stripProtocol(normalized.toString()));
}
return origins;
}
}

@Value
private static class RemoteServerMatch {
Service service;
Expand All @@ -318,4 +292,40 @@ private static boolean isDefaultPort(int port, String scheme) {
("ssh".equals(scheme) && port == 22);
}
}

@Value
public static class RemoteServer {
Service service;
String origin;
List<URI> uris = new ArrayList<>();

public RemoteServer(Service service, String origin, URI... uris) {
this(service, origin, Arrays.asList(uris));
}

public RemoteServer(Service service, String origin, Collection<URI> uris) {
this.service = service;
this.origin = origin;
this.uris.addAll(uris);
}

private GitRemote.Parser.@Nullable RemoteServerMatch match(URI normalizedUri) {
for (URI uri : uris) {
if (normalizedUri.toString().startsWith(Parser.normalize(uri.toString()).toString())) {
return new Parser.RemoteServerMatch(service, origin, uri);
}
}
return null;
}

public Set<String> allOrigins() {
Set<String> origins = new LinkedHashSet<>();
origins.add(origin);
for (URI uri : uris) {
URI normalized = Parser.normalize(uri.toString());
origins.add(Parser.stripProtocol(normalized.toString()));
}
return origins;
}
}
}
15 changes: 15 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.params.provider.CsvSource;

import java.net.URI;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -225,4 +226,18 @@ void shouldNotStripJgit() {
GitRemote remote = parser.parse("https://github.com/openrewrite/jgit");
assertThat(remote.getPath()).isEqualTo("openrewrite/jgit");
}

@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("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);
assertThat(parser.findRemoteServer("scm.company.com/stash").getService()).isEqualTo(GitRemote.Service.Bitbucket);
assertThat(parser.findRemoteServer("scm.unregistered.com").getService()).isEqualTo(GitRemote.Service.Unknown);
assertThat(parser.findRemoteServer("scm.unregistered.com").getOrigin()).isEqualTo("scm.unregistered.com");
assertThat(parser.findRemoteServer("https://scm.unregistered.com").getOrigin()).isEqualTo("scm.unregistered.com");
}
}

0 comments on commit bfb045b

Please sign in to comment.