Skip to content

Commit

Permalink
[FIXED JENKINS-41060] Add new fixed/regression post conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
abayer committed Feb 26, 2018
1 parent b492733 commit 3f433c8
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
@@ -0,0 +1,65 @@
/*
* The MIT License
*
* 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
* 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

/**
* A {@link BuildCondition} for matching builds where the previous build was not SUCCESS but the current build is.
*
* @author Andrew Bayer
*/
@Extension(ordinal=900d) @Symbol("fixed")
class Fixed extends BuildCondition {
@Override
boolean meetsCondition(WorkflowRun r) {
Result execResult = getExecutionResult(r)
// Only look at the previous completed build.
WorkflowRun prev = r.getPreviousCompletedBuild()

// Get the *worst* result of either the execution or the run. If the run's result is null, that's effectively
// SUCCESS.
Result runResult = execResult.combine(r.getResult() ?: Result.SUCCESS)

// If there's no previous build, we can't exactly be fixed, can we?
if (prev == null) {
return false
} else {
return runResult == Result.SUCCESS && prev.getResult() != Result.SUCCESS
}
}

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


static final long serialVersionUID = 1L

}
@@ -0,0 +1,65 @@
/*
* The MIT License
*
* 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
* 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

/**
* A {@link BuildCondition} for matching builds where the previous build was better than the current build.
*
* @author Andrew Bayer
*/
@Extension(ordinal=900d) @Symbol("regression")
class Regression extends BuildCondition {
@Override
boolean meetsCondition(WorkflowRun r) {
Result execResult = getExecutionResult(r)
// Only look at the previous completed build.
WorkflowRun prev = r.getPreviousCompletedBuild()

// Get the *worst* result of either the execution or the run. If the run's result is null, that's effectively
// SUCCESS.
Result runResult = execResult.combine(r.getResult() ?: Result.SUCCESS)

// If there's no previous build, we can't exactly be regressing, can we?
if (prev == null) {
return false
} else {
return runResult.isWorseThan(prev.getResult())
}
}

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


static final long serialVersionUID = 1L

}
Expand Up @@ -28,4 +28,6 @@ Changed.Description=Run if the current build's status is different than the prev
Failure.Description=Run if the build status is "Failure"
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"
Unstable.Description=Run if the build status is "Unstable"
Fixed.Description=Run if the current build is "Success" and the previous build is not
Regression.Description=Run if the current build's status is worse than the previous build's status
Expand Up @@ -77,20 +77,31 @@ public void postOnChanged() throws Exception {
j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b));
j.assertLogContains("[Pipeline] { (foo)", b);
j.assertLogContains("I FAILED", b);
j.assertLogNotContains("I REGRESSED", b);

WorkflowJob job = b.getParent();
job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("postOnChangeChanged"), true));
WorkflowRun b2 = j.buildAndAssertSuccess(job);
j.assertLogContains("[Pipeline] { (foo)", b2);
j.assertLogContains("hello", b2);
j.assertLogContains("I CHANGED", b2);
j.assertLogContains("I AM FIXED", b2);

// Now make sure we don't get any alert this time.
WorkflowRun b3 = j.buildAndAssertSuccess(job);
j.assertLogContains("[Pipeline] { (foo)", b3);
j.assertLogContains("hello", b3);
j.assertLogNotContains("I CHANGED", b3);
j.assertLogNotContains("I FAILED", b3);
j.assertLogNotContains("I AM FIXED", b3);

// And one more time for regression
job.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("postOnChangeFailed"), true));
WorkflowRun b4 = job.scheduleBuild2(0).waitForStart();
j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b4));
j.assertLogContains("[Pipeline] { (foo)", b4);
j.assertLogContains("I FAILED", b4);
j.assertLogContains("I REGRESSED", b4);
}

@Test
Expand Down
Expand Up @@ -35,6 +35,9 @@ pipeline {
changed {
echo "I CHANGED"
}
fixed {
echo "I AM FIXED"
}
failure {
echo "I FAILED"
}
Expand Down
Expand Up @@ -35,6 +35,9 @@ pipeline {
changed {
echo "I CHANGED"
}
regression {
echo "I REGRESSED"
}
failure {
echo "I FAILED"
}
Expand Down

0 comments on commit 3f433c8

Please sign in to comment.