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

Build GitRemote url from it's parts #4411

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.jgit.transport.URIish;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -46,6 +47,39 @@ public enum Service {
Unknown
}

public URI toUri(boolean ssh) {
return buildRemoteUrl(service, ssh, origin, path);
}

public static URI buildRemoteUrl(Service service, boolean ssh, String origin, String path) {
shanman190 marked this conversation as resolved.
Show resolved Hide resolved
StringBuilder url = new StringBuilder();
path = path.replaceFirst("^/", "").replaceFirst("/$", "");
origin = origin.replaceFirst("/$", "");
if (service == Service.Bitbucket && !ssh) {
path = "scm/" + path;
}
if (service == Service.AzureDevOps) {
if (ssh) {
origin = "ssh." + origin;
path = "v3/" + path;
} else {
path = path.replaceFirst("([^/]+)/([^/]+)/(.*)", "$1/$2/_git/$3");
}
} else if (service == Service.GitLab || ssh) {
path += ".git";
}
if (origin.startsWith("https://") || origin.startsWith("http://") || origin.startsWith("ssh://")) {
origin = origin.substring(origin.indexOf("://") + 3);
}
if (ssh) {
url.append("ssh://git@");
bryceatmoderne marked this conversation as resolved.
Show resolved Hide resolved
} else {
url.append("https://");
}
url.append(origin).append("/").append(path);
return URI.create(url.toString());
}

public static class Parser {
private final Map<String, Service> origins;

Expand Down
26 changes: 26 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.net.URI;

import static org.assertj.core.api.Assertions.assertThat;

public class GitRemoteTest {
Expand Down Expand Up @@ -102,4 +104,28 @@ void parseRegisteredRemote(String cloneUrl, String origin, GitRemote.Service ser
assertThat(remote.getOrganization()).isEqualTo(expectedOrganization);
assertThat(remote.getRepositoryName()).isEqualTo(expectedRepositoryName);
}

@ParameterizedTest
@CsvSource(textBlock = """
GitHub, false, github.com, org/repo, https://github.com/org/repo
GitHub, true, github.com, org/repo, ssh://[email protected]/org/repo.git
GitHub, false, https://github.com, org/repo, https://github.com/org/repo
GitHub, true, https://github.com, org/repo, ssh://[email protected]/org/repo.git
GitHub, false, scm.company.com/context/path, org/repo, https://scm.company.com/context/path/org/repo
GitHub, true, scm.company.com/context/path, org/repo, ssh://[email protected]/context/path/org/repo.git
GitLab, false, gitlab.com, group/subgroup/repo, https://gitlab.com/group/subgroup/repo.git
GitLab, true, gitlab.com, group/subgroup/repo, ssh://[email protected]/group/subgroup/repo.git
GitLab, false, scm.company.com/context/path, group/subgroup/repo, https://scm.company.com/context/path/group/subgroup/repo.git
GitLab, true, scm.company.com/context/path, group/subgroup/repo, ssh://[email protected]/context/path/group/subgroup/repo.git
BitbucketCloud, false, bitbucket.org, org/repo, https://bitbucket.org/org/repo
BitbucketCloud, true, bitbucket.org, org/repo, ssh://[email protected]/org/repo.git
Bitbucket, false, scm.company.com/context/path, org/repo, https://scm.company.com/context/path/scm/org/repo
Bitbucket, true, scm.company.com/context/path, org/repo, ssh://[email protected]/context/path/org/repo.git
shanman190 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we agreed on not adding the port 7999 to the ssh URL for Bitbucket?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is in a separate PR, but we probably need to decide that first: #4414

AzureDevOps, false, dev.azure.com, org/repo, https://dev.azure.com/org/repo
AzureDevOps, true, dev.azure.com, org/repo, ssh://[email protected]/v3/org/repo
""")
void buildRemoteUrl(GitRemote.Service service, boolean ssh, String origin, String path, String expectedUrl) {
URI url = GitRemote.buildRemoteUrl(service, ssh, origin, path);
assertThat(url.toString()).isEqualTo(expectedUrl);
}
}
Loading