Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #85 Corrected a connectivity
error on auth with proxy

See also:
* [JENKINS-45726][JENKINS-45726]

[JENKINS-45726]: https://issues.jenkins-ci.org/browse/JENKINS-45726
  • Loading branch information
samrocketman committed Oct 1, 2017
2 parents 66ae724 + ed74c06 commit 608d92d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Expand Up @@ -28,6 +28,9 @@ of this software and associated documentation files (the "Software"), to deal

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;

import hudson.security.SecurityRealm;
import jenkins.model.Jenkins;
import org.acegisecurity.GrantedAuthority;
Expand All @@ -42,9 +45,12 @@ of this software and associated documentation files (the "Software"), to deal
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.RateLimitHandler;

import org.kohsuke.github.extras.OkHttpConnector;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -58,6 +64,8 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nonnull;


/**
* @author mocleiri
Expand Down Expand Up @@ -185,14 +193,43 @@ public String getGithubServer() {

public GitHub getGitHub() throws IOException {
if (this.gh == null) {

String host;
try {
host = new URL(this.githubServer).getHost();
} catch (MalformedURLException e) {
throw new IOException("Invalid GitHub API URL: " + this.githubServer, e);
}

OkHttpClient client = new OkHttpClient().setProxy(getProxy(host));

this.gh = GitHubBuilder.fromEnvironment()
.withEndpoint(this.githubServer)
.withOAuthToken(this.accessToken)
.withRateLimitHandler(RateLimitHandler.FAIL)
.withConnector(new OkHttpConnector(new OkUrlFactory(client)))
.build();
}
return gh;
}

/**
* Uses proxy if configured on pluginManager/advanced page
*
* @param host GitHub's hostname to build proxy to
*
* @return proxy to use it in connector. Should not be null as it can lead to unexpected behaviour
*/
@Nonnull
private static Proxy getProxy(@Nonnull String host) {
Jenkins jenkins = Jenkins.getInstance();

if (jenkins.proxy == null) {
return Proxy.NO_PROXY;
} else {
return jenkins.proxy.createProxy(host);
}
}

@Override
public GrantedAuthority[] getAuthorities() {
Expand Down
Expand Up @@ -10,7 +10,9 @@
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.RateLimitHandler;
import org.kohsuke.github.extras.OkHttpConnector;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
Expand Down Expand Up @@ -63,6 +65,7 @@ private GHMyself mockGHMyselfAs(String username) throws IOException {
PowerMockito.when(builder.withEndpoint("https://api.github.com")).thenReturn(builder);
PowerMockito.when(builder.withOAuthToken("accessToken")).thenReturn(builder);
PowerMockito.when(builder.withRateLimitHandler(RateLimitHandler.FAIL)).thenReturn(builder);
PowerMockito.when(builder.withConnector(Mockito.any(OkHttpConnector.class))).thenReturn(builder);
PowerMockito.when(builder.build()).thenReturn(gh);
GHMyself me = PowerMockito.mock(GHMyself.class);
PowerMockito.when(gh.getMyself()).thenReturn(me);
Expand Down
Expand Up @@ -51,7 +51,9 @@ of this software and associated documentation files (the "Software"), to deal
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.PagedIterable;
import org.kohsuke.github.RateLimitHandler;
import org.kohsuke.github.extras.OkHttpConnector;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
Expand Down Expand Up @@ -176,6 +178,7 @@ private GHMyself mockGHMyselfAs(String username) throws IOException {
PowerMockito.when(builder.withEndpoint("https://api.github.com")).thenReturn(builder);
PowerMockito.when(builder.withOAuthToken("accessToken")).thenReturn(builder);
PowerMockito.when(builder.withRateLimitHandler(RateLimitHandler.FAIL)).thenReturn(builder);
PowerMockito.when(builder.withConnector(Mockito.any(OkHttpConnector.class))).thenReturn(builder);
PowerMockito.when(builder.build()).thenReturn(gh);
GHMyself me = PowerMockito.mock(GHMyself.class);
PowerMockito.when(gh.getMyself()).thenReturn((GHMyself) me);
Expand Down

0 comments on commit 608d92d

Please sign in to comment.