-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathPluginsTracker.java
More file actions
166 lines (144 loc) · 6.44 KB
/
PluginsTracker.java
File metadata and controls
166 lines (144 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.browserstack.automate.ci.common.tracking;
import com.browserstack.automate.ci.common.Tools;
import com.browserstack.automate.ci.common.constants.Constants;
import com.browserstack.automate.ci.common.proxysettings.JenkinsProxySettings;
import okhttp3.Authenticator;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Credentials;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Route;
import org.json.JSONObject;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.Proxy;
import java.time.Instant;
import java.util.Optional;
public class PluginsTracker {
private static final String URL = "https://api.browserstack.com/ci_plugins/track";
private final String trackingId;
private transient OkHttpClient client;
private String username;
private String accessKey;
private String customProxy;
public PluginsTracker(final String username, final String accessKey, @Nullable final String customProxy) {
this.username = username;
this.accessKey = accessKey;
this.customProxy = customProxy;
this.trackingId = Tools.getUniqueString(true, true);
initializeClient();
}
public PluginsTracker() {
this(null);
}
public PluginsTracker(@Nullable final String customProxy) {
this.username = null;
this.accessKey = null;
this.customProxy = customProxy;
this.trackingId = Tools.getUniqueString(true, true);
initializeClient();
}
private void asyncPostRequestSilent(final String url, final String json) {
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// closing the response body is important, else it will start leaking
if (response != null && response.body() != null) {
response.body().close();
}
}
});
}
private void initializeClient() {
JenkinsProxySettings jenkinsProxy;
if (customProxy != null) {
jenkinsProxy = new JenkinsProxySettings(customProxy, null);
} else {
jenkinsProxy = new JenkinsProxySettings(null);
}
final Proxy proxy = jenkinsProxy.getJenkinsProxy();
if (proxy != Proxy.NO_PROXY) {
if (jenkinsProxy.hasAuth()) {
final String username = jenkinsProxy.getUsername();
final String password = jenkinsProxy.getPassword();
Authenticator proxyAuthenticator = new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
final String credential = Credentials.basic(username, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
};
client = new OkHttpClient.Builder().proxy(proxy).proxyAuthenticator(proxyAuthenticator).build();
} else {
client = new OkHttpClient.Builder().proxy(proxy).build();
}
} else {
client = new OkHttpClient.Builder().build();
}
}
public void trackOperation(String operationType, JSONObject data) {
JSONObject requestData = new JSONObject();
requestData.put("source", Constants.JENKINS_CI_PLUGIN);
requestData.put("product", Constants.AUTOMATE);
requestData.put("team", Constants.AUTOMATE);
requestData.put("data", data);
requestData.put("event_timestamp", Instant.now().getEpochSecond());
requestData.put("track_operation_type", operationType);
requestData.put("tracking_id", trackingId);
Optional.ofNullable(username)
.ifPresent(userName -> requestData.put("username", userName));
Optional.ofNullable(accessKey)
.ifPresent(accessKey -> requestData.put("access_key", accessKey));
asyncPostRequestSilent(URL, requestData.toString());
}
public void sendError(String errorMessage, boolean pipelineStatus, String phase) {
JSONObject trackingData = new JSONObject();
trackingData.put("error", errorMessage);
trackingData.put("pipeline", pipelineStatus);
trackingData.put("phase", phase);
trackOperation(PluginsTrackerEvents.CI_PLUGIN_ERROR, trackingData);
}
public void pluginInitialized(String buildName, boolean localStatus, boolean pipelineStatus) {
JSONObject trackingData = new JSONObject();
trackingData.put("build_name", buildName);
trackingData.put("local", localStatus);
trackingData.put("pipeline", pipelineStatus);
trackOperation(PluginsTrackerEvents.CI_PLUGIN_INITIALIZED, trackingData);
}
public void reportGenerationInitialized(String buildName, String product, boolean pipelineStatus) {
JSONObject trackingData = new JSONObject();
trackingData.put("build_name", buildName);
trackingData.put("product", product);
trackingData.put("pipeline", pipelineStatus);
trackOperation(PluginsTrackerEvents.CI_PLUGIN_REPORT_GENERATION_STARTED, trackingData);
}
public void reportGenerationCompleted(String status, String product, boolean pipelineStatus, String buildName, String buildId) {
JSONObject dataToTrack = new JSONObject();
dataToTrack.put("status", status);
dataToTrack.put("product", product);
dataToTrack.put("pipeline", pipelineStatus);
dataToTrack.put("build_name", buildName);
dataToTrack.put("build_id", buildId);
trackOperation(PluginsTrackerEvents.CI_PLUGIN_REPORT_PUBLISHED, dataToTrack);
}
public void setCredentials(String username, String accessKey) {
this.username = Optional.ofNullable(this.username)
.orElse(username);
this.accessKey = Optional.ofNullable(this.accessKey)
.orElse(accessKey);
}
}