diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java index 18baef86..86963bd5 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java @@ -10,6 +10,7 @@ import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; +import hudson.ProxyConfiguration; import hudson.model.AbstractProject; import hudson.model.Cause; import hudson.model.Executor; @@ -54,6 +55,7 @@ public class StashBuildTrigger extends Trigger> { private final String projectPath; private final String cron; private final String stashHost; + private transient JSONObject proxySettings; private final String credentialsId; private final String projectCode; private final String repositoryName; @@ -81,6 +83,7 @@ public StashBuildTrigger( String projectPath, String cron, String stashHost, + JSONObject proxySettings, String credentialsId, String projectCode, String repositoryName, @@ -100,6 +103,7 @@ public StashBuildTrigger( this.projectPath = projectPath; this.cron = cron; this.stashHost = stashHost; + this.proxySettings = proxySettings; this.credentialsId = credentialsId; this.projectCode = projectCode; this.repositoryName = repositoryName; @@ -125,6 +129,50 @@ public String getStashHost() { return stashHost; } + public JSONObject getProxySettings() { + return proxySettings; + } + + public JSONObject getProxyDetails() { + return this.getProxySettings().getJSONObject("proxyChoice"); + } + + private String getProxyType() { + return this.getProxyDetails().getString("value"); + } + + public ProxyConfiguration getProxy() { + ProxyConfiguration proxy = null; + + switch (this.getProxyType()) { + default: + case "jenkins": + proxy = Jenkins.getInstance().proxy; + break; + case "custom": + JSONObject proxyDetails = this.getProxyDetails(); + + proxy = + new ProxyConfiguration( + proxyDetails.getString("proxyHost"), + proxyDetails.getInt("proxyPort"), + proxyDetails.getString("proxyUser"), + proxyDetails.getString("proxyPass")); + } + + return proxy; + } + + /** + * Helper method for the jelly view to determine proxy type. + * + * @param type + * @return + */ + public boolean isProxyType(String type) { + return (getProxyType().equalsIgnoreCase(type)) ? true : false; + } + public String getProjectPath() { return this.projectPath; } diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java index 30f6f04c..24e631b2 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java @@ -69,6 +69,7 @@ public void init() { client = new StashApiClient( trigger.getStashHost(), + trigger.getProxy(), trigger.getUsername(), trigger.getPassword(), trigger.getProjectCode(), diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java index 5bf7f856..b7839d65 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java @@ -1,6 +1,7 @@ package stashpullrequestbuilder.stashpullrequestbuilder.stash; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.ProxyConfiguration; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; @@ -24,6 +25,7 @@ import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; +import jenkins.model.Jenkins; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; @@ -60,12 +62,15 @@ public class StashApiClient { private String apiBaseUrl; + private final ProxyConfiguration proxy; + private String project; private String repositoryName; private Credentials credentials; public StashApiClient( String stashHost, + ProxyConfiguration proxy, String username, String password, String project, @@ -75,6 +80,7 @@ public StashApiClient( this.project = project; this.repositoryName = repositoryName; this.apiBaseUrl = stashHost.replaceAll("/$", "") + "/rest/api/1.0/projects/"; + this.proxy = proxy; if (ignoreSsl) { Protocol easyhttps = new Protocol("https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), 443); @@ -197,21 +203,23 @@ private HttpClient getHttpClient() { httpParams.setParameter( CoreConnectionPNames.SO_TIMEOUT, StashApiClient.HTTP_SOCKET_TIMEOUT_SECONDS * 1000); - // if (Jenkins.getInstance() != null) { - // ProxyConfiguration proxy = Jenkins.getInstance().proxy; - // if (proxy != null) { - // logger.info("Jenkins proxy: " + proxy.name + ":" + proxy.port); - // client.getHostConfiguration().setProxy(proxy.name, proxy.port); - // String username = proxy.getUserName(); - // String password = proxy.getPassword(); - // // Consider it to be passed if username specified. Sufficient? - // if (username != null && !"".equals(username.trim())) { - // logger.info("Using proxy authentication (user=" + username + ")"); - // client.getState().setProxyCredentials(AuthScope.ANY, - // new UsernamePasswordCredentials(username, password)); - // } - // } - // } + if (Jenkins.getInstanceOrNull() != null && proxy != null) { + logger.fine("Using proxy: " + proxy.name + ":" + proxy.port); + client.getHostConfiguration().setProxy(proxy.name, proxy.port); + + String username = proxy.getUserName(); + String password = proxy.getPassword(); + + // Consider it to be passed if username specified. Sufficient? + if (username != null && !"".equals(username.trim())) { + logger.fine("With proxy authentication (user=" + username + ")"); + client + .getState() + .setProxyCredentials( + AuthScope.ANY, new UsernamePasswordCredentials(username, password)); + } + } + return client; } diff --git a/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly index 148beb45..632dfbc8 100644 --- a/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly +++ b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly @@ -6,6 +6,32 @@ + + + + +
+ + + + + + + + + + + + + + + + +
+
+ + +