Skip to content

Commit

Permalink
[JENKINS-43370] Use proxy configuration from Jenkins
Browse files Browse the repository at this point in the history
  • Loading branch information
tisoft authored and Raúl Arabaolaza Barquin committed Aug 16, 2017
1 parent 43fea73 commit 8dc8289
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
Expand Up @@ -56,6 +56,7 @@
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
Expand All @@ -64,6 +65,7 @@
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.io.IOException;
import java.net.Proxy;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -216,6 +218,24 @@ private static <T extends Credentials> T getCredentials(@Nonnull Class<T> type,
CredentialsMatchers.instanceOf(type)));
}

/**
* 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.getActiveInstance();

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

private static GitHub getGitHubIfValid(String credentialsId, String gitApiUrl, Item context) throws IOException {
if (credentialsId == null || credentialsId.isEmpty()) {
throw new IllegalArgumentException(NULL_CREDENTIALS_ID);
Expand All @@ -224,12 +244,19 @@ private static GitHub getGitHubIfValid(String credentialsId, String gitApiUrl, I
if (credentials == null) {
throw new IllegalArgumentException(CREDENTIALS_ID_NOT_EXISTS);
}
GitHub github = null;
GitHubBuilder githubBuilder = new GitHubBuilder();

githubBuilder.withOAuthToken(credentials.getPassword().getPlainText(), credentials.getUsername());

if (gitApiUrl == null || gitApiUrl.isEmpty()) {
github = GitHub.connect(credentials.getUsername(), credentials.getPassword().getPlainText());
githubBuilder = githubBuilder.withProxy(getProxy("https://api.github.com"));
} else {
github = GitHub.connectToEnterprise(gitApiUrl, credentials.getUsername(), credentials.getPassword().getPlainText());
githubBuilder = githubBuilder.withEndpoint(gitApiUrl);
githubBuilder = githubBuilder.withProxy(getProxy(gitApiUrl));
}

GitHub github = githubBuilder.build();

if (github.isCredentialValid()) {
return github;
} else {
Expand Down
Expand Up @@ -20,17 +20,20 @@
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.IOException;
import java.net.Proxy;

import static org.mockito.Matchers.anyString;

@RunWith (PowerMockRunner.class)
@PrepareForTest ({GitHub.class})
@PrepareForTest ({GitHubStatusNotificationStep.class})
@PowerMockIgnore ({"javax.crypto.*" })
public class GitHubNotificationPipelineStepTest {

Expand Down Expand Up @@ -105,9 +108,12 @@ public void buildWithWrongCredentialsMustFailEnterprise() throws Exception {
@Test
public void buildWithWrongRepoMustFail() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHUser user = PowerMockito.mock(GHUser.class);
PowerMockito.when(user.getRepository(anyString())).thenReturn(null);
Expand All @@ -132,9 +138,12 @@ public void buildWithWrongRepoMustFail() throws Exception {
@Test
public void buildWithWrongCommitMustFail() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
Expand All @@ -160,9 +169,12 @@ public void buildWithWrongCommitMustFail() throws Exception {
@Test
public void buildWithInferWithoutCommitMustFail() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
Expand All @@ -188,9 +200,12 @@ public void buildWithInferWithoutCommitMustFail() throws Exception {
@Test
public void buildWithInferWithoutAccountMustFail() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
Expand All @@ -216,9 +231,12 @@ public void buildWithInferWithoutAccountMustFail() throws Exception {
@Test
public void buildWithInferWithoutRepoMustFail() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
Expand All @@ -243,9 +261,12 @@ public void buildWithInferWithoutRepoMustFail() throws Exception {
@Test
public void buildWithInferWithoutCredentialsMustFail() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
Expand All @@ -270,9 +291,12 @@ public void buildWithInferWithoutCredentialsMustFail() throws Exception {
@Test
public void build() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
Expand All @@ -298,9 +322,12 @@ public void build() throws Exception {
@Test
public void buildWithFolderCredentials() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connect("user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
Expand Down Expand Up @@ -328,9 +355,13 @@ public void buildWithFolderCredentials() throws Exception {
@Test
public void buildEnterprise() throws Exception {

GitHubBuilder ghb = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.when(ghb.withProxy(Matchers.<Proxy>anyObject())).thenReturn(ghb);
PowerMockito.when(ghb.withOAuthToken(anyString(), anyString())).thenReturn(ghb);
PowerMockito.when(ghb.withEndpoint("https://api.example.com")).thenReturn(ghb);
PowerMockito.whenNew(GitHubBuilder.class).withNoArguments().thenReturn(ghb);
GitHub gh = PowerMockito.mock(GitHub.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.when(GitHub.connectToEnterprise("https://api.example.com","user", "password")).thenReturn(gh);
PowerMockito.when(ghb.build()).thenReturn(gh);
PowerMockito.when(gh.isCredentialValid()).thenReturn(true);
GHRepository repo = PowerMockito.mock(GHRepository.class);
GHUser user = PowerMockito.mock(GHUser.class);
Expand Down

0 comments on commit 8dc8289

Please sign in to comment.