Skip to content

Commit ee5802c

Browse files
committed
revamp based on new architecture
1 parent aa4225f commit ee5802c

File tree

5 files changed

+40
-124
lines changed

5 files changed

+40
-124
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public class Constants {
1515
public static final String BROWSERSTACK_REPORT_PATH_PATTERN = "**/browserstack-artifacts/*";
1616
public static final String JENKINS_CI_PLUGIN = "JenkinsCiPlugin";
1717

18-
public static final String INTEGRATE_BASE_URL = "https://integrate.browserstack.com/api";
19-
public static final String BROWSERSTACK_CONFIG_DETAILS_ENDPOINT = "/ci-tools/v1/events/report-config";
18+
public static final String CAD_BASE_URL = "https://api-observability.browserstack.com//api";
19+
public static final String BROWSERSTACK_CONFIG_DETAILS_ENDPOINT = "/v1/builds/buildReport";
2020

2121
public static final String BROWSERSTACK_TEST_REPORT_DISPLAY_NAME = "BrowserStack Build Test Reports";
2222
public static final String INTEGRATIONS_TOOL_KEY = "jenkins";
23-
public static final String REPORT_CONFIG_OPERATION_NAME = "report-config-details";
23+
public static final String REPORT_FORMAT = "richHtml";
2424

2525
// Product
2626
public static final String AUTOMATE = "automate";

src/main/java/com/browserstack/automate/ci/jenkins/integrationService/BrowserStackReportStatus.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
public enum BrowserStackReportStatus {
44
IN_PROGRESS,
55
COMPLETED,
6-
FAILED
6+
TEST_AVAILABLE,
7+
NOT_AVAILABLE,
8+
BUILD_NOT_FOUND,
9+
MULTIPLE_BUILD_FOUND
710
}

src/main/java/com/browserstack/automate/ci/jenkins/integrationService/BrowserStackTestReportAction.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.json.JSONObject;
88
import okhttp3.*;
99

10+
import javax.xml.bind.annotation.XmlType;
1011
import java.io.PrintStream;
1112
import java.util.Arrays;
1213
import java.util.HashMap;
@@ -19,15 +20,18 @@ public class BrowserStackTestReportAction implements Action {
1920

2021
private Run<?, ?> run;
2122
private BrowserStackCredentials credentials;
22-
private String reportUrl;
23-
private final String UUID;
2423
private String reportHtml;
24+
25+
private String buildName;
26+
private String buildCreatedAt;
2527
private final transient PrintStream logger;
2628
private String reportStyle;
2729
public String reportName;
2830
public String urlName;
2931

3032
private int maxRetryReportAttempt;
33+
34+
private static final String DEFAULT_REPORT_TIMEOUT = "120";
3135
private static final String REPORT_IN_PROGRESS = "REPORT_IN_PROGRESS";
3236
private static final String REPORT_FAILED = "REPORT_FAILED";
3337

@@ -36,18 +40,16 @@ public class BrowserStackTestReportAction implements Action {
3640
private static final int MAX_ATTEMPTS = 3;
3741
private RequestsUtil requestsUtil;
3842

39-
public BrowserStackTestReportAction(Run<?, ?> run, BrowserStackCredentials credentials, String reportUrl, String UUID, String reportName, String tabUrl, final PrintStream logger) {
43+
public BrowserStackTestReportAction(Run<?, ?> run, BrowserStackCredentials credentials, String buildName, String builldCreatedAt, final PrintStream logger) {
4044
super();
4145
setBuild(run);
4246
this.credentials = credentials;
43-
this.reportUrl = reportUrl;
44-
this.UUID = UUID;
47+
this.buildName = buildName;
48+
this.buildCreatedAt = builldCreatedAt;
4549
this.reportHtml = null;
4650
this.reportStyle = "";
51+
maxRetryReportAttempt = MAX_ATTEMPTS;
4752
this.logger = logger;
48-
this.reportName = reportName;
49-
this.urlName = tabUrl;
50-
this.maxRetryReportAttempt = MAX_ATTEMPTS;
5153

5254
this.requestsUtil = new RequestsUtil();
5355
}
@@ -70,28 +72,37 @@ private void fetchReportConditions() {
7072
}
7173

7274
private void fetchReport() {
73-
if (UUID == null) {
74-
reportHtml = REPORT_FAILED;
75-
return;
76-
}
7775
Map<String, String> params = new HashMap<>();
78-
params.put("UUID", UUID);
79-
params.put("report_name", reportName);
80-
params.put("tool", Constants.INTEGRATIONS_TOOL_KEY);
76+
String pollValue = "FIRST";
77+
params.put("buildCreatedAt", buildCreatedAt);
78+
params.put("buildName", buildName);
79+
params.put("requestingCi", Constants.INTEGRATIONS_TOOL_KEY);
80+
params.put("reportFormat", Arrays.asList(Constants.REPORT_FORMAT).toString());
81+
params.put("requestType", pollValue);
82+
params.put("userTimeout", DEFAULT_REPORT_TIMEOUT);
8183

8284
try {
85+
String reportUrl = Constants.CAD_BASE_URL + Constants.BROWSERSTACK_CONFIG_DETAILS_ENDPOINT;
8386
String ciReportUrlWithParams = requestsUtil.buildQueryParams(reportUrl, params);
8487
log(logger, "Fetching browserstack report " + reportName);
8588
Response response = requestsUtil.makeRequest(ciReportUrlWithParams, credentials);
8689
if (response.isSuccessful()) {
90+
assert response.body() != null;
8791
JSONObject reportResponse = new JSONObject(response.body().string());
88-
String reportStatus = reportResponse.optString("report_status");
89-
if (reportStatus.equalsIgnoreCase(String.valueOf(BrowserStackReportStatus.COMPLETED))) {
92+
String reportStatus = reportResponse.optString("reportStatus");
93+
if (reportStatus.equalsIgnoreCase(String.valueOf(BrowserStackReportStatus.COMPLETED)) ||
94+
reportStatus.equalsIgnoreCase(String.valueOf(BrowserStackReportStatus.TEST_AVAILABLE)) ||
95+
reportStatus.equalsIgnoreCase(String.valueOf(BrowserStackReportStatus.NOT_AVAILABLE))) {
96+
9097
String defaultHTML = "<h1>No Report Found</h1>";
91-
reportHtml = reportResponse.optString("report_html", defaultHTML);
92-
reportStyle = reportResponse.optString("report_style", "");
98+
JSONObject report = reportResponse.optJSONObject("report");
99+
reportHtml = report != null ? report.optString("report_html", defaultHTML) : defaultHTML;
100+
reportStyle = report != null ? report.optString("report_style", "") : "";
101+
93102
} else if (reportStatus.equalsIgnoreCase(String.valueOf(BrowserStackReportStatus.IN_PROGRESS))) {
103+
94104
reportHtml = REPORT_IN_PROGRESS;
105+
95106
} else {
96107
reportHtml = REPORT_FAILED;
97108
}

src/main/java/com/browserstack/automate/ci/jenkins/integrationService/BrowserStackTestReportPublisher.java

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,26 @@
1818
import hudson.Launcher;
1919
import jenkins.tasks.SimpleBuildStep;
2020
import org.jenkinsci.Symbol;
21-
import org.json.JSONArray;
22-
import org.json.JSONObject;
23-
import okhttp3.*;
2421
import org.kohsuke.stapler.DataBoundConstructor;
2522

2623
import java.io.IOException;
2724
import java.io.PrintStream;
28-
import java.net.URISyntaxException;
2925
import java.net.URLEncoder;
3026
import java.text.SimpleDateFormat;
3127
import java.util.*;
3228
import java.util.concurrent.ConcurrentHashMap;
33-
import java.util.concurrent.TimeUnit;
3429
import java.util.logging.Logger;
3530

3631
import static com.browserstack.automate.ci.common.logger.PluginLogger.log;
3732
import static com.browserstack.automate.ci.common.logger.PluginLogger.logError;
3833

3934
public 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;

src/main/resources/com/browserstack/automate/ci/jenkins/integrationService/BrowserStackTestReportAction/index.jelly

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<div class="new-container">
7676
<div class="report-row">
7777
<j:if test="${(it.isReportInProgress())}">
78-
<p class="no-report-para"><strong class="strong-font-custom">BrowserStack ${it.reportName} Report In Progress</strong></p>
78+
<p class="no-report-para"><strong class="strong-font-custom">BrowserStack ${it.reportName} Report In Progress, please refresh after sometime</strong></p>
7979
</j:if>
8080
<j:if test="${(it.isReportFailed())}">
8181
<p class="no-report-para"><strong class="strong-font-custom">No BrowserStack ${it.reportName} Report Available</strong></p>
@@ -85,8 +85,6 @@
8585
<ul class="bullet-points">
8686
<li class="bullet-item">You have set valid BrowserStack credentials via BrowserStack Plugin.</li>
8787
<li class="bullet-item">You have used BROWSERSTACK_BUILD_NAME as your build name</li>
88-
<li class="bullet-item">try passing BROWSERSTACK_PROJECT_NAME environment variable</li>
89-
9088
</ul>
9189
</j:if>
9290
<j:if test="${it.reportRetryRequired()}">

0 commit comments

Comments
 (0)