Skip to content

Commit 3d39987

Browse files
authored
Merge pull request #28 from harshitvasu/ACE-1568-proxy-apache-diverge
ACE-1568 Proxy support for browserstack plugin
2 parents 358be2a + 58ab632 commit 3d39987

File tree

7 files changed

+155
-22
lines changed

7 files changed

+155
-22
lines changed

pom.xml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>browserstack-integration</artifactId>
13-
<version>1.1.9-SNAPSHOT</version>
13+
<version>1.1.10-SNAPSHOT</version>
1414
<packaging>hpi</packaging>
1515

1616
<name>BrowserStack</name>
@@ -102,6 +102,18 @@
102102

103103
<dependencies>
104104

105+
<dependency>
106+
<groupId>com.brsanthu</groupId>
107+
<artifactId>google-analytics-java</artifactId>
108+
<version>1.1.2</version>
109+
<exclusions>
110+
<exclusion>
111+
<groupId>org.apache.httpcomponents</groupId>
112+
<artifactId>httpclient</artifactId>
113+
</exclusion>
114+
</exclusions>
115+
</dependency>
116+
105117
<dependency>
106118
<groupId>org.jmockit</groupId>
107119
<artifactId>jmockit</artifactId>
@@ -138,7 +150,7 @@
138150
<dependency>
139151
<groupId>com.browserstack</groupId>
140152
<artifactId>automate-client-java</artifactId>
141-
<version>0.5</version>
153+
<version>0.6</version>
142154
</dependency>
143155

144156
<dependency>
@@ -150,7 +162,7 @@
150162
<dependency>
151163
<groupId>commons-codec</groupId>
152164
<artifactId>commons-codec</artifactId>
153-
<version>1.10</version>
165+
<version>1.11</version>
154166
</dependency>
155167

156168
<dependency>
@@ -165,12 +177,6 @@
165177
<version>1.10</version>
166178
</dependency>
167179

168-
<dependency>
169-
<groupId>com.brsanthu</groupId>
170-
<artifactId>google-analytics-java</artifactId>
171-
<version>1.1.2</version>
172-
</dependency>
173-
174180
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
175181
<dependency>
176182
<groupId>com.google.code.gson</groupId>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.browserstack.automate.ci.common.proxysettings;
2+
3+
import hudson.ProxyConfiguration;
4+
import jenkins.model.Jenkins;
5+
6+
import java.net.InetSocketAddress;
7+
import java.net.Proxy;
8+
9+
public class JenkinsProxySettings {
10+
11+
private static final ProxyConfiguration jenkinsProxy = Jenkins.getInstanceOrNull() != null ? Jenkins.getInstanceOrNull().proxy : null;
12+
private static final String protocol = "https";
13+
private static final String systemProxyHost = System.getProperty(protocol + ".proxyHost");
14+
private static final int systemProxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", "0"));
15+
private static final String systemProxyUser = System.getProperty(protocol + ".proxyUser");
16+
private static final String systemProxyPassword = System.getProperty(protocol + ".proxyPassword");
17+
18+
public static Proxy getJenkinsProxy() {
19+
if (hasSystemProxy()) {
20+
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(systemProxyHost, systemProxyPort));
21+
}
22+
23+
if (jenkinsProxy == null) return null;
24+
final String proxyHost = jenkinsProxy.name;
25+
final int proxyPort = jenkinsProxy.port;
26+
return (proxyHost != null && proxyPort != 0) ? new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)) : null;
27+
}
28+
29+
public static String getHost() {
30+
if (hasSystemProxy()) {
31+
return systemProxyHost;
32+
}
33+
34+
if (jenkinsProxy == null) return null;
35+
return jenkinsProxy.name;
36+
}
37+
38+
public static int getPort() {
39+
if (hasSystemProxy()) {
40+
return systemProxyPort;
41+
}
42+
43+
if (jenkinsProxy == null) return 0;
44+
return jenkinsProxy.port;
45+
}
46+
47+
public static String getUsername() {
48+
if (hasSystemProxy() && systemProxyUser != null && systemProxyPassword != null) {
49+
return systemProxyUser;
50+
}
51+
52+
if (jenkinsProxy == null) return null;
53+
return jenkinsProxy.getUserName();
54+
}
55+
56+
public static String getPassword() {
57+
if (hasSystemProxy() && systemProxyUser != null && systemProxyPassword != null) {
58+
return systemProxyPassword;
59+
}
60+
61+
if (jenkinsProxy == null) return null;
62+
return jenkinsProxy.getPassword();
63+
}
64+
65+
public static ProxyConfiguration getProxyConfig() {
66+
return jenkinsProxy;
67+
}
68+
69+
public static boolean hasProxy() {
70+
return getHost() != null && getPort() != 0;
71+
}
72+
73+
public static boolean hasSystemProxy() {
74+
return systemProxyHost != null && systemProxyPort != 0;
75+
}
76+
77+
}

