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

Don't fail SCMName detection on non-IDN host names #64

Merged
merged 1 commit into from
Jun 14, 2019
Merged
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
30 changes: 22 additions & 8 deletions src/main/java/jenkins/scm/api/SCMName.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ private static String innerFromUrl(@NonNull String url, @CheckForNull List<Strin
if (StringUtils.isBlank(hostName)) {
return null;
}
// let's see if we can make this more "friendly"
InternetDomainName host = InternetDomainName.from(IDN.toASCII(hostName));
if (host.hasPublicSuffix()) {
String publicName = host.publicSuffix().name();
hostName = StringUtils.removeEnd(StringUtils.removeEnd(host.name(), publicName), ".")
.toLowerCase(Locale.ENGLISH);
} else {
hostName = StringUtils.removeEnd(host.name(), ".").toLowerCase(Locale.ENGLISH);

hostName = resolveInternetDomainName(hostName);
if (hostName == null) {
return null;
}

// let's see if we can make this more "friendly"
if (ignoredPrefixes != null) {
for (String prefix : ignoredPrefixes) {
if (prefix.endsWith(".")) {
Expand All @@ -134,4 +132,20 @@ private static String innerFromUrl(@NonNull String url, @CheckForNull List<Strin
}
return hostName;
}

private static String resolveInternetDomainName(String hostName) {
final InternetDomainName host;
try {
host = InternetDomainName.from(IDN.toASCII(hostName));
} catch (IllegalArgumentException notADomainName) {
return null;
}

if (host.hasPublicSuffix()) {
final String publicName = host.publicSuffix().name();
return StringUtils.removeEnd(StringUtils.removeEnd(host.name(), publicName), ".").toLowerCase(Locale.ENGLISH);
}

return StringUtils.removeEnd(host.name(), ".").toLowerCase(Locale.ENGLISH);
}
}
5 changes: 5 additions & 0 deletions src/test/java/jenkins/scm/api/SCMNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public void given__url_with_idn__when__naming__then__punycode_is_roundtripped2()
assertThat(SCMName.fromUrl("http://\u4F8B\u5B50.\u4E2D\u56FD/"), is("\u4F8B\u5B50"));
}

@Test
public void given__url_with_ipv4address__when__naming__then__no_name_inferred() throws Exception {
assertThat(SCMName.fromUrl("http://127.0.0.1/scm"), is(nullValue()));
}

}