From 08e800239051f5d8ec533ea09e36b24549162d1a Mon Sep 17 00:00:00 2001 From: Ashish Duthade Date: Fri, 9 Aug 2024 14:54:47 +0530 Subject: [PATCH] feat: [SEI-7028]: Jenkins Configuration as Code --- pom.xml | 4 +- .../PropeloJobReporterConfiguration.java | 209 ++++++++++++++++++ .../JenkinsHeartbeatAperiodicWork.java | 16 +- .../extensions/LevelOpsMgmtLink.java | 31 +-- .../plugins/PropeloPluginImpl.java | 181 +++++++-------- .../global.jelly | 30 +++ .../extensions/LevelOpsMgmtLink/index.jelly | 9 +- .../LevelOpsPluginConfigServiceTest.java | 17 +- 8 files changed, 369 insertions(+), 128 deletions(-) create mode 100644 src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java create mode 100644 src/main/resources/io.jenkins.plugins.propelo.commons.models/PropeloJobReporterConfiguration/global.jelly diff --git a/pom.xml b/pom.xml index d2e3519..8733895 100755 --- a/pom.xml +++ b/pom.xml @@ -82,7 +82,7 @@ - 1.0.32 + 1.0.33 UTF-8 @@ -94,7 +94,7 @@ true true 17 - 2.361.4 + 2.426.3 diff --git a/src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java b/src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java new file mode 100644 index 0000000..a21c73c --- /dev/null +++ b/src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java @@ -0,0 +1,209 @@ +package io.jenkins.plugins.propelo.commons.models; + +import hudson.Extension; +import hudson.util.Secret; +import jenkins.model.GlobalConfiguration; +import net.sf.json.JSONObject; +import org.jenkinsci.Symbol; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; +import org.kohsuke.stapler.StaplerRequest; + +import javax.annotation.CheckForNull; + +@Symbol("propelo-job-reporter") +@Extension +public class PropeloJobReporterConfiguration extends GlobalConfiguration { + private Secret levelOpsApiKey; + private String levelOpsPluginPath; + private boolean trustAllCertificates; + private String productIds; + private String jenkinsInstanceName; + public Boolean isRegistered; + private String jenkinsStatus; + private String jenkinsUserName; + private String jenkinsBaseUrl; + private Secret jenkinsUserToken; + private long heartbeatDuration; + private String bullseyeXmlResultPaths; + private long configUpdatedAt; + private ApplicationType applicationType; + + public static PropeloJobReporterConfiguration CONFIGURATION = new PropeloJobReporterConfiguration(); + + @DataBoundConstructor + public PropeloJobReporterConfiguration(Secret levelOpsApiKey, String levelOpsPluginPath, @CheckForNull boolean trustAllCertificates, String productIds, String jenkinsInstanceName, Boolean isRegistered, String jenkinsStatus, String jenkinsUserName, String jenkinsBaseUrl, Secret jenkinsUserToken, long heartbeatDuration, String bullseyeXmlResultPaths, long configUpdatedAt, ApplicationType applicationType) { + this.levelOpsApiKey = levelOpsApiKey; + this.levelOpsPluginPath = levelOpsPluginPath; + this.trustAllCertificates = trustAllCertificates; + this.productIds = productIds; + this.jenkinsInstanceName = jenkinsInstanceName; + this.isRegistered = isRegistered; + this.jenkinsStatus = jenkinsStatus; + this.jenkinsUserName = jenkinsUserName; + this.jenkinsBaseUrl = jenkinsBaseUrl; + this.jenkinsUserToken = jenkinsUserToken; + this.heartbeatDuration = heartbeatDuration; + this.bullseyeXmlResultPaths = bullseyeXmlResultPaths; + this.configUpdatedAt = configUpdatedAt; + this.applicationType = applicationType; + CONFIGURATION = this; + } + + public PropeloJobReporterConfiguration(){ + this.levelOpsApiKey = Secret.fromString(""); + this.levelOpsPluginPath = "${JENKINS_HOME}/levelops-jenkin"; + this.productIds = ""; + this.jenkinsInstanceName = "Jenkins Instance"; + this.isRegistered = false; + this.jenkinsStatus = ""; + this.jenkinsUserName = ""; + this.jenkinsBaseUrl = ""; + this.jenkinsUserToken = Secret.fromString(""); + this.heartbeatDuration = 60; + this.trustAllCertificates = false; + this.bullseyeXmlResultPaths = ""; + this.configUpdatedAt = System.currentTimeMillis(); + this.load(); + CONFIGURATION = this; + } + + public Secret getLevelOpsApiKey() { + return levelOpsApiKey; + } + + @DataBoundSetter + public void setLevelOpsApiKey(Secret levelOpsApiKey) { + this.levelOpsApiKey = levelOpsApiKey; + } + + public String getLevelOpsPluginPath() { + return levelOpsPluginPath; + } + + @DataBoundSetter + public void setLevelOpsPluginPath(String levelOpsPluginPath) { + this.levelOpsPluginPath = levelOpsPluginPath; + } + + public boolean isTrustAllCertificates() { + return trustAllCertificates; + } + + @DataBoundSetter + public void setTrustAllCertificates(boolean trustAllCertificates) { + this.trustAllCertificates = trustAllCertificates; + } + + public String getProductIds() { + return productIds; + } + + @DataBoundSetter + public void setProductIds(String productIds) { + this.productIds = productIds; + } + + public String getJenkinsInstanceName() { + return jenkinsInstanceName; + } + + @DataBoundSetter + public void setJenkinsInstanceName(String jenkinsInstanceName) { + this.jenkinsInstanceName = jenkinsInstanceName; + } + + public Boolean getRegistered() { + return isRegistered; + } + + @DataBoundSetter + public void setRegistered(Boolean registered) { + isRegistered = registered; + } + + public String getJenkinsStatus() { + return jenkinsStatus; + } + + @DataBoundSetter + public void setJenkinsStatus(String jenkinsStatus) { + this.jenkinsStatus = jenkinsStatus; + } + + public String getJenkinsUserName() { + return jenkinsUserName; + } + + @DataBoundSetter + public void setJenkinsUserName(String jenkinsUserName) { + this.jenkinsUserName = jenkinsUserName; + } + + public String getJenkinsBaseUrl() { + return jenkinsBaseUrl; + } + + @DataBoundSetter + public void setJenkinsBaseUrl(String jenkinsBaseUrl) { + this.jenkinsBaseUrl = jenkinsBaseUrl; + } + + public Secret getJenkinsUserToken() { + return jenkinsUserToken; + } + + @DataBoundSetter + public void setJenkinsUserToken(Secret jenkinsUserToken) { + this.jenkinsUserToken = jenkinsUserToken; + } + + public long getHeartbeatDuration() { + return heartbeatDuration; + } + + @DataBoundSetter + public void setHeartbeatDuration(long heartbeatDuration) { + this.heartbeatDuration = heartbeatDuration; + } + + public String getBullseyeXmlResultPaths() { + return bullseyeXmlResultPaths; + } + + @DataBoundSetter + public void setBullseyeXmlResultPaths(String bullseyeXmlResultPaths) { + this.bullseyeXmlResultPaths = bullseyeXmlResultPaths; + } + + public long getConfigUpdatedAt() { + return configUpdatedAt; + } + + @DataBoundSetter + public void setConfigUpdatedAt(long configUpdatedAt) { + this.configUpdatedAt = configUpdatedAt; + } + + public ApplicationType getApplicationType() { + return applicationType; + } + + @DataBoundSetter + public void setApplicationType(ApplicationType applicationType) { + this.applicationType = applicationType; + } + + @Override + public String getDisplayName() { + return "Propelo Job Reporter configuration"; + } + + @Override + public boolean configure(StaplerRequest req, JSONObject json) throws FormException { + // reset optional certificate to default before data-binding + req.bindJSON(this, json); + save(); + return true; + } +} diff --git a/src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/JenkinsHeartbeatAperiodicWork.java b/src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/JenkinsHeartbeatAperiodicWork.java index f8f81ef..f8a15bd 100644 --- a/src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/JenkinsHeartbeatAperiodicWork.java +++ b/src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/JenkinsHeartbeatAperiodicWork.java @@ -26,6 +26,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static io.jenkins.plugins.propelo.commons.models.PropeloJobReporterConfiguration.CONFIGURATION; + @Log4j2 @Extension public class JenkinsHeartbeatAperiodicWork extends AperiodicWork { @@ -41,8 +43,12 @@ public JenkinsHeartbeatAperiodicWork() { @Override public void doAperiodicRun() { try { + if(CONFIGURATION == null || CONFIGURATION.getApplicationType() == null){ + LOGGER.log(Level.FINE, "No configuration found for Propelo Job Reporter's Plugin."); + return; + } monitorNow(System.currentTimeMillis()); - plugin.isRegistered = true; + CONFIGURATION.isRegistered = true; JenkinsStatusService.getInstance().markHeartbeat(plugin.getExpandedLevelOpsPluginDir(), true); } catch (IOException e) { try { @@ -85,10 +91,10 @@ public HeartbeatResponse sendHeartbeat(String hbRequestPayload, GenericRequestSe mapper.getTypeFactory().constructType(HeartbeatResponse.class)); HeartbeatResponse.CiCdInstanceConfig configuration = heartbeatResponse.getConfiguration(); if (configuration != null) { - plugin.setBullseyeXmlResultPath(configuration.getBullseyeReportPaths() != null ? configuration.getBullseyeReportPaths() : ""); - plugin.setHeartbeatDuration(configuration.getHeartbeatDuration() != null ? configuration.getHeartbeatDuration() : 60); - plugin.setConfigUpdatedAt(System.currentTimeMillis()); - plugin.save(); + CONFIGURATION.setBullseyeXmlResultPaths(configuration.getBullseyeReportPaths() != null ? configuration.getBullseyeReportPaths() : ""); + CONFIGURATION.setHeartbeatDuration(configuration.getHeartbeatDuration() != null ? configuration.getHeartbeatDuration() : 60); + CONFIGURATION.setConfigUpdatedAt(System.currentTimeMillis()); + CONFIGURATION.save(); } LOGGER.log(Level.INFO, "Heartbeat duration is : " + this.plugin.getHeartbeatDuration() + " || Heartbeat response : " + genericResponse); diff --git a/src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink.java b/src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink.java index 0e3302b..7d73b35 100644 --- a/src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink.java +++ b/src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink.java @@ -4,6 +4,8 @@ import hudson.model.Hudson; import hudson.model.ManagementLink; import hudson.util.Secret; +import io.jenkins.plugins.propelo.commons.models.ApplicationType; +import io.jenkins.plugins.propelo.commons.models.PropeloJobReporterConfiguration; import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl; import jenkins.model.Jenkins; @@ -17,6 +19,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static io.jenkins.plugins.propelo.commons.models.PropeloJobReporterConfiguration.CONFIGURATION; + @Extension public class LevelOpsMgmtLink extends ManagementLink { @@ -24,7 +28,6 @@ public class LevelOpsMgmtLink extends ManagementLink { public static final String PLUGIN_NAME = "propelo-job-reporter"; public static final String PLUGIN_DISPLAY_NAME = "Harness - SEI Job Reporter"; public static final String PLUGIN_DESCRIPTION = "Reports back to Harness - SEI after each Job Run with metadata and unsuccessful job logs."; - @Override public String getDisplayName() { return PLUGIN_DISPLAY_NAME; @@ -68,23 +71,23 @@ public void doSaveSettings(final StaplerRequest res, final StaplerResponse rsp, Hudson.getInstance().checkPermission(Hudson.ADMINISTER); final PropeloPluginImpl plugin = PropeloPluginImpl.getInstance(); - plugin.setLevelOpsApiKey(Secret.fromString(levelOpsApiKey)); - plugin.setLevelOpsPluginPath(levelOpsPluginPath); - plugin.setJenkinsBaseUrl(Jenkins.get().getRootUrl()); - plugin.setJenkinsUserName(jenkinsUserName); - plugin.setJenkinsUserToken(Secret.fromString(jenkinsUserToken)); - plugin.setBullseyeXmlResultPath(bullseyeXmlResultPaths); - plugin.setProductIds(productIds); - plugin.setJenkinsInstanceName(jenkinsInstanceName); - plugin.setTrustAllCertificates(trustAllCertificates); - plugin.setApplicationType(applicationType); - plugin.save(); + CONFIGURATION.setLevelOpsApiKey(Secret.fromString(levelOpsApiKey)); + CONFIGURATION.setLevelOpsPluginPath(levelOpsPluginPath); + CONFIGURATION.setJenkinsBaseUrl(Jenkins.get().getRootUrl()); + CONFIGURATION.setJenkinsUserName(jenkinsUserName); + CONFIGURATION.setJenkinsUserToken(Secret.fromString(jenkinsUserToken)); + CONFIGURATION.setBullseyeXmlResultPaths(bullseyeXmlResultPaths); + CONFIGURATION.setProductIds(productIds); + CONFIGURATION.setJenkinsInstanceName(jenkinsInstanceName); + CONFIGURATION.setTrustAllCertificates(trustAllCertificates); + CONFIGURATION.setApplicationType(ApplicationType.fromString(applicationType)); + CONFIGURATION.save(); LOGGER.log(Level.CONFIG, "Saving plugin settings done. plugin = {0}", plugin); rsp.sendRedirect(res.getContextPath() + "/" + PLUGIN_NAME); } - public PropeloPluginImpl getConfiguration() { - return PropeloPluginImpl.getInstance(); + public PropeloJobReporterConfiguration getConfiguration() { + return CONFIGURATION; } public String getJenkinsStatus() { diff --git a/src/main/java/io/jenkins/plugins/propelo/job_reporter/plugins/PropeloPluginImpl.java b/src/main/java/io/jenkins/plugins/propelo/job_reporter/plugins/PropeloPluginImpl.java index e731189..f1383ff 100644 --- a/src/main/java/io/jenkins/plugins/propelo/job_reporter/plugins/PropeloPluginImpl.java +++ b/src/main/java/io/jenkins/plugins/propelo/job_reporter/plugins/PropeloPluginImpl.java @@ -1,6 +1,7 @@ package io.jenkins.plugins.propelo.job_reporter.plugins; import antlr.ANTLRException; +import hudson.Extension; import hudson.Plugin; import hudson.scheduler.CronTab; import hudson.util.FormValidation; @@ -46,11 +47,15 @@ import java.util.regex.Pattern; import static io.jenkins.plugins.propelo.commons.plugins.Common.REPORTS_DIR_NAME; +import static io.jenkins.plugins.propelo.commons.models.PropeloJobReporterConfiguration.CONFIGURATION; +@Extension public class PropeloPluginImpl extends Plugin { private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName()); private static final String DATA_DIR_NAME = "run-complete-data"; public static final String PLUGIN_SHORT_NAME = "propelo-job-reporter"; + private static PropeloPluginImpl instance = null; + private static final Pattern OLDER_DIRECTORIES_PATTERN = Pattern.compile("^(run-complete-data-)"); private Secret levelOpsApiKey = Secret.fromString(""); private String levelOpsPluginPath = "${JENKINS_HOME}/levelops-jenkin"; private boolean trustAllCertificates = false; @@ -65,24 +70,32 @@ public class PropeloPluginImpl extends Plugin { private String bullseyeXmlResultPaths = ""; private long configUpdatedAt = System.currentTimeMillis(); private ApplicationType applicationType; - private static PropeloPluginImpl instance = null; - private static final Pattern OLDER_DIRECTORIES_PATTERN = Pattern.compile("^(run-complete-data-)"); - + private Boolean migrated; //ToDo: This is deprecated! Fix soon. public PropeloPluginImpl() { instance = this; } + public Boolean getMigrated(){ + return migrated; + } + public void setMigrated(Boolean migrated){ + this.migrated = migrated; + } + @Override public void start() throws Exception { super.start(); load(); // if there is an instance aleady present, ignore migration - if (StringUtils.isBlank(this.jenkinsBaseUrl)) { // jenkinsBaseUrl is always added during the save process. we can use this value as a check. + if (StringUtils.isBlank(getJenkinsBaseUrl()) && StringUtils.isBlank(jenkinsBaseUrl)) { // jenkinsBaseUrl is always added during the save process. we can use this value as a check. LOGGER.info("No stored configuration detected"); migrateOldPluginConfig(); } + if(migrated == null || !migrated){ + migratePluginConfigToConfiguration(); + } LOGGER.info("Checking work directory permissions..."); checkWorkDirectoryAccess(); LOGGER.info("Deleting Older directories during plugin initialization.Started"); @@ -104,29 +117,31 @@ private void migrateOldPluginConfig() throws Exception{ // if there is a serialized version of the old "io.levelops.plugins.levelops_job_reporter.plugins.LevelOpsPluginImpl" instance and // it hasn't been marked as migrated, then take the values from that config and migrate them to a new instance of PropeloPluginImpl if (StringUtils.isNotBlank(oldValues.getLevelOpsApiKey()) && !oldValues.isMigrated()) { - LOGGER.info("Migrating old LevelOpsPluginImpl configuration..."); + LOGGER.info("Migrating old LevelOpsPluginImpl Configuration..."); // fill in a new instance // PropeloPluginImpl newInstance = new PropeloPluginImpl(); - instance.setBullseyeXmlResultPath(oldValues.getBullseyeXmlResultPaths()); - instance.setLevelOpsPluginPath(oldValues.getLevelOpsPluginPath()); + CONFIGURATION.setBullseyeXmlResultPaths(oldValues.getBullseyeXmlResultPaths()); + CONFIGURATION.setLevelOpsPluginPath(oldValues.getLevelOpsPluginPath()); if(StringUtils.isNotBlank(oldValues.getLevelOpsApiKey())) { - instance.setLevelOpsApiKey(Secret.fromString(oldValues.getLevelOpsApiKey())); + CONFIGURATION.setLevelOpsApiKey(Secret.fromString(oldValues.getLevelOpsApiKey())); } - instance.setTrustAllCertificates(oldValues.isTrustAllCertificates()); - instance.setProductIds(oldValues.getProductIds()); - instance.setJenkinsInstanceName(oldValues.getJenkinsInstanceName()); - instance.setJenkinsBaseUrl(oldValues.getJenkinsBaseUrl()); - instance.setJenkinsStatus(oldValues.getJenkinsStatus()); - instance.setJenkinsUserName(oldValues.getJenkinsUserName()); + CONFIGURATION.setTrustAllCertificates(oldValues.isTrustAllCertificates()); + CONFIGURATION.setProductIds(oldValues.getProductIds()); + CONFIGURATION.setJenkinsInstanceName(oldValues.getJenkinsInstanceName()); + CONFIGURATION.setJenkinsBaseUrl(oldValues.getJenkinsBaseUrl()); + CONFIGURATION.setJenkinsStatus(oldValues.getJenkinsStatus()); + CONFIGURATION.setJenkinsUserName(oldValues.getJenkinsUserName()); if (StringUtils.isNotBlank(oldValues.getJenkinsUserToken())) { - instance.setJenkinsUserToken(Secret.fromString(oldValues.getJenkinsUserToken())); + CONFIGURATION.setJenkinsUserToken(Secret.fromString(oldValues.getJenkinsUserToken())); } - instance.setApplicationType("SEI_LEGACY"); + CONFIGURATION.setApplicationType(ApplicationType.SEI_LEGACY); // persist the migrated values - instance.save(); + CONFIGURATION.save(); // set migrated to true on the old configuration oldValues.setMigrated(true); oldValues.save(); + instance.setMigrated(true); + instance.save(); LOGGER.info("Old LevelOpsPluginImpl Configuration migrated!"); } else { @@ -134,6 +149,32 @@ private void migrateOldPluginConfig() throws Exception{ } } + private void migratePluginConfigToConfiguration() throws Exception{ + // Try Load old plugin values + // if there is a serialized version of the old "io.levelops.plugins.levelops_job_reporter.plugins.PropeloPluginImpl" instance and + // it hasn't been marked as migrated, then take the values from that config and migrate them to a new configuration of PropeloPluginImpl + + LOGGER.info("Migrating old PropeloPluginImpl Configuration..."); + // fill in a new instance + CONFIGURATION.setBullseyeXmlResultPaths(instance.bullseyeXmlResultPaths); + CONFIGURATION.setLevelOpsPluginPath(instance.levelOpsPluginPath); + CONFIGURATION.setLevelOpsApiKey(Secret.fromString(instance.levelOpsApiKey.getPlainText())); + CONFIGURATION.setTrustAllCertificates(instance.trustAllCertificates); + CONFIGURATION.setProductIds(instance.productIds); + CONFIGURATION.setJenkinsInstanceName(instance.jenkinsInstanceName); + CONFIGURATION.setJenkinsBaseUrl(instance.jenkinsBaseUrl); + CONFIGURATION.setJenkinsStatus(instance.jenkinsStatus); + CONFIGURATION.setJenkinsUserName(instance.jenkinsUserName); + CONFIGURATION.setJenkinsUserToken(Secret.fromString(instance.jenkinsUserToken.getPlainText())); + CONFIGURATION.setApplicationType(instance.applicationType); + // persist the migrated values + CONFIGURATION.save(); + // set migrated to true on the old configuration + instance.setMigrated(true); + instance.save(); + LOGGER.info("Old PropeloPluginImpl Configuration migrated!"); + } + public static PropeloPluginImpl getInstance() { // if(instance == null){ // instance = new LevelOpsPluginImpl(); @@ -146,24 +187,16 @@ public File getHudsonHome() { return (jenkins == null) ? null : jenkins.getRootDir(); } - public void setJenkinsBaseUrl(final String jenkinsBaseUrl){ - this.jenkinsBaseUrl = jenkinsBaseUrl; - } - public String getJenkinsBaseUrl(){ - return this.jenkinsBaseUrl; + return CONFIGURATION.getJenkinsBaseUrl(); } public Secret getLevelOpsApiKey() { - return levelOpsApiKey; + return CONFIGURATION.getLevelOpsApiKey(); } public ApplicationType getApplicationType() { - return applicationType; - } - - public void setLevelOpsApiKey(Secret levelOpsApiKey) { - this.levelOpsApiKey = levelOpsApiKey; + return CONFIGURATION.getApplicationType(); } /** @@ -174,62 +207,45 @@ public void setLevelOpsApiKey(Secret levelOpsApiKey) { * @return the path as entered by the user. */ public String getLevelOpsPluginPath() { - return levelOpsPluginPath; - } - - public void setLevelOpsPluginPath(String levelOpsPluginPath) { - this.levelOpsPluginPath = levelOpsPluginPath; + return CONFIGURATION.getLevelOpsPluginPath(); } /** * @return the levelOpsPluginPath path with possibly contained environment variables expanded. */ public String getExpandedLevelOpsPluginPath() { - if (StringUtils.isBlank(levelOpsPluginPath)) { - return levelOpsPluginPath; + if (StringUtils.isBlank(CONFIGURATION.getLevelOpsPluginPath())) { + return CONFIGURATION.getLevelOpsPluginPath(); } String expandedPath = ""; try { - expandedPath = Utils.expandEnvironmentVariables(levelOpsPluginPath); + expandedPath = Utils.expandEnvironmentVariables(CONFIGURATION.getLevelOpsPluginPath()); } catch (final EnvironmentVariableNotDefinedException evnde) { LOGGER.log(Level.SEVERE, evnde.getMessage() + " Using unexpanded path."); - expandedPath = levelOpsPluginPath; + expandedPath = CONFIGURATION.getLevelOpsPluginPath(); } return expandedPath; } public String getJenkinsStatus() { - return jenkinsStatus; - } - - public void setJenkinsStatus(String jenkinsStatus) { - this.jenkinsStatus = jenkinsStatus; + return CONFIGURATION.getJenkinsStatus(); } public long getConfigUpdatedAt() { - return configUpdatedAt; - } - - public void setConfigUpdatedAt(long configUpdatedAt) { - this.configUpdatedAt = configUpdatedAt; + return CONFIGURATION.getConfigUpdatedAt(); } public long getHeartbeatDuration() { - return heartbeatDuration; - } - - public void setHeartbeatDuration(long heartbeatDuration) { - this.heartbeatDuration = heartbeatDuration; + return CONFIGURATION.getHeartbeatDuration(); } public File getExpandedLevelOpsPluginDir() { return new File(this.getExpandedLevelOpsPluginPath()); } - public Boolean isRegistered() { - return isRegistered; + return CONFIGURATION.getRegistered(); } public boolean isExpandedLevelOpsPluginPathNullOrEmpty(){ @@ -323,47 +339,23 @@ public File getDataDirectoryWithRotation() { } public String getJenkinsUserName() { - return jenkinsUserName; - } - - public void setJenkinsUserName(String jenkinsUserName) { - this.jenkinsUserName = jenkinsUserName; + return CONFIGURATION.getJenkinsUserName(); } public Secret getJenkinsUserToken() { - return jenkinsUserToken; - } - - public void setJenkinsUserToken(Secret jenkinsUserToken) { - this.jenkinsUserToken = jenkinsUserToken; + return CONFIGURATION.getJenkinsUserToken(); } public String getBullseyeXmlResultPaths() { - return bullseyeXmlResultPaths; - } - - public void setBullseyeXmlResultPath(String bullseyeXmlResultPaths) { - this.bullseyeXmlResultPaths = bullseyeXmlResultPaths; + return CONFIGURATION.getBullseyeXmlResultPaths(); } public boolean isTrustAllCertificates() { - return trustAllCertificates; - } - - public void setTrustAllCertificates(boolean trustAllCertificates) { - this.trustAllCertificates = trustAllCertificates; - } - - public void setApplicationType(String applicationType) { - this.applicationType = ApplicationType.fromString(applicationType); + return CONFIGURATION.isTrustAllCertificates(); } public String getProductIds() { - return productIds; - } - - public void setProductIds(String productIds) { - this.productIds = productIds; + return CONFIGURATION.getProductIds(); } private List parseProductIdsList(String productIds){ @@ -377,17 +369,12 @@ private List parseProductIdsList(String productIds){ return Arrays.asList(productIdsSplit); } public List getProductIdsList(){ - return parseProductIdsList(this.productIds); + return parseProductIdsList(CONFIGURATION.getProductIds()); } public String getJenkinsInstanceName() { - return jenkinsInstanceName; + return CONFIGURATION.getJenkinsInstanceName(); } - - public void setJenkinsInstanceName(String jenkinsInstanceName) { - this.jenkinsInstanceName = jenkinsInstanceName; - } - @POST public FormValidation doCheckLevelOpsApiKey(final StaplerRequest res, final StaplerResponse rsp, @QueryParameter("value") final Secret levelOpsApiKey) { @@ -396,7 +383,7 @@ public FormValidation doCheckLevelOpsApiKey(final StaplerRequest res, final Stap instance.getExpandedLevelOpsPluginDir(), instance.getDataDirectory(), instance.getDataDirectoryWithVersion()); ProxyConfigService.ProxyConfig proxyConfig = ProxyConfigService.generateConfigFromJenkinsProxyConfiguration(Jenkins.getInstanceOrNull()); - return LevelOpsPluginConfigValidator.performApiKeyValidation(levelOpsApiKey, trustAllCertificates, + return LevelOpsPluginConfigValidator.performApiKeyValidation(levelOpsApiKey, isTrustAllCertificates(), jenkinsInstanceGuidService.createOrReturnInstanceGuid(), instance.getJenkinsInstanceName(), instance.getPluginVersionString(), proxyConfig); } @@ -408,13 +395,13 @@ public FormValidation doCheckLevelOpsStatus(final StaplerRequest res, final Stap JenkinsStatusInfo details = JenkinsStatusService.getInstance().getStatus(resultFile); if (isRegistered()) { // Instance is registered... if (details.getLastFailedHeartbeat().after(details.getLastSuccessfulHeartbeat())) { - setJenkinsStatus("Trying to connect to LevelOps. Disconnected since " + details.getLastSuccessfulHeartbeat()); + CONFIGURATION.setJenkinsStatus("Trying to connect to LevelOps. Disconnected since " + details.getLastSuccessfulHeartbeat()); } else { - setJenkinsStatus("Success, connected since " + details.getLastSuccessfulHeartbeat()); + CONFIGURATION.setJenkinsStatus("Success, connected since " + details.getLastSuccessfulHeartbeat()); } return FormValidation.ok(); } else { - setJenkinsStatus("Registering Jenkins Instance...."); + CONFIGURATION.setJenkinsStatus("Registering Jenkins Instance...."); } } catch (LoadFileException e) { String errorMessage = "Unable to use the work directory provided in 'Propelo Plugins Directory'. The directory provided must be accessible and writeable by the user running the Jenkins process."; @@ -453,7 +440,7 @@ public FormValidation performBlueOceanRestValidation(String jenkinsBaseUrl, Stri ProxyConfigService.ProxyConfig proxyConfig = ProxyConfigService.generateConfigFromJenkinsProxyConfiguration(Jenkins.getInstanceOrNull()); - BlueOceanRestClient restClient = new BlueOceanRestClient(jenkinsBaseUrl, jenkinsUserName, jenkinsUserToken, trustAllCertificates, JsonUtils.buildObjectMapper(), proxyConfig); + BlueOceanRestClient restClient = new BlueOceanRestClient(jenkinsBaseUrl, jenkinsUserName, jenkinsUserToken, isTrustAllCertificates(), JsonUtils.buildObjectMapper(), proxyConfig); List organizations = null; try { organizations = restClient.getOrganizations(); @@ -507,7 +494,7 @@ public FormValidation doCheckJenkinsBaseUrl(final StaplerRequest res, final Stap if(StringUtils.isBlank(jenkinsBaseUrl)) { return FormValidation.error("Jenkins Base Url cannot be null or empty!"); } else { - return performBlueOceanRestValidation(Jenkins.get().getRootUrl(), jenkinsUserName, jenkinsUserToken.getPlainText(), true, false, false); + return performBlueOceanRestValidation(Jenkins.get().getRootUrl(), getJenkinsUserName(), getJenkinsUserToken().getPlainText(), true, false, false); } } @@ -518,7 +505,7 @@ public FormValidation doCheckJenkinsUserName(final StaplerRequest res, final Sta if(StringUtils.isBlank(jenkinsUserName)) { return FormValidation.error("Jenkins User Name cannot be null or empty!"); } else { - return performBlueOceanRestValidation(Jenkins.get().getRootUrl(), jenkinsUserName, jenkinsUserToken.getPlainText(), false, true, false); + return performBlueOceanRestValidation(Jenkins.get().getRootUrl(), jenkinsUserName, getJenkinsUserToken().getPlainText(), false, true, false); } } diff --git a/src/main/resources/io.jenkins.plugins.propelo.commons.models/PropeloJobReporterConfiguration/global.jelly b/src/main/resources/io.jenkins.plugins.propelo.commons.models/PropeloJobReporterConfiguration/global.jelly new file mode 100644 index 0000000..bc8b7de --- /dev/null +++ b/src/main/resources/io.jenkins.plugins.propelo.commons.models/PropeloJobReporterConfiguration/global.jelly @@ -0,0 +1,30 @@ + + + + + + + + SEI-Harness-PROD1 + SEI-Harness-PROD2 + SEI-Harness-PROD3 + SEI-Legacy-US + SEI-Legacy-EU + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink/index.jelly b/src/main/resources/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink/index.jelly index 6c9e86e..de80033 100644 --- a/src/main/resources/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink/index.jelly +++ b/src/main/resources/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink/index.jelly @@ -62,7 +62,7 @@ - + @@ -102,10 +102,9 @@ - + + + diff --git a/src/test/java/io/jenkins/plugins/propelo/commons/service/LevelOpsPluginConfigServiceTest.java b/src/test/java/io/jenkins/plugins/propelo/commons/service/LevelOpsPluginConfigServiceTest.java index d63c81b..b6da894 100644 --- a/src/test/java/io/jenkins/plugins/propelo/commons/service/LevelOpsPluginConfigServiceTest.java +++ b/src/test/java/io/jenkins/plugins/propelo/commons/service/LevelOpsPluginConfigServiceTest.java @@ -3,33 +3,40 @@ import io.jenkins.plugins.propelo.commons.models.ApplicationType; import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; import java.io.File; +import static io.jenkins.plugins.propelo.commons.models.PropeloJobReporterConfiguration.CONFIGURATION; + public class LevelOpsPluginConfigServiceTest { + @Rule + public JenkinsRule r = new JenkinsRule(); + @Test public void testApplicationType() throws Exception { PropeloPluginImpl propeloPlugin = new PropeloPluginImpl(); - propeloPlugin.setApplicationType("SEI-Harness-PROD1"); + CONFIGURATION.setApplicationType(ApplicationType.fromString("SEI-Harness-PROD1")); ApplicationType applicationType = PropeloPluginImpl.getInstance().getApplicationType(); Assert.assertEquals(applicationType, ApplicationType.SEI_HARNESS_PROD1); - propeloPlugin.setApplicationType("SEI-Harness-PROD3"); + CONFIGURATION.setApplicationType(ApplicationType.fromString("SEI-Harness-PROD3")); applicationType = PropeloPluginImpl.getInstance().getApplicationType(); Assert.assertEquals(applicationType, ApplicationType.SEI_HARNESS_PROD3); - propeloPlugin.setApplicationType("SEI-Legacy"); + CONFIGURATION.setApplicationType(ApplicationType.fromString("SEI-Legacy")); applicationType = PropeloPluginImpl.getInstance().getApplicationType(); Assert.assertEquals(applicationType, ApplicationType.SEI_LEGACY); - propeloPlugin.setApplicationType("SEI-Legacy-EU"); + CONFIGURATION.setApplicationType(ApplicationType.fromString("SEI-Legacy-EU")); applicationType = PropeloPluginImpl.getInstance().getApplicationType(); Assert.assertEquals(applicationType, ApplicationType.SEI_LEGACY_EU); - propeloPlugin.setApplicationType("SEI-Harness-PROD2"); + CONFIGURATION.setApplicationType(ApplicationType.fromString("SEI-Harness-PROD2")); applicationType = PropeloPluginImpl.getInstance().getApplicationType(); Assert.assertEquals(applicationType, ApplicationType.SEI_HARNESS_PROD2); }