Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
[FIXED JENKINS-21569] Expose variable lineNumber also when validating…
… the script.
  • Loading branch information
uhafner committed Mar 7, 2014
1 parent a656458 commit 14b3ce92f80a51778b37e2eef44687897c14d562
Showing with 27 additions and 23 deletions.
  1. +6 −15 src/main/java/hudson/plugins/warnings/GroovyParser.java
  2. +21 −8 src/main/java/hudson/plugins/warnings/parser/GroovyExpressionMatcher.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;

@@ -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()));
@@ -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;
}
@@ -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.
*

0 comments on commit 14b3ce9

Please sign in to comment.