src/main/java/com/browserstack/automate/ci/common/tracking/PluginsTracker.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
package com.browserstack.automate.ci.common.tracking;
22

3+
34
import com.browserstack.automate.ci.common.Tools;
45
import com.browserstack.automate.ci.common.constants.Constants;
6+
import com.browserstack.automate.ci.common.proxysettings.JenkinsProxySettings;
7+
import okhttp3.Authenticator;
58
import okhttp3.Call;
69
import okhttp3.Callback;
10+
import okhttp3.Credentials;
711
import okhttp3.MediaType;
812
import okhttp3.OkHttpClient;
913
import okhttp3.Request;
1014
import okhttp3.RequestBody;
1115
import okhttp3.Response;
16+
import okhttp3.Route;
1217
import org.json.JSONObject;
1318

1419
import java.io.IOException;
20+
import java.net.Proxy;
1521
import java.time.Instant;
1622
import java.util.Optional;
1723

1824
public class PluginsTracker {
1925
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
2026
private static final String URL = "https://api.browserstack.com/ci_plugins/track";
21-
private static final OkHttpClient client = new OkHttpClient();
27+
private static OkHttpClient client;
2228
private final String trackingId;
2329
private String username;
2430
private String accessKey;
@@ -27,12 +33,14 @@ public PluginsTracker(final String username, final String accessKey) {
2733
this.username = username;
2834
this.accessKey = accessKey;
2935
this.trackingId = Tools.getUniqueString(true, true);
36+
initializeClient();
3037
}
3138

3239
public PluginsTracker() {
3340
this.username = null;
3441
this.accessKey = null;
3542
this.trackingId = Tools.getUniqueString(true, true);
43+
initializeClient();
3644
}
3745

3846
private static void asyncPostRequestSilent(final String url, final String json) {
@@ -57,6 +65,31 @@ public void onResponse(Call call, Response response) throws IOException {
5765
});
5866
}
5967

