Skip to content

Commit

Permalink
JENKINS-17761 Make ws-cleanup configurable to run or not run on any g…
Browse files Browse the repository at this point in the history
…iven build status instead of just success.
  • Loading branch information
Anusien committed May 2, 2013
1 parent ab50a1c commit 799991e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
43 changes: 33 additions & 10 deletions src/main/java/hudson/plugins/ws_cleanup/WsCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,29 @@ public class WsCleanup extends Notifier implements MatrixAggregatable {

private final List<Pattern> patterns;
private final boolean deleteDirs;
private final boolean skipWhenFailed;

private final boolean cleanWhenSuccess;
private final boolean cleanWhenUnstable;
private final boolean cleanWhenFailure;
private final boolean cleanWhenNotBuilt;
private final boolean cleanWhenAborted;

private final boolean notFailBuild;
private final boolean cleanupMatrixParent;

@DataBoundConstructor
// FIXME can't get repeteable to work with a List<String>
public WsCleanup(List<Pattern> patterns, boolean deleteDirs, final boolean skipWhenFailed, final boolean notFailBuild, final boolean cleanupMatrixParent) {
public WsCleanup(List<Pattern> patterns, boolean deleteDirs, final boolean cleanWhenSuccess, final boolean cleanWhenUnstable, final boolean cleanWhenFailure,
final boolean cleanWhenNotBuilt, final boolean cleanWhenAborted, final boolean notFailBuild, final boolean cleanupMatrixParent) {
this.patterns = patterns;
this.deleteDirs = deleteDirs;
this.skipWhenFailed = skipWhenFailed;
this.notFailBuild = notFailBuild;
this.cleanupMatrixParent = cleanupMatrixParent;
this.cleanWhenSuccess = cleanWhenSuccess;
this.cleanWhenUnstable = cleanWhenUnstable;
this.cleanWhenFailure = cleanWhenFailure;
this.cleanWhenNotBuilt = cleanWhenNotBuilt;
this.cleanWhenAborted = cleanWhenAborted;
}

public List<Pattern> getPatterns(){
Expand All @@ -53,10 +64,6 @@ public List<Pattern> getPatterns(){
public boolean getDeleteDirs(){
return deleteDirs;
}

public boolean getSkipWhenFailed() {
return skipWhenFailed;
}

public boolean getNotFailBuild() {
return notFailBuild;
Expand All @@ -65,6 +72,21 @@ public boolean getNotFailBuild() {
public boolean getCleanupMatrixParent() {
return cleanupMatrixParent;
}

private boolean shouldCleanBuildBasedOnState(Result result) {
if(result.equals(Result.SUCCESS))
return this.cleanWhenSuccess;
if(result.equals(Result.UNSTABLE))
return this.cleanWhenUnstable;
if(result.equals(Result.FAILURE))
return this.cleanWhenFailure;
if(result.equals(Result.NOT_BUILT))
return this.cleanWhenNotBuilt;
if(result.equals(Result.ABORTED))
return this.cleanWhenAborted;

return true;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
Expand All @@ -73,8 +95,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
try {
if (workspace == null || !workspace.exists())
return true;
if ( this.skipWhenFailed && build.getResult().isWorseOrEqualTo(Result.FAILURE)) {
listener.getLogger().append("skipped for failed build\n\n");
if(!shouldCleanBuildBasedOnState(build.getResult())) {
listener.getLogger().append("Skipped based on build state " + build.getResult() + "\n\n");
return true;
}
if (patterns == null || patterns.isEmpty()) {
Expand All @@ -97,7 +119,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

public MatrixAggregator createAggregator(MatrixBuild build, Launcher launcher, BuildListener listener) {
if(cleanupMatrixParent)
return new WsCleanupMatrixAggregator(build, launcher, listener, patterns, deleteDirs, skipWhenFailed, notFailBuild);
return new WsCleanupMatrixAggregator(build, launcher, listener, patterns, deleteDirs, cleanWhenSuccess, cleanWhenUnstable, cleanWhenFailure,
cleanWhenNotBuilt, cleanWhenAborted, notFailBuild);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,47 @@ public class WsCleanupMatrixAggregator extends MatrixAggregator {

private final List<Pattern> patterns;
private final boolean deleteDirs;
private final boolean skipWhenFailed;
private final boolean notFailBuild;

private final boolean cleanWhenSuccess;
private final boolean cleanWhenUnstable;
private final boolean cleanWhenFailure;
private final boolean cleanWhenNotBuilt;
private final boolean cleanWhenAborted;

public WsCleanupMatrixAggregator(MatrixBuild build, Launcher launcher, BuildListener listener, List<Pattern> patterns,
boolean deleteDirs, boolean skipWhenFailed, boolean notFailBuid) {
boolean deleteDirs, final boolean cleanWhenSuccess, final boolean cleanWhenUnstable, final boolean cleanWhenFailure,
final boolean cleanWhenNotBuilt, final boolean cleanWhenAborted, final boolean notFailBuild) {
super(build, launcher, listener);
this.patterns = patterns;
this.deleteDirs = deleteDirs;
this.skipWhenFailed = skipWhenFailed;
this.notFailBuild = notFailBuid;
this.cleanWhenSuccess = cleanWhenSuccess;
this.cleanWhenUnstable = cleanWhenUnstable;
this.cleanWhenFailure = cleanWhenFailure;
this.cleanWhenNotBuilt = cleanWhenNotBuilt;
this.cleanWhenAborted = cleanWhenAborted;
this.notFailBuild = notFailBuild;
}

public boolean endBuild() throws InterruptedException, IOException {
return doWorkspaceCleanup();
}


private boolean shouldCleanBuildBasedOnState(Result result) {
if(result.equals(Result.SUCCESS))
return this.cleanWhenSuccess;
if(result.equals(Result.UNSTABLE))
return this.cleanWhenUnstable;
if(result.equals(Result.FAILURE))
return this.cleanWhenFailure;
if(result.equals(Result.NOT_BUILT))
return this.cleanWhenNotBuilt;
if(result.equals(Result.ABORTED))
return this.cleanWhenAborted;

return true;
}

private boolean doWorkspaceCleanup() throws IOException, InterruptedException {
listener.getLogger().append("\nDeleting matrix project workspace... \n");

Expand Down Expand Up @@ -66,10 +91,10 @@ private boolean doWorkspaceCleanup() throws IOException, InterruptedException {
try {
if (workspace == null || !workspace.exists())
return true;
if ( this.skipWhenFailed && build.getResult().isWorseOrEqualTo(Result.FAILURE)) {
listener.getLogger().append("skipped for failed build\n\n");
return true;
}
if(!shouldCleanBuildBasedOnState(build.getResult())) {
listener.getLogger().append("Skipped based on build state " + build.getResult() + "\n\n");
return true;
}
if (patterns == null || patterns.isEmpty()) {
workspace.deleteRecursive();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
<f:entry title="${%Apply pattern also on directories}">
<f:checkbox field="deleteDirs" checked="${it.deleteDirs}" />
</f:entry>
<f:entry title="${%Skip when build failed}">
<f:checkbox field="skipWhenFailed" checked="${it.skipWhenFailed}" />
</f:entry>
<f:entry title="${%Clean when status is}">
<f:checkbox field="cleanWhenSuccess" checked="${it.cleanWhenSuccess}" title="${%Success}"/>
<f:checkbox field="cleanWhenUnstable" checked="${it.cleanWhenUnstable}" title="${%Unstable}"/>
<f:checkbox field="cleanWhenFailure" checked="${it.cleanWhenFailure}" title="${%Failure}"/>
<f:checkbox field="cleanWhenNotBuilt" checked="${it.cleanWhenNotBuilt}" title="${%Not Built}"/>
<f:checkbox field="cleanWhenAborted" checked="${it.cleanWhenAborted}" title="${%Aborted}"/>
</f:entry>
<f:entry title="${%Don't fail the build if cleanup fails}" help="/plugin/ws-cleanup/help/notFailBuild.html">
<f:checkbox field="notFailBuild" checked="${it.notFailBuild}" />
</f:entry>
Expand Down

0 comments on commit 799991e

Please sign in to comment.