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 regex pattern in folders #41

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -59,9 +64,32 @@
}
;

@Restricted(NoExternalUse.class)
public static class JobPattern {
private String jobPattern;

@DataBoundConstructor
public JobPattern(String jobPattern) {
this.jobPattern = jobPattern;
}

Check warning on line 74 in src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 72-74 are not covered by tests
}

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);

Check warning on line 87 in src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 87 is only partially covered, one branch is missing
if (this.useJobFilter) {

Check warning on line 88 in src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 88 is only partially covered, one branch is missing
this.jobPattern = jobFilter.jobPattern;

Check warning on line 89 in src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 89 is not covered by tests
}
}

public FolderBasedJobInclusionStrategy(String folderName) {
this.folderName = folderName;
}
Expand All @@ -70,8 +98,56 @@
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;

Check warning on line 151 in src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 102-151 are not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<j:forEach var="folder" items="${descriptor.listFolderItems}">
<f:option value="${folder.value}" selected="${folder.value==instance.folderName}">${folder.name}</f:option>
</j:forEach>
<f:optionalBlock name="jobFilter" checked="${instance.useJobFilter}" title="Use a regular expression to only include a subset of the included Jobs">
<f:entry title="Regular Expression">
<f:textbox name="jobPattern" field="jobPattern" value="${instance.jobPattern}" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help file is missing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed now

</f:entry>
</f:optionalBlock>
</select>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The regex pattern to be used to filter jobs inside the folder.
</div>