Skip to content

Commit

Permalink
Merge pull request #2 from levelops/prop-2226
Browse files Browse the repository at this point in the history
[PROP-2226] ADD: auto migration from the stored configuration used in previous versions
  • Loading branch information
ivan-levelops authored Nov 15, 2022
2 parents 35afee1 + 9c1f3ce commit bb31ff7
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import io.jenkins.plugins.propelo.commons.service.LevelOpsPluginConfigService;
import io.jenkins.plugins.propelo.commons.service.ProxyConfigService;
import io.jenkins.plugins.propelo.commons.utils.JsonUtils;
import io.jenkins.plugins.propelo.job_reporter.plugins.LevelOpsPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl;
import jenkins.model.Jenkins;

import org.jetbrains.annotations.NotNull;
Expand All @@ -31,7 +31,7 @@ public class JenkinsHeartbeatAperiodicWork extends AperiodicWork {

private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private static final ObjectMapper mapper = JsonUtils.buildObjectMapper();
private final LevelOpsPluginImpl plugin = LevelOpsPluginImpl.getInstance();
private final PropeloPluginImpl plugin = PropeloPluginImpl.getInstance();
private final LevelOpsPluginConfigService levelOpsPluginConfigService = new LevelOpsPluginConfigService();

public JenkinsHeartbeatAperiodicWork() {
Expand Down Expand Up @@ -73,7 +73,7 @@ private HeartbeatResponse sendHeartbeat(HeartbeatRequest heartbeatRequest) throw

@NotNull
public HeartbeatResponse sendHeartbeat(String hbRequestPayload, GenericRequestService genericRequestService,
LevelOpsPluginImpl plugin, final ProxyConfigService.ProxyConfig proxyConfig) throws IOException {
PropeloPluginImpl plugin, final ProxyConfigService.ProxyConfig proxyConfig) throws IOException {
GenericResponse genericResponse = genericRequestService.performGenericRequest(plugin.getLevelOpsApiKey().getPlainText(),
"JenkinsHeartbeat", hbRequestPayload, plugin.isTrustAllCertificates(), null, proxyConfig);
HeartbeatResponse heartbeatResponse = mapper.readValue(genericResponse.getPayload(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io.jenkins.plugins.propelo.commons.service.JobRunParserService;
import io.jenkins.plugins.propelo.commons.service.JobSCMStorageService;
import io.jenkins.plugins.propelo.commons.utils.JsonUtils;
import io.jenkins.plugins.propelo.job_reporter.plugins.LevelOpsPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.service.ConfigChangeService;

import org.apache.commons.lang.StringUtils;
Expand All @@ -27,7 +27,7 @@
public class JobConfigContentChangeListener extends SaveableListener {
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private static final ObjectMapper mapper = JsonUtils.buildObjectMapper();
private final LevelOpsPluginImpl plugin = LevelOpsPluginImpl.getInstance();
private final PropeloPluginImpl plugin = PropeloPluginImpl.getInstance();

/** {@inheritDoc} */
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.jenkins.plugins.propelo.commons.service.JobRunParserService;
import io.jenkins.plugins.propelo.commons.service.JobSCMStorageService;
import io.jenkins.plugins.propelo.commons.utils.JsonUtils;
import io.jenkins.plugins.propelo.job_reporter.plugins.LevelOpsPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.service.ConfigChangeService;

import org.apache.commons.lang.StringUtils;
Expand All @@ -25,7 +25,7 @@
public class JobConfigStateChangeListener extends ItemListener {
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private static final ObjectMapper mapper = JsonUtils.buildObjectMapper();
private final LevelOpsPluginImpl plugin = LevelOpsPluginImpl.getInstance();
private final PropeloPluginImpl plugin = PropeloPluginImpl.getInstance();

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import hudson.model.Hudson;
import hudson.model.ManagementLink;
import hudson.util.Secret;
import io.jenkins.plugins.propelo.job_reporter.plugins.LevelOpsPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl;
import jenkins.model.Jenkins;

import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void doSaveSettings(final StaplerRequest res, final StaplerResponse rsp,

Hudson.getInstance().checkPermission(Hudson.ADMINISTER);

final LevelOpsPluginImpl plugin = LevelOpsPluginImpl.getInstance();
final PropeloPluginImpl plugin = PropeloPluginImpl.getInstance();
plugin.setLevelOpsApiKey(Secret.fromString(levelOpsApiKey));
plugin.setLevelOpsPluginPath(levelOpsPluginPath);
plugin.setJenkinsBaseUrl(Jenkins.get().getRootUrl());
Expand All @@ -81,8 +81,8 @@ public void doSaveSettings(final StaplerRequest res, final StaplerResponse rsp,
rsp.sendRedirect(res.getContextPath() + "/" + PLUGIN_NAME);
}

public LevelOpsPluginImpl getConfiguration() {
return LevelOpsPluginImpl.getInstance();
public PropeloPluginImpl getConfiguration() {
return PropeloPluginImpl.getInstance();
}

public String getJenkinsStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.jenkins.plugins.propelo.commons.service.ProxyConfigService;
import io.jenkins.plugins.propelo.commons.utils.DateUtils;
import io.jenkins.plugins.propelo.commons.utils.JsonUtils;
import io.jenkins.plugins.propelo.job_reporter.plugins.LevelOpsPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.service.JobRunCodeCoverageResultsService;
import io.jenkins.plugins.propelo.job_reporter.service.JobRunCodeCoverageXmlResultsService;
import io.jenkins.plugins.propelo.job_reporter.service.JobRunCompleteDataService;
Expand Down Expand Up @@ -49,7 +49,7 @@
@Extension
public class LevelOpsRunListener extends RunListener<Run> {
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private final LevelOpsPluginImpl plugin = LevelOpsPluginImpl.getInstance();
private final PropeloPluginImpl plugin = PropeloPluginImpl.getInstance();
private static final ObjectMapper mapper = JsonUtils.buildObjectMapper();
private static final ObjectMapper xmlMapper = new XmlMapper();
private static final String JOB_SUCCESSFUL = "SUCCESS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import hudson.scheduler.CronTab;
import hudson.util.FormValidation;
import hudson.util.Secret;
import hudson.util.VersionNumber;
import io.jenkins.plugins.propelo.commons.models.JenkinsStatusInfo;
import io.jenkins.plugins.propelo.commons.models.PluginVersion;
import io.jenkins.plugins.propelo.commons.models.blue_ocean.Organization;
import io.jenkins.plugins.propelo.commons.service.BlueOceanRestClient;
import io.jenkins.plugins.propelo.commons.service.JenkinsInstanceGuidService;
Expand All @@ -19,6 +17,7 @@
import io.jenkins.plugins.propelo.commons.utils.JsonUtils;
import io.jenkins.plugins.propelo.commons.utils.Utils;
import io.jenkins.plugins.propelo.job_reporter.extensions.LevelOpsMgmtLink;
import io.levelops.plugins.levelops_job_reporter.plugins.LevelOpsPluginImpl;
import jenkins.model.Jenkins;

import org.apache.commons.io.FileUtils;
Expand All @@ -45,7 +44,7 @@

import static io.jenkins.plugins.propelo.commons.plugins.Common.REPORTS_DIR_NAME;

public class LevelOpsPluginImpl extends Plugin {
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";
Expand All @@ -62,25 +61,73 @@ public class LevelOpsPluginImpl extends Plugin {
private long heartbeatDuration = 60;
private String bullseyeXmlResultPaths = "";
private long configUpdatedAt = System.currentTimeMillis();
private static LevelOpsPluginImpl instance = null;
private static PropeloPluginImpl instance = null;
private static final Pattern OLDER_DIRECTORIES_PATTERN = Pattern.compile("^(run-complete-data-)");


//ToDo: This is deprecated! Fix soon.
public LevelOpsPluginImpl() {
public PropeloPluginImpl() {
instance = this;
}

@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.
LOGGER.info("No stored configuration detected");
migrateOldPluginConfig();
}
LOGGER.info("Deleting Older directories during plugin initialization.Started");
deleteOlderDirectories();
LOGGER.info("Deleting Older directories during plugin initialization.Completed");
LOGGER.fine("'" + LevelOpsMgmtLink.PLUGIN_DISPLAY_NAME + "' plugin initialized.");
}

public static LevelOpsPluginImpl getInstance() {
/**
* Migration for the configuration stored from LevelOpsPluginImpl to PropeloPluginImpl
* Should only be run if there is no a new configuration stored and if the migration has never been migrated
*
* @throws Exception
*/
private void migrateOldPluginConfig() throws Exception{
// Try Load old plugin values
LevelOpsPluginImpl oldValues = LevelOpsPluginImpl.getInstance();
oldValues.load();
// 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...");
// fill in a new instance
// PropeloPluginImpl newInstance = new PropeloPluginImpl();
instance.setBullseyeXmlResultPath(oldValues.getBullseyeXmlResultPaths());
instance.setLevelOpsPluginPath(oldValues.getLevelOpsPluginPath());
if(StringUtils.isNotBlank(oldValues.getLevelOpsApiKey())) {
instance.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());
if (StringUtils.isNotBlank(oldValues.getJenkinsUserToken())) {
instance.setJenkinsUserToken(Secret.fromString(oldValues.getJenkinsUserToken()));
}
// persist the migrated values
instance.save();
// set migrated to true on the old configuration
oldValues.setMigrated(true);
oldValues.save();
LOGGER.info("Old LevelOpsPluginImpl Configuration migrated!");
}
else {
LOGGER.info("Not migrating old settings from LevelOpsPluginImpl to PropeloPluginImpl");
}
}

public static PropeloPluginImpl getInstance() {
// if(instance == null){
// instance = new LevelOpsPluginImpl();
// }
Expand Down Expand Up @@ -201,7 +248,7 @@ private void deleteOlderDirectories() {

public String getPluginVersionString() {
LOGGER.log(Level.FINEST, "getPluginVersionString starting");
String pluginVersionString = Jenkins.get().getPluginManager().getPlugin(LevelOpsPluginImpl.PLUGIN_SHORT_NAME).getVersion();
String pluginVersionString = Jenkins.get().getPluginManager().getPlugin(PropeloPluginImpl.PLUGIN_SHORT_NAME).getVersion();
LOGGER.log(Level.FINEST, "getPluginVersionString completed pluginVersionString = {0}", pluginVersionString);
return pluginVersionString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import io.jenkins.plugins.propelo.commons.service.LevelOpsPluginConfigService;
import io.jenkins.plugins.propelo.commons.service.ProxyConfigService;
import io.jenkins.plugins.propelo.commons.utils.MimickedUser;
import io.jenkins.plugins.propelo.job_reporter.plugins.LevelOpsPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl;
import jenkins.model.Jenkins;

import java.io.File;
Expand All @@ -29,12 +29,12 @@

public class ConfigChangeService {
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private final LevelOpsPluginImpl plugin;
private final PropeloPluginImpl plugin;
private final JobSCMStorageService jobSCMStorageService;
private final JobRunParserService jobRunParserService;
private final ObjectMapper mapper;

public ConfigChangeService(LevelOpsPluginImpl plugin, JobSCMStorageService jobSCMStorageService, JobRunParserService jobRunParserService, ObjectMapper mapper) {
public ConfigChangeService(PropeloPluginImpl plugin, JobSCMStorageService jobSCMStorageService, JobRunParserService jobRunParserService, ObjectMapper mapper) {
this.plugin = plugin;
this.jobSCMStorageService = jobSCMStorageService;
this.jobRunParserService = jobRunParserService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.jenkins.plugins.propelo.commons.service.BlueOceanRestClient;
import io.jenkins.plugins.propelo.commons.service.JobFullNameConverter;
import io.jenkins.plugins.propelo.commons.service.ProxyConfigService;
import io.jenkins.plugins.propelo.job_reporter.plugins.LevelOpsPluginImpl;
import io.jenkins.plugins.propelo.job_reporter.plugins.PropeloPluginImpl;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
Expand All @@ -34,11 +34,11 @@ public class JobRunCompleteDataService {
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private static int MAX_RECURSION_LEVELS = 50;

private final LevelOpsPluginImpl plugin;
private final PropeloPluginImpl plugin;
private final ObjectMapper mapper;
private final ProxyConfigService.ProxyConfig proxyConfig;

public JobRunCompleteDataService(LevelOpsPluginImpl plugin, ObjectMapper mapper, final ProxyConfigService.ProxyConfig proxyConfig) {
public JobRunCompleteDataService(PropeloPluginImpl plugin, ObjectMapper mapper, final ProxyConfigService.ProxyConfig proxyConfig) {
this.plugin = plugin;
this.mapper = mapper;
this.proxyConfig = proxyConfig;
Expand Down
Loading

0 comments on commit bb31ff7

Please sign in to comment.