diff --git a/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java b/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java index afd99044..95ce4443 100644 --- a/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java +++ b/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java @@ -24,14 +24,19 @@ package jenkins.advancedqueue.jobinclusion.strategy; import com.cloudbees.hudson.plugins.folder.Folder; +import edu.umd.cs.findbugs.annotations.CheckForNull; import hudson.Extension; import hudson.model.Job; import hudson.util.ListBoxModel; import java.util.List; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import jenkins.advancedqueue.DecisionLogger; import jenkins.advancedqueue.Messages; import jenkins.advancedqueue.jobinclusion.JobInclusionStrategy; import jenkins.model.Jenkins; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.DataBoundConstructor; /** @@ -59,9 +64,32 @@ public ListBoxModel getListFolderItems() { } ; + @Restricted(NoExternalUse.class) + public static class JobPattern { + private String jobPattern; + + @DataBoundConstructor + public JobPattern(String jobPattern) { + this.jobPattern = jobPattern; + } + } + private String folderName; + private boolean useJobFilter = false; + + private String jobPattern; + private transient Pattern compiledPattern; + @DataBoundConstructor + public FolderBasedJobInclusionStrategy(String folderName, JobPattern jobFilter) { + this.folderName = folderName; + this.useJobFilter = (jobFilter != null); + if (this.useJobFilter) { + this.jobPattern = jobFilter.jobPattern; + } + } + public FolderBasedJobInclusionStrategy(String folderName) { this.folderName = folderName; } @@ -70,8 +98,56 @@ public String getFolderName() { return folderName; } + public boolean isUseJobFilter() { + return useJobFilter; + } + + @CheckForNull + public String getJobPattern() { + return jobPattern; + } + + @CheckForNull + private Pattern getCompiledPattern() throws PatternSyntaxException { + if (jobPattern == null) return null; + + if (compiledPattern == null) compiledPattern = Pattern.compile(jobPattern); + + return compiledPattern; + } + @Override public boolean contains(DecisionLogger decisionLogger, Job job) { - return job.getFullName().startsWith(folderName); + if (job != null && job.getFullName().startsWith(folderName)) { + if (!isUseJobFilter() || getJobPattern() == null || getCompiledPattern() == null) { + decisionLogger.addDecisionLog(2, "Not using filter ..."); + return true; + } else { + decisionLogger.addDecisionLog(2, "Using filter ..."); + try { + Pattern pattern = getCompiledPattern(); + if (pattern == null) { + decisionLogger.addDecisionLog(3, "Job filter is null ..."); + return false; + } + if (job.getName() == null) { + decisionLogger.addDecisionLog(3, "Job name is null ..."); + return false; + } + java.util.regex.Matcher matcher = pattern.matcher(job.getName()); + if (matcher.matches()) { + decisionLogger.addDecisionLog(3, "Job is matching the filter ..."); + return true; + } else { + decisionLogger.addDecisionLog(3, "Job is not matching the filter ..."); + return false; + } + } catch (PatternSyntaxException e) { + decisionLogger.addDecisionLog(3, "Filter has syntax error"); + return false; + } + } + } + return false; } } diff --git a/src/main/resources/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy/config.jelly b/src/main/resources/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy/config.jelly index d0626434..7c79f0d7 100644 --- a/src/main/resources/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy/config.jelly +++ b/src/main/resources/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy/config.jelly @@ -5,6 +5,11 @@ ${folder.name} + + + + + \ No newline at end of file diff --git a/src/main/resources/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy/help-jobPattern.html b/src/main/resources/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy/help-jobPattern.html new file mode 100644 index 00000000..f1e0a317 --- /dev/null +++ b/src/main/resources/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy/help-jobPattern.html @@ -0,0 +1,3 @@ +
+ The regex pattern to be used to filter jobs inside the folder. +
\ No newline at end of file