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

[FIXED JENKINS-53662] Add isRestartedStage() when condition #293

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -292,6 +292,28 @@ class Utils {
return cause != null
}

/**
* Check if this run was caused by a restart and we're currently running the restarted stage.
*/
@Whitelisted
static boolean isRestartedStage(CpsScript script) {
WorkflowRun r = script.$build()
RestartDeclarativePipelineCause cause = r.getCause(RestartDeclarativePipelineCause.class)
if (cause != null) {
CpsThread thread = CpsThread.current()
CpsFlowExecution execution = thread.execution

LinearBlockHoppingScanner scanner = new LinearBlockHoppingScanner()

FlowNode stageNode = execution.currentHeads.find { h ->
scanner.findFirstMatch(h, isStageWithOptionalName())
}

return stageNode.displayName == cause.originStage
}
return false
}

@Restricted(NoExternalUse.class)
static void updateRunAndJobActions(CpsScript script, String astUUID) throws Exception {
WorkflowRun r = script.$build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.when.impl;

import hudson.Extension;
import org.codehaus.groovy.ast.expr.Expression;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTWhenContent;
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.ASTParserUtils;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditional;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditionalDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.CheckForNull;

/**
* True if this run was restarted, and this particular stage is where it was restarted.
*/
public class IsRestartedStageConditional extends DeclarativeStageConditional<IsRestartedStageConditional> {

@DataBoundConstructor
public IsRestartedStageConditional() {
}

@Extension
@Symbol("isRestartedStage")
public static class DescriptorImpl extends DeclarativeStageConditionalDescriptor<IsRestartedStageConditional> {
@Override
public Expression transformToRuntimeAST(@CheckForNull ModelASTWhenContent original) {
return ASTParserUtils.transformWhenContentToRuntimeAST(original);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
~ 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.
-->

<p>
Execute the stage if the current run was restarted from a stage in an earlier run and the current stage is the stage we have restarted from.
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.when.impl

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditionalScript
import org.jenkinsci.plugins.workflow.cps.CpsScript

class IsRestartedStageConditionalScript extends DeclarativeStageConditionalScript<IsRestartedStageConditional> {
IsRestartedStageConditionalScript(CpsScript s, IsRestartedStageConditional c) {
super(s, c)
}

@Override
boolean evaluate() {
return Utils.isRestartedStage(script)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static Iterable<Object[]> configsWithErrors() {
result.add(new Object[]{"agentUnknownParamForType", Messages.ModelValidatorImpl_InvalidAgentParameter("fruit", "otherField", "[label, otherField, nested]")});
result.add(new Object[]{"notificationsSectionRemoved", "additional properties are not allowed"});
result.add(new Object[]{"unknownWhenConditional", Messages.ModelValidatorImpl_UnknownWhenConditional("banana",
"allOf, anyOf, branch, buildingTag, changeRequest, changelog, changeset, environment, equals, expression, isRestartedRun, not, tag")});
"allOf, anyOf, branch, buildingTag, changeRequest, changelog, changeset, environment, equals, expression, isRestartedRun, isRestartedStage, not, tag")});
result.add(new Object[]{"whenInvalidParameterType", Messages.ModelValidatorImpl_InvalidUnnamedParameterType("class java.lang.String", 4, Integer.class)});
result.add(new Object[]{"whenMissingRequiredParameter", Messages.ModelValidatorImpl_MissingRequiredStepParameter("value")});
result.add(new Object[]{"whenUnknownParameter", Messages.ModelValidatorImpl_InvalidStepParameter("banana", "name")});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,31 @@ public void isRestartedRunCondition() throws Exception {
j.assertLogNotContains("This shouldn't show up on second run", b2);
}

@Issue("JENKINS-53662")
@Test
public void isRestartedStageCondition() throws Exception {
WorkflowRun original = expect("restart", "isRestartedStageCondition")
.logContains("This shouldn't show up on second run")
.logNotContains("This should only run on restart",
"This shouldn't ever show up")
.go();

WorkflowJob p = original.getParent();

HtmlPage redirect = restartFromStageInUI(original, "restart");

assertNotNull(redirect);
assertEquals(p.getAbsoluteUrl(), redirect.getUrl().toString());

j.waitUntilNoActivity();
WorkflowRun b2 = p.getBuildByNumber(2);
assertNotNull(b2);
j.assertBuildStatusSuccess(b2);
j.assertLogContains("This should only run on restart", b2);
j.assertLogNotContains("This shouldn't show up on second run", b2);
j.assertLogNotContains("This shouldn't ever show up", b2);
}

@Issue("JENKINS-52261")
@Test
public void skippedParallelStagesMarkedNotExecuted() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.ChangeSetConditional;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.EnvironmentConditional;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.IsRestartedRunConditional;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.IsRestartedStageConditional;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.NotConditional;
import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jenkinsci.plugins.structs.describable.DescribableParameter;
Expand Down Expand Up @@ -563,6 +564,15 @@ public void whenIsRestartedRun() throws Exception {
"}");
}

@Issue("JENKINS-53662")
@Test
public void whenIsRestartedStage() throws Exception {
WhenDirective when = new WhenDirective(new IsRestartedStageConditional(), false);
assertGenerateDirective(when, "when {\n" +
" isRestartedStage()\n" +
"}");
}

/**
* Tests a form submitting part of the generator.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

pipeline {
agent any

stages {
stage('skip-on-restart') {
steps {
echo "This shouldn't show up on second run"
}
}
stage('restart') {
when {
isRestartedStage()
}
steps {
echo "This should only run on restart"
}
}
stage('after-restart') {
when {
isRestartedStage()
}
steps {
echo "This shouldn't ever show up"
}
}
}
}