|
| 1 | +package com.browserstack.automate.ci.jenkins; |
| 2 | + |
| 3 | +import com.browserstack.automate.ci.common.constants.Constants; |
| 4 | +import com.browserstack.automate.ci.common.enums.ProjectType; |
| 5 | +import com.browserstack.automate.ci.common.tracking.PluginsTracker; |
| 6 | +import hudson.model.Run; |
| 7 | +import org.apache.commons.io.IOUtils; |
| 8 | +import org.json.JSONArray; |
| 9 | +import org.json.JSONObject; |
| 10 | + |
| 11 | +import java.io.*; |
| 12 | +import java.util.*; |
| 13 | + |
| 14 | +import static com.browserstack.automate.ci.common.logger.PluginLogger.logError; |
| 15 | + |
| 16 | +public class BrowserStackCypressReportForBuild extends AbstractBrowserStackCypressReportForBuild { |
| 17 | + private static PrintStream logger; |
| 18 | + private final String buildName; |
| 19 | + private final transient JSONObject result; |
| 20 | + private final Map<String, String> resultAggregation; |
| 21 | + private final ProjectType projectType; |
| 22 | + // to make them available in jelly |
| 23 | + private final String passedConst = Constants.SessionStatus.PASSED; |
| 24 | + private final String failedConst = Constants.SessionStatus.FAILED; |
| 25 | + private final transient PluginsTracker tracker; |
| 26 | + private final boolean pipelineStatus; |
| 27 | + |
| 28 | + public BrowserStackCypressReportForBuild(final Run<?, ?> build, |
| 29 | + final ProjectType projectType, |
| 30 | + final String buildName, |
| 31 | + final PrintStream logger, |
| 32 | + final PluginsTracker tracker, |
| 33 | + final boolean pipelineStatus) { |
| 34 | + super(); |
| 35 | + setBuild(build); |
| 36 | + this.buildName = buildName; |
| 37 | + this.result = new JSONObject(); |
| 38 | + this.resultAggregation = new HashMap<>(); |
| 39 | + this.projectType = projectType; |
| 40 | + this.logger = logger; |
| 41 | + this.tracker = tracker; |
| 42 | + this.pipelineStatus = pipelineStatus; |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + public JSONObject getCypressMatrix(String filepath) { |
| 47 | + JSONObject report = null; |
| 48 | + final String reportJSONPath = filepath + "/results/browserstack-cypress-report.json"; |
| 49 | + |
| 50 | + try { |
| 51 | + InputStream is = new FileInputStream(reportJSONPath); |
| 52 | + String jsonTxt = IOUtils.toString(is, "UTF-8"); |
| 53 | + report = new JSONObject(jsonTxt); |
| 54 | + } catch (FileNotFoundException e) { |
| 55 | + logError(logger, "No BrowserStackBuildAction found"); |
| 56 | + tracker.sendError("BrowserStack Cypress Report Not Found", pipelineStatus, "CypressReportGeneration"); |
| 57 | + } catch (IOException e) { |
| 58 | + logError(logger, "There was a problem while reading report files"); |
| 59 | + tracker.sendError(e.toString(), pipelineStatus, "CypressReportGeneration"); |
| 60 | + } |
| 61 | + return report; |
| 62 | + } |
| 63 | + |
| 64 | + public boolean generateBrowserStackCypressReport(String workspace) { |
| 65 | + if (result.length() == 0) { |
| 66 | + JSONObject matrix = getCypressMatrix(workspace); |
| 67 | + |
| 68 | + if(matrix == null) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + String buildNameWithBuildNumber = matrix.optString("build_name"); |
| 73 | + String buildNameWithoutBuildNumber = buildNameWithBuildNumber.substring(0, buildNameWithBuildNumber.lastIndexOf(": ")); |
| 74 | + |
| 75 | + if (buildNameWithoutBuildNumber == null) { |
| 76 | + logError(logger, "BrowserStack Cypress Report not generated, result json may have been corrupted. Please retry."); |
| 77 | + tracker.sendError("Report not generated", pipelineStatus, "CypressReportGeneration"); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + if (!buildNameWithoutBuildNumber.equalsIgnoreCase(this.buildName)) { |
| 82 | + logError(logger, "BrowserStack Cypress Report not generated, build name mismatch."); |
| 83 | + tracker.sendError("Report not generated", pipelineStatus, "CypressReportGeneration"); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + generateResult(matrix); |
| 88 | + |
| 89 | + if (result.length() > 0) { |
| 90 | + generateAggregationInfo(); |
| 91 | + return true; |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + private void generateResult(JSONObject matrix) { |
| 99 | + result.put("buildName", matrix.getString("build_name")); |
| 100 | + result.put("buildId", matrix.getString("build_id")); |
| 101 | + result.put("projectName", matrix.getString("project_name")); |
| 102 | + result.put("buildUrl", matrix.getString("build_url")); |
| 103 | + |
| 104 | + JSONArray specs = new JSONArray(); |
| 105 | + JSONObject rows = matrix.getJSONObject("rows"); |
| 106 | + rows.keySet().forEach(specName -> |
| 107 | + { |
| 108 | + JSONObject spec = new JSONObject(); |
| 109 | + JSONObject specData = rows.getJSONObject(specName); |
| 110 | + JSONObject specMeta = specData.getJSONObject("meta"); |
| 111 | + |
| 112 | + spec.put("name", specName); |
| 113 | + spec.put("path", specData.getString("path")); |
| 114 | + |
| 115 | + // Meta |
| 116 | + spec.put("total", specMeta.getInt("total")); |
| 117 | + spec.put("failed", specMeta.getInt("failed")); |
| 118 | + spec.put("passed", specMeta.getInt("passed")); |
| 119 | + |
| 120 | + // Sessions |
| 121 | + JSONArray sessions = specData.getJSONArray("sessions"); |
| 122 | + spec.put("sessions", sessions); |
| 123 | + |
| 124 | + specs.put(spec); |
| 125 | + }); |
| 126 | + |
| 127 | + result.put("specs", specs); |
| 128 | + } |
| 129 | + |
| 130 | + private void generateAggregationInfo() { |
| 131 | + int totalSpecs = 0, totalErrors = 0; |
| 132 | + |
| 133 | + JSONArray specs = result.getJSONArray("specs"); |
| 134 | + |
| 135 | + for(int i = 0; i < specs.length(); i++){ |
| 136 | + JSONObject spec = specs.getJSONObject(i); |
| 137 | + totalSpecs += spec.getInt("total"); |
| 138 | + totalErrors += spec.getInt("failed"); |
| 139 | + } |
| 140 | + |
| 141 | + resultAggregation.put("totalSpecs", String.valueOf(totalSpecs)); |
| 142 | + resultAggregation.put("totalErrors", String.valueOf(totalErrors)); |
| 143 | + } |
| 144 | + |
| 145 | + public JSONObject getResult() { |
| 146 | + return result; |
| 147 | + } |
| 148 | + |
| 149 | + public Map<String, String> getResultAggregation() { |
| 150 | + return resultAggregation; |
| 151 | + } |
| 152 | + |
| 153 | + public String getBuildName() { |
| 154 | + return buildName; |
| 155 | + } |
| 156 | + |
| 157 | + public ProjectType getProjectType() { |
| 158 | + return projectType; |
| 159 | + } |
| 160 | + |
| 161 | + public String getPassedConst() { |
| 162 | + return passedConst; |
| 163 | + } |
| 164 | + |
| 165 | + public String getFailedConst() { |
| 166 | + return failedConst; |
| 167 | + } |
| 168 | +} |
0 commit comments