Skip to content
Permalink
Browse files

[FIXED JENKINS-45984] Fix the crazy logic where 0 was semantically eq…

…uivalent to 'empty' but actually wasn't

- All this crazy just to match the way LogRotator in core behaves
  • Loading branch information...
stephenc committed Aug 4, 2017
1 parent 3eeb444 commit 6c9061bd71f5a48f4131d54300957744d7f824d8
@@ -186,7 +186,7 @@ protected ComputedFolder(ItemGroup parent, String name) {
protected final void init() {
super.init();
if (orphanedItemStrategy == null) {
orphanedItemStrategy = new DefaultOrphanedItemStrategy(true, "0", "0");
orphanedItemStrategy = new DefaultOrphanedItemStrategy(true, "", "");
}
if (triggers == null) {
triggers = new DescribableList<Trigger<?>,TriggerDescriptor>(this);
@@ -34,6 +34,7 @@
import hudson.model.TopLevelItem;
import hudson.tasks.LogRotator;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -86,6 +87,32 @@ public DefaultOrphanedItemStrategy(boolean pruneDeadBranches, @CheckForNull Stri
this.numToKeep = pruneDeadBranches ? fromString(numToKeepStr) : -1;
}

/**
* Programmatic constructor.
*
* @param pruneDeadBranches remove dead branches.
* @param daysToKeep how old a branch must be to remove.
* @param numToKeep how many branches to keep.
*/
public DefaultOrphanedItemStrategy(boolean pruneDeadBranches, int daysToKeep, int numToKeep) {
this.pruneDeadBranches = pruneDeadBranches;
this.daysToKeep = pruneDeadBranches && daysToKeep > 0? daysToKeep : -1;
this.numToKeep = pruneDeadBranches && numToKeep > 0 ? numToKeep : -1;
}

/**
* Migrate legacy configurations to correct configuration.
*
* @return the deserialized object.
* @throws ObjectStreamException if something goes wrong.
*/
private Object readResolve() throws ObjectStreamException {
if (daysToKeep == 0 || numToKeep == 0) {
return new DefaultOrphanedItemStrategy(pruneDeadBranches, daysToKeep, numToKeep);
}
return this;
}

/**
* Gets the number of days to keep dead branches.
*
@@ -204,7 +231,7 @@ private static boolean disabled(TopLevelItem item) {
@Override
public <I extends TopLevelItem> Collection<I> orphanedItems(ComputedFolder<I> owner, Collection<I> orphaned, TaskListener listener) throws IOException, InterruptedException {
List<I> toRemove = new ArrayList<I>();
if (pruneDeadBranches && (numToKeep != -1 || daysToKeep != -1)) {
if (pruneDeadBranches) {
listener.getLogger().printf("Evaluating orphaned items in %s%n", owner.getFullDisplayName());
List<I> candidates = new ArrayList<I>(orphaned);
Collections.sort(candidates, new Comparator<I>() {
@@ -268,6 +295,13 @@ public int compare(I i1, I i2) {
toRemove.add(item);
}
}
if (daysToKeep == -1 && numToKeep == -1) {
// special case, we have not said to keep for any count or duration, hence remove them all
for (I item : candidates) {
listener.getLogger().printf("Will remove %s%n", item.getDisplayName());
toRemove.add(item);
}
}
}
return toRemove;
}

0 comments on commit 6c9061b

Please sign in to comment.
You can’t perform that action at this time.