Skip to content

Commit

Permalink
Merge pull request #145 from stephenc/jenkins-45436
Browse files Browse the repository at this point in the history
[JENKINS-45436] Pick up SCMName API
  • Loading branch information
stephenc committed Jul 11, 2017
2 parents a507a73 + 7e8f57f commit 388545b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -23,7 +23,7 @@
<properties>
<jenkins.version>1.625.3</jenkins.version>
<workflow.version>1.14.2</workflow.version>
<scm-api.version>2.2.0-beta-1</scm-api.version>
<scm-api.version>2.2.0-20170711.135617-15</scm-api.version>
</properties>

<scm>
Expand Down
Expand Up @@ -45,6 +45,7 @@
import java.util.logging.Logger;
import java.util.logging.Level;

import jenkins.scm.api.SCMName;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand All @@ -70,54 +71,13 @@ public class Endpoint extends AbstractDescribableImpl<Endpoint> {
private final String name;
private final String apiUri;

/**
* Makes best effort to guess a "sensible" display name from the hostname in the apiUri.
*
* @param apiUri the apiUri.
* @return the display name or {@code null}
* @throws LinkageError if Guava changes their API that we have depended on.
*/
@CheckForNull
/*package*/ static String inferDisplayName(@CheckForNull String apiUri) throws LinkageError {
if (apiUri == null) {
return apiUri;
}
String hostName;
try {
URI serverUri = new URI(apiUri);
hostName = serverUri.getHost();
if (hostName != null) {
// let's see if we can make this more "friendly"
InternetDomainName host = InternetDomainName.from(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);
}
for (String prefix : COMMON_PREFIX_HOSTNAMES) {
if (hostName.startsWith(prefix)) {
hostName = hostName.substring(prefix.length());
break;
}
}
}
} catch (URISyntaxException e) {
// ignore, best effort
hostName = null;
}
return hostName;
}

@DataBoundConstructor
public Endpoint(String apiUri, String name) {
this.apiUri = GitHubConfiguration.normalizeApiUri(Util.fixEmptyAndTrim(apiUri));
name = Util.fixEmptyAndTrim(name);
if (name == null) {
this.name = inferDisplayName(apiUri);
if (StringUtils.isBlank(name)) {
this.name = SCMName.fromUrl(this.apiUri, COMMON_PREFIX_HOSTNAMES);
} else {
this.name = name;
this.name = name.trim();
}
}

Expand Down

0 comments on commit 388545b

Please sign in to comment.