Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
[FIXED JENKINS-8833] Allow configuration of parsers for each file.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Jul 6, 2011
1 parent db0ac73 commit 585be41
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 108 deletions.
3 changes: 2 additions & 1 deletion go.sh
@@ -1,6 +1,7 @@
rm -rf $HUDSON_HOME/plugins/warnings*

mvn install
mvn install || { echo "Build failed"; exit 1; }

cp -f target/*.hpi $HUDSON_HOME/plugins/

cd $HUDSON_HOME
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>analysis-core</artifactId>
<version>1.23</version>
<version>1.24-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/hudson/plugins/warnings/ParserConfiguration.java
@@ -0,0 +1,46 @@
package hudson.plugins.warnings;

import org.kohsuke.stapler.DataBoundConstructor;

/**
* Defines the configuration to parse a set of files using a predefined parser.
*
* @author Ulli Hafner
*/
public class ParserConfiguration {
private final String pattern;
private final String parserName;

/**
* Creates a new instance of {@link ParserConfiguration}.
*
* @param pattern
* the pattern of files to parse
* @param parserName
* the name of the parser to use
*/
@DataBoundConstructor
public ParserConfiguration(final String pattern, final String parserName) {
this.pattern = pattern;
this.parserName = parserName;
}

/**
* Returns the name of the parser.
*
* @return the parser name
*/
public String getParserName() {
return parserName;
}

/**
* Returns the file pattern.
*
* @return the pattern
*/
public String getPattern() {
return pattern;
}
}

45 changes: 34 additions & 11 deletions src/main/java/hudson/plugins/warnings/WarningsDescriptor.java
Expand Up @@ -3,14 +3,17 @@
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractProject;
import hudson.plugins.analysis.core.PluginDescriptor;
import hudson.plugins.warnings.parser.Warning;
import hudson.util.CopyOnWriteList;
import hudson.util.FormValidation;
import hudson.util.FormValidation.Kind;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -19,6 +22,7 @@
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -30,6 +34,9 @@
*/
@Extension(ordinal = 100) // NOCHECKSTYLE
public final class WarningsDescriptor extends PluginDescriptor {
private static final String CONSOLE_LOG_PARSERS_KEY = "consoleLogParsers";
private static final String FILE_LOCATIONS_KEY = "locations";
private static final String PARSER_NAME_ATTRIBUTE = "parserName";
private static final String PLUGIN_NAME = "warnings";
private static final String NEWLINE = "\n";
private static final int MAX_MESSAGE_LENGTH = 60;
Expand Down Expand Up @@ -89,41 +96,45 @@ public boolean isApplicable(final Class<? extends AbstractProject> jobType) {
/** {@inheritDoc} */
@Override
public WarningsPublisher newInstance(final StaplerRequest request, final JSONObject formData) throws FormException {
Set<String> parsers = extractParsers(formData);
Set<String> consoleLogParsers = extractConsoleLogParsers(formData);
List<ParserConfiguration> parserConfigurations = request.bindJSONToList(ParserConfiguration.class, formData.get(FILE_LOCATIONS_KEY));

WarningsPublisher publisher = request.bindJSON(WarningsPublisher.class, formData);
publisher.setParserNames(parsers);
publisher.setConsoleLogParsers(consoleLogParsers);
publisher.setParserConfigurations(parserConfigurations);

return publisher;
}

/**
* Extract the list of parsers to use from the JSON form data.
* Extract the list of locations and associated parsers from the JSON form data.
*
* @param formData
* the JSON form data
* @return the list of parsers to use
*/
private Set<String> extractParsers(final JSONObject formData) {
private Set<String> extractConsoleLogParsers(final JSONObject formData) {
Object values = formData.get(CONSOLE_LOG_PARSERS_KEY);
Set<String> parsers = new HashSet<String>();
Object values = formData.get("parsers");
if (values instanceof JSONArray) {
JSONArray array = (JSONArray)values;
for (int i = 0; i < array.size(); i++) {
JSONObject element = array.getJSONObject(i);
parsers.add(element.getString("parserName"));
add(parsers, array.getJSONObject(i));
}
formData.remove("parsers");
formData.remove(CONSOLE_LOG_PARSERS_KEY);
}
else if (values instanceof JSONObject) {
JSONObject object = (JSONObject)values;
parsers.add(object.getString("parserName"));
formData.remove("parsers");
add(parsers, (JSONObject)values);
formData.remove(CONSOLE_LOG_PARSERS_KEY);
}

return parsers;
}

private boolean add(final Set<String> parsers, final JSONObject element) {
return parsers.add(element.getString(PARSER_NAME_ATTRIBUTE));
}

/**
* Returns the configured Groovy parsers.
*
Expand All @@ -142,6 +153,18 @@ public boolean configure(final StaplerRequest req, final JSONObject formData) {
return true;
}

@Override
public FormValidation doCheckPattern(@AncestorInPath final AbstractProject<?, ?> project,
@QueryParameter final String pattern) throws IOException {
FormValidation required = FormValidation.validateRequired(pattern);
if (required.kind == FormValidation.Kind.OK) {
return FilePath.validateFileMask(project.getSomeWorkspace(), pattern);
}
else {
return required;
}
}

/**
* Performs on-the-fly validation on the name of the parser that needs to be unique.
*
Expand Down

0 comments on commit 585be41

Please sign in to comment.