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

Use 7999 as Bitbucket DC default port #4414

Closed
wants to merge 1 commit 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
38 changes: 20 additions & 18 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public Parser() {

public Parser registerRemote(Service service, String origin) {
if (origin.startsWith("https://") || origin.startsWith("http://") || origin.startsWith("ssh://")) {
origin = new Parser.HostAndPath(origin).concat();
origin = new UrlCleaner(origin).build(service);
}
if (origin.contains("@")) {
origin = new Parser.HostAndPath("https://" + origin).concat();
origin = new UrlCleaner("https://" + origin).build(service);
}
if (service == Service.Unknown) {
// Do not override a known with an unknown service
Expand All @@ -75,17 +75,18 @@ public Parser registerRemote(Service service, String origin) {
}

public GitRemote parse(String url) {
Parser.HostAndPath hostAndPath = new Parser.HostAndPath(url);
UrlCleaner urlCleaner = new UrlCleaner(url);

String origin = hostAndPath.host;
if (hostAndPath.port > 0) {
origin = origin + ':' + hostAndPath.port;
String origin = urlCleaner.host;
if (urlCleaner.port > 0) {
origin = origin + ':' + urlCleaner.port;
}
Service service = origins.get(origin);
if (service == null) {
for (String maybeOrigin : origins.keySet()) {
if (hostAndPath.concat().startsWith(maybeOrigin)) {
service = origins.get(maybeOrigin);
Service maybeService = origins.get(maybeOrigin);
if (urlCleaner.build(maybeService).startsWith(maybeOrigin)) {
service = maybeService;
origin = maybeOrigin;
break;
}
Expand All @@ -95,7 +96,7 @@ public GitRemote parse(String url) {
if (service == null) {
// If we cannot find a service, we assume the last 2 path segments are the organization and repository name
service = Service.Unknown;
String hostPath = hostAndPath.concat();
String hostPath = urlCleaner.build(service);
String[] segments = hostPath.split("/");
if (segments.length <= 2) {
origin = null;
Expand All @@ -104,7 +105,7 @@ public GitRemote parse(String url) {
}
}

String repositoryPath = hostAndPath.repositoryPath(origin);
String repositoryPath = urlCleaner.repositoryPath(origin, service);

switch (service) {
case AzureDevOps:
Expand Down Expand Up @@ -132,13 +133,13 @@ public GitRemote parse(String url) {
return new GitRemote(service, url, origin, repositoryPath, organization, repositoryName);
}

private static class HostAndPath {
private static class UrlCleaner {
String scheme;
String host;
int port;
String path;

public HostAndPath(String url) {
public UrlCleaner(String url) {
try {
URIish uri = new URIish(url);
scheme = uri.getScheme();
Expand All @@ -155,12 +156,12 @@ public HostAndPath(String url) {
}
}

private String concat() {
private String build(Service service) {
StringBuilder builder = new StringBuilder(64);
if (host != null) {
builder.append(host);
}
if (!isDefaultPort()) {
if (!isDefaultPort(service)) {
builder.append(':').append(port);
}
if (!path.isEmpty()) {
Expand All @@ -172,18 +173,19 @@ private String concat() {
return builder.toString();
}

private boolean isDefaultPort() {
private boolean isDefaultPort(Service service) {
return port < 1 ||
("https".equals(scheme) && port == 443) ||
("http".equals(scheme) && port == 80) ||
("ssh".equals(scheme) && port == 22);
("ssh".equals(scheme) && port == 22 && service != Service.Bitbucket) ||
("ssh".equals(scheme) && port == 7999 && service == Service.Bitbucket);
}

private String repositoryPath(@Nullable String origin) {
private String repositoryPath(@Nullable String origin, Service service) {
if (origin == null) {
origin = "";
}
String hostAndPath = concat();
String hostAndPath = build(service);
if (!hostAndPath.startsWith(origin)) {
throw new IllegalArgumentException("Unable to find origin '" + origin + "' in '" + hostAndPath + "'");
}
Expand Down
2 changes: 2 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ void parseUnknownRemote(String cloneUrl, String expectedOrigin, String expectedP
[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:22/stash/org/repo, scm.company.com:22/stash, Bitbucket, org/repo, org, repo
ssh://scm.company.com:7999/stash/org/repo, scm.company.com/stash, Bitbucket, org/repo, org, repo
ssh://scm.company.com:7999/stash/org/repo, scm.company.com:7999/stash, Bitbucket, org/repo, org, repo

Copy link
Contributor

Choose a reason for hiding this comment

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

aren't these the same inputs? expected different results?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, the origin is both input and expected result

https://scm.company.com/very/long/context/path/org/repo.git, scm.company.com/very/long/context/path, Bitbucket, org/repo, org, repo
Expand Down
Loading