Skip to content
Permalink
Browse files

Fix JENKINS-42699

- Add data parameter which can accept BUILD_URL, PROJECT_NAME, or BUILD_NUMBER
  • Loading branch information
slide committed Oct 22, 2017
1 parent 66ede22 commit 4d24aa5716f8d84e236824ab36d87c90a9897e8d
@@ -20,6 +20,9 @@
public static final String MACRO_NAME = "BUILD_CAUSE";
public static final String ALTERNATE_MACRO_NAME = "CAUSE";

@Parameter
public String data = null;

@Override
public boolean acceptsMacroName(String macroName) {
return macroName.equals(MACRO_NAME) || macroName.equals(ALTERNATE_MACRO_NAME);
@@ -55,12 +58,25 @@ private String formatCauses(List<Cause> causes) {
return "N/A";
}

List<String> causeNames = new LinkedList<String>();
List<String> causeData = new LinkedList<String>();
for (Cause cause : causes) {
causeNames.add(cause.getShortDescription());
if(data != null) {
if(cause instanceof Cause.UpstreamCause) {
Cause.UpstreamCause upstreamCause = (Cause.UpstreamCause) cause;
if (data.equals("BUILD_URL")) {
causeData.add(upstreamCause.getUpstreamUrl());
} else if (data.equals("PROJECT_NAME")) {
causeData.add(upstreamCause.getUpstreamProject());
} else if (data.equals("BUILD_NUMBER")) {
causeData.add("" + upstreamCause.getUpstreamBuild());
}
}
} else {
causeData.add(cause.getShortDescription());
}
}

return StringUtils.join(causeNames, ", ");
return StringUtils.join(causeData, ", ");
}
}

@@ -69,6 +69,29 @@ public void shouldReturnMultipleCausesSeperatedByCommas()
assertEquals("Cause1, Cause2, Cause3", buildCauseMacro.evaluate(build, listener, BuildCauseMacro.MACRO_NAME));
}

@Test
public void testCauseData()
throws Exception {
CauseAction causeAction = mock(CauseAction.class);
final Cause.UpstreamCause upstreamCause = mock(Cause.UpstreamCause.class);

when(upstreamCause.getUpstreamBuild()).thenReturn(3);
when(upstreamCause.getUpstreamProject()).thenReturn("Upstream");
when(upstreamCause.getUpstreamUrl()).thenReturn("http://localhost/jenkins/jobs/Upstream/3");

when(causeAction.getCauses()).thenReturn(new LinkedList<Cause>() {{
add(upstreamCause);
}});

when(build.getAction(CauseAction.class)).thenReturn(causeAction);
buildCauseMacro.data = "BUILD_NUMBER";
assertEquals("3", buildCauseMacro.evaluate(build,listener,BuildCauseMacro.MACRO_NAME));
buildCauseMacro.data = "PROJECT_NAME";
assertEquals("Upstream", buildCauseMacro.evaluate(build,listener,BuildCauseMacro.MACRO_NAME));
buildCauseMacro.data = "BUILD_URL";
assertEquals("http://localhost/jenkins/jobs/Upstream/3",buildCauseMacro.evaluate(build,listener,BuildCauseMacro.MACRO_NAME));
}

private class CauseStub extends Cause {
private String name;

0 comments on commit 4d24aa5

Please sign in to comment.