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
[FIXED JENKINS-21569] Expose variable lineNumber also when validating…
… the script.
  • Loading branch information
uhafner committed Mar 7, 2014
1 parent a656458 commit 14b3ce9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
21 changes: 6 additions & 15 deletions src/main/java/hudson/plugins/warnings/GroovyParser.java
@@ -1,29 +1,22 @@
package hudson.plugins.warnings;

import javax.annotation.CheckForNull;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.annotation.CheckForNull;

import org.apache.commons.lang.StringUtils;
import org.codehaus.groovy.control.CompilationFailedException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import hudson.Extension;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;

import hudson.plugins.warnings.parser.AbstractWarningsParser;
import hudson.plugins.warnings.parser.DynamicDocumentParser;
import hudson.plugins.warnings.parser.DynamicParser;
import hudson.plugins.warnings.parser.Warning;

import hudson.plugins.warnings.parser.*;
import hudson.util.FormValidation;
import hudson.util.FormValidation.Kind;

Expand Down Expand Up @@ -356,20 +349,18 @@ private FormValidation parseExample(final String script, final String example, f
}
Matcher matcher = pattern.matcher(example);
if (matcher.find()) {
Binding binding = new Binding();
binding.setVariable("matcher", matcher);
GroovyShell shell = new GroovyShell(WarningsDescriptor.class.getClassLoader(), binding);
GroovyExpressionMatcher checker = new GroovyExpressionMatcher(script, null);
Object result = null;
try {
result = shell.evaluate(script);
result = checker.run(matcher, 0);
}
catch (Exception exception) { // NOCHECKSTYLE: catch all exceptions of the Groovy script
return FormValidation.error(
Messages.Warnings_GroovyParser_Error_Example_exception(exception.getMessage()));
}
if (result instanceof Warning) {
StringBuilder okMessage = new StringBuilder(Messages.Warnings_GroovyParser_Error_Example_ok_title());
Warning warning = (Warning)result;
Warning warning = (Warning) result;
message(okMessage, Messages.Warnings_GroovyParser_Error_Example_ok_file(warning.getFileName()));
message(okMessage, Messages.Warnings_GroovyParser_Error_Example_ok_line(warning.getPrimaryLineNumber()));
message(okMessage, Messages.Warnings_GroovyParser_Error_Example_ok_priority(warning.getPriority().getLongLocalizedString()));
Expand Down
Expand Up @@ -64,15 +64,8 @@ private void compileScriptIfNotYetDone() {
* @return a new annotation for the specified pattern
*/
public Warning createWarning(final Matcher matcher, final int lineNumber) {
compileScriptIfNotYetDone();

Binding binding = new Binding();
binding.setVariable("matcher", matcher);
binding.setVariable("lineNumber", lineNumber);
Object result = null;
try {
compiled.setBinding(binding);
result = compiled.run();
Object result = run(matcher, lineNumber);
if (result instanceof Warning) {
return (Warning)result;
}
Expand All @@ -83,6 +76,26 @@ public Warning createWarning(final Matcher matcher, final int lineNumber) {
return falsePositive;
}

/**
* Runs the groovy script. No exceptions are caught.
*
* @param matcher
* the regular expression matcher
* @param lineNumber
* the current line number
* @return unchecked result of the script
*/
public Object run(final Matcher matcher, final int lineNumber) {
compileScriptIfNotYetDone();

Binding binding = new Binding();
binding.setVariable("matcher", matcher);
binding.setVariable("lineNumber", lineNumber);

compiled.setBinding(binding);
return compiled.run();
}

/**
* Creates a new annotation for the specified match.
*
Expand Down

0 comments on commit 14b3ce9

Please sign in to comment.