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

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-12307] Added describables for all parser and pattern selection.
  • Loading branch information
uhafner committed May 30, 2012
1 parent 0c94865 commit ef880b8
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 204 deletions.
92 changes: 92 additions & 0 deletions src/main/java/hudson/plugins/warnings/ConsoleParser.java
@@ -0,0 +1,92 @@
package hudson.plugins.warnings;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.plugins.warnings.parser.ParserDescription;
import hudson.plugins.warnings.parser.ParserRegistry;
import hudson.util.ListBoxModel;

import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

import com.google.common.collect.Lists;

/**
* Defines the configuration to parse a set of files using a predefined parser.
*
* @author Ulli Hafner
*/
public class ConsoleParser extends AbstractDescribableImpl<ConsoleParser> {
private final String parserName;

/**
* Creates a new instance of {@link ConsoleParser}.
*
* @param parserName
* the name of the parser to use
*/
@DataBoundConstructor
public ConsoleParser(final String parserName) {
super();

this.parserName = parserName;
}

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

/**
* Removes non-existing parsers from the specified list.
*
* @param parsers
* the parsers
* @return a new list containing the filtered parsers
*/
public static ConsoleParser[] filterExisting(final Collection<? extends ConsoleParser> parsers) {
List<ConsoleParser> existing = Lists.newArrayList();
for (ConsoleParser parser : parsers) {
if (ParserRegistry.exists(parser.getParserName())) {
existing.add(parser);
}
}
return existing.toArray(new ConsoleParser[existing.size()]);
}

/**
* Dummy descriptor for {@link ConsoleParser}.
*
* @author Ulli Hafner
*/
@Extension
public static class DescriptorImpl extends Descriptor<ConsoleParser> {
/**
* Returns the available parsers. These values will be shown in the list
* box of the config.jelly view part.
*
* @return the model of the list box
*/
public ListBoxModel doFillParserNameItems() {
ListBoxModel items = new ListBoxModel();
for (ParserDescription parser : ParserRegistry.getAvailableParsers()) {
items.add(parser.getGroup());
}
return items;
}

@Override
public String getDisplayName() {
return StringUtils.EMPTY;
}
}

}
62 changes: 62 additions & 0 deletions src/main/java/hudson/plugins/warnings/ParserConfiguration.java
@@ -1,11 +1,24 @@
package hudson.plugins.warnings;

import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractDescribableImpl;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.plugins.warnings.parser.ParserDescription;
import hudson.plugins.warnings.parser.ParserRegistry;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

import java.io.IOException;
import java.util.List;

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

import com.google.common.collect.Lists;

/**
* Defines the configuration to parse a set of files using a predefined parser.
Expand All @@ -26,6 +39,8 @@ public class ParserConfiguration extends AbstractDescribableImpl<ParserConfigura
*/
@DataBoundConstructor
public ParserConfiguration(final String pattern, final String parserName) {
super();

this.pattern = pattern;
this.parserName = parserName;
}
Expand All @@ -48,17 +63,64 @@ public String getPattern() {
return pattern;
}

@Override
public String toString() {
return String.format("%s (%s)", parserName, pattern);
}

/**
* Removes non-existing parsers from the specified collection of parsers.
*
* @param parsers
* the parsers
* @return a new list containing the filtered parsers
*/
public static ParserConfiguration[] filterExisting(final List<? extends ParserConfiguration> parsers) {
List<ParserConfiguration> existing = Lists.newArrayList();
for (ParserConfiguration parser : parsers) {
if (ParserRegistry.exists(parser.getParserName())) {
existing.add(parser);
}
}
return existing.toArray(new ParserConfiguration[existing.size()]);
}

/**
* Dummy descriptor for {@link ParserConfiguration}.
*
* @author Ulli Hafner
*/
@Extension
public static class DescriptorImpl extends Descriptor<ParserConfiguration> {
/**
* Returns the available parsers. These values will be shown in the list
* box of the config.jelly view part.
*
* @return the model of the list box
*/
public ListBoxModel doFillParserNameItems() {
ListBoxModel items = new ListBoxModel();
for (ParserDescription parser : ParserRegistry.getAvailableParsers()) {
items.add(parser.getGroup());
}
return items;
}

@Override
public String getDisplayName() {
return StringUtils.EMPTY;
}

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

51 changes: 0 additions & 51 deletions src/main/java/hudson/plugins/warnings/WarningsDescriptor.java
Expand Up @@ -12,11 +12,7 @@
import hudson.util.FormValidation;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kohsuke.stapler.Ancestor;
Expand All @@ -34,10 +30,6 @@
*/
@Extension(ordinal = 100) // NOCHECKSTYLE
public final class WarningsDescriptor extends PluginDescriptor implements StaplerProxy {
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";

/** The ID of this plug-in is used as URL. */
static final String PLUGIN_ID = "warnings";
/** The URL of the result action. */
Expand Down Expand Up @@ -145,55 +137,12 @@ public String getIconUrl() {
return SMALL_ICON_URL;
}

/** {@inheritDoc} */
@SuppressWarnings("rawtypes")
@Override
public boolean isApplicable(final Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public WarningsPublisher newInstance(final StaplerRequest request, final JSONObject formData) throws FormException {
JSONObject flattenedData = convertHierarchicalFormData(formData);
Set<String> consoleLogParsers = extractConsoleLogParsers(flattenedData);
List<ParserConfiguration> parserConfigurations = request.bindJSONToList(ParserConfiguration.class, flattenedData.get(FILE_LOCATIONS_KEY));

WarningsPublisher publisher = request.bindJSON(WarningsPublisher.class, flattenedData);
publisher.setConsoleLogParsers(consoleLogParsers);
publisher.setParserConfigurations(parserConfigurations);

return publisher;
}

/**
* 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> extractConsoleLogParsers(final JSONObject formData) {
Object values = formData.get(CONSOLE_LOG_PARSERS_KEY);
Set<String> parsers = new HashSet<String>();
if (values instanceof JSONArray) {
JSONArray array = (JSONArray)values;
for (int i = 0; i < array.size(); i++) {
add(parsers, array.getJSONObject(i));
}
formData.remove(CONSOLE_LOG_PARSERS_KEY);
}
else if (values instanceof JSONObject) {
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 Down

0 comments on commit ef880b8

Please sign in to comment.