|
| 1 | +package com.browserstack.automate.ci.jenkins; |
| 2 | + |
| 3 | +import com.browserstack.automate.ci.common.BrowserStackEnvVars; |
| 4 | +import com.browserstack.automate.ci.common.constants.Constants; |
| 5 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 6 | +import hudson.EnvVars; |
| 7 | +import hudson.Extension; |
| 8 | +import hudson.model.AbstractProject; |
| 9 | +import hudson.model.Run; |
| 10 | +import hudson.model.TaskListener; |
| 11 | +import hudson.tasks.BuildStepDescriptor; |
| 12 | +import hudson.tasks.BuildStepMonitor; |
| 13 | +import hudson.tasks.Publisher; |
| 14 | +import hudson.tasks.Recorder; |
| 15 | +import hudson.FilePath; |
| 16 | +import hudson.Launcher; |
| 17 | +import jenkins.tasks.SimpleBuildStep; |
| 18 | +import org.json.JSONArray; |
| 19 | +import org.json.JSONObject; |
| 20 | +import okhttp3.*; |
| 21 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.PrintStream; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Optional; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import java.util.logging.Logger; |
| 30 | + |
| 31 | +import static com.browserstack.automate.ci.common.logger.PluginLogger.log; |
| 32 | +import static com.browserstack.automate.ci.common.logger.PluginLogger.logError; |
| 33 | + |
| 34 | +public class BrowserStackReportFetcherPublisher extends Recorder implements SimpleBuildStep { |
| 35 | + private static final Logger LOGGER = Logger.getLogger(BrowserStackReportFetcherPublisher.class.getName()); |
| 36 | + private static final OkHttpClient client = new OkHttpClient(); |
| 37 | + private static final int MAX_ATTEMPTS = 3; |
| 38 | + private static final int RETRY_DELAY_SECONDS = 10; |
| 39 | + private final Map<String, String> customEnvVars; |
| 40 | + |
| 41 | + makeRequestsUtil requestsUtil; |
| 42 | + @DataBoundConstructor |
| 43 | + public BrowserStackReportFetcherPublisher(Map<String, String> customEnvVars) { |
| 44 | + this.customEnvVars = customEnvVars != null ? new HashMap<>(customEnvVars) : new HashMap<>(); |
| 45 | + requestsUtil = new makeRequestsUtil(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void perform(Run<?, ?> build, @NonNull FilePath workspace, @NonNull Launcher launcher, TaskListener listener) throws IOException, InterruptedException { |
| 50 | + final PrintStream logger = listener.getLogger(); |
| 51 | + log(logger, "Adding BrowserStack Report"); |
| 52 | + |
| 53 | + EnvVars parentEnvs = build.getEnvironment(listener); |
| 54 | + parentEnvs.putAll(getCustomEnvVars()); |
| 55 | + |
| 56 | + String browserStackBuildName = Optional.ofNullable(parentEnvs.get(BrowserStackEnvVars.BROWSERSTACK_BUILD_NAME)) |
| 57 | + .orElse(parentEnvs.get(Constants.JENKINS_BUILD_TAG)); |
| 58 | + String projectName = parentEnvs.get(BrowserStackEnvVars.BROWSERSTACK_PROJECT_NAME); |
| 59 | + |
| 60 | + BrowserStackBuildAction buildAction = build.getAction(BrowserStackBuildAction.class); |
| 61 | + if (buildAction == null || buildAction.getBrowserStackCredentials() == null) { |
| 62 | + logError(logger, "No BrowserStackBuildAction or credentials found"); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + BrowserStackCredentials credentials = buildAction.getBrowserStackCredentials(); |
| 67 | + |
| 68 | + LOGGER.info("Adding BrowserStack Report Action"); |
| 69 | + |
| 70 | + JSONObject reportConfigDetailsResponse = fetchConfigDetails(logger, Constants.BROWSERSTACK_CONFIG_DETAILS_ENDPOINT, credentials); |
| 71 | + if (reportConfigDetailsResponse == null) { |
| 72 | + logError(logger, "Could not fetch the report config details"); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + JSONArray configDetails = reportConfigDetailsResponse.getJSONArray("config_details"); |
| 77 | + String lookUpURL = reportConfigDetailsResponse.getString("lookup_endpoint"); |
| 78 | + |
| 79 | + String UUID = fetchUUID(logger, lookUpURL, credentials, browserStackBuildName, projectName); |
| 80 | + if (UUID == null) { |
| 81 | + logError(logger, "Cannot find a build with name " + browserStackBuildName); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + for (int i = 0; i < configDetails.length(); i++) { |
| 86 | + JSONObject config = configDetails.getJSONObject(i); |
| 87 | + |
| 88 | + String reportUrl = config.getString("report_url"); |
| 89 | + String reportName = config.getString("report_name"); |
| 90 | + String reportTabUrl = config.getString("report_tab_url"); |
| 91 | + |
| 92 | + build.addAction(new BrowserStackReportFetcherForBuild(build, credentials, reportUrl, UUID, reportName, reportTabUrl, logger)); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + private String fetchUUID(PrintStream logger, String lookUpURL, BrowserStackCredentials credentials , String buildName, String projectName) throws InterruptedException { |
| 98 | + String lookUpURLWithParams = lookUpURL + "?build_name=" + buildName; |
| 99 | + if(projectName != null) { |
| 100 | + lookUpURLWithParams = lookUpURLWithParams + "&project_name" + projectName; |
| 101 | + } |
| 102 | + |
| 103 | + log(logger, "Fetching build...."); |
| 104 | + for (int attempts = 0; attempts < MAX_ATTEMPTS; attempts++) { |
| 105 | + try { |
| 106 | + Response response = requestsUtil.makeRequest(lookUpURLWithParams, credentials); |
| 107 | + if (response.isSuccessful()) { |
| 108 | + |
| 109 | + JSONObject lookUpResponse = new JSONObject(response.body().string()); |
| 110 | + String UUID = lookUpResponse.optString("UUID", null); |
| 111 | + |
| 112 | + if(UUID !=null) { |
| 113 | + log(logger, "build found with "+ buildName + "and project name" + projectName); |
| 114 | + return UUID; |
| 115 | + } |
| 116 | + |
| 117 | + LOGGER.info("build not found will retry in sometime.." + lookUpResponse.getString("message")); |
| 118 | + } |
| 119 | + } catch (Exception e) { |
| 120 | + logError(logger, "Attempt " + (attempts + 1) + " failed: " + e.getMessage()); |
| 121 | + } |
| 122 | + TimeUnit.SECONDS.sleep(RETRY_DELAY_SECONDS); |
| 123 | + } |
| 124 | + |
| 125 | + LOGGER.info("build not found even after "+ MAX_ATTEMPTS + "attempts"); |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + private JSONObject fetchConfigDetails(PrintStream logger, String configDetailsURL, BrowserStackCredentials credentials) { |
| 130 | + log(logger, "Fetching config details for reports"); |
| 131 | + try { |
| 132 | + Response response = requestsUtil.makeRequest(configDetailsURL, credentials); |
| 133 | + if (response.isSuccessful()) { |
| 134 | + return new JSONObject(response.body().string()); |
| 135 | + } |
| 136 | + logError(logger, "Failed to fetch config details: " + response.code()); |
| 137 | + } catch (Exception e) { |
| 138 | + logError(logger, "Exception occurred while fetching config details: " + e.getMessage()); |
| 139 | + } |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + public Map<String, String> getCustomEnvVars() { |
| 144 | + return customEnvVars; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public BuildStepMonitor getRequiredMonitorService() { |
| 149 | + return BuildStepMonitor.NONE; |
| 150 | + } |
| 151 | + |
| 152 | + @Extension |
| 153 | + public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { |
| 154 | + |
| 155 | + @Override |
| 156 | + @SuppressWarnings("rawtypes") |
| 157 | + public boolean isApplicable(Class<? extends AbstractProject> aClass) { |
| 158 | + // indicates that this builder can be used with all kinds of project types |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | +} |
0 commit comments