Skip to content

Commit

Permalink
Allow building uri from origin, path and service type (#4767)
Browse files Browse the repository at this point in the history
* Allow building uri from origin, path and service type

* remove redundant test
  • Loading branch information
pstreef authored Dec 11, 2024
1 parent bee3e2f commit ecfb0e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
30 changes: 17 additions & 13 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,51 +113,55 @@ public Parser() {
* @return the clone url
*/
public URI toUri(GitRemote remote, String protocol) {
return buildUri(remote.service, remote.origin, remote.path, protocol);
}

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);
}
URI selectedBaseUrl;

if (remote.service == Service.Unknown) {
if (PORT_PATTERN.matcher(remote.origin).find()) {
throw new IllegalArgumentException("Unable to determine protocol/port combination for an unregistered origin with a port: " + remote.origin);
if (service == Service.Unknown) {
if (PORT_PATTERN.matcher(origin).find()) {
throw new IllegalArgumentException("Unable to determine protocol/port combination for an unregistered origin with a port: " + origin);
}
selectedBaseUrl = URI.create(protocol + "://" + stripProtocol(remote.origin));
selectedBaseUrl = URI.create(protocol + "://" + stripProtocol(origin));
} else {
selectedBaseUrl = servers.stream()
.filter(server -> server.allOrigins()
.stream()
.anyMatch(origin -> origin.equalsIgnoreCase(stripProtocol(remote.origin)))
.anyMatch(o -> o.equalsIgnoreCase(stripProtocol(origin)))
)
.flatMap(server -> server.getUris().stream())
.filter(uri -> uri.getScheme().equals(protocol))
.findFirst()
.orElseGet(() -> {
URI normalizedUri = Parser.normalize(remote.origin);
URI normalizedUri = Parser.normalize(origin);
if (!normalizedUri.getScheme().equals(protocol)) {
throw new IllegalStateException("No matching server found that supports ssh for origin: " + remote.origin);
throw new IllegalStateException("No matching server found that supports ssh for origin: " + origin);
}
return normalizedUri;
});
}

String path = remote.path.replaceFirst("^/", "");
path = path.replaceFirst("^/", "");
boolean ssh = protocol.equals("ssh");
switch (remote.service) {
switch (service) {
case Bitbucket:
if (!ssh) {
path = "scm/" + remote.path;
path = "scm/" + path;
}
break;
case AzureDevOps:
if (ssh) {
path = "v3/" + remote.path;
path = "v3/" + path;
} else {
path = remote.path.replaceFirst("([^/]+)/([^/]+)/(.*)", "$1/$2/_git/$3");
path = path.replaceFirst("([^/]+)/([^/]+)/(.*)", "$1/$2/_git/$3");
}
break;
}
if (remote.service != Service.AzureDevOps) {
if (service != Service.AzureDevOps) {
path += ".git";
}
String maybeSlash = selectedBaseUrl.toString().endsWith("/") ? "" : "/";
Expand Down
24 changes: 12 additions & 12 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,18 @@ void parseOriginCaseInsensitive(String cloneUrl, String expectedOrigin, String e

@ParameterizedTest
@CsvSource(textBlock = """
GitHub, GitHub
GITLAB, GitLab
bitbucket, Bitbucket
BitbucketCloud, BitbucketCloud
Bitbucket Cloud, BitbucketCloud
BITBUCKET_CLOUD, BitbucketCloud
AzureDevOps, AzureDevOps
AZURE_DEVOPS, AzureDevOps
Azure DevOps, AzureDevOps
idontknow, Unknown
""")
void findServiceForName(String name, GitRemote.Service service){
GitHub, GitHub
GITLAB, GitLab
bitbucket, Bitbucket
BitbucketCloud, BitbucketCloud
Bitbucket Cloud, BitbucketCloud
BITBUCKET_CLOUD, BitbucketCloud
AzureDevOps, AzureDevOps
AZURE_DEVOPS, AzureDevOps
Azure DevOps, AzureDevOps
idontknow, Unknown
""")
void findServiceForName(String name, GitRemote.Service service) {
assertThat(GitRemote.Service.forName(name)).isEqualTo(service);
}

Expand Down

0 comments on commit ecfb0e5

Please sign in to comment.