1818
1919public class BrowserStackTestReportAction implements Action {
2020
21- private Run <?, ?> run ;
22- private BrowserStackCredentials credentials ;
23- private String reportHtml ;
24-
25- private String buildName ;
26- private String buildCreatedAt ;
27- private final transient PrintStream logger ;
28- private String reportStyle ;
29- public String reportName ;
30- public String urlName ;
31-
32- private int maxRetryReportAttempt ;
33-
3421 private static final String DEFAULT_REPORT_TIMEOUT = "120" ;
22+ private static final String SUCCESS_REPORT = "SUCCESS_REPORT" ;
3523 private static final String REPORT_IN_PROGRESS = "REPORT_IN_PROGRESS" ;
3624 private static final String REPORT_FAILED = "REPORT_FAILED" ;
37-
3825 private static final String RETRY_REPORT = "RETRY_REPORT" ;
39-
4026 private static final String RATE_LIMIT = "RATE_LIMIT" ;
41-
27+ private static final String TEST_AVAILABLE = "TEST_AVAILABLE" ;
4228 private static final int MAX_ATTEMPTS = 3 ;
43- private RequestsUtil requestsUtil ;
4429
45- public BrowserStackTestReportAction (Run <?, ?> run , BrowserStackCredentials credentials , String buildName , String builldCreatedAt , final PrintStream logger ) {
46- super ();
47- setBuild (run );
30+ private final transient PrintStream logger ;
31+ private final RequestsUtil requestsUtil ;
32+ private final BrowserStackCredentials credentials ;
33+ private final String buildName ;
34+ private final String buildCreatedAt ;
35+
36+ private Run <?, ?> run ;
37+ private String reportHtml ;
38+ private String reportStyle ;
39+ private String reportStatus ;
40+ private int maxRetryReportAttempt ;
41+
42+ public BrowserStackTestReportAction (Run <?, ?> run , BrowserStackCredentials credentials , String buildName , String buildCreatedAt , final PrintStream logger ) {
43+ this .run = run ;
4844 this .credentials = credentials ;
4945 this .buildName = buildName ;
50- this .buildCreatedAt = builldCreatedAt ;
51- this .reportHtml = null ;
52- this .reportStyle = "" ;
53- maxRetryReportAttempt = MAX_ATTEMPTS ;
46+ this .buildCreatedAt = buildCreatedAt ;
5447 this .logger = logger ;
55-
5648 this .requestsUtil = new RequestsUtil ();
49+ this .reportHtml = null ;
50+ this .reportStyle = "" ;
51+ this .reportStatus = "" ;
52+ this .maxRetryReportAttempt = MAX_ATTEMPTS ;
5753 }
5854
59-
6055 public String getReportHtml () {
61- fetchReportConditions ();
56+ ensureReportFetched ();
6257 return reportHtml ;
6358 }
6459
6560 public String getReportStyle () {
66- fetchReportConditions ();
61+ ensureReportFetched ();
6762 return reportStyle ;
6863 }
6964
70- private void fetchReportConditions () {
71- if (reportHtml == null || reportHtml . equals ( REPORT_IN_PROGRESS ) || reportHtml . equals ( RETRY_REPORT ) || reportHtml . equals ( RATE_LIMIT )) {
65+ private void ensureReportFetched () {
66+ if (! isReportCompletedOrFailed ( )) {
7267 fetchReport ();
7368 }
7469 }
7570
71+ private boolean isReportCompletedOrFailed () {
72+ return reportStatus .equals (SUCCESS_REPORT ) || reportStatus .equals (REPORT_FAILED );
73+ }
74+
7675 private void fetchReport () {
76+ Map <String , String > params = createReportParams ();
77+ String reportUrl = Constants .CAD_BASE_URL + Constants .BROWSERSTACK_CONFIG_DETAILS_ENDPOINT ;
78+
79+ try {
80+ log (logger , "Fetching BrowserStack report..." );
81+ Response response = requestsUtil .makeRequest (reportUrl , credentials , createRequestBody (params ));
82+ handleResponse (response );
83+ } catch (Exception e ) {
84+ handleFetchException (e );
85+ }
86+ }
87+
88+ private Map <String , String > createReportParams () {
7789 Map <String , String > params = new HashMap <>();
78- String pollValue = "FIRST" ;
7990 params .put ("buildCreatedAt" , buildCreatedAt );
8091 params .put ("buildName" , buildName );
8192 params .put ("requestingCi" , Constants .INTEGRATIONS_TOOL_KEY );
8293 params .put ("reportFormat" , Arrays .asList (Constants .REPORT_FORMAT ).toString ());
83- params .put ("requestType" , pollValue );
94+ params .put ("requestType" , "POLL" );
8495 params .put ("userTimeout" , DEFAULT_REPORT_TIMEOUT );
96+ return params ;
97+ }
8598
86- try {
87- String reportUrl = Constants .CAD_BASE_URL + Constants .BROWSERSTACK_CONFIG_DETAILS_ENDPOINT ;
88- Gson gson = new Gson ();
89- String json = gson .toJson (params );
90- RequestBody ciReportBody = RequestBody .create (MediaType .parse ("application/json" ), json );
91- log (logger , "Fetching browserstack report " + reportName );
92- Response response = requestsUtil .makeRequest (reportUrl , credentials , ciReportBody );
93- if (response .isSuccessful ()) {
94- assert response .body () != null ;
95- JSONObject reportResponse = new JSONObject (response .body ().string ());
96- String reportStatus = reportResponse .optString ("reportStatus" );
97- if (reportStatus .equalsIgnoreCase (String .valueOf (BrowserStackReportStatus .COMPLETED )) ||
98- reportStatus .equalsIgnoreCase (String .valueOf (BrowserStackReportStatus .TEST_AVAILABLE )) ||
99- reportStatus .equalsIgnoreCase (String .valueOf (BrowserStackReportStatus .NOT_AVAILABLE ))) {
100-
101- String defaultHTML = "<h1>No Report Found</h1>" ;
102- JSONObject report = reportResponse .optJSONObject ("report" );
103- reportHtml = report != null ? report .optString ("richHtml" , defaultHTML ) : defaultHTML ;
104- reportStyle = report != null ? report .optString ("richCss" , "" ) : "" ;
105-
106- } else if (reportStatus .equalsIgnoreCase (String .valueOf (BrowserStackReportStatus .IN_PROGRESS ))) {
107-
108- reportHtml = REPORT_IN_PROGRESS ;
109-
110- } else {
111- reportHtml = REPORT_FAILED ;
112- }
113- } else if (response .code () == 429 ) {
114- reportHtml = RATE_LIMIT ;
115- } else {
116- reportHtml = REPORT_FAILED ;
117- logError (logger , "Received Non success response while fetching report" + response .code ());
118- }
119- } catch (Exception e ) {
120- reportHtml = RETRY_REPORT ;
121- this .maxRetryReportAttempt --;
122- if (this .maxRetryReportAttempt < 0 ) {
123- reportHtml = REPORT_FAILED ;
124- }
125- logError (logger , "Exception while fetching the report" + Arrays .toString (e .getStackTrace ()));
99+ private RequestBody createRequestBody (Map <String , String > params ) {
100+ Gson gson = new Gson ();
101+ String json = gson .toJson (params );
102+ return RequestBody .create (MediaType .parse ("application/json" ), json );
103+ }
104+
105+ private void handleResponse (Response response ) throws Exception {
106+ if (response .isSuccessful ()) {
107+ processSuccessfulResponse (response );
108+ } else if (response .code () == 429 ) {
109+ reportStatus = RATE_LIMIT ;
110+ } else {
111+ reportStatus = REPORT_FAILED ;
112+ logError (logger , "Non-success response while fetching report: " + response .code ());
113+ }
114+ }
115+
116+ private void processSuccessfulResponse (Response response ) throws Exception {
117+ assert response .body () != null ;
118+ JSONObject reportResponse = new JSONObject (response .body ().string ());
119+ String responseReportStatus = reportResponse .optString ("reportStatus" );
120+ JSONObject report = reportResponse .optJSONObject ("report" );
121+
122+ switch (responseReportStatus .toUpperCase ()) {
123+ case "COMPLETED" :
124+ case "NOT_AVAILABLE" :
125+ setReportSuccess (report );
126+ break ;
127+ case "IN_PROGRESS" :
128+ reportStatus = REPORT_IN_PROGRESS ;
129+ break ;
130+ case "TEST_AVAILABLE" :
131+ setReportSuccess (report );
132+ reportStatus = TEST_AVAILABLE ;
133+ break ;
134+ default :
135+ reportStatus = REPORT_FAILED ;
136+ }
137+ }
138+
139+ private void setReportSuccess (JSONObject report ) {
140+ String defaultHTML = "<h1>No Report Found</h1>" ;
141+ reportStatus = SUCCESS_REPORT ;
142+ reportHtml = report != null ? report .optString ("richHtml" , defaultHTML ) : defaultHTML ;
143+ reportStyle = report != null ? report .optString ("richCss" , "" ) : "" ;
144+ }
145+
146+ private void handleFetchException (Exception e ) {
147+ reportStatus = RETRY_REPORT ;
148+ maxRetryReportAttempt --;
149+ if (maxRetryReportAttempt < 0 ) {
150+ reportStatus = REPORT_FAILED ;
126151 }
152+ logError (logger , "Exception while fetching the report: " + Arrays .toString (e .getStackTrace ()));
127153 }
128154
129155 public boolean isReportInProgress () {
130- return reportHtml .equals (REPORT_IN_PROGRESS );
156+ return reportStatus .equals (REPORT_IN_PROGRESS );
131157 }
132158
133159 public boolean isReportFailed () {
134- return reportHtml .equals (REPORT_FAILED );
160+ return reportStatus .equals (REPORT_FAILED );
135161 }
136162
137163 public boolean reportRetryRequired () {
138- return reportHtml .equals (RETRY_REPORT );
164+ return reportStatus .equals (RETRY_REPORT );
139165 }
140166
141- public boolean isUserRateLimited () { return reportHtml .equals (RATE_LIMIT ); }
167+ public boolean isUserRateLimited () {
168+ return reportStatus .equals (RATE_LIMIT );
169+ }
142170
143171 public boolean isReportAvailable () {
144- if (reportHtml != null && !reportHtml .equals (REPORT_IN_PROGRESS ) && !reportHtml .equals (REPORT_FAILED ) && !reportHtml .equals (RETRY_REPORT )) {
145- return true ;
146- }
147- return false ;
172+ return reportStatus .equals (SUCCESS_REPORT ) || reportStatus .equals (TEST_AVAILABLE );
148173 }
149174
150175 public boolean reportHasStatus () {
151- if (reportHtml == null ) return false ;
152- return reportHtml .equals (REPORT_IN_PROGRESS ) || reportHtml .equals (REPORT_FAILED );
176+ return !reportStatus .isEmpty () && (reportStatus .equals (REPORT_IN_PROGRESS ) || reportStatus .equals (REPORT_FAILED ));
153177 }
154178
155179 public Run <?, ?> getBuild () {
@@ -174,5 +198,4 @@ public String getDisplayName() {
174198 public String getUrlName () {
175199 return Constants .BROWSERSTACK_TEST_REPORT_URL ;
176200 }
177-
178- }
201+ }
0 commit comments