Skip to content

Commit d0aa897

Browse files
committed
fix: proxy issues. update: automate-client-java version
1. PluginsTracker static to instance specific client to prevent overriding 2. Credentials check via proxy 3. pom update for automate-client-java for optional proxy-auth params
1 parent 77070c9 commit d0aa897

File tree

8 files changed

+40
-17
lines changed

8 files changed

+40
-17
lines changed

src/main/java/com/browserstack/automate/ci/common/clienthandler/ClientHandler.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212

1313
public class ClientHandler {
1414

15+
/**
16+
* Returns BrowserStackClient based on Project, i.e Automate or App Automate.
17+
* Also decides and sets the proxy for the client
18+
* @param project ProjectType
19+
* @param username Username of BrowserStack
20+
* @param accessKey Access Key of BrowserStack
21+
* @param customProxy Custom Proxy String
22+
* @param logger Logger
23+
* @return BrowserStackClient
24+
*/
1525
public static BrowserStackClient getBrowserStackClient(@Nonnull final ProjectType project, @Nonnull final String username,
1626
@Nonnull final String accessKey, @Nullable final String customProxy,
1727
@Nullable final PrintStream logger) {
@@ -31,6 +41,13 @@ public static BrowserStackClient getBrowserStackClient(@Nonnull final ProjectTyp
3141
return client;
3242
}
3343

44+
/**
45+
* Initializes BrowserStack client based on project type
46+
* @param project ProjectType
47+
* @param username Username of BrowserStack
48+
* @param accessKey Access Key of BrowserStack
49+
* @return BrowserStackClient
50+
*/
3451
private static BrowserStackClient decideAndGetClient(@Nonnull final ProjectType project, @Nonnull final String username, @Nonnull final String accessKey) {
3552
if (project == ProjectType.APP_AUTOMATE) {
3653
return new AppAutomateClient(username, accessKey);

src/main/java/com/browserstack/automate/ci/common/proxysettings/JenkinsProxySettings.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ private void decideJenkinsProxy(final String customProxyString) {
152152
if (this.finalProxyHost != null && this.finalProxyPort != 0) {
153153
this.hasProxy = true;
154154

155-
String proxyDataToLog = "\nHost: " + this.getHost() + "\nPort: " + this.getPort();
155+
String proxyDataToLog = "Host: " + this.getHost() + ", Port: " + this.getPort();
156156
if (this.hasAuth()) {
157-
proxyDataToLog += "\nUsername: " + this.getUsername() + "\nPassword: " + Tools.maskString(this.getPassword());
157+
proxyDataToLog += ", Username: " + this.getUsername() + ", Password: " + Tools.maskString(this.getPassword());
158158
}
159159

160160
if (logger != null) log(logger, "Proxy Selected for BrowserStack Plugin: " + proxyDataToLog);
@@ -178,6 +178,10 @@ private String getSystemProxyString() {
178178
return systemHttpsProxyEnv == null ? systemHttpProxyEnv : systemHttpsProxyEnv;
179179
}
180180

181+
/**
182+
* Returns Jenkins proxy configuration in Proxy object
183+
* @return Proxy object
184+
*/
181185
public Proxy getJenkinsProxy() {
182186
if (this.hasProxy()) {
183187
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.finalProxyHost, this.finalProxyPort));

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class PluginsTracker {
2626
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
2727
private static final String URL = "https://api.browserstack.com/ci_plugins/track";
28-
private static OkHttpClient client;
28+
private transient OkHttpClient client;
2929
private final String trackingId;
3030
private String username;
3131
private String accessKey;
@@ -51,7 +51,7 @@ public PluginsTracker(@Nullable final String customProxy) {
5151
initializeClient();
5252
}
5353

54-
private static void asyncPostRequestSilent(final String url, final String json) {
54+
private void asyncPostRequestSilent(final String url, final String json) {
5555
RequestBody body = RequestBody.create(JSON, json);
5656
Request request = new Request.Builder()
5757
.url(url)
@@ -86,7 +86,7 @@ private void initializeClient() {
8686

8787
final Proxy proxy = jenkinsProxy.getJenkinsProxy();
8888
if (proxy != Proxy.NO_PROXY) {
89-
System.out.println("Selected some proxy for plugins tracker. " + proxy.toString());
89+
System.out.println("Selected some proxy for plugins tracker. " + proxy.toString() + ". And auth: " + jenkinsProxy.hasAuth());
9090
if (jenkinsProxy.hasAuth()) {
9191
final String username = jenkinsProxy.getUsername();
9292
final String password = jenkinsProxy.getPassword();
@@ -99,12 +99,12 @@ public Request authenticate(Route route, Response response) throws IOException {
9999
.build();
100100
}
101101
};
102-
this.client = new OkHttpClient.Builder().proxy(proxy).proxyAuthenticator(proxyAuthenticator).build();
102+
client = new OkHttpClient.Builder().proxy(proxy).proxyAuthenticator(proxyAuthenticator).build();
103103
} else {
104-
this.client = new OkHttpClient.Builder().proxy(proxy).build();
104+
client = new OkHttpClient.Builder().proxy(proxy).build();
105105
}
106106
} else {
107-
this.client = new OkHttpClient.Builder().build();
107+
client = new OkHttpClient.Builder().build();
108108
}
109109
}
110110

src/main/java/com/browserstack/automate/ci/common/uploader/AppUploader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AppUploader {
1313

1414
String appPath;
1515
BrowserStackCredentials credentials;
16-
private final PrintStream logger;
16+
private final transient PrintStream logger;
1717
private final String customProxy;
1818

1919
public AppUploader(String appPath, BrowserStackCredentials credentials, final String customProxy, final PrintStream logger) {
@@ -36,6 +36,8 @@ public String uploadFile()
3636
}
3737

3838
if (proxy.hasProxy()) {
39+
System.out.println("App upload setting proxy for app automate client...");
40+
System.out.println(proxy.getHost() + ":" + proxy.getPort() + "," + proxy.getUsername() + ":" + proxy.getPassword());
3941
appAutomateClient.setProxy(proxy.getHost(), proxy.getPort(), proxy.getUsername(), proxy.getPassword());
4042
}
4143
return appAutomateClient.uploadApp(this.appPath).getAppUrl();

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
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;
5+
import com.browserstack.automate.ci.common.clienthandler.ClientHandler;
6+
import com.browserstack.automate.ci.common.enums.ProjectType;
67
import com.browserstack.automate.exception.AutomateException;
78
import com.cloudbees.plugins.credentials.BaseCredentials;
89
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
@@ -72,11 +73,8 @@ public static FormValidation testAuthentication(final String username, final Str
7273
}
7374

7475
try {
75-
AutomateClient client = new AutomateClient(username, accesskey);
76-
System.out.println("Inside BrowserStackCredentials....");
77-
// if (JenkinsProxySettings.hasProxy()) {
78-
// client.setProxy(JenkinsProxySettings.getHost(), JenkinsProxySettings.getPort(), JenkinsProxySettings.getUsername(), JenkinsProxySettings.getPassword());
79-
// }
76+
AutomateClient client =
77+
(AutomateClient) ClientHandler.getBrowserStackClient(ProjectType.AUTOMATE, username, accesskey, null, null);
8078
if (client.getAccountUsage() != null) {
8179
return FormValidation.ok(OK_VALID_AUTH);
8280
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class BrowserStackReportForBuild extends AbstractBrowserStackReportForBui
3737
private final ProjectType projectType;
3838
private final transient PrintStream logger;
3939
private final String customProxy;
40-
private final PluginsTracker tracker;
40+
private final transient PluginsTracker tracker;
4141
private final boolean pipelineStatus;
4242
// to make them available in jelly
4343
private final String errorConst = Constants.SessionStatus.ERROR;
@@ -61,6 +61,7 @@ public BrowserStackReportForBuild(final Run<?, ?> build,
6161
this.projectType = projectType;
6262
this.logger = logger;
6363
this.customProxy = customProxy;
64+
System.out.println("Initialized BrowserStackReportForBuild with customProxy: " + this.customProxy);
6465
this.tracker = tracker;
6566
this.pipelineStatus = pipelineStatus;
6667
fetchBuildAndSessions();

src/main/java/com/browserstack/automate/ci/jenkins/pipeline/AppUploadStepExecution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected Void run() throws Exception {
4040
String customProxy = parentContextEnvVars.get("https_proxy");
4141
customProxy = Optional.ofNullable(customProxy).orElse(parentContextEnvVars.get("http_proxy"));
4242

43+
System.out.println("App upload custom proxy: " + customProxy);
4344
String appId = AppUploaderHelper.uploadApp(run, logger, this.appPath, customProxy);
4445

4546
if (StringUtils.isEmpty(appId)) {

src/main/java/com/browserstack/automate/ci/jenkins/pipeline/BrowserStackPipelineStepExecution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public boolean start() throws Exception {
5555

5656
final PluginsTracker tracker = new PluginsTracker(customProxy);
5757

58-
System.out.println("\ncustomProxy: " + customProxy);
58+
System.out.println("\ncustomProxy in pipeline execution: " + customProxy);
5959

6060
BrowserStackCredentials credentials =
6161
BrowserStackCredentials.getCredentials(run.getParent(), credentialsId);

0 commit comments

Comments
 (0)