Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UPSTREAM_RUN_NAME macro that will have the display name for the upstream job.
- Loading branch information
Showing
3 changed files
with
90 additions
and
15 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/org/jenkinsci/plugins/tokenmacro/impl/UpstreamRunNameMacro.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.jenkinsci.plugins.tokenmacro.impl; | ||
|
||
import hudson.model.AbstractBuild; | ||
import hudson.model.Cause; | ||
import hudson.model.CauseAction; | ||
import hudson.model.TaskListener; | ||
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro; | ||
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Created by acearl on 10/22/2015. | ||
*/ | ||
public class UpstreamRunNameMacro extends DataBoundTokenMacro { | ||
|
||
public static final String MACRO_NAME = "UPSTREAM_RUN_NAME"; | ||
|
||
@Override | ||
public boolean acceptsMacroName(String macroName) { | ||
return macroName.equals("UPSTREAM_RUN_NAME"); | ||
} | ||
|
||
@Override | ||
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException { | ||
CauseAction action = context.getAction(CauseAction.class); | ||
if(action != null) { | ||
for(Cause c : action.getCauses()) { | ||
if(c instanceof Cause.UpstreamCause) { | ||
Cause.UpstreamCause u = (Cause.UpstreamCause)c; | ||
if(u.getUpstreamRun() != null) { | ||
return u.getUpstreamRun().getDisplayName(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return "Unknown"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/org/jenkinsci/plugins/tokenmacro/impl/UpstreamRunNameMacroTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.jenkinsci.plugins.tokenmacro.impl; | ||
|
||
import hudson.model.Cause; | ||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.TaskListener; | ||
import hudson.util.StreamTaskListener; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Created by acearl on 10/23/2015. | ||
*/ | ||
public class UpstreamRunNameMacroTest { | ||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
@Issue("JENKINS-27542") | ||
public void testNoUpstreamJob() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject(); | ||
TaskListener listener = StreamTaskListener.fromStdout(); | ||
FreeStyleBuild build = project.scheduleBuild2(0).get(); | ||
|
||
UpstreamRunNameMacro content = new UpstreamRunNameMacro(); | ||
assertEquals("Unknown", content.evaluate(build, listener, UpstreamRunNameMacro.MACRO_NAME)); | ||
} | ||
|
||
@Test | ||
@Issue("JENKINS-27542") | ||
public void testUpstreamJob() throws Exception { | ||
FreeStyleProject upstream = j.createFreeStyleProject("FOO"); | ||
TaskListener listener = StreamTaskListener.fromStdout(); | ||
|
||
FreeStyleProject project = j.createFreeStyleProject("BAR"); | ||
|
||
FreeStyleBuild upstreamBuild = upstream.scheduleBuild2(0).get(); | ||
upstreamBuild.setDisplayName("FOO"); | ||
Cause.UpstreamCause cause = new Cause.UpstreamCause(upstreamBuild); | ||
|
||
FreeStyleBuild build = project.scheduleBuild2(0, cause).get(); | ||
|
||
UpstreamRunNameMacro content = new UpstreamRunNameMacro(); | ||
assertEquals("FOO", content.evaluate(build, listener, UpstreamRunNameMacro.MACRO_NAME)); | ||
} | ||
} |