2222import java .nio .file .Path ;
2323import java .nio .file .Paths ;
2424import java .sql .Timestamp ;
25+ import java .util .logging .Logger ;
2526
2627@ Extension
2728public 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
328368class QualityDashboardGetDetailsForPipeline implements Serializable {
0 commit comments