Skip to content

Commit

Permalink
fix: No protocol in origin (#4435)
Browse files Browse the repository at this point in the history
  • Loading branch information
pstreef authored Aug 21, 2024
1 parent 1dfe938 commit 32a14d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
11 changes: 7 additions & 4 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ public Parser registerRemote(Service service, URI remoteUri, Collection<URI> alt
* @return this
*/
public Parser registerRemote(Service service, String origin) {
add(new RemoteServer(service, origin, normalize(origin)));
URI normalizedUri = normalize(origin);
String maybePort = maybePort(normalizedUri.getPort(), normalizedUri.getScheme());
String normalizedOrigin = normalizedUri.getHost() + maybePort + normalizedUri.getPath();
add(new RemoteServer(service, normalizedOrigin, normalize(origin)));
return this;
}

Expand Down Expand Up @@ -195,11 +198,11 @@ public GitRemote parse(String url) {
.findFirst()
.orElseGet(() -> {
String[] segments = normalizedUri.getPath().split("/");
String origin1 = normalizedUri.getHost() + maybePort(normalizedUri.getPort(), normalizedUri.getScheme());
String origin = normalizedUri.getHost() + maybePort(normalizedUri.getPort(), normalizedUri.getScheme());
if (segments.length > 2) {
origin1 += Arrays.stream(segments, 0, segments.length - 2).collect(Collectors.joining("/"));
origin += Arrays.stream(segments, 0, segments.length - 2).collect(Collectors.joining("/"));
}
return new RemoteServerMatch(Service.Unknown, origin1, URI.create(normalizedUri.getScheme() + "://" + origin1));
return new RemoteServerMatch(Service.Unknown, origin, URI.create(normalizedUri.getScheme() + "://" + origin));
});
}

Expand Down
50 changes: 25 additions & 25 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,34 @@ 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
http://scm.company.com:80/stash/scm/org/repo.git, http://scm.company.com/stash, Bitbucket, org/repo, org, repo
http://scm.company.com:8080/stash/scm/org/repo.git, http://scm.company.com:8080/stash, Bitbucket, org/repo, org, repo
https://scm.company.com:443/stash/scm/org/repo.git, scm.company.com/stash, Bitbucket, org/repo, org, repo
https://scm.company.com:1234/stash/scm/org/repo.git, https://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
ssh://scm.company.com:22/stash/org/repo, scm.company.com/stash, Bitbucket, org/repo, org, repo
ssh://scm.company.com:7999/stash/org/repo, ssh://scm.company.com:7999/stash, Bitbucket, org/repo, org, repo
https://scm.company.com/stash/scm/org/repo.git, scm.company.com/stash, Bitbucket, scm.company.com/stash, org/repo, org, repo
http://scm.company.com:80/stash/scm/org/repo.git, http://scm.company.com/stash, Bitbucket, scm.company.com/stash, org/repo, org, repo
http://scm.company.com:8080/stash/scm/org/repo.git, http://scm.company.com:8080/stash, Bitbucket, scm.company.com:8080/stash, org/repo, org, repo
https://scm.company.com:443/stash/scm/org/repo.git, scm.company.com/stash, Bitbucket, scm.company.com/stash, org/repo, org, repo
https://scm.company.com:1234/stash/scm/org/repo.git, https://scm.company.com:1234/stash, Bitbucket, scm.company.com:1234/stash, org/repo, org, repo
[email protected]:stash/org/repo.git, scm.company.com/stash, Bitbucket, scm.company.com/stash, org/repo, org, repo
ssh://scm.company.com/stash/org/repo, scm.company.com/stash, Bitbucket, scm.company.com/stash, org/repo, org, repo
ssh://scm.company.com:22/stash/org/repo, scm.company.com/stash, Bitbucket, scm.company.com/stash, org/repo, org, repo
ssh://scm.company.com:7999/stash/org/repo, ssh://scm.company.com:7999/stash, Bitbucket, scm.company.com:7999/stash, 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, https://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/very/long/context/path/org/repo.git, scm.company.com/very/long/context/path, Bitbucket, scm.company.com/very/long/context/path, org/repo, org, repo
https://scm.company.com:1234/very/long/context/path/org/repo.git, https://scm.company.com:1234/very/long/context/path, Bitbucket, 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, Bitbucket, scm.company.com/very/long/context/path, 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, https://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
[email protected]:group/subgroup/subergroup/subestgroup/repo.git, ssh://scm.company.com, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://scm.company.com:443/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, ssh://scm.company.com, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://scm.company.com:222/group/subgroup/subergroup/subestgroup/repo.git, ssh://scm.company.com:222, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://scm.company.com/group/subgroup/subergroup/subestgroup/repo, scm.company.com, GitLab, scm.company.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://scm.company.com:1234/group/subgroup/subergroup/subestgroup/repo, https://scm.company.com:1234, GitLab, scm.company.com:1234, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
[email protected]/group/subgroup/subergroup/subestgroup/repo.git, scm.company.com, GitLab, scm.company.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
[email protected]:group/subgroup/subergroup/subestgroup/repo.git, ssh://scm.company.com, GitLab, scm.company.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://scm.company.com:443/group/subgroup/subergroup/subestgroup/repo.git, scm.company.com, GitLab, scm.company.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://scm.company.com:22/group/subgroup/subergroup/subestgroup/repo.git, ssh://scm.company.com, GitLab, scm.company.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://scm.company.com:222/group/subgroup/subergroup/subestgroup/repo.git, ssh://scm.company.com:222, GitLab, scm.company.com:222, 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
https://scm.company.com/very/long/context/path/group/subgroup/subergroup/subestgroup/repo, scm.company.com/very/long/context/path, GitLab, scm.company.com/very/long/context/path, 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) {
GitRemote.Parser parser = new GitRemote.Parser().registerRemote(service, origin);
void parseRegisteredRemote(String cloneUrl, String originToRegister, GitRemote.Service service, String expectedOrigin, String expectedPath, String expectedOrganization, String expectedRepositoryName) {
GitRemote.Parser parser = new GitRemote.Parser().registerRemote(service, originToRegister);
GitRemote remote = parser.parse(cloneUrl);
assertThat(remote.getOrigin()).isEqualTo(origin);
assertThat(remote.getOrigin()).isEqualTo(expectedOrigin);
assertThat(remote.getPath()).isEqualTo(expectedPath);
assertThat(remote.getOrganization()).isEqualTo(expectedOrganization);
assertThat(remote.getRepositoryName()).isEqualTo(expectedRepositoryName);
Expand Down Expand Up @@ -203,7 +203,7 @@ void normalizePortWithoutSchemaNotSupported() {
void buildUri(GitRemote.Service service, String origin, String path, String protocol, String expectedUri) {
GitRemote remote = new GitRemote(service, null, origin, path, null, null);
URI uri = new GitRemote.Parser()
.registerRemote(GitRemote.Service.Bitbucket, URI.create("https://scm.company.com/context/bitbucket/"), List.of( URI.create("http://scm.company.com/context/bitbucket/"),URI.create("ssh://[email protected]:7999/context/bitbucket")))
.registerRemote(GitRemote.Service.Bitbucket, URI.create("https://scm.company.com/context/bitbucket/"), List.of(URI.create("http://scm.company.com/context/bitbucket/"), URI.create("ssh://[email protected]:7999/context/bitbucket")))
.registerRemote(GitRemote.Service.GitHub, URI.create("https://scm.company.com/context/github"), List.of(URI.create("ssh://[email protected]/context/github/")))
.registerRemote(GitRemote.Service.GitLab, URI.create("https://scm.company.com/context/gitlab/"), List.of(URI.create("ssh://[email protected]:8022/context/gitlab")))
.registerRemote(GitRemote.Service.GitLab, URI.create("https://scm.company.com:8443/context/gitlab"), List.of(URI.create("https://scm.company.com/context/gitlab/")))
Expand All @@ -220,7 +220,7 @@ void buildUriUnregisteredOriginWithPortNotSupported() {
}

@Test
void shouldNotStripJgit(){
void shouldNotStripJgit() {
GitRemote.Parser parser = new GitRemote.Parser();
GitRemote remote = parser.parse("https://github.com/openrewrite/jgit");
assertThat(remote.getPath()).isEqualTo("openrewrite/jgit");
Expand Down

0 comments on commit 32a14d7

Please sign in to comment.