Skip to content
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

Added possibility to have multiple finders #47

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/main/java/hudson/plugins/textfinder/TextFinderModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package hudson.plugins.textfinder;

import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import java.io.Serializable;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public final class TextFinderModel extends AbstractDescribableImpl<TextFinderModel>
implements Serializable {
private static final long serialVersionUID = 1L;

private String fileSet;
private final String regexp;
private boolean succeedIfFound;
private boolean unstableIfFound;
private boolean notBuiltIfFound;
/** True to also scan the whole console output */
private boolean alsoCheckConsoleOutput;

@DataBoundConstructor
public TextFinderModel(String regexp) {
this.regexp = regexp;
// Attempt to compile regular expression
try {
Pattern.compile(regexp);
} catch (PatternSyntaxException e) {
// falls through
}
}

@DataBoundSetter
public void setFileSet(String fileSet) {
if (fileSet == null) {
this.fileSet = null;
} else {
if (fileSet.trim().isEmpty()) {
this.fileSet = null;
} else {
this.fileSet = fileSet.trim();
}
}
}

@DataBoundSetter
public void setSucceedIfFound(boolean succeedIfFound) {
this.succeedIfFound = succeedIfFound;
}

@DataBoundSetter
public void setUnstableIfFound(boolean unstableIfFound) {
this.unstableIfFound = unstableIfFound;
}

@DataBoundSetter
public void setNotBuiltIfFound(boolean notBuiltIfFound) {
this.notBuiltIfFound = notBuiltIfFound;
}

@DataBoundSetter
public void setAlsoCheckConsoleOutput(boolean alsoCheckConsoleOutput) {
this.alsoCheckConsoleOutput = alsoCheckConsoleOutput;
}

@Deprecated
private TextFinderModel(
String fileSet,
String regexp,
boolean succeedIfFound,
boolean unstableIfFound,
boolean alsoCheckConsoleOutput,
boolean notBuiltIfFound) {
this.fileSet = fileSet != null ? Util.fixEmpty(fileSet.trim()) : null;
this.regexp = regexp;
this.succeedIfFound = succeedIfFound;
this.unstableIfFound = unstableIfFound;
this.alsoCheckConsoleOutput = alsoCheckConsoleOutput;
this.notBuiltIfFound = notBuiltIfFound;

// Attempt to compile regular expression
try {
Pattern.compile(regexp);
} catch (PatternSyntaxException e) {
// falls through
}
}

public String getFileSet() {
return fileSet;
}

public String getRegexp() {
return regexp;
}

public boolean isSucceedIfFound() {
return succeedIfFound;
}

public boolean isUnstableIfFound() {
return unstableIfFound;
}

public boolean isNotBuiltIfFound() {
return notBuiltIfFound;
}

public boolean isAlsoCheckConsoleOutput() {
return alsoCheckConsoleOutput;
}

@Extension
public static class DescriptorImpl extends Descriptor<TextFinderModel> {
@Override
public String getDisplayName() {
return "Text Finder";
}
}
}
141 changes: 107 additions & 34 deletions src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import java.io.Reader;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.servlet.ServletException;
import jenkins.MasterToSlaveFileCallable;
import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildStep;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
Expand All @@ -50,18 +53,15 @@
*/
public class TextFinderPublisher extends Recorder implements Serializable, SimpleBuildStep {

public String fileSet;
public final String regexp;
public boolean succeedIfFound;
public boolean unstableIfFound;
public boolean notBuiltIfFound;
/** True to also scan the whole console output */
public boolean alsoCheckConsoleOutput;
/** This is the primary text finder in the configuration. */
private final TextFinderModel primaryTextFinder;

/** Additional text finder configurations are stored here. */
private final List<TextFinderModel> additionalTextFinders = new ArrayList<>();;

@DataBoundConstructor
public TextFinderPublisher(String regexp) {
this.regexp = regexp;

this.primaryTextFinder = new TextFinderModel(regexp);
// Attempt to compile regular expression
try {
Pattern.compile(regexp);
Expand All @@ -70,43 +70,64 @@ public TextFinderPublisher(String regexp) {
}
}

/**
* @param fileSet Kept for backward compatibility with old configuration.
* @param regexp Kept for backward compatibility with old configuration.
* @param succeedIfFound Kept for backward compatibility with old configuration.
* @param unstableIfFound Kept for backward compatibility with old configuration.
* @param notBuiltIfFound Kept for backward compatibility with old configuration.
* @param alsoCheckConsoleOutput Kept for backward compatibility with old configuration.
* @param additionalTextFinders configuration for additional textFinders
*/
@Deprecated
public TextFinderPublisher(
private 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;
boolean notBuiltIfFound,
boolean alsoCheckConsoleOutput,
List<TextFinderModel> additionalTextFinders) {
this.primaryTextFinder = new TextFinderModel(regexp);
this.primaryTextFinder.setFileSet(Util.fixEmpty(fileSet != null ? fileSet.trim() : ""));
this.primaryTextFinder.setSucceedIfFound(succeedIfFound);
this.primaryTextFinder.setUnstableIfFound(unstableIfFound);
this.primaryTextFinder.setAlsoCheckConsoleOutput(alsoCheckConsoleOutput);
this.primaryTextFinder.setNotBuiltIfFound(notBuiltIfFound);
this.setAdditionalTextFinders(additionalTextFinders);
}

@DataBoundSetter
public void setFileSet(String fileSet) {
this.fileSet = Util.fixEmpty(fileSet.trim());
this.primaryTextFinder.setFileSet(fileSet);
}

@DataBoundSetter
public void setSucceedIfFound(boolean succeedIfFound) {
this.succeedIfFound = succeedIfFound;
this.primaryTextFinder.setSucceedIfFound(succeedIfFound);
}

@DataBoundSetter
public void setUnstableIfFound(boolean unstableIfFound) {
this.unstableIfFound = unstableIfFound;
this.primaryTextFinder.setUnstableIfFound(unstableIfFound);
}

@DataBoundSetter
public void setNotBuiltIfFound(boolean notBuiltIfFound) {
this.notBuiltIfFound = notBuiltIfFound;
this.primaryTextFinder.setNotBuiltIfFound(notBuiltIfFound);
}

@DataBoundSetter
public void setAlsoCheckConsoleOutput(boolean alsoCheckConsoleOutput) {
this.alsoCheckConsoleOutput = alsoCheckConsoleOutput;
this.primaryTextFinder.setAlsoCheckConsoleOutput(alsoCheckConsoleOutput);
}

@DataBoundSetter
public void setAdditionalTextFinders(List<TextFinderModel> additionalTextFinders) {
this.additionalTextFinders.clear();
if (additionalTextFinders != null && !additionalTextFinders.isEmpty()) {
this.additionalTextFinders.addAll(additionalTextFinders);
}
}

@Override
Expand All @@ -117,56 +138,64 @@ public BuildStepMonitor getRequiredMonitorService() {
@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)
throws InterruptedException, IOException {
findText(run, workspace, listener);
findText(primaryTextFinder, run, workspace, listener);
for (TextFinderModel additionalTextFinder : additionalTextFinders) {
findText(additionalTextFinder, run, workspace, listener);
}
}

/** Indicates an orderly abortion of the processing. */
private static final class AbortException extends RuntimeException {}

private void findText(Run<?, ?> run, FilePath workspace, TaskListener listener)
private void findText(
TextFinderModel textFinder, Run<?, ?> run, FilePath workspace, TaskListener listener)
throws IOException, InterruptedException {
try {
PrintStream logger = listener.getLogger();
boolean foundText = false;

if (alsoCheckConsoleOutput) {
if (textFinder.isAlsoCheckConsoleOutput()) {
// Do not mention the pattern we are looking for to avoid false positives
logger.println("[Text Finder] Scanning console output...");
foundText |= checkConsole(run, compilePattern(logger, regexp), logger);
foundText |=
checkConsole(run, compilePattern(logger, textFinder.getRegexp()), logger);
logger.println(
"[Text Finder] Finished looking for pattern "
+ "'"
+ regexp
+ textFinder.getRegexp()
+ "'"
+ " in the console output");
}

final RemoteOutputStream ros = new RemoteOutputStream(logger);

if (fileSet != null) {
if (textFinder.getFileSet() != null) {
logger.println(
"[Text Finder] Looking for pattern "
+ "'"
+ regexp
+ textFinder.getRegexp()
+ "'"
+ " in the files at "
+ "'"
+ fileSet
+ textFinder.getFileSet()
+ "'");
foundText |= workspace.act(new FileChecker(ros, fileSet, regexp));
foundText |=
workspace.act(
new FileChecker(
ros, textFinder.getFileSet(), textFinder.getRegexp()));
}

if (foundText != succeedIfFound) {
if (foundText != textFinder.isSucceedIfFound()) {
final Result finalResult;
if (notBuiltIfFound) {
if (textFinder.isNotBuiltIfFound()) {
finalResult = Result.NOT_BUILT;
} else {
finalResult = unstableIfFound ? Result.UNSTABLE : Result.FAILURE;
finalResult = textFinder.isUnstableIfFound() ? Result.UNSTABLE : Result.FAILURE;
}
run.setResult(finalResult);
}
} catch (AbortException e) {
// no test file found
// files presented, but no test file found.
run.setResult(Result.UNSTABLE);
}
}
Expand Down Expand Up @@ -244,6 +273,41 @@ private static Pattern compilePattern(PrintStream logger, String regexp) {
return pattern;
}

@SuppressWarnings("unused")
public List<TextFinderModel> getAdditionalTextFinders() {
return additionalTextFinders;
}

@SuppressWarnings("unused")
public String getFileSet() {
return this.primaryTextFinder.getFileSet();
}

@SuppressWarnings("unused")
public String getRegexp() {
return this.primaryTextFinder.getRegexp();
}

@SuppressWarnings("unused")
public boolean isSucceedIfFound() {
return this.primaryTextFinder.isSucceedIfFound();
}

@SuppressWarnings("unused")
public boolean isUnstableIfFound() {
return this.primaryTextFinder.isUnstableIfFound();
}

@SuppressWarnings("unused")
public boolean isNotBuiltIfFound() {
return this.primaryTextFinder.isNotBuiltIfFound();
}

@SuppressWarnings("unused")
public boolean isAlsoCheckConsoleOutput() {
return this.primaryTextFinder.isAlsoCheckConsoleOutput();
}

@Symbol("findText")
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
Expand All @@ -262,6 +326,15 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

public List<TextFinderModel.DescriptorImpl> getItemDescriptors() {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins != null) {
return jenkins.getDescriptorList(TextFinderModel.class);
} else {
throw new NullPointerException("not able to get Jenkins instance");
}
}

/**
* Checks the regular expression validity.
*
Expand Down
Loading