Skip to content
Permalink
Browse files

[FIXED JENKINS-41244] Be defensive when trying to set the browser

  • Loading branch information
stephenc committed Jan 20, 2017
1 parent 5e71f59 commit 68187e4fc4590e64244f3dac44b367847a84f25f
Showing with 32 additions and 4 deletions.
  1. +32 −4 src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource.java
@@ -70,6 +70,8 @@
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLHandshakeException;
@@ -120,6 +122,10 @@
public static final String VALID_GITHUB_USER_NAME = "^[0-9A-Za-z]([0-9A-Za-z._-]+[0-9A-Za-z])$";
public static final String VALID_GIT_SHA1 = "^[a-fA-F0-9]{40}$";
public static final String GITHUB_URL = GitHubServerConfig.GITHUB_URL;
/**
* Log spam protection, only log at most once every 5 minutes.
*/
private static final AtomicLong jenkins41244Warning = new AtomicLong();
private static final Logger LOGGER = Logger.getLogger(GitHubSCMSource.class.getName());

private final String apiUri;
@@ -815,7 +821,7 @@ public SCM build(SCMHead head, SCMRevision revision) {
GitSCM scm = (GitSCM) super.build(head, null);
String repoUrl = repositoryUrl(getRepoOwner(), getRepository());
if (repoUrl != null) {
scm.setBrowser(new GithubWeb(repoUrl));
setBrowser(scm, repoUrl);
}
return scm;
} else if (head instanceof PullRequestSCMHead) {
@@ -836,7 +842,7 @@ public SCM build(SCMHead head, SCMRevision revision) {
String repoUrl = repositoryUrl(((PullRequestSCMHead) head).getSourceOwner(),
((PullRequestSCMHead) head).getSourceRepo());
if (repoUrl != null) {
scm.setBrowser(new GithubWeb(repoUrl));
setBrowser(scm, repoUrl);
}
return scm;
} else {
@@ -846,20 +852,42 @@ public SCM build(SCMHead head, SCMRevision revision) {
GitSCM scm = (GitSCM) super.build(head, revision);
String repoUrl = repositoryUrl(getRepoOwner(), getRepository());
if (repoUrl != null) {
scm.setBrowser(new GithubWeb(repoUrl));
setBrowser(scm, repoUrl);
}
return scm;
}
} else {
GitSCM scm = (GitSCM) super.build(head, /* casting just as an assertion */(SCMRevisionImpl) revision);
String repoUrl = repositoryUrl(getRepoOwner(), getRepository());
if (repoUrl != null) {
scm.setBrowser(new GithubWeb(repoUrl));
setBrowser(scm, repoUrl);
}
return scm;
}
}

private void setBrowser(GitSCM scm, String repoUrl) {
try {
scm.setBrowser(new GithubWeb(repoUrl));
} catch (NoSuchMethodError e) {
Level level;
long now = System.currentTimeMillis();
long next = jenkins41244Warning.get();
if (now <= next) {
long newNext = now + TimeUnit.MINUTES.toMillis(5);
if (jenkins41244Warning.compareAndSet(next, newNext)) {
level = Level.WARNING;
} else {
level = Level.FINE;
}
} else {
level = Level.FINE;
}
LOGGER.log(level, "JENKINS-41244: GitHub Branch Source cannot set browser url with currently "
+ "installed version of Git plugin", e);
}
}

/**
* Tries as best as possible to guess the repository HTML url to use with {@link GithubWeb}.
* @param owner the owner.

0 comments on commit 68187e4

Please sign in to comment.
You can’t perform that action at this time.