Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-49997 - Added Unsuccessful build condition for _post_ #300

Merged
merged 3 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
joseblas marked this conversation as resolved.
Show resolved Hide resolved
*
* 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 org.jenkinsci.plugins.pipeline.modeldefinition.model.conditions

import hudson.Extension
import hudson.model.Result
import org.jenkinsci.Symbol
import org.jenkinsci.plugins.pipeline.modeldefinition.model.BuildCondition
import org.jenkinsci.plugins.workflow.job.WorkflowRun

import javax.annotation.Nonnull

/**
* A {@link BuildCondition} for matching Unsuccessful builds.
*
* @author Jose Taboada
*/
@Extension(ordinal=650d) @Symbol("unsuccessful")
class Unsuccessful extends BuildCondition {
@Deprecated
@Override
boolean meetsCondition(@Nonnull WorkflowRun r) {
return meetsCondition(r, null, null)
}

@Override
boolean meetsCondition(@Nonnull WorkflowRun r, Object context, Throwable error) {
return isNotNull(r) || ! is(r, Result.SUCCESS)
joseblas marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean isNotNull(WorkflowRun r){
return r.getResult() != null && getExecutionResult(r) != null
}

private boolean is(WorkflowRun r, Result res){
Result execResult = getExecutionResult(r)
return execResult == res || r.getResult() == res
}

@Override
String getDescription() {
return Messages.Unsuccessful_Description()
}

static final long serialVersionUID = 1L
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ NotBuilt.Description=Run if the build status is "Not Built"
Success.Description=Run if the build status is "Success" or hasn\'t been set yet
Unstable.Description=Run if the build status is "Unstable"
Regression.Description=Run if the current build\'s status is worse than the previous build\'s status
Unsuccessful.Description=Run if the current build\'s status is "Aborted", "Failure" or "Unstable"
Cleanup.Description=Always run after all other conditions, regardless of build status
Fixed.Description=Run if the previous build was not successful and the current build\'s status is "Success"
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ public void withAllLocalUnstable() throws Exception {

}

@Test
public void withAllLocalUnsuccessfulWithUnstable() throws Exception {
env(s).put("MAKE_RESULT", Result.UNSTABLE.toString()).set();
expect(Result.UNSTABLE, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.logContains("I FAILED YOU, SORRY")
.logContains("I AM UNSTABLE")
.go();

}

@Test
public void withAllLocalUnsuccessfulWithAborted() throws Exception {
env(s).put("MAKE_RESULT", Result.ABORTED.toString()).set();
expect(Result.ABORTED, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.logContains("I FAILED YOU, SORRY")
.go();
}

@Test
public void withAllLocalUnsuccessfulWithFailure() throws Exception {
env(s).put("MAKE_RESULT", Result.FAILURE.toString()).set();
expect(Result.FAILURE, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.logContains("I FAILED YOU, SORRY")
.go();

}

@Test
public void withAllLocalUnsuccessfulWithSuccess() throws Exception {
env(s).put("MAKE_RESULT", Result.SUCCESS.toString()).set();
expect(Result.SUCCESS, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.go();

}
@Test
public void withAllLocalUnsuccessfulWithNorBuilt() throws Exception {
joseblas marked this conversation as resolved.
Show resolved Hide resolved
env(s).put("MAKE_RESULT", Result.NOT_BUILT.toString()).set();
expect(Result.NOT_BUILT, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.go();

}

@Test
public void withAllLocalFailure() throws Exception {
env(s).put("MAKE_RESULT", Result.FAILURE.toString()).set();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
joseblas marked this conversation as resolved.
Show resolved Hide resolved
*
* 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.
*/

pipeline {
agent {
label "here"
}
stages {
stage("foo") {
steps {
echo "hello"
script {
String res = env.MAKE_RESULT
if (res != null) {
echo "Setting build result ${res}"
currentBuild.result = res
// JENKINS-52114 - can't tell the difference between setting currentBuild.result in this stage
// and an error in a parallel stage, so let's error.
if (res == "FAILURE") {
error "Failing explicitly"
}
} else {
echo "All is well"
}
}
}
post {
unsuccessful {
echo "I FAILED YOU, SORRY"
}
unstable {
echo "I AM UNSTABLE"
}
always {
echo "I LOVE YOU VIRGINIA"
}
}
}
}
post {
always {
echo "And AAAAIIIAAAIAI"
}
}
}