Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

public class Constants {
public static final String BROWSERSTACK_REPORT_DISPLAY_NAME = "BrowserStack Test Report";
public static final String BROWSERSTACK_CYPRESS_REPORT_DISPLAY_NAME = "BrowserStack Cypress Test Report";
public static final String BROWSERSTACK_LOGO = String.format("%s/plugin/browserstack-integration/images/logo.png", Jenkins.RESOURCE_PATH);
public static final String BROWSERSTACK_REPORT_URL = "testReportBrowserStack";
public static final String BROWSERSTACK_CYPRESS_REPORT_URL = "testReportBrowserStackCypress";
public static final String BROWSERSTACK_REPORT_PIPELINE_FUNCTION = "browserStackReportPublisher";
public static final String JENKINS_CI_PLUGIN = "JenkinsCiPlugin";

Expand Down Expand Up @@ -40,5 +42,6 @@ public static final class SessionStatus {
public static final String ERROR = "error";
public static final String FAILED = "failed";
public static final String UNMARKED = "unmarked";
public static final String PASSED = "passed";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.browserstack.automate.ci.jenkins;

import com.browserstack.automate.ci.common.constants.Constants;
import hudson.model.Action;
import hudson.model.Run;

public abstract class AbstractBrowserStackCypressReportForBuild implements Action {
private Run<?, ?> build;

@Override
public String getIconFileName() {
return Constants.BROWSERSTACK_LOGO;
}

@Override
public String getDisplayName() {
return Constants.BROWSERSTACK_CYPRESS_REPORT_DISPLAY_NAME;
}

@Override
public String getUrlName() {
return Constants.BROWSERSTACK_CYPRESS_REPORT_URL;
}

public Run<?, ?> getBuild() {
return build;
}

public void setBuild(Run<?, ?> build) {
this.build = build;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.browserstack.automate.ci.jenkins;

import com.browserstack.automate.ci.common.constants.Constants;
import com.browserstack.automate.ci.common.enums.ProjectType;
import com.browserstack.automate.ci.common.tracking.PluginsTracker;
import hudson.model.Run;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.util.*;

import static com.browserstack.automate.ci.common.logger.PluginLogger.logError;

public class BrowserStackCypressReportForBuild extends AbstractBrowserStackCypressReportForBuild {
private static PrintStream logger;
private final String buildName;
private final transient JSONObject result;
private final Map<String, String> resultAggregation;
private final ProjectType projectType;
// to make them available in jelly
private final String passedConst = Constants.SessionStatus.PASSED;
private final String failedConst = Constants.SessionStatus.FAILED;
private final transient PluginsTracker tracker;
private final boolean pipelineStatus;

public BrowserStackCypressReportForBuild(final Run<?, ?> build,
final ProjectType projectType,
final String buildName,
final PrintStream logger,
final PluginsTracker tracker,
final boolean pipelineStatus) {
super();
setBuild(build);
this.buildName = buildName;
this.result = new JSONObject();
this.resultAggregation = new HashMap<>();
this.projectType = projectType;
this.logger = logger;
this.tracker = tracker;
this.pipelineStatus = pipelineStatus;
}


public JSONObject getCypressMatrix(String filepath) {
JSONObject report = null;
final String reportJSONPath = filepath + "/results/browserstack-cypress-report.json";

try {
InputStream is = new FileInputStream(reportJSONPath);
String jsonTxt = IOUtils.toString(is, "UTF-8");
report = new JSONObject(jsonTxt);
} catch (FileNotFoundException e) {
logError(logger, "No BrowserStackBuildAction found");
tracker.sendError("BrowserStack Cypress Report Not Found", pipelineStatus, "CypressReportGeneration");
} catch (IOException e) {
logError(logger, "There was a problem while reading report files");
tracker.sendError(e.toString(), pipelineStatus, "CypressReportGeneration");
}
return report;
}

public boolean generateBrowserStackCypressReport(String workspace) {
if (result.length() == 0) {
JSONObject matrix = getCypressMatrix(workspace);

if(matrix == null) {
return false;
}

String buildNameWithBuildNumber = matrix.optString("build_name");
String buildNameWithoutBuildNumber = buildNameWithBuildNumber.substring(0, buildNameWithBuildNumber.lastIndexOf(": "));

if (buildNameWithoutBuildNumber == null) {
logError(logger, "BrowserStack Cypress Report not generated, result json may have been corrupted. Please retry.");
tracker.sendError("Report not generated", pipelineStatus, "CypressReportGeneration");
return false;
}

if (!buildNameWithoutBuildNumber.equalsIgnoreCase(this.buildName)) {
logError(logger, "BrowserStack Cypress Report not generated, build name mismatch.");
tracker.sendError("Report not generated", pipelineStatus, "CypressReportGeneration");
return false;
}

generateResult(matrix);

if (result.length() > 0) {
generateAggregationInfo();
return true;
}
return false;
}
return true;
}

private void generateResult(JSONObject matrix) {
result.put("buildName", matrix.getString("build_name"));
result.put("buildId", matrix.getString("build_id"));
result.put("projectName", matrix.getString("project_name"));
result.put("buildUrl", matrix.getString("build_url"));

JSONArray specs = new JSONArray();
JSONObject rows = matrix.getJSONObject("rows");
rows.keySet().forEach(specName ->
{
JSONObject spec = new JSONObject();
JSONObject specData = rows.getJSONObject(specName);
JSONObject specMeta = specData.getJSONObject("meta");

spec.put("name", specName);
spec.put("path", specData.getString("path"));

// Meta
spec.put("total", specMeta.getInt("total"));
spec.put("failed", specMeta.getInt("failed"));
spec.put("passed", specMeta.getInt("passed"));

// Sessions
JSONArray sessions = specData.getJSONArray("sessions");
spec.put("sessions", sessions);

specs.put(spec);
});

result.put("specs", specs);
}

private void generateAggregationInfo() {
int totalSpecs = 0, totalErrors = 0;

JSONArray specs = result.getJSONArray("specs");

for(int i = 0; i < specs.length(); i++){
JSONObject spec = specs.getJSONObject(i);
totalSpecs += spec.getInt("total");
totalErrors += spec.getInt("failed");
}

resultAggregation.put("totalSpecs", String.valueOf(totalSpecs));
resultAggregation.put("totalErrors", String.valueOf(totalErrors));
}

public JSONObject getResult() {
return result;
}

public Map<String, String> getResultAggregation() {
return resultAggregation;
}

public String getBuildName() {
return buildName;
}

public ProjectType getProjectType() {
return projectType;
}

public String getPassedConst() {
return passedConst;
}

public String getFailedConst() {
return failedConst;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.browserstack.automate.ci.jenkins;

import com.browserstack.automate.ci.common.BrowserStackEnvVars;
import com.browserstack.automate.ci.common.constants.Constants;
import com.browserstack.automate.ci.common.enums.ProjectType;
import com.browserstack.automate.ci.common.tracking.PluginsTracker;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import jenkins.tasks.SimpleBuildStep;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Optional;

import static com.browserstack.automate.ci.common.logger.PluginLogger.log;

public class BrowserStackCypressReportPublisher extends Recorder implements SimpleBuildStep {

@DataBoundConstructor
public BrowserStackCypressReportPublisher() {
}

public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}


@Override
public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
final PrintStream logger = listener.getLogger();
final PluginsTracker tracker = new PluginsTracker();
final boolean pipelineStatus = false;

log(logger, "Generating BrowserStack Cypress Test Report");

final EnvVars parentEnvs = build.getEnvironment(listener);
String browserStackBuildName = parentEnvs.get(BrowserStackEnvVars.BROWSERSTACK_BUILD_NAME);
browserStackBuildName = Optional.ofNullable(browserStackBuildName).orElse(parentEnvs.get(Constants.JENKINS_BUILD_TAG));
final String jenkinsFolder = parentEnvs.get("WORKSPACE");
ProjectType product = ProjectType.AUTOMATE;

tracker.reportGenerationInitialized(browserStackBuildName, product.name(), pipelineStatus);
log(logger, "BrowserStack Cypress Project identified as : " + product.name());

final BrowserStackCypressReportForBuild bstackReportAction =
new BrowserStackCypressReportForBuild(build, product, browserStackBuildName, logger, tracker, pipelineStatus);
final boolean reportResult = bstackReportAction.generateBrowserStackCypressReport(jenkinsFolder);
build.addAction(bstackReportAction);

String reportStatus = reportResult ? Constants.ReportStatus.SUCCESS : Constants.ReportStatus.FAILED;
log(logger, "BrowserStack Cypress Report Status: " + reportStatus);
}

@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {

@Override
@SuppressWarnings("rawtypes")
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// indicates that this builder can be used with all kinds of project types
return true;
}

/**
* This human readable name is used in the configuration screen.
*/
@Override
public String getDisplayName() {
return Constants.BROWSERSTACK_CYPRESS_REPORT_DISPLAY_NAME;
}
}
}
Loading