-
Notifications
You must be signed in to change notification settings - Fork 70
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
4ddcf07
a3ea8d1
4ec1ab8
7ee4bb7
67020ee
8670d0c
9a2aea9
b150b21
4ed7cfc
ed3313e
1151f98
35ce6b2
7d3d3f4
d2c87b5
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 |
---|---|---|
|
@@ -28,6 +28,8 @@ | |
import hudson.util.ListBoxModel; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
import jenkins.advancedqueue.DecisionLogger; | ||
import jenkins.advancedqueue.jobinclusion.JobInclusionStrategy; | ||
|
@@ -43,6 +45,8 @@ | |
*/ | ||
public class FolderBasedJobInclusionStrategy extends JobInclusionStrategy { | ||
|
||
private final static Logger LOGGER = Logger.getLogger(FolderBasedJobInclusionStrategy.class.getName()); | ||
|
||
@Extension(optional = true) | ||
static public class FolderBasedJobInclusionStrategyDescriptor extends | ||
AbstractJobInclusionStrategyDescriptor<FolderBasedJobInclusionStrategy> { | ||
|
@@ -62,19 +66,70 @@ public ListBoxModel getListFolderItems() { | |
|
||
}; | ||
|
||
static public class JobPattern { | ||
private String jobPattern; | ||
|
||
@DataBoundConstructor | ||
public JobPattern(String jobPattern) { | ||
this.jobPattern = jobPattern; | ||
} | ||
|
||
} | ||
|
||
private String folderName; | ||
|
||
private boolean useJobFilter = false; | ||
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. Actually there is no need to keep and persist this flag. |
||
|
||
private String jobPattern = ".*"; | ||
|
||
@DataBoundConstructor | ||
public FolderBasedJobInclusionStrategy(String folderName) { | ||
public FolderBasedJobInclusionStrategy(String folderName, JobPattern jobFilter) { | ||
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. It breaks the binary compatibility. The original constructor should be retained 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'm not overly familiar with Jenkins' ecosystem (yet) - is it enough to retain the original constructor without the 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. yes, it is |
||
this.folderName = folderName; | ||
this.useJobFilter = (jobFilter != null); | ||
if (this.useJobFilter) { | ||
this.jobPattern = jobFilter.jobPattern; | ||
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. Empty pattern from config will override the default value. I would rather make the field nullable, get rid of the default, add |
||
} | ||
} | ||
|
||
public String getFolderName() { | ||
return folderName; | ||
} | ||
|
||
public boolean isUseJobFilter() { | ||
return useJobFilter; | ||
} | ||
|
||
public String getJobPattern() { | ||
return jobPattern; | ||
} | ||
|
||
@Override | ||
public boolean contains(DecisionLogger decisionLogger, Job<?, ?> job) { | ||
return job.getFullName().startsWith(folderName); | ||
if (job.getFullName().startsWith(folderName)) { | ||
if (!isUseJobFilter() || getJobPattern().trim().isEmpty()) { | ||
decisionLogger.addDecisionLog(2, "Not using filter ..."); | ||
LOGGER.info("Not using filter ..."); | ||
return true; | ||
} else { | ||
decisionLogger.addDecisionLog(2, "Using filter ..."); | ||
LOGGER.info("Using filter ..."); | ||
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. Such general-purpose logging provides few info to the admin, because it does not reference job. |
||
try { | ||
if (job.getName().matches(getJobPattern())) { | ||
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. Such approach will require creation of the regex pattern every time. It is inefficient, especially when it comes to the Queue-locking logic. It it better to compile pattern only once and cache it as a transient field of JobPattern 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 this, however, I was following the way the same functionality is implemented in |
||
decisionLogger.addDecisionLog(3, "Job is matching the filter ..."); | ||
LOGGER.info("Job is matching the filter ..."); | ||
return true; | ||
} else { | ||
decisionLogger.addDecisionLog(3, "Job is not matching the filter ..."); | ||
LOGGER.info("Job is not matching the filter ..."); | ||
return false; | ||
} | ||
} catch (PatternSyntaxException e) { | ||
decisionLogger.addDecisionLog(3, "Filter has syntax error"); | ||
LOGGER.info("Filter has syntax error"); | ||
return false; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,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}" /> | ||
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. help file is missing 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. fixed now |
||
</f:entry> | ||
</f:optionalBlock> | ||
</select> | ||
</f:entry> | ||
</j:jelly> |
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.
If you want to create a new class, make it describable and use
optionalProperty
. OTOH I am not sure it is required in this caseThere 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.
Is it needed? I've been looking at how it's implemented in
ViewBasedJobInclusionStrategy
and it doesn't seem to be required there.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.
In the new implementation I still do not see much reason to have the class in the current state
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.
Well, I was not an author of that :( I rather suggest a proper design from the Jenkins standpoint. The current implementation is rather a hack based on the JSON structure presumptions. It should work, but I am not sure if we can guarantee compatibility of it in the Jenkins core
If you want to keep the class as is, mark it as
@Restricted(NoExternalUse.class)
to prevent the external usage. But it will also require the constructor modification then.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.
I've tried to make it work with a normal
String
instead of theJobPattern
but to no avail, it seems like I'm missing something obvious here, probably due to my low understanding of the ecosystem. I had to go with the@Restricted
route. I'm not sure what constructor modifications you mean, though.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.
OK. I will probably just create a pull request on the top of your changes