Skip to content

Commit 5056c1a

Browse files
committed
Adding Caching for QD Enabled and check for Creds
1 parent b1d7a80 commit 5056c1a

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed

src/main/java/com/browserstack/automate/ci/common/constants/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public static final class SessionStatus {
6565
public static final class QualityDashboardAPI {
6666
public static final String QEI_DEFAULT_URL = "https://quality-engineering-insights.browserstack.com";
6767
public static String host = QEI_DEFAULT_URL;
68+
// Cache configuration
69+
public static final long CACHE_DURATION_MS = 60 * 60 * 1000L; // 1 hour in milliseconds
6870

6971
public static String getHost() {
7072
return host;

src/main/java/com/browserstack/automate/ci/jenkins/qualityDashboard/QualityDashboardInit.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
import java.net.HttpURLConnection;
2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import java.util.logging.Logger;
2526

2627
@Extension
2728
public class QualityDashboardInit {
2829

30+
private static final Logger LOGGER = Logger.getLogger(QualityDashboardInit.class.getName());
2931
static QualityDashboardAPIUtil apiUtil = new QualityDashboardAPIUtil();
3032

31-
@Initializer(after = InitMilestone.PLUGINS_PREPARED)
33+
@Initializer(after = InitMilestone.JOB_LOADED)
3234
public static void postInstall() {
3335
try {
3436
initQDSetupIfRequired();
@@ -52,9 +54,11 @@ private static String exceptionToString(Throwable throwable) {
5254
private static void initQDSetupIfRequired() throws JsonProcessingException {
5355
BrowserStackCredentials browserStackCredentials = QualityDashboardUtil.getBrowserStackCreds();
5456
try {
55-
if(browserStackCredentials!=null) {
57+
if(browserStackCredentials != null) {
5658
apiUtil.logToQD(browserStackCredentials,"Starting plugin data export to QD");
5759
checkQDIntegrationAndDumpMetaData(browserStackCredentials);
60+
} else {
61+
LOGGER.info("BrowserStack credentials not found. Skipping Quality Dashboard initialization.");
5862
}
5963
} catch (Exception e) {
6064
try {

src/main/java/com/browserstack/automate/ci/jenkins/qualityDashboard/QualityDashboardInitItemListener.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,22 @@
1212
import okhttp3.RequestBody;
1313
import okhttp3.Response;
1414
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
15-
import java.io.IOException;
1615
import java.io.Serializable;
16+
import java.util.logging.Logger;
1717

1818
@Extension
1919
public class QualityDashboardInitItemListener extends ItemListener {
2020

21+
private static final Logger LOGGER = Logger.getLogger(QualityDashboardInitItemListener.class.getName());
22+
2123
@Override
2224
public void onCreated(Item job) {
2325
try {
2426
BrowserStackCredentials browserStackCredentials = QualityDashboardUtil.getBrowserStackCreds();
27+
if(browserStackCredentials == null) {
28+
LOGGER.info("BrowserStackCredentials not found. Please ensure they are configured correctly.");
29+
return;
30+
}
2531
QualityDashboardAPIUtil apiUtil = new QualityDashboardAPIUtil();
2632
apiUtil.logToQD(browserStackCredentials, "Item Created : " + job.getClass().getName()) ;
2733

@@ -35,7 +41,8 @@ public void onCreated(Item job) {
3541
String jsonBody = getJsonReqBody(new ItemUpdate(itemName, itemType));
3642
syncItemListToQD(jsonBody, Constants.QualityDashboardAPI.getItemCrudEndpoint(), "POST");
3743

38-
} catch(IOException e) {
44+
} catch(Exception e) {
45+
LOGGER.info("Error syncing item creation to Quality Dashboard: " + e.getMessage());
3946
e.printStackTrace();
4047
}
4148
}
@@ -52,7 +59,8 @@ public void onDeleted(Item job) {
5259
try {
5360
String jsonBody = getJsonReqBody(new ItemUpdate(itemName, itemType));
5461
syncItemListToQD(jsonBody, Constants.QualityDashboardAPI.getItemCrudEndpoint(), "DELETE");
55-
} catch(IOException e) {
62+
} catch(Exception e) {
63+
LOGGER.info("Error syncing item deletion to Quality Dashboard: " + e.getMessage());
5664
e.printStackTrace();
5765
}
5866
}
@@ -67,7 +75,8 @@ public void onRenamed(Item job, String oldName, String newName) {
6775
newName = job.getParent().getFullName() + "/" + newName;
6876
String jsonBody = getJsonReqBody(new ItemRename(oldName, newName, itemType));
6977
syncItemListToQD(jsonBody, Constants.QualityDashboardAPI.getItemCrudEndpoint(), "PUT");
70-
} catch(IOException e) {
78+
} catch(Exception e) {
79+
LOGGER.info("Error syncing item rename to Quality Dashboard: " + e.getMessage());
7180
e.printStackTrace();
7281
}
7382
}
@@ -93,6 +102,10 @@ private Response syncItemListToQD(String jsonBody, String url, String typeOfRequ
93102
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonBody);
94103
QualityDashboardAPIUtil apiUtil = new QualityDashboardAPIUtil();
95104
BrowserStackCredentials browserStackCredentials = QualityDashboardUtil.getBrowserStackCreds();
105+
if(browserStackCredentials == null) {
106+
LOGGER.info("BrowserStack credentials not found. Please ensure they are configured correctly.");
107+
return null;
108+
}
96109
if(typeOfRequest.equals("PUT")) {
97110
apiUtil.logToQD(browserStackCredentials, "Syncing Item Update - PUT");
98111
return apiUtil.makePutRequestToQd(url, browserStackCredentials, requestBody);

src/main/java/com/browserstack/automate/ci/jenkins/qualityDashboard/QualityDashboardPipelineTracker.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@
2222
import java.nio.file.Path;
2323
import java.nio.file.Paths;
2424
import java.sql.Timestamp;
25+
import java.util.logging.Logger;
2526

2627
@Extension
2728
public class QualityDashboardPipelineTracker extends RunListener<Run<?, ?>> {
2829

30+
private static final Logger LOGGER = Logger.getLogger(QualityDashboardPipelineTracker.class.getName());
2931
QualityDashboardAPIUtil apiUtil = new QualityDashboardAPIUtil();
3032

3133
@Override
3234
public void onCompleted(Run<?, ?> run, TaskListener listener) {
3335
super.onCompleted(run, listener);
3436
BrowserStackCredentials browserStackCredentials = QualityDashboardUtil.getBrowserStackCreds();
35-
if(browserStackCredentials!=null) {
36-
String jobName = getJobNameFromRun(run);
37+
if(browserStackCredentials != null) {
38+
String jobName = getJobNameFromRun(run, browserStackCredentials);
3739
int buildNumber = run.getNumber();
3840
try {
3941
if(isQDEnabled(browserStackCredentials) && isPipelineEnabledForQD(browserStackCredentials, jobName)) {
@@ -53,6 +55,9 @@ public void onCompleted(Run<?, ?> run, TaskListener listener) {
5355
throw new RuntimeException(e);
5456
}
5557
}
58+
else {
59+
LOGGER.info("BrowserStack credentials not found. Please ensure they are configured correctly.");
60+
}
5661
}
5762

5863

@@ -186,16 +191,35 @@ private String getUrlForPipeline(Run<?, ?> build) {
186191
}
187192

188193
private boolean isQDEnabled(BrowserStackCredentials browserStackCredentials) throws IOException {
194+
// Check if we have a valid cached value
195+
Boolean cachedResult = QDEnabledCache.getCachedValue();
196+
if (cachedResult != null) {
197+
LOGGER.info("Using cached QD enabled status: " + cachedResult);
198+
return cachedResult;
199+
}
200+
201+
// Cache expired or not set, make API call
202+
LOGGER.info("QD enabled status cache expired or not set, making API call");
189203
Response response = apiUtil.makeGetRequestToQd(Constants.QualityDashboardAPI.getIsQdEnabledEndpoint(), browserStackCredentials);
190-
if (response != null && response.code() == HttpURLConnection.HTTP_OK) {
204+
boolean isEnabled = false;
205+
206+
if (response != null && response.code() == HttpURLConnection.HTTP_OK) {
191207
ResponseBody responseBody = response.body();
192-
if(responseBody != null && Boolean.parseBoolean(response.body().string())) {
208+
if (responseBody != null && Boolean.parseBoolean(response.body().string())) {
209+
isEnabled = true;
193210
apiUtil.logToQD(browserStackCredentials, "QD enabled check passed");
194-
return true;
195211
}
196212
}
197-
apiUtil.logToQD(browserStackCredentials, "QD enabled check failed");
198-
return false;
213+
214+
if (!isEnabled) {
215+
apiUtil.logToQD(browserStackCredentials, "QD enabled check failed");
216+
}
217+
218+
// Cache the result for 1 hour
219+
QDEnabledCache.setCachedValue(isEnabled);
220+
LOGGER.info("Cached QD enabled status: " + isEnabled + " for 1 hour");
221+
222+
return isEnabled;
199223
}
200224

201225
private boolean isPipelineEnabledForQD(BrowserStackCredentials browserStackCredentials, String pipelineName) throws IOException {
@@ -302,7 +326,7 @@ private void copyDirectoryToParentIfRequired(Run<?, ?> run, String finalParentPa
302326
}
303327
}
304328

305-
private String getJobNameFromRun(Run<?, ?> run){
329+
private String getJobNameFromRun(Run<?, ?> run, BrowserStackCredentials browserStackCredentials) {
306330
try {
307331
// Check if parent is a Job (covers all job types)
308332
if (run.getParent() instanceof Job) {
@@ -316,13 +340,29 @@ private String getJobNameFromRun(Run<?, ?> run){
316340
}
317341
} catch (Exception e) {
318342
try {
319-
apiUtil.logToQD(QualityDashboardUtil.getBrowserStackCreds(), "Error getting job name from run: " + e.getMessage());
343+
apiUtil.logToQD(browserStackCredentials, "Error getting job name from run: " + e.getMessage());
320344
} catch (JsonProcessingException jsonEx) {
321345
jsonEx.printStackTrace();
322346
}
323347
}
324348
return null;
325349
}
350+
private static class QDEnabledCache {
351+
private static volatile Boolean qdEnabled = null;
352+
private static volatile long expiryTime = 0L;
353+
354+
public static Boolean getCachedValue() {
355+
if (qdEnabled != null && System.currentTimeMillis() < expiryTime) {
356+
return qdEnabled;
357+
}
358+
return null; // Cache expired or not set
359+
}
360+
361+
public static void setCachedValue(boolean value) {
362+
qdEnabled = value;
363+
expiryTime = System.currentTimeMillis() + Constants.QualityDashboardAPI.CACHE_DURATION_MS;
364+
}
365+
}
326366
}
327367

328368
class QualityDashboardGetDetailsForPipeline implements Serializable {

0 commit comments

Comments
 (0)