68+
private void initializeClient() {
69+
70+
final Proxy proxy = JenkinsProxySettings.getJenkinsProxy() != null ? JenkinsProxySettings.getJenkinsProxy() : Proxy.NO_PROXY;
71+
if (proxy != Proxy.NO_PROXY) {
72+
final String username = JenkinsProxySettings.getUsername();
73+
final String password = JenkinsProxySettings.getPassword();
74+
if (username != null && password != null) {
75+
Authenticator proxyAuthenticator = new Authenticator() {
76+
@Override
77+
public Request authenticate(Route route, Response response) throws IOException {
78+
final String credential = Credentials.basic(username, password);
79+
return response.request().newBuilder()
80+
.header("Proxy-Authorization", credential)
81+
.build();
82+
}
83+
};
84+
this.client = new OkHttpClient.Builder().proxy(proxy).proxyAuthenticator(proxyAuthenticator).build();
85+
} else {
86+
this.client = new OkHttpClient.Builder().proxy(proxy).build();
87+
}
88+
} else {
89+
this.client = new OkHttpClient.Builder().build();
90+
}
91+
}
92+
6093
public void trackOperation(String operationType, JSONObject data) {
6194
JSONObject requestData = new JSONObject();
6295
requestData.put("source", Constants.JENKINS_CI_PLUGIN);
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
package com.browserstack.automate.ci.common.uploader;
22

33
import java.io.FileNotFoundException;
4+
45
import com.browserstack.appautomate.AppAutomateClient;
6+
import com.browserstack.automate.ci.common.proxysettings.JenkinsProxySettings;
57
import com.browserstack.automate.ci.jenkins.BrowserStackCredentials;
68
import com.browserstack.automate.exception.AppAutomateException;
79
import com.browserstack.automate.exception.InvalidFileExtensionException;
810

911
public class AppUploader {
1012

11-
String appPath;
12-
BrowserStackCredentials credentials;
13+
String appPath;
14+
BrowserStackCredentials credentials;
1315

14-
public AppUploader(String appPath, BrowserStackCredentials credentials) {
15-
this.appPath = appPath;
16-
this.credentials = credentials;
17-
}
16+
public AppUploader(String appPath, BrowserStackCredentials credentials) {
17+
this.appPath = appPath;
18+
this.credentials = credentials;
19+
}
1820

19-
public String uploadFile()
20-
throws AppAutomateException, FileNotFoundException, InvalidFileExtensionException {
21-
AppAutomateClient appAutomateClient =
22-
new AppAutomateClient(credentials.getUsername(), credentials.getDecryptedAccesskey());
23-
return appAutomateClient.uploadApp(this.appPath).getAppUrl();
24-
}
21+
public String uploadFile()
22+
throws AppAutomateException, FileNotFoundException, InvalidFileExtensionException {
23+
AppAutomateClient appAutomateClient =
24+
new AppAutomateClient(credentials.getUsername(), credentials.getDecryptedAccesskey());
25+
if (JenkinsProxySettings.hasProxy()) {
26+
appAutomateClient.setProxy(JenkinsProxySettings.getHost(), JenkinsProxySettings.getPort(), JenkinsProxySettings.getUsername(), JenkinsProxySettings.getPassword());
27+
}
28+
return appAutomateClient.uploadApp(this.appPath).getAppUrl();
29+
}
2530
}

src/main/java/com/browserstack/automate/ci/jenkins/AutomateTestAction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.browserstack.automate.ci.common.analytics.Analytics;
66
import com.browserstack.automate.ci.common.enums.ProjectType;
77
import com.browserstack.automate.ci.common.model.BrowserStackSession;
8+
import com.browserstack.automate.ci.common.proxysettings.JenkinsProxySettings;
89
import com.browserstack.automate.ci.jenkins.BrowserStackBuildWrapper.BuildWrapperItem;
910
import com.browserstack.automate.exception.AppAutomateException;
1011
import com.browserstack.automate.exception.AutomateException;
@@ -112,6 +113,9 @@ private Session getSession(BrowserStackCredentials credentials, ProjectType proj
112113
} else {
113114
client = new AutomateClient(credentials.getUsername(), credentials.getDecryptedAccesskey());
114115
}
116+
if (JenkinsProxySettings.hasProxy()) {
117+
client.setProxy(JenkinsProxySettings.getHost(), JenkinsProxySettings.getPort(), JenkinsProxySettings.getUsername(), JenkinsProxySettings.getPassword());
118+
}
115119
try {
116120
activeSession = client.getSession(this.browserStackSession.getSessionId());
117121
Analytics.trackIframeRequest();

src/main/java/com/browserstack/automate/ci/jenkins/BrowserStackCredentials.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.browserstack.automate.AutomateClient;
44
import com.browserstack.automate.ci.common.analytics.Analytics;
5+
import com.browserstack.automate.ci.common.proxysettings.JenkinsProxySettings;
56
import com.browserstack.automate.exception.AutomateException;
67
import com.cloudbees.plugins.credentials.BaseCredentials;
78
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
@@ -72,6 +73,9 @@ public static FormValidation testAuthentication(final String username, final Str
7273

7374
try {
7475
AutomateClient client = new AutomateClient(username, accesskey);
76+
if (JenkinsProxySettings.hasProxy()) {
77+
client.setProxy(JenkinsProxySettings.getHost(), JenkinsProxySettings.getPort(), JenkinsProxySettings.getUsername(), JenkinsProxySettings.getPassword());
78+
}
7579
if (client.getAccountUsage() != null) {
7680
return FormValidation.ok(OK_VALID_AUTH);
7781
}

src/main/java/com/browserstack/automate/ci/jenkins/BrowserStackReportForBuild.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.browserstack.automate.ci.common.Tools;
66
import com.browserstack.automate.ci.common.constants.Constants;
77
import com.browserstack.automate.ci.common.enums.ProjectType;
8+
import com.browserstack.automate.ci.common.proxysettings.JenkinsProxySettings;
89
import com.browserstack.automate.ci.common.tracking.PluginsTracker;
910
import com.browserstack.automate.exception.BuildNotFound;
1011
import com.browserstack.automate.model.Build;
@@ -86,6 +87,9 @@ private void fetchBuildAndSessions() {
8687
} else {
8788
client = new AutomateClient(credentials.getUsername(), credentials.getDecryptedAccesskey());
8889
}
90+
if (JenkinsProxySettings.hasProxy()) {
91+
client.setProxy(JenkinsProxySettings.getHost(), JenkinsProxySettings.getPort(), JenkinsProxySettings.getUsername(), JenkinsProxySettings.getPassword());
92+
}
8993

9094
browserStackBuild = fetchBrowserStackBuild(client, buildName);
9195

0 commit comments

Comments
 (0)