Skip to content
Permalink
Browse files

[JENKINS-45726] Corrected a connectivity error on auth when there's a…

… network proxy
  • Loading branch information
esteveblanch committed Jul 24, 2017
1 parent 66ae724 commit b1ce0a05b18f7b2c0aa89929c507e2b26256bc89
@@ -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;
@@ -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;
@@ -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
@@ -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() {
@@ -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;
@@ -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);
@@ -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;
@@ -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);

0 comments on commit b1ce0a0

Please sign in to comment.