-
Notifications
You must be signed in to change notification settings - Fork 53
Add Cypress Report Generator #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../java/com/browserstack/automate/ci/jenkins/AbstractBrowserStackCypressReportForBuild.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
168 changes: 168 additions & 0 deletions
168
src/main/java/com/browserstack/automate/ci/jenkins/BrowserStackCypressReportForBuild.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
83 changes: 83 additions & 0 deletions
83
src/main/java/com/browserstack/automate/ci/jenkins/BrowserStackCypressReportPublisher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
SagarGaniga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.