Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Global configuration was not persisted at all (in most cases).
  • Loading branch information
amandel committed May 1, 2019
1 parent 5dd13b8 commit 2f426e4
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 206 deletions.
157 changes: 62 additions & 95 deletions log-file-filter.iml

Large diffs are not rendered by default.

20 changes: 7 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.17</version>
<version>3.43</version>
</parent>

<groupId>com.tsystems.sbs</groupId>
<artifactId>log-file-filter</artifactId>
<version>1.11-SNAPSHOT</version>
<packaging>hpi</packaging>

<properties>
<!-- Baseline Jenkins version you use to build the plugin. Users must have
this version or newer to run. -->
<jenkins.version>2.60.3</jenkins.version>
<!-- Java Level to use. Java 7 required when using core >= 1.612 -->
<java.level>7</java.level>
<jenkins.version>2.107.3</jenkins.version>
<java.level>8</java.level>
<!-- Jenkins Test Harness version you use to test the plugin. -->
<!-- For Jenkins version >= 1.580.1 use JTH 2.x or higher. -->
<jenkins-test-harness.version>2.13</jenkins-test-harness.version>
<hpi-plugin.version>1.95</hpi-plugin.version>
<!-- Other properties you may want to use: ~ stapler-plugin.version: The
<!-- <jenkins-test-harness.version>2.13</jenkins-test-harness.version> -->
<!-- Other properties you may want to use: ~ stapler-plugin.version: The
Stapler Maven plugin version required by the plugin. -->
</properties>

Expand Down Expand Up @@ -49,9 +46,6 @@
<!-- If you want this to appear on the wiki page: <developers> <developer>
<id>bhacker</id> <name>Bob Q. Hacker</name> <email>[email protected]</email>
</developer> </developers> -->
<!-- Assuming you want to host on @jenkinsci: <scm> <connection>scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git</connection>
<developerConnection>scm:git:[email protected]:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
<url>http://github.com/jenkinsci/${project.artifactId}-plugin</url> </scm> -->
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
Expand Down Expand Up @@ -113,7 +107,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.9</version>
<version>2.14</version>
</dependency>
</dependencies>

Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.List;

