Skip to content

Commit

Permalink
Merge pull request #22 from jenkinsci/SEI/feature/SEI-7028
Browse files Browse the repository at this point in the history
feat: [SEI-7028]: Jenkins Configuration as Code
  • Loading branch information
ashish-levelops authored Aug 20, 2024
2 parents 5b65a73 + 08e8002 commit a07a88d
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 128 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
</profiles>

<properties>
<revision>1.0.32</revision>
<revision>1.0.33</revision>
<!-- <changelist>999999-SNAPSHOT</changelist> -->
<changelist></changelist>
<product.build.sourceEncoding>UTF-8</product.build.sourceEncoding>
Expand All @@ -94,7 +94,7 @@
<findbugs.skip>true</findbugs.skip>
<spotbugs.skip>true</spotbugs.skip>
<java.level>17</java.level>
<jenkins.version>2.361.4</jenkins.version>
<jenkins.version>2.426.3</jenkins.version>
</properties>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

Check warning on line 51 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 35-51 are not covered by tests

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;

Check warning on line 72 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 72 is not covered by tests
}

@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;

Check warning on line 90 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 90 is not covered by tests
}

@DataBoundSetter
public void setTrustAllCertificates(boolean trustAllCertificates) {
this.trustAllCertificates = trustAllCertificates;
}

public String getProductIds() {
return productIds;

Check warning on line 99 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 99 is not covered by tests
}

@DataBoundSetter
public void setProductIds(String productIds) {
this.productIds = productIds;
}

public String getJenkinsInstanceName() {
return jenkinsInstanceName;

Check warning on line 108 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 108 is not covered by tests
}

@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;

Check warning on line 126 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 117-126 are not covered by tests
}

@DataBoundSetter
public void setJenkinsStatus(String jenkinsStatus) {
this.jenkinsStatus = jenkinsStatus;
}

public String getJenkinsUserName() {
return jenkinsUserName;

Check warning on line 135 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 135 is not covered by tests
}

@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;

Check warning on line 153 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 153 is not covered by tests
}

@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;

Check warning on line 171 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 167-171 are not covered by tests
}

@DataBoundSetter
public void setBullseyeXmlResultPaths(String bullseyeXmlResultPaths) {
this.bullseyeXmlResultPaths = bullseyeXmlResultPaths;
}

public long getConfigUpdatedAt() {
return configUpdatedAt;
}

@DataBoundSetter
public void setConfigUpdatedAt(long configUpdatedAt) {
this.configUpdatedAt = configUpdatedAt;
}

Check warning on line 186 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 180-186 are not covered by tests

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;

Check warning on line 207 in src/main/java/io/jenkins/plugins/propelo/commons/models/PropeloJobReporterConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 205-207 are not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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();

Check warning on line 97 in src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/JenkinsHeartbeatAperiodicWork.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 46-97 are not covered by tests
}
LOGGER.log(Level.INFO, "Heartbeat duration is : " + this.plugin.getHeartbeatDuration() +
" || Heartbeat response : " + genericResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,14 +19,15 @@
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 {
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
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;
Expand Down Expand Up @@ -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;

Check warning on line 90 in src/main/java/io/jenkins/plugins/propelo/job_reporter/extensions/LevelOpsMgmtLink.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 74-90 are not covered by tests
}

public String getJenkinsStatus() {
Expand Down
Loading

0 comments on commit a07a88d

Please sign in to comment.