From 376acf09ab131310703b6b86cee2cc7b59a53971 Mon Sep 17 00:00:00 2001 From: Jose Blas Camacho Taboada Date: Wed, 28 Nov 2018 09:51:46 +0100 Subject: [PATCH 1/3] JENKINS-49997 added new build condition for post --- .../model/conditions/Unsuccessful.groovy | 67 +++++++++++++++++++ .../model/conditions/Messages.properties | 1 + .../modeldefinition/PostStageTest.java | 47 +++++++++++++ .../resources/postStage/unsuccessful.groovy | 66 ++++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy create mode 100644 pipeline-model-definition/src/test/resources/postStage/unsuccessful.groovy diff --git a/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy b/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy new file mode 100644 index 000000000..50d5657a4 --- /dev/null +++ b/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy @@ -0,0 +1,67 @@ +/* + * The MIT License + * + * Copyright (c) 2016, CloudBees, Inc. + * + * 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 unstable builds. + * + * @author Andrew Bayer + */ +@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) + } + + 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 +} diff --git a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties index 58a464035..622d90ab2 100644 --- a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties +++ b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties @@ -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" diff --git a/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java b/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java index 236ee5e3f..cdef95c77 100644 --- a/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java +++ b/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java @@ -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 { + 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(); diff --git a/pipeline-model-definition/src/test/resources/postStage/unsuccessful.groovy b/pipeline-model-definition/src/test/resources/postStage/unsuccessful.groovy new file mode 100644 index 000000000..a621ababf --- /dev/null +++ b/pipeline-model-definition/src/test/resources/postStage/unsuccessful.groovy @@ -0,0 +1,66 @@ +/* + * The MIT License + * + * Copyright (c) 2016, CloudBees, Inc. + * + * 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" + } + } +} From 7f5b3dfbefd3847b4cb229cb93445db9a90d080c Mon Sep 17 00:00:00 2001 From: Jose Blas Camacho Taboada Date: Wed, 28 Nov 2018 15:50:40 +0100 Subject: [PATCH 2/3] JENKINS-49997 - cosmetic changesw --- .../modeldefinition/model/conditions/Unsuccessful.groovy | 4 ++-- .../modeldefinition/model/conditions/Messages.properties | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy b/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy index 50d5657a4..e5c2ea34f 100644 --- a/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy +++ b/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy @@ -32,9 +32,9 @@ import org.jenkinsci.plugins.workflow.job.WorkflowRun import javax.annotation.Nonnull /** - * A {@link BuildCondition} for matching unstable builds. + * A {@link BuildCondition} for matching Unsuccessful builds. * - * @author Andrew Bayer + * @author Jose Taboada */ @Extension(ordinal=650d) @Symbol("unsuccessful") class Unsuccessful extends BuildCondition { diff --git a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties index 622d90ab2..6a3eb8c5a 100644 --- a/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties +++ b/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties @@ -30,6 +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 +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" From 42b3c6c39b9c542d402e43d5d2ab6902e808f211 Mon Sep 17 00:00:00 2001 From: Jose Blas Camacho Taboada Date: Wed, 28 Nov 2018 17:08:41 +0100 Subject: [PATCH 3/3] JENKINS-49997 - cosmetic changes --- .../modeldefinition/model/conditions/Unsuccessful.groovy | 2 +- .../plugins/pipeline/modeldefinition/PostStageTest.java | 2 +- .../src/test/resources/postStage/unsuccessful.groovy | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy b/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy index e5c2ea34f..5ed235235 100644 --- a/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy +++ b/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Unsuccessful.groovy @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2016, CloudBees, Inc. + * Copyright (c) 2018, CloudBees, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java b/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java index cdef95c77..3ddcf1794 100644 --- a/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java +++ b/pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/PostStageTest.java @@ -107,7 +107,7 @@ public void withAllLocalUnsuccessfulWithSuccess() throws Exception { } @Test - public void withAllLocalUnsuccessfulWithNorBuilt() throws Exception { + public void withAllLocalUnsuccessfulWithNotBuilt() throws Exception { env(s).put("MAKE_RESULT", Result.NOT_BUILT.toString()).set(); expect(Result.NOT_BUILT, "unsuccessful") .logContains("I LOVE YOU VIRGINIA") diff --git a/pipeline-model-definition/src/test/resources/postStage/unsuccessful.groovy b/pipeline-model-definition/src/test/resources/postStage/unsuccessful.groovy index a621ababf..a58c30736 100644 --- a/pipeline-model-definition/src/test/resources/postStage/unsuccessful.groovy +++ b/pipeline-model-definition/src/test/resources/postStage/unsuccessful.groovy @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2016, CloudBees, Inc. + * Copyright (c) 2018, CloudBees, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal