Skip to content

Commit

Permalink
[JENKINS-45436] Address oleg's review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Jul 11, 2017
1 parent 54f70c0 commit 4f7f0a6
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions src/main/java/jenkins/scm/api/SCMName.java
Expand Up @@ -51,10 +51,12 @@ private SCMName() {

/**
* Makes best effort to guess a "sensible" display name from the hostname in the URL.
* For example {@code SCMName.fromUrl("https://github.example.com", "github.")} should return {@code "example"}.
*
* @param url the URL.
* @return the display name or {@code null}
*/
@CheckForNull
public static String fromUrl(@NonNull String url, @CheckForNull String... ignoredPrefixes) {
try {
return innerFromUrl(url, ignoredPrefixes.length == 0 ? null : Arrays.asList(ignoredPrefixes));
Expand All @@ -65,10 +67,13 @@ public static String fromUrl(@NonNull String url, @CheckForNull String... ignore

/**
* Makes best effort to guess a "sensible" display name from the hostname in the URL.
* For example {@code SCMName.fromUrl("https://github.example.com", Arrays.asList("github."))} should return
* {@code "example"}.
*
* @param url the URL.
* @return the display name or {@code null}
*/
@CheckForNull
public static String fromUrl(@NonNull String url, @CheckForNull List<String> ignoredPrefixes) {
try {
return innerFromUrl(url, ignoredPrefixes);
Expand All @@ -85,45 +90,46 @@ public static String fromUrl(@NonNull String url, @CheckForNull List<String> ign
* @throws LinkageError if Guava changes their API that we have depended on.
*/
@CheckForNull
private static String innerFromUrl(@NonNull String url, List<String> ignoredPrefixes) throws LinkageError {
private static String innerFromUrl(@NonNull String url, @CheckForNull List<String> ignoredPrefixes)
throws LinkageError {
String hostName;
try {
URL serverUri = new URL(url);
hostName = serverUri.getHost();
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);
}
if (ignoredPrefixes != null) {
for (String prefix : ignoredPrefixes) {
if (prefix.endsWith(".")) {
if (hostName.startsWith(prefix)) {
hostName = hostName.substring(prefix.length());
break;
}
} else {
if (hostName.startsWith(prefix + ".")) {
hostName = hostName.substring(prefix.length() + 1);
break;
}
}
}
}
if (StringUtils.isNotBlank(hostName)) {
hostName = IDN.toUnicode(hostName).replace('.', ' ').replace('-', ' ');
}
} catch (MalformedURLException e) {
// ignore, best effort
hostName = null;
}
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);
}
if (ignoredPrefixes != null) {
for (String prefix : ignoredPrefixes) {
if (prefix.endsWith(".")) {
if (hostName.startsWith(prefix)) {
hostName = hostName.substring(prefix.length());
break;
}
} else {
if (hostName.startsWith(prefix + ".")) {
hostName = hostName.substring(prefix.length() + 1);
break;
}
}
}
}
if (StringUtils.isNotBlank(hostName)) {
hostName = IDN.toUnicode(hostName).replace('.', ' ').replace('-', ' ');
}
return hostName;
}
}

0 comments on commit 4f7f0a6

Please sign in to comment.