Skip to content

Commit

Permalink
[JENKINS-45436] Pick up SCMName API
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Jul 11, 2017
1 parent 399b3b8 commit 1a9e017
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -22,7 +22,7 @@

<properties>
<jenkins.version>1.625.3</jenkins.version>
<scm-api.version>2.2.0-alpha-1</scm-api.version>
<scm-api.version>2.2.0-20170711.135617-15</scm-api.version>
</properties>

<scm>
Expand Down
53 changes: 4 additions & 49 deletions src/main/java/org/jenkinsci/plugin/gitea/servers/GiteaServer.java
Expand Up @@ -47,6 +47,7 @@
import javax.annotation.Nonnull;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMName;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugin.gitea.client.api.GiteaAuth;
import org.jenkinsci.plugin.gitea.client.api.GiteaConnection;
Expand Down Expand Up @@ -108,55 +109,9 @@ public GiteaServer(@CheckForNull String displayName, @NonNull String serverUrl,
this.manageHooks = manageHooks && StringUtils.isNotBlank(credentialsId);
this.credentialsId = manageHooks ? credentialsId : null;
this.serverUrl = GiteaServers.normalizeServerUrl(serverUrl);
if (displayName == null || StringUtils.isBlank(displayName)) {
// try to infer the display name
String hostName = null;
try {
hostName = inferDisplayName(serverUrl);
} catch (LinkageError e) {
// guava changed their @Beta API that we have compiled against
}
this.displayName = hostName;
} else {
this.displayName = displayName;
}
}

/**
* Makes best effort to guess a "sensible" display name from the hostname in the server URL.
*
* @param serverUrl the server URL.
* @return the display name or {@code null}
* @throws LinkageError if Guava changes their API that we have depended on.
*/
@CheckForNull
/*package*/ static String inferDisplayName(@NonNull String serverUrl) throws LinkageError {
String hostName;
try {
URI serverUri = new URI(serverUrl);
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;
this.displayName = StringUtils.isBlank(displayName)
? SCMName.fromUrl(this.serverUrl, COMMON_PREFIX_HOSTNAMES)
: displayName;
}

/**
Expand Down

0 comments on commit 1a9e017

Please sign in to comment.