/**
* The default regexes (which use is activated by the checkbox 'Default regexp') are defined in this class.
Expand All @@ -13,14 +12,14 @@
*/
public final class DefaultRegexpPairs {

private final static Set<RegexpPair> DEFAULT_REGEXES
= Collections.unmodifiableSet(
new LinkedHashSet<RegexpPair>(Arrays.asList(
private final static List<RegexpPair> DEFAULT_REGEXES
= Collections.<RegexpPair>unmodifiableList(
Arrays.<RegexpPair>asList(
new RegexpPair("(https?+://[^:\\s]++):[^@\\s]++@", "$1:********@"),//Passwd URL MASKING
new RegexpPair("password=\\S*", "password=********") //PASSWORD MASKING
)));
));

public static Set<RegexpPair> getDefaultRegexes() {
public static List<RegexpPair> getDefaultRegexes() {
return DEFAULT_REGEXES;
}
}
98 changes: 22 additions & 76 deletions src/main/java/com/tsystems/sbs/LogFileFilterConfig.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.tsystems.sbs;

import hudson.Extension;
import hudson.util.PersistedList;
import jenkins.model.GlobalConfiguration;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
* This class deals with the plugin configuration and persistence of the data.
*
Expand All @@ -27,24 +24,17 @@ public class LogFileFilterConfig extends GlobalConfiguration implements Serializ
private static final Logger LOGGER = Logger.getLogger(LogFileFilterConfig.class.getName());

public static LogFileFilterConfig get() {
LogFileFilterConfig config = null;
final LogFileFilterConfig config;
try {
config = GlobalConfiguration.all().get(LogFileFilterConfig.class);
} catch (IllegalStateException e) {
LOGGER.log(Level.SEVERE, "Config not found!");
LOGGER.log(Level.SEVERE, "Config not found! " + e);
throw e;
}
LOGGER.log(Level.INFO, "Found config.");
return config;
}


/**
* To persist global configuration information, simply store it in a field and call save().
*
* <p>
* If you don't want fields to be persisted, use <tt>transient</tt>.
*/
/**
* Determines whether the plugin is enabled globally for ALL BUILDS.
*/
Expand All @@ -58,84 +48,40 @@ public static LogFileFilterConfig get() {
/**
* Represents the custom regexp pairs specified by the user in the global settings.
*/
private final Set<RegexpPair> regexpPairs = new LinkedHashSet<RegexpPair>();

/**
* This human readable name is used in the configuration screen.
*/
@Override
public String getDisplayName() {
return "";
}
private List<RegexpPair> regexpPairs = new PersistedList<>(this);

@Override
public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws FormException {

//Clear the list, so it will be overwritten by a new one
regexpPairs.clear();

if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Reading configuration " + json.toString());
}

//enabledGlobally (determines whether or not to filter the console output using global parameters)
if (json.has("enabledGlobally")) {
enabledGlobally = json.getBoolean("enabledGlobally");
}

if (json.has("enabledDefaultRegexp")) {
enabledDefaultRegexp = json.getBoolean("enabledDefaultRegexp");
}

//The regexp pairs which determine which contents in the console will be filtered
if (json.has("Regexp Pairs")) {

//Get all submitted pairs
Object o = json.get("Regexp Pairs");

//If the regexpPairs from the request contains more than 1 element it is a JSONArray
if (o instanceof JSONArray) {
JSONArray regexpPairsJson = json.getJSONArray("Regexp Pairs");

for (int i = 0; i < regexpPairsJson.size(); i++) {
JSONObject regexpPair = regexpPairsJson.getJSONObject(i);
String regexp = regexpPair.getString("regexp");
String replacement = regexpPair.getString("replacement");

regexpPairs.add(new RegexpPair(regexp, replacement));
}
} else if (o instanceof JSONObject) {//If the regexpPairs from the request contains only 1 element it is a JSONObject
JSONObject regexpPairObj = json.getJSONObject("Regexp Pairs");

String regexp = regexpPairObj.getString("regexp");
String replacement = regexpPairObj.getString("replacement");

regexpPairs.add(new RegexpPair(regexp, replacement));
}
}

save();//Persist the changed config
return true; //Indicate that everything is good so far
public LogFileFilterConfig() {
load();
}

public boolean isEnabledGlobally() {
return enabledGlobally;
}

@DataBoundSetter
public void setEnabledGlobally(boolean enabledGlobally) {
this.enabledGlobally = enabledGlobally;
save();
}

public boolean isEnabledDefaultRegexp() {
return enabledDefaultRegexp;
}

@DataBoundSetter
public void setEnabledDefaultRegexp(boolean enabledDefaultRegexp) {
this.enabledDefaultRegexp = enabledDefaultRegexp;
save();
}

public Set<RegexpPair> getRegexpPairs() {
public List<RegexpPair> getRegexpPairs() {
return regexpPairs;
}

@DataBoundSetter
public void setRegexpPairs(List<RegexpPair> regexpPairs) {
this.regexpPairs = regexpPairs;
save();
}

}
19 changes: 9 additions & 10 deletions src/main/java/com/tsystems/sbs/LogFileFilterOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.tsystems.sbs;

import hudson.console.LineTransformationOutputStream;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Set;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;

import hudson.console.LineTransformationOutputStream;

/**
* This class deals with the actual filtering of the console using the configured regexes. Bear in mind that for now
* only one line expressions are possible.
Expand All @@ -30,8 +29,8 @@ public class LogFileFilterOutputStream extends LineTransformationOutputStream {
//Global settings
private final boolean isEnabledGlobally;
private final boolean isEnabledDefaultRegexp;
private final Set<RegexpPair> defaultRegexpPairs;
private final Set<RegexpPair> customRegexpPairs;
private final List<RegexpPair> defaultRegexpPairs;
private final List<RegexpPair> customRegexpPairs;
private final String jobName;


Expand All @@ -53,11 +52,11 @@ public LogFileFilterOutputStream(OutputStream out, Charset charset, String jobNa
if (isEnabledDefaultRegexp) {
defaultRegexpPairs = DefaultRegexpPairs.getDefaultRegexes();
} else {
defaultRegexpPairs = Collections.emptySet();
defaultRegexpPairs = Collections.<RegexpPair>emptyList();
}
} else {
customRegexpPairs = Collections.emptySet();
defaultRegexpPairs = Collections.emptySet();
customRegexpPairs = Collections.<RegexpPair>emptyList();
defaultRegexpPairs = Collections.<RegexpPair>emptyList();
}
}

Expand Down
24 changes: 22 additions & 2 deletions src/main/java/com/tsystems/sbs/RegexpPair.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.tsystems.sbs;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.Serializable;
import java.util.regex.Pattern;

Expand All @@ -10,13 +16,14 @@
* @author ccapdevi
*
*/
public class RegexpPair implements Serializable {
public class RegexpPair extends AbstractDescribableImpl<RegexpPair> implements Serializable{

private static final long serialVersionUID = -5445325528650992703L;
private String regexp;
private String replacement;
private Pattern compiledRegexp;
private transient Pattern compiledRegexp;

@DataBoundConstructor
public RegexpPair(String regexp, String replacement) {
this.regexp = regexp;
this.replacement = replacement;
Expand All @@ -27,6 +34,7 @@ public String getRegexp() {
return regexp;
}

@DataBoundSetter
public void setRegexp(String regexp) {
this.regexp = regexp;
this.compiledRegexp = Pattern.compile(this.regexp);
Expand All @@ -36,6 +44,7 @@ public String getReplacement() {
return replacement;
}

@DataBoundSetter
public void setReplacement(String replacement) {
this.replacement = replacement;
}
Expand All @@ -44,4 +53,15 @@ public Pattern getCompiledRegexp() {
return compiledRegexp;
}

@SuppressWarnings("unused")
protected Object readResolve() {
this.compiledRegexp = Pattern.compile(this.regexp);
return this;
}

@Extension
public static class DescriptorImpl extends Descriptor<RegexpPair> {
public String getDisplayName() { return "Regular expression and replacement string pair."; }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@

<!-- Defines the regexp patterns to filter the console logs -->
<f:entry title="Custom regexp pairs" field="regexpPairs">
<f:repeatable name="Regexp Pairs" items="${descriptor.regexpPairs}" var="regexpPair">
<f:repeatable name="Regexp Pairs" field="regexpPairs">
<table width="100%">
<tr>
<td width="10%" align="right">Regexp</td>
<td width="30%">
<f:textbox name="regexpPair.regexp" value="${!empty regexpPair.regexp?regexpPair.regexp:''}"/>
<f:textbox field="regexp" />
</td>

<td width="10%" align="right">Replacement</td>
<td width="30%">
<f:textbox name="regexpPair.replacement" value="${!empty regexpPair.replacement?regexpPair.replacement:''}"/>
<f:textbox field="replacement" />
</td>
<td width="20%" align="right"><f:repeatableDeleteButton/></td>
</tr>
Expand Down

0 comments on commit 2f426e4

Please sign in to comment.