Skip to content

Commit

Permalink
Fix JENKINS-44322
Browse files Browse the repository at this point in the history
  • Loading branch information
slide committed Jul 10, 2017
1 parent 9ad9f3b commit 6561df6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Expand Up @@ -14,6 +14,8 @@
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

@Extension
public class BuildStatusMacro extends DataBoundTokenMacro {
Expand All @@ -39,6 +41,12 @@ public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String
@Override
public String evaluate(Run<?,?> run, FilePath workspace, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {

// In the case of pipeline jobs, if the status hasn't been set to a non-null value, then it is considered "success"
if(run instanceof WorkflowRun && null == run.getResult()) {
return Result.SUCCESS.toString();
}

// Build can be "building" when the pre-build trigger is used. (and in this case there is not result set yet for the build)
// Reporting "success", "still failing", etc doesn't make sense in this case.
// When using on matrix build, the build is still in building stage when matrix aggregator end build trigger is fired, though
Expand Down
@@ -1,19 +1,34 @@
package org.jenkinsci.plugins.tokenmacro.impl;

import hudson.model.AbstractBuild;
import hudson.model.Result;
import hudson.util.StreamTaskListener;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;

import java.io.StringWriter;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@SuppressWarnings({"unchecked"})
public class BuildStatusMacroTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
@Issue("JENKINS-953")
@WithoutJenkins
public void testGetContent_whenBuildIsBuildingThenStatusShouldBeBuilding()
throws Exception {
AbstractBuild build = mock(AbstractBuild.class);
Expand All @@ -23,4 +38,17 @@ public void testGetContent_whenBuildIsBuildingThenStatusShouldBeBuilding()

assertEquals("Building", content);
}

@Test
@Issue("JENKINS-44322")
public void testGetContent_whenPipelineBuildIsBuildingThenStatusShouldBeSuccess() throws Exception {
WorkflowJob job = j.createProject(WorkflowJob.class);
job.setDefinition(new CpsFlowDefinition("node { echo tm('$BUILD_STATUS') }", false));
WorkflowRun run = job.scheduleBuild2(0).get();

j.assertBuildStatus(Result.SUCCESS, run);
StringWriter w = new StringWriter();
run.getLogText().writeLogTo(0, w);
assertTrue(w.toString().contains(Result.SUCCESS.toString()));
}
}

0 comments on commit 6561df6

Please sign in to comment.