From 92b29a00dc2a2df16399cea70834ca0d49ef0029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20F=C3=BCrbacher?= Date: Thu, 30 Nov 2017 15:41:21 +0100 Subject: [PATCH 1/5] Make plugin pipeline compatible. --- pom.xml | 2 +- .../TextFinderPipelinePublisher.java | 78 +++++ .../textfinder/TextFinderPublisher.java | 299 ++++++++++-------- .../TextFinderPipelinePublisher/config.jelly | 11 + .../config_ja.properties | 26 ++ .../help-regexp.html | 3 + .../help-regexp_ja.html | 3 + .../help-succeedIfFound.html | 3 + .../help-succeedIfFound_ja.html | 3 + .../help-unstableIfFound.html | 3 + .../help-unstableIfFound_ja.html | 3 + 11 files changed, 293 insertions(+), 141 deletions(-) create mode 100644 src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java create mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config.jelly create mode 100755 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config_ja.properties create mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp.html create mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp_ja.html create mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound.html create mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound_ja.html create mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound.html create mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound_ja.html diff --git a/pom.xml b/pom.xml index 44b4a7f..24df80b 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 1.480 + 1.577 text-finder diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java new file mode 100644 index 0000000..641f015 --- /dev/null +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java @@ -0,0 +1,78 @@ +package hudson.plugins.textfinder; + +import static hudson.Util.fixEmpty; + +import java.io.IOException; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +import javax.servlet.ServletException; + +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.QueryParameter; + +import hudson.Extension; +import hudson.FilePath; +import hudson.Launcher; +import hudson.model.AbstractProject; +import hudson.model.Run; +import hudson.model.TaskListener; +import hudson.tasks.BuildStepDescriptor; +import hudson.tasks.Publisher; +import hudson.util.FormValidation; +import jenkins.tasks.SimpleBuildStep; + +/** + * Text Finder plugin for Jenkins. Search in the workspace using a regular + * expression and determine build outcome based on matches. + * + * @author Santiago.PericasGeertsen@sun.com + */ +public class TextFinderPipelinePublisher extends TextFinderPublisher implements SimpleBuildStep { + + @DataBoundConstructor + public TextFinderPipelinePublisher(String regexp, + boolean succeedIfFound, boolean unstableIfFound) { + super(null, regexp, succeedIfFound, unstableIfFound, true); + } + + @Override + public void perform(Run run, FilePath workspace, Launcher launcher, + TaskListener listener) throws InterruptedException, IOException { + findText(run, listener.getLogger()); + } + + @Extension + public static final class DescriptorImpl extends BuildStepDescriptor { + public String getDisplayName() { + return Messages.DisplayName(); + } + + @Override + public String getHelpFile() { + return "/plugin/text-finder/help.html"; + } + + public boolean isApplicable(Class jobType) { + return true; + } + + /** + * Checks the regular expression validity. + */ + public FormValidation doCheckRegexp(@QueryParameter String value) throws IOException, ServletException { + value = fixEmpty(value); + if(value==null) + return FormValidation.ok(); // not entered yet + + try { + Pattern.compile(value); + return FormValidation.ok(); + } catch (PatternSyntaxException e) { + return FormValidation.error(e.getMessage()); + } + } + } + +} + diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 9d0f0a4..128c68d 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -1,14 +1,34 @@ package hudson.plugins.textfinder; +import static hudson.Util.fixEmpty; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintStream; +import java.io.Serializable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +import javax.servlet.ServletException; + +import org.apache.commons.io.IOUtils; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.types.FileSet; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.QueryParameter; + +import hudson.Extension; import hudson.FilePath.FileCallable; import hudson.Launcher; import hudson.Util; -import hudson.Extension; -import static hudson.Util.fixEmpty; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.BuildListener; import hudson.model.Result; +import hudson.model.Run; import hudson.remoting.RemoteOutputStream; import hudson.remoting.VirtualChannel; import hudson.tasks.BuildStepDescriptor; @@ -16,22 +36,6 @@ import hudson.tasks.Publisher; import hudson.tasks.Recorder; import hudson.util.FormValidation; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.types.FileSet; -import org.apache.commons.io.IOUtils; -import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.QueryParameter; - -import javax.servlet.ServletException; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.PrintStream; -import java.io.Serializable; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; /** * Text Finder plugin for Jenkins. Search in the workspace using a regular @@ -52,7 +56,7 @@ public class TextFinderPublisher extends Recorder implements Serializable { @DataBoundConstructor public TextFinderPublisher(String fileSet, String regexp, boolean succeedIfFound, boolean unstableIfFound, boolean alsoCheckConsoleOutput) { - this.fileSet = Util.fixEmpty(fileSet.trim()); + this.fileSet = fileSet != null ? Util.fixEmpty(fileSet.trim()) : null; this.regexp = regexp; this.succeedIfFound = succeedIfFound; this.unstableIfFound = unstableIfFound; @@ -71,135 +75,150 @@ public BuildStepMonitor getRequiredMonitorService() { } public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { - findText(build, listener.getLogger()); + findText(build, listener.getLogger()); return true; } + + /** + * Indicates an orderly abortion of the processing. + */ + private static final class AbortException extends RuntimeException { + } + + protected void findText(Run run, PrintStream logger) + throws IOException, InterruptedException { + try { + boolean foundText = false; - /** - * Indicates an orderly abortion of the processing. - */ - private static final class AbortException extends RuntimeException { - } + if (alsoCheckConsoleOutput) { + logger.println("Checking console output"); + foundText |= checkFile(run.getLogFile(), compilePattern(logger), + logger, true); + } else { + // printing this when checking console output will cause the + // plugin + // to find this line, which would be pointless. + // doing this only when fileSet!=null to avoid + logger.println("Checking " + regexp); + } - private void findText(AbstractBuild build, PrintStream logger) throws IOException, InterruptedException { - try { - boolean foundText = false; - - if(alsoCheckConsoleOutput) { - logger.println("Checking console output"); - foundText |= checkFile(build.getLogFile(), compilePattern(logger), logger, true); - } else { - // printing this when checking console output will cause the plugin - // to find this line, which would be pointless. - // doing this only when fileSet!=null to avoid - logger.println("Checking " + regexp); - } + final RemoteOutputStream ros = new RemoteOutputStream(logger); - final RemoteOutputStream ros = new RemoteOutputStream(logger); - - if(fileSet!=null) { - foundText |= build.getWorkspace().act(new FileCallable() { - public Boolean invoke(File ws, VirtualChannel channel) throws IOException { - PrintStream logger = new PrintStream(ros); - - // Collect list of files for searching - FileSet fs = new FileSet(); - org.apache.tools.ant.Project p = new org.apache.tools.ant.Project(); - fs.setProject(p); - fs.setDir(ws); - fs.setIncludes(fileSet); - DirectoryScanner ds = fs.getDirectoryScanner(p); - - // Any files in the final set? - String[] files = ds.getIncludedFiles(); - if (files.length == 0) { - logger.println("Jenkins Text Finder: File set '" + - fileSet + "' is empty"); - throw new AbortException(); - } - - Pattern pattern = compilePattern(logger); - - boolean foundText = false; - - for (String file : files) { - File f = new File(ws, file); - - if (!f.exists()) { - logger.println("Jenkins Text Finder: Unable to" + - " find file '" + f + "'"); - continue; - } - if (!f.canRead()) { - logger.println("Jenkins Text Finder: Unable to" + - " read from file '" + f + "'"); - continue; - } - - foundText |= checkFile(f, pattern, logger, false); - } - - return foundText; - } - }); - } + if (fileSet != null && run instanceof AbstractBuild) { + AbstractBuild build = (AbstractBuild) run; + foundText |= build.getWorkspace() + .act(new FileCallable() { + public Boolean invoke(File ws, + VirtualChannel channel) throws IOException { + PrintStream logger = new PrintStream(ros); - if (foundText != succeedIfFound) - build.setResult(unstableIfFound ? Result.UNSTABLE : Result.FAILURE); - } catch (AbortException e) { - // no test file found - build.setResult(Result.UNSTABLE); - } - } + // Collect list of files for searching + FileSet fs = new FileSet(); + org.apache.tools.ant.Project p = new org.apache.tools.ant.Project(); + fs.setProject(p); + fs.setDir(ws); + fs.setIncludes(fileSet); + DirectoryScanner ds = fs.getDirectoryScanner(p); - /** - * Search the given regexp pattern in the file. - * - * @param abortAfterFirstHit - * true to return immediately as soon as the first hit is found. this is necessary - * when we are scanning the console output, because otherwise we'll loop forever. - */ - private boolean checkFile(File f, Pattern pattern, PrintStream logger, boolean abortAfterFirstHit) { - boolean logFilename = true; - boolean foundText = false; - BufferedReader reader=null; - try { - // Assume default encoding and text files - String line; - reader = new BufferedReader(new FileReader(f)); - while ((line = reader.readLine()) != null) { - Matcher matcher = pattern.matcher(line); - if (matcher.find()) { - if (logFilename) {// first occurrence - logger.println(f + ":"); - logFilename = false; - } - logger.println(line); - foundText = true; - if(abortAfterFirstHit) - return true; - } - } - } catch (IOException e) { - logger.println("Jenkins Text Finder: Error reading" + - " file '" + f + "' -- ignoring"); - } finally { - IOUtils.closeQuietly(reader); - } - return foundText; - } + // Any files in the final set? + String[] files = ds.getIncludedFiles(); + if (files.length == 0) { + logger.println( + "Jenkins Text Finder: File set '" + + fileSet + "' is empty"); + throw new AbortException(); + } - private Pattern compilePattern(PrintStream logger) { - Pattern pattern; - try { - pattern = Pattern.compile(regexp); - } catch (PatternSyntaxException e) { - logger.println("Jenkins Text Finder: Unable to compile" - + "regular expression '" + regexp + "'"); - throw new AbortException(); - } - return pattern; - } + Pattern pattern = compilePattern(logger); + + boolean foundText = false; + + for (String file : files) { + File f = new File(ws, file); + + if (!f.exists()) { + logger.println( + "Jenkins Text Finder: Unable to" + + " find file '" + f + + "'"); + continue; + } + if (!f.canRead()) { + logger.println( + "Jenkins Text Finder: Unable to" + + " read from file '" + + f + "'"); + continue; + } + + foundText |= checkFile(f, pattern, logger, + false); + } + + return foundText; + } + }); + } + + if (foundText != succeedIfFound) + run.setResult( + unstableIfFound ? Result.UNSTABLE : Result.FAILURE); + } catch (AbortException e) { + // no test file found + run.setResult(Result.UNSTABLE); + } + } + + /** + * Search the given regexp pattern in the file. + * + * @param abortAfterFirstHit + * true to return immediately as soon as the first hit is found. + * this is necessary when we are scanning the console output, + * because otherwise we'll loop forever. + */ + private boolean checkFile(File f, Pattern pattern, PrintStream logger, + boolean abortAfterFirstHit) { + boolean logFilename = true; + boolean foundText = false; + BufferedReader reader = null; + try { + // Assume default encoding and text files + String line; + reader = new BufferedReader(new FileReader(f)); + while ((line = reader.readLine()) != null) { + Matcher matcher = pattern.matcher(line); + if (matcher.find()) { + if (logFilename) {// first occurrence + logger.println(f + ":"); + logFilename = false; + } + logger.println(line); + foundText = true; + if (abortAfterFirstHit) + return true; + } + } + } catch (IOException e) { + logger.println("Jenkins Text Finder: Error reading" + " file '" + f + + "' -- ignoring"); + } finally { + IOUtils.closeQuietly(reader); + } + return foundText; + } + + private Pattern compilePattern(PrintStream logger) { + Pattern pattern; + try { + pattern = Pattern.compile(regexp); + } catch (PatternSyntaxException e) { + logger.println("Jenkins Text Finder: Unable to compile" + + "regular expression '" + regexp + "'"); + throw new AbortException(); + } + return pattern; + } @Extension public static final class DescriptorImpl extends BuildStepDescriptor { diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config.jelly b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config.jelly new file mode 100644 index 0000000..5dfb92f --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config.jelly @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config_ja.properties b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config_ja.properties new file mode 100755 index 0000000..40440bb --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config_ja.properties @@ -0,0 +1,26 @@ +# The MIT License +# +# Copyright (c) 2004-2010, Sun Microsystems, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +Regular\ expression=\u6b63\u898f\u8868\u73fe +Succeed\ if\ found=\u691c\u51fa\u6642\u306b\u6210\u529f\u6271\u3044\u306b\u3059\u308b +Unstable\ if\ found=\u691c\u51fa\u6642\u306b\u4e0d\u5b89\u5b9a\u6271\u3044\u306b\u3059\u308b +Also\ search\ the\ console\ output=\u30b3\u30f3\u30bd\u30fc\u30eb\u51fa\u529b\u3082\u691c\u7d22\u3059\u308b diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp.html new file mode 100644 index 0000000..56efc3a --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp.html @@ -0,0 +1,3 @@ +
+ Specify a regular expression using the syntax supported by the Java Pattern class. +
diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp_ja.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp_ja.html new file mode 100644 index 0000000..ab411db --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp_ja.html @@ -0,0 +1,3 @@ + diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound.html new file mode 100644 index 0000000..9273d78 --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound.html @@ -0,0 +1,3 @@ +
+ Use this option to force a build to succeed or fail depending on whether a string was found. +
diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound_ja.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound_ja.html new file mode 100644 index 0000000..291ed2d --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound_ja.html @@ -0,0 +1,3 @@ +
+ 文字列を検出したときに、ビルドを強制的に成功扱いにする場合は、このオプションを設定してください。 +
diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound.html new file mode 100644 index 0000000..3450bc7 --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound.html @@ -0,0 +1,3 @@ +
+ Use this option to set build unstable instead of failing the build. +
diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound_ja.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound_ja.html new file mode 100644 index 0000000..84526ec --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound_ja.html @@ -0,0 +1,3 @@ +
+ ビルドを失敗扱いではなく不安定扱いにする場合は、このオプションを設定してください。 +
From 409f8e713e4fc4fedbc1e9169abf9c7d09fa43f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20F=C3=BCrbacher?= Date: Thu, 30 Nov 2017 15:59:37 +0100 Subject: [PATCH 2/5] Reformat class. --- .../textfinder/TextFinderPublisher.java | 297 +++++++++--------- 1 file changed, 140 insertions(+), 157 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 128c68d..93f2a5d 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -1,29 +1,10 @@ package hudson.plugins.textfinder; -import static hudson.Util.fixEmpty; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.PrintStream; -import java.io.Serializable; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -import javax.servlet.ServletException; - -import org.apache.commons.io.IOUtils; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.types.FileSet; -import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.QueryParameter; - -import hudson.Extension; import hudson.FilePath.FileCallable; import hudson.Launcher; import hudson.Util; +import hudson.Extension; +import static hudson.Util.fixEmpty; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.BuildListener; @@ -36,6 +17,22 @@ import hudson.tasks.Publisher; import hudson.tasks.Recorder; import hudson.util.FormValidation; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.types.FileSet; +import org.apache.commons.io.IOUtils; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.QueryParameter; + +import javax.servlet.ServletException; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintStream; +import java.io.Serializable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; /** * Text Finder plugin for Jenkins. Search in the workspace using a regular @@ -75,150 +72,136 @@ public BuildStepMonitor getRequiredMonitorService() { } public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { - findText(build, listener.getLogger()); + findText(build, listener.getLogger()); return true; } - - /** - * Indicates an orderly abortion of the processing. - */ - private static final class AbortException extends RuntimeException { - } - - protected void findText(Run run, PrintStream logger) - throws IOException, InterruptedException { - try { - boolean foundText = false; - - if (alsoCheckConsoleOutput) { - logger.println("Checking console output"); - foundText |= checkFile(run.getLogFile(), compilePattern(logger), - logger, true); - } else { - // printing this when checking console output will cause the - // plugin - // to find this line, which would be pointless. - // doing this only when fileSet!=null to avoid - logger.println("Checking " + regexp); - } - - final RemoteOutputStream ros = new RemoteOutputStream(logger); - if (fileSet != null && run instanceof AbstractBuild) { - AbstractBuild build = (AbstractBuild) run; - foundText |= build.getWorkspace() - .act(new FileCallable() { - public Boolean invoke(File ws, - VirtualChannel channel) throws IOException { - PrintStream logger = new PrintStream(ros); - - // Collect list of files for searching - FileSet fs = new FileSet(); - org.apache.tools.ant.Project p = new org.apache.tools.ant.Project(); - fs.setProject(p); - fs.setDir(ws); - fs.setIncludes(fileSet); - DirectoryScanner ds = fs.getDirectoryScanner(p); - - // Any files in the final set? - String[] files = ds.getIncludedFiles(); - if (files.length == 0) { - logger.println( - "Jenkins Text Finder: File set '" - + fileSet + "' is empty"); - throw new AbortException(); - } - - Pattern pattern = compilePattern(logger); - - boolean foundText = false; - - for (String file : files) { - File f = new File(ws, file); - - if (!f.exists()) { - logger.println( - "Jenkins Text Finder: Unable to" - + " find file '" + f - + "'"); - continue; - } - if (!f.canRead()) { - logger.println( - "Jenkins Text Finder: Unable to" - + " read from file '" - + f + "'"); - continue; - } + /** + * Indicates an orderly abortion of the processing. + */ + private static final class AbortException extends RuntimeException { + } - foundText |= checkFile(f, pattern, logger, - false); - } + protected void findText(Run run, PrintStream logger) throws IOException, InterruptedException { + try { + boolean foundText = false; + + if(alsoCheckConsoleOutput) { + logger.println("Checking console output"); + foundText |= checkFile(run.getLogFile(), compilePattern(logger), logger, true); + } else { + // printing this when checking console output will cause the plugin + // to find this line, which would be pointless. + // doing this only when fileSet!=null to avoid + logger.println("Checking " + regexp); + } - return foundText; - } - }); - } + final RemoteOutputStream ros = new RemoteOutputStream(logger); + + if(fileSet!=null && run instanceof AbstractBuild) { + AbstractBuild build = (AbstractBuild) run; + foundText |= build.getWorkspace().act(new FileCallable() { + public Boolean invoke(File ws, VirtualChannel channel) throws IOException { + PrintStream logger = new PrintStream(ros); + + // Collect list of files for searching + FileSet fs = new FileSet(); + org.apache.tools.ant.Project p = new org.apache.tools.ant.Project(); + fs.setProject(p); + fs.setDir(ws); + fs.setIncludes(fileSet); + DirectoryScanner ds = fs.getDirectoryScanner(p); + + // Any files in the final set? + String[] files = ds.getIncludedFiles(); + if (files.length == 0) { + logger.println("Jenkins Text Finder: File set '" + + fileSet + "' is empty"); + throw new AbortException(); + } + + Pattern pattern = compilePattern(logger); + + boolean foundText = false; + + for (String file : files) { + File f = new File(ws, file); + + if (!f.exists()) { + logger.println("Jenkins Text Finder: Unable to" + + " find file '" + f + "'"); + continue; + } + if (!f.canRead()) { + logger.println("Jenkins Text Finder: Unable to" + + " read from file '" + f + "'"); + continue; + } + + foundText |= checkFile(f, pattern, logger, false); + } + + return foundText; + } + }); + } - if (foundText != succeedIfFound) - run.setResult( - unstableIfFound ? Result.UNSTABLE : Result.FAILURE); - } catch (AbortException e) { - // no test file found - run.setResult(Result.UNSTABLE); - } - } + if (foundText != succeedIfFound) + run.setResult(unstableIfFound ? Result.UNSTABLE : Result.FAILURE); + } catch (AbortException e) { + // no test file found + run.setResult(Result.UNSTABLE); + } + } - /** - * Search the given regexp pattern in the file. - * - * @param abortAfterFirstHit - * true to return immediately as soon as the first hit is found. - * this is necessary when we are scanning the console output, - * because otherwise we'll loop forever. - */ - private boolean checkFile(File f, Pattern pattern, PrintStream logger, - boolean abortAfterFirstHit) { - boolean logFilename = true; - boolean foundText = false; - BufferedReader reader = null; - try { - // Assume default encoding and text files - String line; - reader = new BufferedReader(new FileReader(f)); - while ((line = reader.readLine()) != null) { - Matcher matcher = pattern.matcher(line); - if (matcher.find()) { - if (logFilename) {// first occurrence - logger.println(f + ":"); - logFilename = false; - } - logger.println(line); - foundText = true; - if (abortAfterFirstHit) - return true; - } - } - } catch (IOException e) { - logger.println("Jenkins Text Finder: Error reading" + " file '" + f - + "' -- ignoring"); - } finally { - IOUtils.closeQuietly(reader); - } - return foundText; - } + /** + * Search the given regexp pattern in the file. + * + * @param abortAfterFirstHit + * true to return immediately as soon as the first hit is found. this is necessary + * when we are scanning the console output, because otherwise we'll loop forever. + */ + private boolean checkFile(File f, Pattern pattern, PrintStream logger, boolean abortAfterFirstHit) { + boolean logFilename = true; + boolean foundText = false; + BufferedReader reader=null; + try { + // Assume default encoding and text files + String line; + reader = new BufferedReader(new FileReader(f)); + while ((line = reader.readLine()) != null) { + Matcher matcher = pattern.matcher(line); + if (matcher.find()) { + if (logFilename) {// first occurrence + logger.println(f + ":"); + logFilename = false; + } + logger.println(line); + foundText = true; + if(abortAfterFirstHit) + return true; + } + } + } catch (IOException e) { + logger.println("Jenkins Text Finder: Error reading" + + " file '" + f + "' -- ignoring"); + } finally { + IOUtils.closeQuietly(reader); + } + return foundText; + } - private Pattern compilePattern(PrintStream logger) { - Pattern pattern; - try { - pattern = Pattern.compile(regexp); - } catch (PatternSyntaxException e) { - logger.println("Jenkins Text Finder: Unable to compile" - + "regular expression '" + regexp + "'"); - throw new AbortException(); - } - return pattern; - } + private Pattern compilePattern(PrintStream logger) { + Pattern pattern; + try { + pattern = Pattern.compile(regexp); + } catch (PatternSyntaxException e) { + logger.println("Jenkins Text Finder: Unable to compile" + + "regular expression '" + regexp + "'"); + throw new AbortException(); + } + return pattern; + } @Extension public static final class DescriptorImpl extends BuildStepDescriptor { From ebbb9c64a9e1607aacbc39ea8fa8fd4620405a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20F=C3=BCrbacher?= Date: Thu, 30 Nov 2017 16:03:14 +0100 Subject: [PATCH 3/5] No FormValidation in Pipeline snippet generator. --- .../TextFinderPipelinePublisher.java | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java index 641f015..25fd0ea 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java @@ -26,7 +26,7 @@ * Text Finder plugin for Jenkins. Search in the workspace using a regular * expression and determine build outcome based on matches. * - * @author Santiago.PericasGeertsen@sun.com + * @author Santiago.PericasGeertsen@sun.com, jochen.fuerbacher@1und1.de */ public class TextFinderPipelinePublisher extends TextFinderPublisher implements SimpleBuildStep { @@ -56,22 +56,6 @@ public String getHelpFile() { public boolean isApplicable(Class jobType) { return true; } - - /** - * Checks the regular expression validity. - */ - public FormValidation doCheckRegexp(@QueryParameter String value) throws IOException, ServletException { - value = fixEmpty(value); - if(value==null) - return FormValidation.ok(); // not entered yet - - try { - Pattern.compile(value); - return FormValidation.ok(); - } catch (PatternSyntaxException e) { - return FormValidation.error(e.getMessage()); - } - } } } From 77ed40f0f19053ef280c59a53687cd478feebf6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20F=C3=BCrbacher?= Date: Fri, 1 Dec 2017 07:47:04 +0100 Subject: [PATCH 4/5] Added fileSet support to pipeline step; merged PipelinePublisher into parent class. --- .../TextFinderPipelinePublisher.java | 62 ------------------- .../textfinder/TextFinderPublisher.java | 27 ++++---- .../TextFinderPipelinePublisher/config.jelly | 11 ---- .../config_ja.properties | 26 -------- .../help-regexp.html | 3 - .../help-regexp_ja.html | 3 - .../help-succeedIfFound.html | 3 - .../help-succeedIfFound_ja.html | 3 - .../help-unstableIfFound.html | 3 - .../help-unstableIfFound_ja.html | 3 - 10 files changed, 15 insertions(+), 129 deletions(-) delete mode 100644 src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java delete mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config.jelly delete mode 100755 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config_ja.properties delete mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp.html delete mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp_ja.html delete mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound.html delete mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound_ja.html delete mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound.html delete mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound_ja.html diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java deleted file mode 100644 index 25fd0ea..0000000 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPipelinePublisher.java +++ /dev/null @@ -1,62 +0,0 @@ -package hudson.plugins.textfinder; - -import static hudson.Util.fixEmpty; - -import java.io.IOException; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -import javax.servlet.ServletException; - -import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.QueryParameter; - -import hudson.Extension; -import hudson.FilePath; -import hudson.Launcher; -import hudson.model.AbstractProject; -import hudson.model.Run; -import hudson.model.TaskListener; -import hudson.tasks.BuildStepDescriptor; -import hudson.tasks.Publisher; -import hudson.util.FormValidation; -import jenkins.tasks.SimpleBuildStep; - -/** - * Text Finder plugin for Jenkins. Search in the workspace using a regular - * expression and determine build outcome based on matches. - * - * @author Santiago.PericasGeertsen@sun.com, jochen.fuerbacher@1und1.de - */ -public class TextFinderPipelinePublisher extends TextFinderPublisher implements SimpleBuildStep { - - @DataBoundConstructor - public TextFinderPipelinePublisher(String regexp, - boolean succeedIfFound, boolean unstableIfFound) { - super(null, regexp, succeedIfFound, unstableIfFound, true); - } - - @Override - public void perform(Run run, FilePath workspace, Launcher launcher, - TaskListener listener) throws InterruptedException, IOException { - findText(run, listener.getLogger()); - } - - @Extension - public static final class DescriptorImpl extends BuildStepDescriptor { - public String getDisplayName() { - return Messages.DisplayName(); - } - - @Override - public String getHelpFile() { - return "/plugin/text-finder/help.html"; - } - - public boolean isApplicable(Class jobType) { - return true; - } - } - -} - diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 93f2a5d..c4a4d8e 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -4,12 +4,13 @@ import hudson.Launcher; import hudson.Util; import hudson.Extension; +import hudson.FilePath; + import static hudson.Util.fixEmpty; -import hudson.model.AbstractBuild; import hudson.model.AbstractProject; -import hudson.model.BuildListener; import hudson.model.Result; import hudson.model.Run; +import hudson.model.TaskListener; import hudson.remoting.RemoteOutputStream; import hudson.remoting.VirtualChannel; import hudson.tasks.BuildStepDescriptor; @@ -17,6 +18,8 @@ import hudson.tasks.Publisher; import hudson.tasks.Recorder; import hudson.util.FormValidation; +import jenkins.tasks.SimpleBuildStep; + import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.types.FileSet; import org.apache.commons.io.IOUtils; @@ -40,7 +43,7 @@ * * @author Santiago.PericasGeertsen@sun.com */ -public class TextFinderPublisher extends Recorder implements Serializable { +public class TextFinderPublisher extends Recorder implements Serializable, SimpleBuildStep { public final String fileSet; public final String regexp; @@ -70,11 +73,12 @@ public TextFinderPublisher(String fileSet, String regexp, boolean succeedIfFound public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; } - - public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { - findText(build, listener.getLogger()); - return true; - } + + @Override + public void perform(Run run, FilePath workspace, Launcher launcher, + TaskListener listener) throws InterruptedException, IOException { + findText(run, workspace, listener.getLogger()); + } /** * Indicates an orderly abortion of the processing. @@ -82,7 +86,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen private static final class AbortException extends RuntimeException { } - protected void findText(Run run, PrintStream logger) throws IOException, InterruptedException { + protected void findText(Run run, FilePath workspace, PrintStream logger) throws IOException, InterruptedException { try { boolean foundText = false; @@ -98,9 +102,8 @@ protected void findText(Run run, PrintStream logger) throws IOException, I final RemoteOutputStream ros = new RemoteOutputStream(logger); - if(fileSet!=null && run instanceof AbstractBuild) { - AbstractBuild build = (AbstractBuild) run; - foundText |= build.getWorkspace().act(new FileCallable() { + if(fileSet!=null) { + foundText |= workspace.act(new FileCallable() { public Boolean invoke(File ws, VirtualChannel channel) throws IOException { PrintStream logger = new PrintStream(ros); diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config.jelly b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config.jelly deleted file mode 100644 index 5dfb92f..0000000 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config.jelly +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config_ja.properties b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config_ja.properties deleted file mode 100755 index 40440bb..0000000 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/config_ja.properties +++ /dev/null @@ -1,26 +0,0 @@ -# The MIT License -# -# Copyright (c) 2004-2010, Sun Microsystems, Inc. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. - -Regular\ expression=\u6b63\u898f\u8868\u73fe -Succeed\ if\ found=\u691c\u51fa\u6642\u306b\u6210\u529f\u6271\u3044\u306b\u3059\u308b -Unstable\ if\ found=\u691c\u51fa\u6642\u306b\u4e0d\u5b89\u5b9a\u6271\u3044\u306b\u3059\u308b -Also\ search\ the\ console\ output=\u30b3\u30f3\u30bd\u30fc\u30eb\u51fa\u529b\u3082\u691c\u7d22\u3059\u308b diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp.html deleted file mode 100644 index 56efc3a..0000000 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp.html +++ /dev/null @@ -1,3 +0,0 @@ -
diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp_ja.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp_ja.html deleted file mode 100644 index ab411db..0000000 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-regexp_ja.html +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound.html deleted file mode 100644 index 9273d78..0000000 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound.html +++ /dev/null @@ -1,3 +0,0 @@ -
- Use this option to force a build to succeed or fail depending on whether a string was found. -
diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound_ja.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound_ja.html deleted file mode 100644 index 291ed2d..0000000 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-succeedIfFound_ja.html +++ /dev/null @@ -1,3 +0,0 @@ -
- 文字列を検出したときに、ビルドを強制的に成功扱いにする場合は、このオプションを設定してください。 -
diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound.html deleted file mode 100644 index 3450bc7..0000000 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound.html +++ /dev/null @@ -1,3 +0,0 @@ -
- Use this option to set build unstable instead of failing the build. -
diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound_ja.html b/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound_ja.html deleted file mode 100644 index 84526ec..0000000 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPipelinePublisher/help-unstableIfFound_ja.html +++ /dev/null @@ -1,3 +0,0 @@ -
- ビルドを失敗扱いではなく不安定扱いにする場合は、このオプションを設定してください。 -
From 712d23dbca476350340f7bcd210e4062a1a23614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20F=C3=BCrbacher?= Date: Fri, 1 Dec 2017 07:48:36 +0100 Subject: [PATCH 5/5] Make findText private again. --- .../java/hudson/plugins/textfinder/TextFinderPublisher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index c4a4d8e..ee3d64d 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -86,7 +86,7 @@ public void perform(Run run, FilePath workspace, Launcher launcher, private static final class AbortException extends RuntimeException { } - protected void findText(Run run, FilePath workspace, PrintStream logger) throws IOException, InterruptedException { + private void findText(Run run, FilePath workspace, PrintStream logger) throws IOException, InterruptedException { try { boolean foundText = false;