Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-23552] Replace "Allow priorities directly on Jobs"
 * Added new JobProperty Strategy
 * Removed code for special handling of priorities on Jobs
 * Only show the JobProperty if some JobGroup is using it
 * Removed the checkbox on the main config page
  • Loading branch information
emsa23 committed Jun 25, 2014
1 parent 748ad0e commit 318597a
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 33 deletions.
Expand Up @@ -24,15 +24,16 @@
package jenkins.advancedqueue;

import hudson.Extension;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import hudson.model.AbstractProject;
import hudson.model.Descriptor.FormException;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import hudson.util.ListBoxModel;

import java.io.IOException;
import java.util.logging.Logger;

import jenkins.advancedqueue.priority.PriorityStrategy;
import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -75,6 +76,22 @@ public DescriptorImpl getDescriptor() {

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {

private PriorityConfigurationCallback dummyCallback = new PriorityConfigurationCallback() {

public PriorityConfigurationCallback setPrioritySelection(int priority, int jobGroupId, PriorityStrategy reason) {
return this;
}

public PriorityConfigurationCallback setPrioritySelection(int priority) {
return this;
}

public PriorityConfigurationCallback addDecisionLog(int indent, String log) {
return this;
}
};

@Override
public String getDisplayName() {
return Messages.AdvancedQueueSorterJobProperty_displayName();
Expand All @@ -89,9 +106,10 @@ public ListBoxModel getPriorities() {
return items;
}

public boolean isUsed() {
PrioritySorterConfiguration configuration = PrioritySorterConfiguration.get();
return configuration.getAllowPriorityOnJobs();
public boolean isUsed(Job<?,?> owner) {
PriorityConfiguration configuration = PriorityConfiguration.get();
JobGroup jobGroup = configuration.getJobGroup(dummyCallback, owner);
return jobGroup != null;
}
}
}
11 changes: 0 additions & 11 deletions src/main/java/jenkins/advancedqueue/PriorityConfiguration.java
Expand Up @@ -226,17 +226,6 @@ private PriorityConfigurationCallback getPriorityInternal(Queue.Item item, Prior
return priorityCallback.setPrioritySelection(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority());
}

if (PrioritySorterConfiguration.get().getAllowPriorityOnJobs()) {
AdvancedQueueSorterJobProperty priorityProperty = job.getProperty(AdvancedQueueSorterJobProperty.class);
if (priorityProperty != null && priorityProperty.getUseJobPriority()) {
int priority = priorityProperty.priority;
if (priority == PriorityCalculationsUtil.getUseDefaultPriorityPriority()) {
priority = PrioritySorterConfiguration.get().getStrategy().getDefaultPriority();
}
priorityCallback.addDecisionLog(0, "Using priority taken directly from the Job");
return priorityCallback.setPrioritySelection(priority);
}
}
//
JobGroup jobGroup = getJobGroup(priorityCallback, job);
if (jobGroup != null) {
Expand Down
Expand Up @@ -64,6 +64,10 @@ public class PrioritySorterConfiguration extends GlobalConfiguration {
private Integer legacyMaxPriority = Integer.MAX_VALUE;
private Integer legacyMinPriority = Integer.MIN_VALUE;

/**
* @deprecated used in 2.x - replaces with XXX
*/
@Deprecated
private boolean allowPriorityOnJobs;

private boolean onlyAdminsMayEditPriorityConfiguration = false;
Expand All @@ -77,7 +81,7 @@ public static void init() {
PrioritySorterConfiguration prioritySorterConfiguration = PrioritySorterConfiguration.get();
// Make sure default is good for updating from legacy
prioritySorterConfiguration.strategy = DEFAULT_STRATEGY; // TODO: replace with class ref
prioritySorterConfiguration.allowPriorityOnJobs = true;
prioritySorterConfiguration.allowPriorityOnJobs = false;
// Check for legacy
prioritySorterConfiguration.checkLegacy();
if (!prioritySorterConfiguration.getLegacyMode()) {
Expand All @@ -98,7 +102,6 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
}
//
onlyAdminsMayEditPriorityConfiguration = json.getBoolean("onlyAdminsMayEditPriorityConfiguration");
allowPriorityOnJobs = json.getBoolean("allowPriorityOnJobs");
if (getLegacyMode()) {
Boolean advanced = json.getBoolean("advanced");
if (advanced) {
Expand All @@ -115,10 +118,6 @@ private final boolean getLegacyMode() {
return legacyMode;
}

public boolean getAllowPriorityOnJobs() {
return allowPriorityOnJobs;
}

public boolean getOnlyAdminsMayEditPriorityConfiguration() {
return onlyAdminsMayEditPriorityConfiguration;
}
Expand Down Expand Up @@ -254,7 +253,7 @@ private void convertFromLegacyToAdvanced() {
for (AbstractProject<?, ?> project : allProjects) {
PrioritySorterJobProperty legacyPriorityProperty = project
.getProperty(PrioritySorterJobProperty.class);
if (legacyPriorityProperty != null && getAllowPriorityOnJobs()) {
if (legacyPriorityProperty != null) {
int advancedPriority = legacyPriorityToAdvancedPriority(legacyMinPriority, legacyMaxPriority,
strategy.getNumberOfPriorities(), legacyPriorityProperty.priority);
AdvancedQueueSorterJobProperty advancedQueueSorterJobProperty = new AdvancedQueueSorterJobProperty(
Expand All @@ -273,7 +272,7 @@ private void convertFromLegacyToAdvanced() {
LOGGER.warning("Failed to remove Legacy Job Priority From " + project.getName());
}
}

// Finally, switch Legacy Mode
legacyMode = false;
}
Expand Down
@@ -0,0 +1,75 @@
/*
* 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.priority.strategy;

import hudson.Extension;
import hudson.model.Job;
import hudson.model.Queue;
import hudson.model.Queue.Item;
import jenkins.advancedqueue.AdvancedQueueSorterJobProperty;

import org.kohsuke.stapler.DataBoundConstructor;

/**
* @author Magnus Sandberg
* @since 3.0
*/
public class JobPropertyStrategy extends AbstractDynamicPriorityStrategy {

@Extension
public static class UserIdCauseStrategyDescriptor extends AbstractDynamicPriorityStrategyDescriptor {

public UserIdCauseStrategyDescriptor() {
super("Take the priority from Property on the Job");
}

}

@DataBoundConstructor
public JobPropertyStrategy() {
}


private Integer getPriorityInternal(Queue.Item item) {
if(item.task instanceof Job<?, ?>) {
Job<?, ?> job = (Job<?, ?>) item.task;
AdvancedQueueSorterJobProperty priorityProperty = job.getProperty(AdvancedQueueSorterJobProperty.class);
if (priorityProperty != null && priorityProperty.getUseJobPriority()) {
return priorityProperty.priority;
}
}
return null;
}

@Override
public boolean isApplicable(Queue.Item item) {
return getPriorityInternal(item) != null;
}

@Override
public int getPriority(Item item) {
return getPriorityInternal(item);
}

}
Expand Up @@ -6,7 +6,7 @@
xmlns:t="/lib/hudson"
xmlns:f="/lib/form">

<j:if test="${descriptor.isUsed()}">
<j:if test="${descriptor.isUsed(it)}">
<f:optionalBlock name="useJobPriority" inline="true" checked="${instance.useJobPriority}" title="Use specific priority for this Job">
<f:entry title="Job Priority" field="priority">
<select name="priority">
Expand Down
Expand Up @@ -9,7 +9,6 @@
<div>
<h2>Job Priorities</h2>
Assign Priorities by creating JobGroups. Priorities are assigned top down by first match.
If a Job has a Priority assigned directly on the Job that Priority will be used and the below will not be used.
<br/>
<br/>
</div>
Expand Down
Expand Up @@ -5,9 +5,6 @@
<f:optionalBlock title="${%Switch to Advanced Priority Sorter Mode}"
checked="false" inline="true" name="advanced">
<f:dropdownDescriptorSelector field="strategy" title="${%Strategy}"/>
<f:entry title="Allow priorities directly on Jobs" description="Check if you still want to have the ability to set priority directly on Jobs" field="allowPriorityOnJobs">
<f:checkbox id="ps_allowPriorityOnJobs" name="allowPriorityOnJobs" checked="${allowPriorityOnJobs}"/>
</f:entry>
<f:entry title="Only Admins can edit Job Priorities" description="Check if only Administrators should be allowed to view and edit the Job Priorities" field="onlyAdminsMayEditPriorityConfiguration">
<f:checkbox name="onlyAdminsMayEditPriorityConfiguration" checked="${onlyAdminsMayEditPriorityConfiguration}"/>
</f:entry>
Expand All @@ -16,9 +13,6 @@
<!-- TODO: remove code duplication -->
<j:if test="${not instance.legacyMode}">
<f:dropdownDescriptorSelector field="strategy" title="${%Strategy}"/>
<f:entry title="Allow priorities directly on Jobs" description="Check if you still want to have the ability to set priority directly on Jobs" field="allowPriorityOnJobs">
<f:checkbox id="ps_allowPriorityOnJobs" name="allowPriorityOnJobs" checked="${allowPriorityOnJobs}"/>
</f:entry>
<f:entry title="Only Admins can edit Job Priorities" description="Check if only Administrators should be allowed to view and edit the Job Priorities" field="onlyAdminsMayEditPriorityConfiguration">
<f:checkbox name="onlyAdminsMayEditPriorityConfiguration" checked="${onlyAdminsMayEditPriorityConfiguration}"/>
</f:entry>
Expand Down
@@ -0,0 +1,3 @@
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<f:description>Take the priority from the the Priority assigned directly on the Job.</f:description>
</j:jelly>

0 comments on commit 318597a

Please sign in to comment.