Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize server URLs to avoid missing scheme #4780

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,19 @@ public URI toUri(GitRemote remote, String 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
* @return the clone URL if it could be created.
* @throws IllegalArgumentException if the protocol is not supported by the server.
*/
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);
throw new IllegalStateException("Invalid protocol: " + protocol + ". Must be one of: " + ALLOWED_PROTOCOLS);
}
URI selectedBaseUrl;

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);
Expand All @@ -142,12 +143,12 @@ public URI buildUri(Service service, String origin, String path, String protocol
.anyMatch(o -> o.equalsIgnoreCase(stripProtocol(origin)))
)
.flatMap(server -> server.getUris().stream())
.filter(uri -> uri.getScheme().equals(protocol))
.filter(uri -> Parser.normalize(uri).getScheme().equals(protocol))
.findFirst()
.orElseGet(() -> {
bryceatmoderne marked this conversation as resolved.
Show resolved Hide resolved
URI normalizedUri = Parser.normalize(origin);
if (!normalizedUri.getScheme().equals(protocol)) {
throw new IllegalStateException("No matching server found that supports ssh for origin: " + origin);
throw new IllegalStateException("Unable to build clone URL. No matching server found that supports " + protocol + " for origin: " + origin);
}
return normalizedUri;
});
Expand Down Expand Up @@ -217,6 +218,7 @@ public Parser registerRemote(Service service, String origin) {

/**
* 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.
*/
Expand Down Expand Up @@ -299,6 +301,10 @@ private String repositoryPath(RemoteServerMatch match, URI normalizedUri) {

private static final Pattern PORT_PATTERN = Pattern.compile(":\\d+(/.+)(/.+)+");

static URI normalize(URI url) {
return normalize(url.toString());
}

static URI normalize(String url) {
try {
URIish uri = new URIish(url);
Expand Down Expand Up @@ -392,10 +398,11 @@ public Set<String> allOrigins() {
Set<String> origins = new LinkedHashSet<>();
origins.add(origin);
for (URI uri : uris) {
URI normalized = Parser.normalize(uri.toString());
URI normalized = Parser.normalize(uri);
origins.add(Parser.stripProtocol(normalized.toString()));
}
return origins;
}

}
}
Loading