Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #6 from jacobrobertson/master
FIXED JENKINS-16305
- Loading branch information
Showing
3 changed files
with
137 additions
and
56 deletions.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
src/main/java/configurationslicing/project/AbstractSimpleProjectSlicer.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,82 @@ | ||
package configurationslicing.project; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import hudson.model.Item; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Hudson; | ||
import configurationslicing.UnorderedStringSlicer; | ||
|
||
public abstract class AbstractSimpleProjectSlicer extends UnorderedStringSlicer<AbstractProject<?,?>> { | ||
|
||
public AbstractSimpleProjectSlicer(AbstractSimpleProjectSliceSpec spec) { | ||
super(spec); | ||
} | ||
|
||
public static abstract class AbstractSimpleProjectSliceSpec extends UnorderedStringSlicerSpec<AbstractProject<?,?>> { | ||
|
||
private static final String DISABLED = "(Disabled)"; | ||
|
||
public String getDefaultValueString() { | ||
return DISABLED; | ||
} | ||
public String getName(AbstractProject<?, ?> item) { | ||
return item.getFullName(); | ||
} | ||
public List<String> getValues(AbstractProject<?, ?> item) { | ||
List<String> values = new ArrayList<String>(); | ||
String value = getValue(item); | ||
if (value != null) { | ||
values.add(value); | ||
} | ||
if (values.isEmpty()) { | ||
values.add(DISABLED); | ||
} | ||
return values; | ||
} | ||
protected abstract String getValue(AbstractProject<?, ?> project); | ||
|
||
@SuppressWarnings({ "unchecked" }) | ||
public List<AbstractProject<?, ?>> getWorkDomain() { | ||
return (List) Hudson.getInstance().getAllItems(getWorkDomainClass()); | ||
} | ||
/** | ||
* Override if needed. | ||
*/ | ||
protected Class<? extends Item> getWorkDomainClass() { | ||
return FreeStyleProject.class; | ||
} | ||
|
||
public boolean setValues(AbstractProject<?, ?> item, List<String> set) { | ||
try { | ||
String value; | ||
if (set.isEmpty()) { | ||
value = null; | ||
} else { | ||
value = set.iterator().next(); | ||
} | ||
if (DISABLED.equals(value)) { | ||
value = null; | ||
} | ||
String old = getValue(item); | ||
// check for equal - we don't want to trigger a change for no reason | ||
if (!StringUtils.equals(value, old)) { | ||
setValue(item, value); | ||
} | ||
return true; | ||
} catch (IOException ioe) { | ||
return false; | ||
} | ||
} | ||
/** | ||
* @throws IOException for the save operation | ||
*/ | ||
protected abstract void setValue(AbstractProject<?, ?> project, String value) throws IOException; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/configurationslicing/project/QuietPeriodSlicer.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,40 @@ | ||
package configurationslicing.project; | ||
|
||
import hudson.Extension; | ||
import hudson.model.AbstractProject; | ||
|
||
import java.io.IOException; | ||
|
||
@Extension | ||
public class QuietPeriodSlicer extends AbstractSimpleProjectSlicer { | ||
|
||
public QuietPeriodSlicer() { | ||
super(new QuietPeriodSliceSpec()); | ||
} | ||
|
||
public static class QuietPeriodSliceSpec extends AbstractSimpleProjectSlicer.AbstractSimpleProjectSliceSpec { | ||
|
||
public String getName() { | ||
return "Quiet Period Slicer"; | ||
} | ||
public String getUrl() { | ||
return "quietperiod"; | ||
} | ||
@Override | ||
protected String getValue(AbstractProject<?, ?> project) { | ||
int q = project.getQuietPeriod(); | ||
return String.valueOf(q); | ||
} | ||
@Override | ||
protected void setValue(AbstractProject<?, ?> project, String value) throws IOException { | ||
Integer q; | ||
try { | ||
q = Integer.parseInt(value); | ||
} catch (NumberFormatException e) { | ||
q = null; | ||
} | ||
project.setQuietPeriod(q); | ||
} | ||
} | ||
|
||
} |