Skip to content

Commit

Permalink
[JENKINS-55667] Add support for CasC (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dohbedoh committed Sep 13, 2022
1 parent 8261baa commit 1b4290d
Show file tree
Hide file tree
Showing 37 changed files with 363 additions and 180 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
126 changes: 99 additions & 27 deletions src/main/java/hudson/plugins/jobConfigHistory/JobConfigHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
package hudson.plugins.jobConfigHistory;

import hudson.Plugin;
import hudson.BulkChange;
import hudson.Extension;
import hudson.XmlFile;
import hudson.maven.MavenModule;
import hudson.model.AbstractProject;
import hudson.model.Descriptor.FormException;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.Saveable;
Expand All @@ -36,13 +36,18 @@
import hudson.security.PermissionGroup;
import hudson.security.PermissionScope;
import hudson.util.FormValidation;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
Expand All @@ -57,7 +62,9 @@
*
* @author John Borghi
*/
public class JobConfigHistory extends Plugin {
@Symbol("jobConfigHistory")
@Extension
public class JobConfigHistory extends GlobalConfiguration {

private static final PermissionGroup PERMISSION_GROUP = new PermissionGroup(JobConfigHistory.class, Messages._displayName());
protected static final Permission DELETEENTRY_PERMISSION =
Expand Down Expand Up @@ -99,7 +106,7 @@ public class JobConfigHistory extends Plugin {
/**
* Regular expression pattern for 'system' configuration files to exclude from saving.
*/
private String excludePattern;
private String excludePattern = JobConfigHistoryConsts.DEFAULT_EXCLUDE;
/**
* Compiled regular expression pattern.
*/
Expand All @@ -118,28 +125,27 @@ public class JobConfigHistory extends Plugin {
*/
private boolean showChangeReasonCommentWindow = true;

@Override
public void start() throws Exception {
@DataBoundConstructor
@Restricted(NoExternalUse.class)
public JobConfigHistory() {
load();
loadRegexpPatterns();
}

@Override
public void configure(StaplerRequest req, JSONObject formData)
throws IOException, ServletException, FormException {

historyRootDir = formData.getString("historyRootDir").trim();
setMaxHistoryEntries(formData.getString("maxHistoryEntries").trim());
setMaxDaysToKeepEntries(formData.getString("maxDaysToKeepEntries").trim());
setMaxEntriesPerPage(formData.getString("maxEntriesPerPage").trim());
skipDuplicateHistory = formData.getBoolean("skipDuplicateHistory");
excludePattern = formData.getString("excludePattern");
saveModuleConfiguration = formData.getBoolean("saveModuleConfiguration");
showBuildBadges = formData.getString("showBuildBadges");
excludedUsers = formData.getString("excludedUsers");
showChangeReasonCommentWindow = formData.getBoolean("showChangeReasonCommentWindow");
save();
loadRegexpPatterns();
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
try (BulkChange bc = new BulkChange(this)) {
req.bindJSON(this, formData);
bc.commit();
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to save " + getConfigFile(), e);
}
return true;
}

@Override
protected XmlFile getConfigFile() {
return new XmlFile(new File(Jenkins.get().getRootDir(),"jobConfigHistory.xml"));
}

/**
Expand All @@ -159,6 +165,17 @@ public String getDefaultRootDir() {
public String getHistoryRootDir() {
return historyRootDir;
}

/**
* Sets the root directory for storing histories.
*
* @param historyRootDir The root directory.
*/
@DataBoundSetter
public void setHistoryRootDir(String historyRootDir) {
this.historyRootDir = historyRootDir;
save();
}

/**
* Gets the maximum number of history entries to keep.
Expand All @@ -174,11 +191,13 @@ public String getMaxHistoryEntries() {
*
* @param maxEntryInput The maximum number of history entries to keep.
*/
@DataBoundSetter
public void setMaxHistoryEntries(String maxEntryInput) {
String trimmedValue = StringUtils.trimToNull(maxEntryInput);
if (trimmedValue == null || isPositiveInteger(trimmedValue)) {
maxHistoryEntries = trimmedValue;
}
save();
}

/**
Expand All @@ -195,11 +214,13 @@ public String getMaxEntriesPerPage() {
*
* @param maxEntryInput The maximum number of history entries to show per page.
*/
@DataBoundSetter
public void setMaxEntriesPerPage(String maxEntryInput) {
String trimmedValue = StringUtils.trimToNull(maxEntryInput);
if (trimmedValue == null || isPositiveInteger(trimmedValue)) {
maxEntriesPerPage = trimmedValue;
}
save();
}

/**
Expand All @@ -216,11 +237,13 @@ public String getMaxDaysToKeepEntries() {
*
* @param maxDaysInput The maximum number of days to keep history entries.
*/
@DataBoundSetter
public void setMaxDaysToKeepEntries(String maxDaysInput) {
String trimmedValue = StringUtils.trimToNull(maxDaysInput);
if (trimmedValue == null || isPositiveInteger(trimmedValue)) {
maxDaysToKeepEntries = trimmedValue;
}
save();
}

/**
Expand Down Expand Up @@ -261,13 +284,15 @@ public boolean getSkipDuplicateHistory() {
}

/**
* Used by the configuration page.
* Sets whether to skip saving history when it is a duplication of the previous saved configuration.
*
* @return The default regular expression for 'system' file names to exclude
* from saving.
* @param skipDuplicateHistory Whether to skip saving history when it is a duplication of the previous saved
* configuration.
*/
public String getDefaultExcludePattern() {
return JobConfigHistoryConsts.DEFAULT_EXCLUDE;
@DataBoundSetter
public void setSkipDuplicateHistory(boolean skipDuplicateHistory) {
this.skipDuplicateHistory = skipDuplicateHistory;
save();
}

/**
Expand All @@ -278,6 +303,18 @@ public String getDefaultExcludePattern() {
public String getExcludePattern() {
return excludePattern;
}

/**
* Sets the regular expression pattern for 'system' configuration files to exclude from saving.
*
* @param excludePattern The regular expression pattern.
*/
@DataBoundSetter
public void setExcludePattern(String excludePattern) {
this.excludePattern = excludePattern;
loadRegexpPatterns();
save();
}

/**
* Gets whether to save the config history of Maven modules.
Expand All @@ -288,6 +325,17 @@ public boolean getSaveModuleConfiguration() {
return saveModuleConfiguration;
}

/**
* Sets whether to save the config history of Maven modules.
*
* @param saveModuleConfiguration Whether to save the config history of Maven modules.
*/
@DataBoundSetter
public void setSaveModuleConfiguration(boolean saveModuleConfiguration) {
this.saveModuleConfiguration = saveModuleConfiguration;
save();
}

/**
* Gets whether build badges should appear when the config of a job has changed since the last build.
*
Expand All @@ -302,8 +350,10 @@ public String getShowBuildBadges() {
*
* @param showBuildBadges Possible values: "never", "always", "userWithConfigPermission", "adminUser"
*/
@DataBoundSetter
public void setShowBuildBadges(String showBuildBadges) {
this.showBuildBadges = showBuildBadges;
save();
}

/**
Expand All @@ -314,6 +364,17 @@ public void setShowBuildBadges(String showBuildBadges) {
public boolean getShowChangeReasonCommentWindow() {
return showChangeReasonCommentWindow;
}

/**
* Sets whether a change reason comment window should be shown on a jobs' configure page.
*
* @param showChangeReasonCommentWindow Whether a comment window should be shown.
*/
@DataBoundSetter
public void setShowChangeReasonCommentWindow(boolean showChangeReasonCommentWindow) {
this.showChangeReasonCommentWindow = showChangeReasonCommentWindow;
save();
}

/**
* Whether build badges should appear for the builds of this project.
Expand Down Expand Up @@ -409,6 +470,17 @@ public File getConfigFile(File historyDir) {
public String getExcludedUsers() {
return excludedUsers;
}

/**
* Sets usernames whose changes should not get detected.
*
* @param excludedUsers Comma separated list of usernames.
*/
@DataBoundSetter
public void setExcludedUsers(String excludedUsers) {
this.excludedUsers = excludedUsers;
save();
}

/**
* Returns true if configuration for this item should be saved, based on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import hudson.Plugin;
import hudson.model.User;
import hudson.security.ACL;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;

import java.io.File;
Expand Down Expand Up @@ -54,7 +55,7 @@ private PluginUtils() {
* @return plugin
*/
public static JobConfigHistory getPlugin() {
return Jenkins.get().getPlugin(JobConfigHistory.class);
return GlobalConfiguration.all().get(JobConfigHistory.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,45 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:section title="${%Job Config History}">
<f:entry title="${%Use different history directory than default}:" help="/plugin/jobConfigHistory/help/help-historyRootDir.html">
<f:textbox value="${it.historyRootDir}" name="historyRootDir"/>
<f:entry title="${%Use different history directory than default}" field="historyRootDir">
<f:textbox/>
</f:entry>
<f:advanced>
<f:entry title="${%Max number of history entries to keep}" help="/plugin/jobConfigHistory/help/help-maxHistoryEntries.html">
<f:textbox name="maxHistoryEntries" value="${it.maxHistoryEntries}"
checkUrl="'${rootURL}/plugin/jobConfigHistory/checkMaxHistoryEntries?value='+escape(this.value)"/>
</f:entry>
<f:entry title="${%Max number of days to keep history entries}" help="/plugin/jobConfigHistory/help/help-maxDaysToKeepEntries.html">
<f:textbox name="maxDaysToKeepEntries" value="${it.maxDaysToKeepEntries}"
checkUrl="'${rootURL}/plugin/jobConfigHistory/checkMaxDaysToKeepEntries?value='+escape(this.value)"/>
</f:entry>
<f:entry title="${%Max number of history entries to show per page}" help="/plugin/jobConfigHistory/help/help-maxEntriesPerPage.html">
<f:textbox name="maxEntriesPerPage" value="${it.maxEntriesPerPage}"
checkUrl="'${rootURL}/plugin/jobConfigHistory/checkMaxEntriesPerPage?value='+escape(this.value)"/>
</f:entry>
<f:entry title="${%Configuration exclude file pattern}" help="/plugin/jobConfigHistory/help/help-excludePattern.html">
<f:textbox name="excludePattern" value="${it.excludePattern}" default="${it.defaultExcludePattern}"
checkUrl="'${rootURL}/plugin/jobConfigHistory/checkExcludePattern?value='+escape(this.value)"/>
</f:entry>
<f:entry help="/plugin/jobConfigHistory/help/help-skipDuplicateHistory.html">
<f:checkbox name="skipDuplicateHistory"
title="${%Do not save duplicate history}"
checked="${it.skipDuplicateHistory}"/>
</f:entry>
<f:entry help="/plugin/jobConfigHistory/help/help-saveModuleConfiguration.html">
<f:checkbox
title="${%Save Maven module configuration changes}"
name="saveModuleConfiguration"
checked="${it.saveModuleConfiguration}"/>
</f:entry>
<f:entry title="${%Show build badges}" help="/plugin/jobConfigHistory/help/help-showBuildBadges.html">
<f:radio name="showBuildBadges" title="${%Never}" value="never" checked="${it.getShowBuildBadges() == 'never'}"/>
<br />
<f:radio name="showBuildBadges" title="${%Always}" value="always" checked="${it.getShowBuildBadges() == 'always'}"/>
<br />
<f:radio name="showBuildBadges" title="${%Only for users with configuration permission}" value="userWithConfigPermission" checked="${it.getShowBuildBadges() == 'userWithConfigPermission'}"/>
<br />
<f:radio name="showBuildBadges" title="${%Only for administrators}" value="adminUser" checked="${it.getShowBuildBadges() == 'adminUser'}"/>
</f:entry>
<f:entry title="${%Exclude users}" help="/plugin/jobConfigHistory/help/help-excludedUsers.html">
<f:textbox name="excludedUsers" value="${it.excludedUsers}" />
</f:entry>
<f:entry title="${%Show change message window}" help="/plugin/jobConfigHistory/help/help-showChangeReasonCommentWindow.html">
<f:checkbox
title="${%Show the change message window in jobs' configure pages.}"
name="showChangeReasonCommentWindow"
checked="${it.showChangeReasonCommentWindow}"/>
<f:entry title="${%Max number of history entries to keep}" field="maxHistoryEntries">
<f:textbox/>
</f:entry>
<f:entry title="${%Max number of days to keep history entries}" field="maxDaysToKeepEntries">
<f:textbox/>
</f:entry>
<f:entry title="${%Max number of history entries to show per page}" field="maxEntriesPerPage">
<f:textbox/>
</f:entry>
<f:entry title="${%Configuration exclude file pattern}" field="excludePattern">
<f:textbox/>
</f:entry>
<f:entry title="${%Do not save duplicate history}" field="skipDuplicateHistory">
<f:checkbox/>
</f:entry>
<f:entry title="${%Save Maven module configuration changes}" field="saveModuleConfiguration">
<f:checkbox/>
</f:entry>
<f:entry title="${%Show build badges}" field="showBuildBadges" >
<f:radio name="showBuildBadges" title="${%Never}" value="never"
checked="${descriptor.showBuildBadges == 'never'}"/>
<f:radio name="showBuildBadges" title="${%Always}" value="always"
checked="${descriptor.showBuildBadges == 'always'}"/>
<f:radio name="showBuildBadges" title="${%Only for users with configuration permission}"
value="userWithConfigPermission"
checked="${descriptor.showBuildBadges == 'userWithConfigPermission'}"/>
<f:radio name="showBuildBadges" title="${%Only for administrators}" value="adminUser"
checked="${descriptor.showBuildBadges == 'adminUser'}"/>
</f:entry>
<f:entry title="${%Exclude users}" field="excludedUsers">
<f:textbox/>
</f:entry>
<f:entry title="${%Show the change message window in jobs' configure pages}"
field="showChangeReasonCommentWindow">
<f:checkbox/>
</f:entry>
</f:advanced>
</f:section>
Expand Down

0 comments on commit 1b4290d

Please sign in to comment.