Skip to content

Commit 0df3c15

Browse files
authored
Merge pull request #64 from twz123/detect-scmname-gracefully
Don't fail SCMName detection on non-IDN host names
2 parents b6751c6 + df6e300 commit 0df3c15

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/main/java/jenkins/scm/api/SCMName.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,13 @@ private static String innerFromUrl(@NonNull String url, @CheckForNull List<Strin
105105
if (StringUtils.isBlank(hostName)) {
106106
return null;
107107
}
108-
// let's see if we can make this more "friendly"
109-
InternetDomainName host = InternetDomainName.from(IDN.toASCII(hostName));
110-
if (host.hasPublicSuffix()) {
111-
String publicName = host.publicSuffix().name();
112-
hostName = StringUtils.removeEnd(StringUtils.removeEnd(host.name(), publicName), ".")
113-
.toLowerCase(Locale.ENGLISH);
114-
} else {
115-
hostName = StringUtils.removeEnd(host.name(), ".").toLowerCase(Locale.ENGLISH);
108+
109+
hostName = resolveInternetDomainName(hostName);
110+
if (hostName == null) {
111+
return null;
116112
}
113+
114+
// let's see if we can make this more "friendly"
117115
if (ignoredPrefixes != null) {
118116
for (String prefix : ignoredPrefixes) {
119117
if (prefix.endsWith(".")) {
@@ -134,4 +132,20 @@ private static String innerFromUrl(@NonNull String url, @CheckForNull List<Strin
134132
}
135133
return hostName;
136134
}
135+
136+
private static String resolveInternetDomainName(String hostName) {
137+
final InternetDomainName host;
138+
try {
139+
host = InternetDomainName.from(IDN.toASCII(hostName));
140+
} catch (IllegalArgumentException notADomainName) {
141+
return null;
142+
}
143+
144+
if (host.hasPublicSuffix()) {
145+
final String publicName = host.publicSuffix().name();
146+
return StringUtils.removeEnd(StringUtils.removeEnd(host.name(), publicName), ".").toLowerCase(Locale.ENGLISH);
147+
}
148+
149+
return StringUtils.removeEnd(host.name(), ".").toLowerCase(Locale.ENGLISH);
150+
}
137151
}

src/test/java/jenkins/scm/api/SCMNameTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ public void given__url_with_idn__when__naming__then__punycode_is_roundtripped2()
6868
assertThat(SCMName.fromUrl("http://\u4F8B\u5B50.\u4E2D\u56FD/"), is("\u4F8B\u5B50"));
6969
}
7070

71+
@Test
72+
public void given__url_with_ipv4address__when__naming__then__no_name_inferred() throws Exception {
73+
assertThat(SCMName.fromUrl("http://127.0.0.1/scm"), is(nullValue()));
74+
}
75+
7176
}

0 commit comments

Comments
 (0)