-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JENKINS-34983] - Add pipeline support #12
Changes from 1 commit
118ceb7
8a3753a
dcaeee1
ad508d1
fc58b29
563aaa3
5906137
73c6bb1
1bf70f4
4f4eb26
1ff8ffd
3b31863
4c89b0c
410dc7e
54f6a0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
package hudson.plugins.textfinder; | ||
|
||
import hudson.FilePath.FileCallable; | ||
import hudson.FilePath; | ||
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.remoting.RemoteOutputStream; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.remoting.VirtualChannel; | ||
import hudson.tasks.BuildStepDescriptor; | ||
import hudson.tasks.BuildStepMonitor; | ||
import hudson.tasks.Publisher; | ||
import hudson.tasks.Recorder; | ||
import hudson.util.FormValidation; | ||
import jenkins.MasterToSlaveFileCallable; | ||
import jenkins.tasks.SimpleBuildStep; | ||
import org.apache.tools.ant.DirectoryScanner; | ||
import org.apache.tools.ant.types.FileSet; | ||
import org.apache.commons.io.IOUtils; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
import org.kohsuke.stapler.QueryParameter; | ||
|
||
import javax.servlet.ServletException; | ||
|
@@ -39,24 +42,20 @@ | |
* | ||
* @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 String fileSet; | ||
public final String regexp; | ||
public final boolean succeedIfFound; | ||
public final boolean unstableIfFound; | ||
public boolean succeedIfFound; | ||
public boolean unstableIfFound; | ||
/** | ||
* True to also scan the whole console output | ||
*/ | ||
public final boolean alsoCheckConsoleOutput; | ||
public boolean alsoCheckConsoleOutput; | ||
|
||
@DataBoundConstructor | ||
public TextFinderPublisher(String fileSet, String regexp, boolean succeedIfFound, boolean unstableIfFound, boolean alsoCheckConsoleOutput) { | ||
this.fileSet = Util.fixEmpty(fileSet.trim()); | ||
public TextFinderPublisher(String regexp) { | ||
this.regexp = regexp; | ||
this.succeedIfFound = succeedIfFound; | ||
this.unstableIfFound = unstableIfFound; | ||
this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; | ||
|
||
// Attempt to compile regular expression | ||
try { | ||
|
@@ -66,13 +65,69 @@ public TextFinderPublisher(String fileSet, String regexp, boolean succeedIfFound | |
} | ||
} | ||
|
||
@Deprecated | ||
public TextFinderPublisher( | ||
String fileSet, | ||
String regexp, | ||
boolean succeedIfFound, | ||
boolean unstableIfFound, | ||
boolean alsoCheckConsoleOutput) { | ||
this(regexp); | ||
this.fileSet = Util.fixEmpty(fileSet.trim()); | ||
this.succeedIfFound = succeedIfFound; | ||
this.unstableIfFound = unstableIfFound; | ||
this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; | ||
} | ||
|
||
public String getRegexp() { | ||
return regexp; | ||
} | ||
|
||
public String getFileSet() { | ||
return fileSet; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setFileSet(String fileSet) { | ||
this.fileSet = Util.fixEmpty(fileSet.trim()); | ||
} | ||
|
||
public boolean isSucceedIfFound() { | ||
return succeedIfFound; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setSucceedIfFound(boolean succeedIfFound) { | ||
this.succeedIfFound = succeedIfFound; | ||
} | ||
|
||
public boolean isUnstableIfFound() { | ||
return unstableIfFound; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setUnstableIfFound(boolean unstableIfFound) { | ||
this.unstableIfFound = unstableIfFound; | ||
} | ||
|
||
public boolean isAlsoCheckConsoleOutput() { | ||
return alsoCheckConsoleOutput; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setAlsoCheckConsoleOutput(boolean alsoCheckConsoleOutput) { | ||
this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; | ||
} | ||
|
||
@Override | ||
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); | ||
} | ||
|
||
/** | ||
|
@@ -81,74 +136,32 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen | |
private static final class AbortException extends RuntimeException { | ||
} | ||
|
||
private void findText(AbstractBuild build, PrintStream logger) throws IOException, InterruptedException { | ||
private void findText(Run<?, ?> run, FilePath workspace, TaskListener listener) | ||
throws IOException, InterruptedException { | ||
try { | ||
PrintStream logger = listener.getLogger(); | ||
boolean foundText = false; | ||
|
||
if(alsoCheckConsoleOutput) { | ||
logger.println("Checking console output"); | ||
foundText |= checkFile(build.getLogFile(), compilePattern(logger), logger, true); | ||
foundText |= | ||
checkFile(run.getLogFile(), compilePattern(logger, regexp), 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) { | ||
foundText |= build.getWorkspace().act(new FileCallable<Boolean>() { | ||
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; | ||
} | ||
}); | ||
foundText |= workspace.act(new FileChecker(listener, fileSet, regexp)); | ||
} | ||
|
||
if (foundText != succeedIfFound) | ||
build.setResult(unstableIfFound ? Result.UNSTABLE : Result.FAILURE); | ||
run.setResult(unstableIfFound ? Result.UNSTABLE : Result.FAILURE); | ||
} catch (AbortException e) { | ||
// no test file found | ||
build.setResult(Result.UNSTABLE); | ||
run.setResult(Result.UNSTABLE); | ||
} | ||
} | ||
|
||
|
@@ -159,7 +172,8 @@ public Boolean invoke(File ws, VirtualChannel channel) throws IOException { | |
* 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) { | ||
private static boolean checkFile( | ||
File f, Pattern pattern, PrintStream logger, boolean abortAfterFirstHit) { | ||
boolean logFilename = true; | ||
boolean foundText = false; | ||
BufferedReader reader=null; | ||
|
@@ -189,7 +203,7 @@ private boolean checkFile(File f, Pattern pattern, PrintStream logger, boolean a | |
return foundText; | ||
} | ||
|
||
private Pattern compilePattern(PrintStream logger) { | ||
private static Pattern compilePattern(PrintStream logger, String regexp) { | ||
Pattern pattern; | ||
try { | ||
pattern = Pattern.compile(regexp); | ||
|
@@ -201,8 +215,10 @@ private Pattern compilePattern(PrintStream logger) { | |
return pattern; | ||
} | ||
|
||
@Symbol("textFinder") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed the symbol to |
||
@Extension | ||
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { | ||
@Override | ||
public String getDisplayName() { | ||
return Messages.DisplayName(); | ||
} | ||
|
@@ -212,6 +228,7 @@ public String getHelpFile() { | |
return "/plugin/text-finder/help.html"; | ||
} | ||
|
||
@Override | ||
public boolean isApplicable(Class<? extends AbstractProject> jobType) { | ||
return true; | ||
} | ||
|
@@ -233,6 +250,61 @@ public FormValidation doCheckRegexp(@QueryParameter String value) throws IOExcep | |
} | ||
} | ||
|
||
private static class FileChecker extends MasterToSlaveFileCallable<Boolean> { | ||
|
||
private final TaskListener listener; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend to pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed {{TaskListener}} to {{RemoteOutputStream}} as you requested. |
||
private final String fileSet; | ||
private final String regexp; | ||
|
||
public FileChecker(TaskListener listener, String fileSet, String regexp) { | ||
this.listener = listener; | ||
this.fileSet = fileSet; | ||
this.regexp = regexp; | ||
} | ||
|
||
@Override | ||
public Boolean invoke(File ws, VirtualChannel channel) throws IOException { | ||
PrintStream logger = listener.getLogger(); | ||
|
||
// 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, regexp); | ||
|
||
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; | ||
} | ||
} | ||
|
||
private static final long serialVersionUID = 1L; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.14 now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, 3.15 now ;-)