1818import hudson .Launcher ;
1919import jenkins .tasks .SimpleBuildStep ;
2020import org .jenkinsci .Symbol ;
21- import org .json .JSONArray ;
22- import org .json .JSONObject ;
23- import okhttp3 .*;
2421import org .kohsuke .stapler .DataBoundConstructor ;
2522
2623import java .io .IOException ;
2724import java .io .PrintStream ;
28- import java .net .URISyntaxException ;
2925import java .net .URLEncoder ;
3026import java .text .SimpleDateFormat ;
3127import java .util .*;
3228import java .util .concurrent .ConcurrentHashMap ;
33- import java .util .concurrent .TimeUnit ;
3429import java .util .logging .Logger ;
3530
3631import static com .browserstack .automate .ci .common .logger .PluginLogger .log ;
3732import static com .browserstack .automate .ci .common .logger .PluginLogger .logError ;
3833
3934public class BrowserStackTestReportPublisher extends Recorder implements SimpleBuildStep {
4035 private static final Logger LOGGER = Logger .getLogger (BrowserStackTestReportPublisher .class .getName ());
41- private static final int MAX_ATTEMPTS = 3 ;
42- private static final int RETRY_DELAY_SECONDS = 5 ;
4336 private final Map <String , String > customEnvVars ;
4437
45- private RequestsUtil requestsUtil ;
46-
4738 @ DataBoundConstructor
4839 public BrowserStackTestReportPublisher (Map <String , String > customEnvVars ) {
4940 this .customEnvVars = customEnvVars != null && !customEnvVars .isEmpty () ? new ConcurrentHashMap <>(customEnvVars ) : new ConcurrentHashMap <>();
50- this .requestsUtil = new RequestsUtil ();
5141 }
5242
5343 @ Override
@@ -60,7 +50,6 @@ public void perform(Run<?, ?> build, @NonNull FilePath workspace, @NonNull Launc
6050
6151 String browserStackBuildName = Optional .ofNullable (parentEnvs .get (BrowserStackEnvVars .BROWSERSTACK_BUILD_NAME ))
6252 .orElse (parentEnvs .get (Constants .JENKINS_BUILD_TAG ));
63- String projectName = parentEnvs .get (BrowserStackEnvVars .BROWSERSTACK_PROJECT_NAME );
6453
6554 BrowserStackBuildAction buildAction = build .getAction (BrowserStackBuildAction .class );
6655 if (buildAction == null || buildAction .getBrowserStackCredentials () == null ) {
@@ -72,105 +61,20 @@ public void perform(Run<?, ?> build, @NonNull FilePath workspace, @NonNull Launc
7261
7362 LOGGER .info ("Adding BrowserStack Report Action" );
7463
75- String configDetailsEndpoint = Constants .INTEGRATE_BASE_URL + Constants .BROWSERSTACK_CONFIG_DETAILS_ENDPOINT ;
76- JSONObject reportConfigDetailsResponse = fetchConfigDetails (logger , configDetailsEndpoint , credentials );
77- if (reportConfigDetailsResponse == null ) {
78- logError (logger , "Could not fetch the report config details" );
79- return ;
80- }
8164
82- JSONArray configDetails = reportConfigDetailsResponse .getJSONArray ("config_details" );
83- String lookUpURL = reportConfigDetailsResponse .getString ("lookup_endpoint" );
8465 Date buildTimestamp = new Date (build .getStartTimeInMillis ());
8566
8667 // Format the timestamp (e.g., YYYY-MM-DD HH:MM:SS)
8768 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd_HH:mm:ss" );
8869 String formattedTime = sdf .format (buildTimestamp );
8970
9071 // Encode the timestamp to make it URL-safe
91- String encodedTimestamp = URLEncoder .encode (formattedTime , "UTF-8" );
92-
93- String UUID = fetchBuildUUID (logger , lookUpURL , credentials , browserStackBuildName , projectName , encodedTimestamp );
94- if (UUID == null ) {
95- logError (logger , "Cannot find a build with name " + browserStackBuildName );
96- }
72+ String buildCreatedAt = URLEncoder .encode (formattedTime , "UTF-8" );
9773
98- for (int i = 0 ; i < configDetails .length (); i ++) {
99- JSONObject config = configDetails .getJSONObject (i );
100-
101- String reportUrl = config .getString ("report_fetch_url" );
102- String reportName = config .getString ("report_name" );
103- String reportTabUrl = config .getString ("report_tab_url" );
104- build .addAction (new BrowserStackTestReportAction (build , credentials , reportUrl , UUID , reportName , reportTabUrl , logger ));
105- }
74+ build .addAction (new BrowserStackTestReportAction (build , credentials , browserStackBuildName ,buildCreatedAt , logger ));
10675
10776 }
10877
109- private String fetchBuildUUID (PrintStream logger , String lookUpURL , BrowserStackCredentials credentials , String buildName , String projectName , String encodedTimestamp ) throws InterruptedException {
110-
111- Map <String , String > params = new HashMap <>();
112- params .put ("build_name" , buildName );
113- params .put ("pipeline_timestamp" , encodedTimestamp );
114- params .put ("tool" , Constants .INTEGRATIONS_TOOL_KEY );
115- if (projectName != null ) {
116- params .put ("project_name" , projectName );
117- }
118-
119- log (logger , "Fetching build...." );
120- String lookUpURLWithParams ;
121-
122- //constructing build params
123- try {
124- lookUpURLWithParams = requestsUtil .buildQueryParams (lookUpURL , params );
125- } catch (URISyntaxException uriSyntaxException ) {
126- logError (logger , "Could not build look up url with params" + Arrays .toString (uriSyntaxException .getStackTrace ()));
127- return null ;
128- }
129-
130- //making attempts
131- for (int attempts = 0 ; attempts < MAX_ATTEMPTS ; attempts ++) {
132- try {
133- Response response = requestsUtil .makeRequest (lookUpURLWithParams , credentials );
134- if (response .isSuccessful ()) {
135- assert response .body () != null ;
136- JSONObject lookUpResponse = new JSONObject (response .body ().string ());
137- String UUID = lookUpResponse .optString ("UUID" , null );
138-
139- if (UUID != null ) {
140- log (logger , "build found with " + buildName + " and project name " + projectName );
141- return UUID ;
142- }
143-
144- LOGGER .info ("build not found will retry in sometime.." + lookUpResponse .getString ("message" ));
145- }
146- } catch (Exception e ) {
147- logError (logger , "Attempt " + (attempts + 1 ) + " failed: " + Arrays .toString (e .getStackTrace ()));
148- }
149- TimeUnit .SECONDS .sleep (RETRY_DELAY_SECONDS );
150- }
151-
152- LOGGER .info ("build not found even after " + MAX_ATTEMPTS + "attempts" );
153- return null ;
154- }
155-
156- private JSONObject fetchConfigDetails (PrintStream logger , String configDetailsURL , BrowserStackCredentials credentials ) {
157- log (logger , "Fetching config details for reports" );
158- Map <String , String > params = new HashMap <>();
159- params .put ("tool" , Constants .INTEGRATIONS_TOOL_KEY );
160- params .put ("operation" , Constants .REPORT_CONFIG_OPERATION_NAME );
161- try {
162- String configDetailsURLWithParams = requestsUtil .buildQueryParams (configDetailsURL , params );
163- Response response = requestsUtil .makeRequest (configDetailsURLWithParams , credentials );
164- if (response .isSuccessful ()) {
165- assert response .body () != null ;
166- return new JSONObject (response .body ().string ());
167- }
168- logError (logger , "Failed to fetch config details: " + response .code ());
169- } catch (Exception e ) {
170- logError (logger , "Exception occurred while fetching config details: " + Arrays .toString (e .getStackTrace ()));
171- }
172- return null ;
173- }
17478
17579 public Map <String , String > getCustomEnvVars () {
17680 return customEnvVars ;
0 commit comments