Skip to content

Commit

Permalink
fix: Include port in GitRemote origin (#4393)
Browse files Browse the repository at this point in the history
* fix: Include port in GitRemote origin

* Cache GitRemote origins with port if present
  • Loading branch information
bryceatmoderne authored Aug 7, 2024
1 parent b453525 commit d028592
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
25 changes: 18 additions & 7 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public Parser() {
origins = new LinkedHashMap<>();
origins.put("github.com", Service.GitHub);
origins.put("gitlab.com", Service.GitLab);
origins.put("gitlab.com:22", Service.GitLab);
origins.put("bitbucket.org", Service.BitbucketCloud);
origins.put("dev.azure.com", Service.AzureDevOps);
origins.put("ssh.dev.azure.com", Service.AzureDevOps);
Expand All @@ -78,6 +79,9 @@ public GitRemote parse(String url) {
Parser.HostAndPath hostAndPath = new Parser.HostAndPath(url);

String origin = hostAndPath.host;
if (hostAndPath.port > 0) {
origin = origin + ':' + hostAndPath.port;
}
Service service = origins.get(origin);
if (service == null) {
for (String maybeOrigin : origins.keySet()) {
Expand Down Expand Up @@ -132,13 +136,15 @@ public GitRemote parse(String url) {
private static class HostAndPath {
String scheme;
String host;
int port;
String path;

public HostAndPath(String url) {
try {
URIish uri = new URIish(url);
scheme = uri.getScheme();
host = uri.getHost();
port = uri.getPort();
if (host == null && !"file".equals(scheme)) {
throw new IllegalStateException("No host in url: " + url);
}
Expand All @@ -151,14 +157,20 @@ public HostAndPath(String url) {
}

private String concat() {
String hostAndPath = host == null ? "" : host;
StringBuilder builder = new StringBuilder(64);
if (host != null) {
builder.append(host);
}
if (port > 0) {
builder.append(':').append(port);
}
if (!path.isEmpty()) {
if (!hostAndPath.isEmpty()) {
hostAndPath += "/";
if (builder.length() != 0) {
builder.append('/');
}
hostAndPath += path;
builder.append(path);
}
return hostAndPath;
return builder.toString();
}

private String repositoryPath(@Nullable String origin) {
Expand All @@ -169,8 +181,7 @@ private String repositoryPath(@Nullable String origin) {
if (!hostAndPath.startsWith(origin)) {
throw new IllegalArgumentException("Unable to find origin '" + origin + "' in '" + hostAndPath + "'");
}
return hostAndPath.substring(origin.length())
.replaceFirst("^/", "");
return hostAndPath.substring(origin.length()).replaceFirst("^/", "");
}
}
}
Expand Down
26 changes: 16 additions & 10 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ public class GitRemoteTest {
https://github.com/org/repo, github.com, org/repo, org, repo
[email protected]:org/repo.git, github.com, org/repo, org, repo
ssh://github.com/org/repo.git, github.com, org/repo, org, repo
https://gitlab.com/group/repo.git, gitlab.com, group/repo, group, repo
https://gitlab.com/group/subgroup/subergroup/subestgroup/repo.git, gitlab.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
[email protected]:group/subgroup/subergroup/subestgroup/repo.git, gitlab.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://[email protected]:22/group/subgroup/subergroup/subestgroup/repo.git, gitlab.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://[email protected]:22/group/subgroup/subergroup/subestgroup/repo.git, gitlab.com:22, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://bitbucket.org/PRJ/repo, bitbucket.org, PRJ/repo, PRJ, repo
[email protected]:PRJ/repo.git, bitbucket.org, PRJ/repo, PRJ, repo
ssh://bitbucket.org/PRJ/repo.git, bitbucket.org, PRJ/repo, PRJ, repo
https://[email protected]/org/project/_git/repo, dev.azure.com, org/project/repo, org/project, repo
https://dev.azure.com/org/project/_git/repo, dev.azure.com, org/project/repo, org/project, repo
[email protected]:v3/org/project/repo, dev.azure.com, org/project/repo, org/project, repo
ssh://ssh.dev.azure.com:22/v3/org/project/repo, dev.azure.com, org/project/repo, org/project, repo
""")
void parseKnownRemotes(String cloneUrl, String expectedOrigin, String expectedPath, String expectedOrganization, String expectedRepositoryName) {
GitRemote.Parser parser = new GitRemote.Parser();
Expand All @@ -53,9 +53,11 @@ void parseKnownRemotes(String cloneUrl, String expectedOrigin, String expectedPa
@ParameterizedTest
@CsvSource(textBlock = """
https://scm.company.com/stash/scm/org/repo.git, scm.company.com/stash/scm, org/repo, org, repo
https://scm.company.com:1234/stash/scm/org/repo.git, scm.company.com:1234/stash/scm, org/repo, org, repo
[email protected]:stash/org/repo.git, scm.company.com/stash, org/repo, org, repo
ssh://scm.company.com/stash/org/repo, scm.company.com/stash, org/repo, org, repo
https://scm.company.com:1234/very/long/context/path/org/repo.git, scm.company.com:1234/very/long/context/path, org/repo, org, repo
[email protected]:very/long/context/path/org/repo.git, scm.company.com/very/long/context/path, org/repo, org, repo
""")
void parseUnknownRemote(String cloneUrl, String expectedOrigin, String expectedPath, String expectedOrganization, String expectedRepositoryName) {
Expand All @@ -70,15 +72,19 @@ void parseUnknownRemote(String cloneUrl, String expectedOrigin, String expectedP
@ParameterizedTest
@CsvSource(textBlock = """
https://scm.company.com/stash/scm/org/repo.git, scm.company.com/stash, Bitbucket, org/repo, org, repo
https://scm.company.com:1234/stash/scm/org/repo.git, scm.company.com:1234/stash, Bitbucket, org/repo, org, repo
[email protected]:stash/org/repo.git, scm.company.com/stash, Bitbucket, org/repo, org, repo
ssh://scm.company.com/stash/org/repo, scm.company.com/stash, Bitbucket, org/repo, org, repo
https://scm.company.com/very/long/context/path/org/repo.git, scm.company.com/very/long/context/path, Bitbucket, org/repo, org, repo
https://scm.company.com:1234/very/long/context/path/org/repo.git, scm.company.com:1234/very/long/context/path, Bitbucket, org/repo, org, repo
[email protected]:very/long/context/path/org/repo.git, scm.company.com/very/long/context/path, Bitbucket, org/repo, org, repo
https://scm.company.com/group/subgroup/subergroup/subestgroup/repo, scm.company.com, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://scm.company.com:1234/group/subgroup/subergroup/subestgroup/repo, scm.company.com:1234, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
[email protected]:group/subgroup/subergroup/subestgroup/repo.git, scm.company.com, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://scm.company.com:22/group/subgroup/subergroup/subestgroup/repo.git, scm.company.com, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://scm.company.com:22/group/subgroup/subergroup/subestgroup/repo.git, scm.company.com:22, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://scm.company.com/very/long/context/path/group/subgroup/subergroup/subestgroup/repo, scm.company.com/very/long/context/path, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
""")
void parseRegisteredRemote(String cloneUrl, String origin, GitRemote.Service service, String expectedPath, String expectedOrganization, String expectedRepositoryName) {
Expand Down

0 comments on commit d028592

Please sign in to comment.