Skip to content

Commit

Permalink
[FIXED JENKINS-41244] Be defensive when trying to set the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Jan 20, 2017
1 parent 5e71f59 commit 68187e4
Showing 1 changed file with 32 additions and 4 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -120,6 +122,10 @@ public class GitHubSCMSource extends AbstractGitSCMSource {
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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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.
Expand Down

0 comments on commit 68187e4

Please sign in to comment.