Skip to content

Commit

Permalink
[JENKINS-21356] Add support for using JobProperty
Browse files Browse the repository at this point in the history
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
emsa23 committed Jan 20, 2014
1 parent 6744810 commit 6845575
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 0 deletions.
@@ -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;
}

}
@@ -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;
}
}

}
@@ -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;
}
}

}
@@ -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;
}

}
@@ -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>
@@ -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>
@@ -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>

0 comments on commit 6845575

Please sign in to comment.