Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-21356] Add support for using JobProperty
Added support for creating JobGroups that support JobProperty and optionally FolderProperty to add a Job or all Jobs in a Folder to the JobGroup.
- Loading branch information
Showing
7 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/jenkins/advancedqueue/jobinclusion/strategy/CloudbeesPropertyLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package jenkins.advancedqueue.jobinclusion.strategy; | ||
|
||
import hudson.model.ItemGroup; | ||
import hudson.model.Job; | ||
import hudson.model.TopLevelItem; | ||
import hudson.util.DescribableList; | ||
import jenkins.advancedqueue.DecisionLogger; | ||
|
||
import com.cloudbees.hudson.plugins.folder.FolderProperty; | ||
import com.cloudbees.hudson.plugins.folder.FolderPropertyDescriptor; | ||
import com.cloudbees.hudson.plugins.folder.Folder; | ||
|
||
public class CloudbeesPropertyLoader { | ||
|
||
static public String getJobViewName(DecisionLogger decisionLogger, Job<?, ?> job) { | ||
ItemGroup<?> parent = job.getParent(); | ||
decisionLogger.addDecisionLog(2, "Checking for Cloudbees Folder inclusion ..."); | ||
while(parent != null) { | ||
if(parent instanceof Folder) { | ||
Folder folder = (Folder) parent; | ||
decisionLogger.addDecisionLog(3, "Evaluating Folder [" + folder.getFullName() + "] ..."); | ||
DescribableList<FolderProperty<?>,FolderPropertyDescriptor> properties = folder.getProperties(); | ||
for(FolderProperty<?> property : properties) { | ||
if(property instanceof JobInclusionCloudbeesFolderProperty) { | ||
JobInclusionCloudbeesFolderProperty incProperty = (JobInclusionCloudbeesFolderProperty) property; | ||
if(incProperty.isUseJobGroup()) { | ||
String name = incProperty.getJobGroupName(); | ||
decisionLogger.addDecisionLog(4, "JobGroup is enabled, with JobGroup [" + name + "] ..."); | ||
return name; | ||
} | ||
} | ||
} | ||
} | ||
if(parent instanceof TopLevelItem) { | ||
parent = ((TopLevelItem) parent).getParent(); | ||
} else { | ||
parent = null; | ||
} | ||
} | ||
decisionLogger.addDecisionLog(2, "No match ..."); | ||
return null; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionCloudbeesFolderProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package jenkins.advancedqueue.jobinclusion.strategy; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Descriptor.FormException; | ||
import hudson.util.ListBoxModel; | ||
import net.sf.json.JSONObject; | ||
|
||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
|
||
import com.cloudbees.hudson.plugins.folder.FolderProperty; | ||
import com.cloudbees.hudson.plugins.folder.FolderPropertyDescriptor; | ||
import com.cloudbees.hudson.plugins.folder.Folder; | ||
|
||
public class JobInclusionCloudbeesFolderProperty extends FolderProperty<Folder> { | ||
|
||
private boolean useJobGroup; | ||
|
||
private String jobGroupName; | ||
|
||
@DataBoundConstructor | ||
public JobInclusionCloudbeesFolderProperty(Boolean useJobGroup, String jobGroupName) { | ||
this.useJobGroup = useJobGroup; | ||
this.jobGroupName = jobGroupName; | ||
} | ||
|
||
public String getJobGroupName() { | ||
return jobGroupName; | ||
} | ||
|
||
public boolean isUseJobGroup() { | ||
return useJobGroup; | ||
} | ||
|
||
|
||
@Override | ||
public DescriptorImpl getDescriptor() { | ||
return (DescriptorImpl) super.getDescriptor(); | ||
} | ||
|
||
@Extension | ||
public static final class DescriptorImpl extends FolderPropertyDescriptor { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "XXX"; | ||
} | ||
|
||
public ListBoxModel getJobGroups() { | ||
return PropertyBasedJobInclusionStrategy.getPropertyBasesJobGroups(); | ||
} | ||
|
||
public boolean isUsed() { | ||
return PropertyBasedJobInclusionStrategy.getPropertyBasesJobGroups().size() > 0; | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionJobProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package jenkins.advancedqueue.jobinclusion.strategy; | ||
|
||
import hudson.Extension; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.Descriptor.FormException; | ||
import hudson.model.JobProperty; | ||
import hudson.model.JobPropertyDescriptor; | ||
import hudson.util.ListBoxModel; | ||
import net.sf.json.JSONObject; | ||
|
||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
|
||
public class JobInclusionJobProperty extends JobProperty<AbstractProject<?, ?>> { | ||
|
||
private boolean useJobGroup; | ||
|
||
private String jobGroupName; | ||
|
||
@DataBoundConstructor | ||
public JobInclusionJobProperty(Boolean useJobGroup, String jobGroupName) { | ||
this.useJobGroup = useJobGroup; | ||
this.jobGroupName = jobGroupName; | ||
} | ||
|
||
public String getJobGroupName() { | ||
return jobGroupName; | ||
} | ||
|
||
public boolean isUseJobGroup() { | ||
return useJobGroup; | ||
} | ||
|
||
|
||
@Override | ||
public DescriptorImpl getDescriptor() { | ||
return (DescriptorImpl) super.getDescriptor(); | ||
} | ||
|
||
@Extension | ||
public static final class DescriptorImpl extends JobPropertyDescriptor { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "XXX"; | ||
} | ||
|
||
public ListBoxModel getJobGroups() { | ||
return PropertyBasedJobInclusionStrategy.getPropertyBasesJobGroups(); | ||
} | ||
|
||
public boolean isUsed() { | ||
return PropertyBasedJobInclusionStrategy.getPropertyBasesJobGroups().size() > 0; | ||
} | ||
} | ||
|
||
} |
126 changes: 126 additions & 0 deletions
126
...n/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2014, Magnus Sandberg | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package jenkins.advancedqueue.jobinclusion.strategy; | ||
|
||
import hudson.Extension; | ||
import hudson.Plugin; | ||
import hudson.model.Descriptor; | ||
import hudson.model.Job; | ||
import hudson.util.ListBoxModel; | ||
|
||
import java.util.List; | ||
|
||
import jenkins.advancedqueue.DecisionLogger; | ||
import jenkins.advancedqueue.JobGroup; | ||
import jenkins.advancedqueue.PriorityConfiguration; | ||
import jenkins.advancedqueue.jobinclusion.JobInclusionStrategy; | ||
import jenkins.model.Jenkins; | ||
|
||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* @author Magnus Sandberg | ||
* @since 2.7 | ||
*/ | ||
public class PropertyBasedJobInclusionStrategy extends JobInclusionStrategy { | ||
|
||
@Extension | ||
static public class PropertyBasedJobInclusionStrategyDescriptor extends Descriptor<JobInclusionStrategy> { | ||
|
||
private boolean cloudbeesFolders = true; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
if (cloudbeesFolders) { | ||
return "Add Properties to Jobs and Cloudbees Folders to include in Job Group"; | ||
} else { | ||
return "Add Properties to Jobs to include in Job Group"; | ||
} | ||
} | ||
|
||
public PropertyBasedJobInclusionStrategyDescriptor() { | ||
Plugin plugin = Jenkins.getInstance().getPlugin("cloudbees-folder"); | ||
if(plugin == null || !plugin.getWrapper().isEnabled()){ | ||
cloudbeesFolders = false; | ||
} | ||
} | ||
|
||
}; | ||
|
||
private String name; | ||
|
||
@DataBoundConstructor | ||
public PropertyBasedJobInclusionStrategy(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean contains(DecisionLogger decisionLogger, Job<?, ?> job) { | ||
JobInclusionJobProperty property = job.getProperty(JobInclusionJobProperty.class); | ||
decisionLogger.addDecisionLog(2, "Checking for Job Property inclusion for [" + name + "]..."); | ||
if (property != null && property.isUseJobGroup()) { | ||
decisionLogger.addDecisionLog(3, "JobGroup is enabled on job, with JobGroup [" + property.getJobGroupName() | ||
+ "] ..."); | ||
boolean match = name.equals(property.getJobGroupName()); | ||
if (match) { | ||
decisionLogger.addDecisionLog(3, "Job is included in JobGroup ..."); | ||
} else { | ||
decisionLogger.addDecisionLog(3, "Job is not included in JobGroup ..."); | ||
} | ||
return match; | ||
} | ||
if (((PropertyBasedJobInclusionStrategyDescriptor) getDescriptor()).cloudbeesFolders) { | ||
String jobViewName = CloudbeesPropertyLoader.getJobViewName(decisionLogger, job); | ||
if (jobViewName == null) { | ||
return false; | ||
} | ||
boolean match = name.equals(jobViewName); | ||
if (match) { | ||
decisionLogger.addDecisionLog(4, "Job is included in JobGroup ..."); | ||
} else { | ||
decisionLogger.addDecisionLog(4, "Job is not included in JobGroup ..."); | ||
} | ||
return match; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public static ListBoxModel getPropertyBasesJobGroups() { | ||
List<JobGroup> jobGroups = PriorityConfiguration.get().getJobGroups(); | ||
ListBoxModel strategies = new ListBoxModel(); | ||
for (JobGroup jobGroup : jobGroups) { | ||
JobInclusionStrategy inclusionStrategy = jobGroup.getJobGroupStrategy(); | ||
if (inclusionStrategy instanceof PropertyBasedJobInclusionStrategy) { | ||
strategies.add(((PropertyBasedJobInclusionStrategy) inclusionStrategy).getName()); | ||
} | ||
} | ||
return strategies; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...kins/advancedqueue/jobinclusion/strategy/JobInclusionCloudbeesFolderProperty/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<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"> | ||
|
||
<j:if test="${descriptor.isUsed()}"> | ||
<f:optionalBlock name="useJobGroup" inline="true" checked="${instance.useJobGroup}" title="Include Jobs in this Folder to a Job Group"> | ||
<f:entry title="Job Group" field="jobGroupName"> | ||
<select name="jobGroupName"> | ||
<j:forEach var="jobGroup" items="${descriptor.getJobGroups()}"> | ||
<f:option value="${jobGroup.value}" selected="${jobGroup.value != instance.jobGroupName}">${jobGroup.name}</f:option> | ||
</j:forEach> | ||
</select> | ||
</f:entry> | ||
</f:optionalBlock> | ||
</j:if> | ||
|
||
</j:jelly> |
21 changes: 21 additions & 0 deletions
21
...esources/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionJobProperty/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<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"> | ||
|
||
<j:if test="${descriptor.isUsed()}"> | ||
<f:optionalBlock name="useJobGroup" inline="true" checked="${instance.useJobGroup}" title="Include this Job in a Job Group"> | ||
<f:entry title="Job Group" field="jobGroupName"> | ||
<select name="jobGroupName"> | ||
<j:forEach var="jobGroup" items="${descriptor.getJobGroups()}"> | ||
<f:option value="${jobGroup.value}" selected="${jobGroup.value != instance.jobGroupName}">${jobGroup.name}</f:option> | ||
</j:forEach> | ||
</select> | ||
</f:entry> | ||
</f:optionalBlock> | ||
</j:if> | ||
|
||
</j:jelly> |
5 changes: 5 additions & 0 deletions
5
...enkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategy/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:l="/lib/layout" xmlns:st="jelly:stapler"> | ||
<f:entry title="Job Group Name" field="name"> | ||
<f:textbox /> | ||
</f:entry> | ||
</j:jelly> |