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

Add support for Jenkins pipeline support #6

Closed
wants to merge 9 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
47 changes: 37 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.480</version>
<version>2.17</version>
</parent>

<artifactId>text-finder</artifactId>
<packaging>hpi</packaging>
<version>1.11-SNAPSHOT</version>
<name>Jenkins TextFinder plugin</name>
<version>1.11-SNAPSHOT</version>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin</url>

<properties>
<jenkins.version>1.609</jenkins.version>
<java.level>6</java.level>
<workflow.version>1.4.3</workflow.version>
</properties>

<developers>
<developer>
<id>kohsuke</id>
Expand All @@ -22,11 +28,11 @@
<name>Santiago Pericas-Geertsen</name>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
</scm>
<scm>
<connection>scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
</scm>

<repositories>
<repository>
Expand All @@ -41,6 +47,27 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>


<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>


89 changes: 60 additions & 29 deletions src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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.model.*;
import hudson.remoting.RemoteOutputStream;
import hudson.remoting.VirtualChannel;
import hudson.tasks.BuildStepDescriptor;
Expand All @@ -21,6 +19,8 @@
import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.BufferedReader;
Expand All @@ -33,46 +33,71 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import jenkins.model.Jenkins;
import org.jenkinsci.remoting.RoleChecker;
import org.jenkinsci.remoting.RoleSensitive;
import jenkins.tasks.SimpleBuildStep;

import javax.annotation.Nonnull;

/**
* Text Finder plugin for Jenkins. Search in the workspace using a regular
* expression and determine build outcome based on matches.
* 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 TextFinderPublisher extends Recorder implements Serializable {
public final String fileSet;
public final String regexp;
public final boolean succeedIfFound;
public final boolean unstableIfFound;
public class TextFinderPublisher extends Recorder implements Serializable, SimpleBuildStep {

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

@DataBoundConstructor
public TextFinderPublisher(String fileSet, String regexp, boolean succeedIfFound, boolean unstableIfFound, boolean alsoCheckConsoleOutput) {
public TextFinderPublisher() {
}

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

@DataBoundSetter
public void setRegexp(String regexp) {
this.regexp = regexp;
this.succeedIfFound = succeedIfFound;
this.unstableIfFound = unstableIfFound;
this.alsoCheckConsoleOutput = alsoCheckConsoleOutput;

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

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

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

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

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 filePath, Launcher launcher, TaskListener taskListener) throws InterruptedException, IOException {
findText(run, filePath, taskListener.getLogger());
}

/**
Expand All @@ -81,7 +106,7 @@ 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<?, ?> build, FilePath workspace, PrintStream logger) throws IOException, InterruptedException {
try {
boolean foundText = false;

Expand All @@ -98,9 +123,9 @@ private void findText(AbstractBuild build, PrintStream logger) throws IOExceptio
final RemoteOutputStream ros = new RemoteOutputStream(logger);

if(fileSet!=null) {
foundText |= build.getWorkspace().act(new FileCallable<Boolean>() {
foundText |= workspace.act(new FileCallable<Boolean>() {
public Boolean invoke(File ws, VirtualChannel channel) throws IOException {
PrintStream logger = new PrintStream(ros);
PrintStream logger = new PrintStream(ros, false, "utf-8");

// Collect list of files for searching
FileSet fs = new FileSet();
Expand Down Expand Up @@ -141,6 +166,10 @@ public Boolean invoke(File ws, VirtualChannel channel) throws IOException {

return foundText;
}
@Override
public void checkRoles(RoleChecker rc) throws SecurityException
{
}
});
}

Expand All @@ -157,7 +186,7 @@ public Boolean invoke(File ws, VirtualChannel channel) throws IOException {
*
* @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.
* 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;
Expand Down Expand Up @@ -192,7 +221,7 @@ private boolean checkFile(File f, Pattern pattern, PrintStream logger, boolean a
private Pattern compilePattern(PrintStream logger) {
Pattern pattern;
try {
pattern = Pattern.compile(regexp);
pattern = Pattern.compile(this.regexp);
} catch (PatternSyntaxException e) {
logger.println("Jenkins Text Finder: Unable to compile"
+ "regular expression '" + regexp + "'");
Expand All @@ -204,7 +233,7 @@ private Pattern compilePattern(PrintStream logger) {
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public String getDisplayName() {
return Messages.DisplayName();
return Messages.displayName();
}

@Override
Expand All @@ -218,6 +247,8 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {

/**
* Checks the regular expression validity.
* @param value The expression to check
* @return The form validation result
*/
public FormValidation doCheckRegexp(@QueryParameter String value) throws IOException, ServletException {
value = fixEmpty(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DisplayName=Jenkins Text Finder
displayName=Jenkins Text Finder
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DisplayName=Jenkins\u6587\u5b57\u5217\u691c\u7d22
displayName=Jenkins\u6587\u5b57\u5217\u691c\u7d22
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry field="fileSet" title="${%Files}">
<f:textbox/>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/index.jelly
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<div>
This plugin is used to search for strings in workspace files. The outcome
of this search can be used to mark the build as normal or failed.
Expand Down