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-70808] Add a specific isRestartable method that accept a sta… #596

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,6 +38,7 @@
import org.jenkinsci.plugins.pipeline.StageStatus;
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStage;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStages;
import org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
Expand Down Expand Up @@ -170,6 +171,30 @@ public List<String> getRestartableStages() {
return stages;
}

/**
* Returns whether a stage is restartable.
* @param stageName the stage name
* @return true is restartable, false otherwise
*/
public boolean isRestartable(String stageName) {
FlowExecution execution = getExecution();
if (execution != null) {
ExecutionModelAction execAction = run.getAction(ExecutionModelAction.class);
if (execAction != null) {
ModelASTStages stages = execAction.getStages();
if (stages != null) {
for (ModelASTStage s : stages.getStages()) {
if(s.getName().equals(stageName)) {
return !Utils.stageHasStatusOf(s.getName(), execution,
StageStatus.getSkippedForFailure(), StageStatus.getSkippedForUnstable());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So a failed/unstable stage is not restartable? The logic here is unclear. Is this just a copy / adaptation of code elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jglick it is the same logic as isRestartable() but specific to a single stage.

}
}
}
}
}
return false;
}

public String getCheckUrl() {
return Jenkins.get().getRootUrl() + run.getUrl() + getUrlName() + "/" + "checkStageName";
}
Expand Down
Expand Up @@ -72,6 +72,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -729,6 +730,53 @@ public void skippedParallelStagesMarkedNotExecuted() throws Exception {
assertFalse(stageStatusPredicate("second-parallel", StageStatus.getFailedAndContinued()).apply(secondRunSecondParallelStart));
}

@Issue("JENKINS-70808")
@Test
public void isRestartable() throws Exception {
WorkflowRun original = expect(Result.FAILURE, "restart", "isRestartable")
.logContains("This shouldn't show up on second run", "Odd numbered build, failing")
.logNotContains("This should only run on restart")
.go();

WorkflowJob p = original.getParent();

FlowExecution firstExecution = original.getExecution();
assertNotNull(firstExecution);
assertNotNull(firstExecution.getCauseOfFailure());

RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class);
assertNotNull(action);
assertTrue(action.isRestartEnabled());
List<String> restartableStages = action.getRestartableStages();
assertThat(restartableStages, is(Arrays.asList("skip-on-restart", "restart")));
assertTrue(action.isRestartable("skip-on-restart"));
assertTrue(action.isRestartable("restart"));
assertFalse(action.isRestartable("post-restart"));

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("Even numbered build, success", b2);
j.assertLogContains("Now we're post-restart", b2);
j.assertLogNotContains("This shouldn't show up on second run", b2);

action = b2.getAction(RestartDeclarativePipelineAction.class);
assertNotNull(action);
assertTrue(action.isRestartEnabled());
restartableStages = action.getRestartableStages();
assertThat(restartableStages, is(Arrays.asList("skip-on-restart", "restart", "post-restart")));
assertTrue(action.isRestartable("skip-on-restart"));
assertTrue(action.isRestartable("restart"));
assertTrue(action.isRestartable("post-restart"));

}

@Test
public void testDoRestartPipeline() throws Exception {
String jobName = "simplePipeline";
Expand Down
@@ -0,0 +1,51 @@
/*
* The MIT License
*
* Copyright (c) 2017, 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') {
steps {
script {
if (currentBuild.getNumber() % 2 == 1) {
error("Odd numbered build, failing")
} else {
echo "Even numbered build, success"
}
}
}
}
stage('post-restart') {
steps {
echo "Now we're post-restart"
}
}
}
}