Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-30801] Fix missing UpstreamCause
UpstreamCause was being removed if it contained a UserIdCause.
We really only want to remove the UserIdCause itself, and preserve
the rest of the CauseAction to save upstream / downstream
relationships.
  • Loading branch information
Dan Alvizu committed Oct 12, 2015
1 parent eaca628 commit 702b877
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
Expand Up @@ -576,7 +576,7 @@ private static ParametersAction mergeParameters(final ParametersAction base, fin

/**
* Filter out the list of actions so that it only includes {@link ParametersAction} and
* CauseActions, but exclude CauseActions containing a UserIdCause
* CauseActions, removing the UserIdAction from the CauseAction's list of Causes.
*
* We want to include CauseAction because that includes upstream cause actions, which
* are inherited in downstream builds.
Expand All @@ -597,9 +597,8 @@ private List<Action> filterActions(final List<Action> actions) {
for (final Action action : actions) {
if (action instanceof CauseAction) {
final CauseAction causeAction = (CauseAction) action;
if (!actionHasUserIdCause(causeAction)) {
retval.add(action);
}
filterOutUserIdCause(causeAction);
retval.add(causeAction);
} else if (action instanceof ParametersAction) {
retval.add(action);
}
Expand All @@ -608,17 +607,21 @@ private List<Action> filterActions(final List<Action> actions) {
}

/**
* Filter out {@link UserIdCause} from the given {@link CauseAction}.
*
* We want to do this because re-run will want to contribute its own
* {@link UserIdCause}, not copy it from the previous run.
*
* @param causeAction
* the causeAction to query
* the causeAction to remove UserIdCause from
* @return whether the action has a {@link UserIdCause}
*/
private boolean actionHasUserIdCause(CauseAction causeAction) {
private void filterOutUserIdCause(CauseAction causeAction) {
for (final Cause cause : causeAction.getCauses()) {
if (cause instanceof UserIdCause) {
return true;
causeAction.getCauses().remove(cause);
}
}
return false;
}

/**
Expand Down
Expand Up @@ -47,6 +47,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -220,7 +221,8 @@ public void testGetBuilderConfigDescriptors() throws Exception {

@Test
@Bug(22665)
public void testManualTriggerCause() throws Exception {
public void testManualTriggerCause() throws Exception
{
FreeStyleProject projectA = jenkins.createFreeStyleProject("A");
FreeStyleProject projectB = jenkins.createFreeStyleProject("B");
projectA.getPublishersList().add(new BuildPipelineTrigger("B", null));
Expand All @@ -239,6 +241,41 @@ public void testManualTriggerCause() throws Exception {
assertNotNull(cause);
//Check that cause is of core class Cause.UserIdCause and not MyUserIdCause
assertEquals(Cause.UserIdCause.class.getName(), cause.getClass().getName());
Cause.UpstreamCause upstreamCause = build.getCause(Cause.UpstreamCause.class);
assertNotNull(upstreamCause);
}

@Test
@Issue("JENKINS-24883")
public void testReRunBuildPipelineTrigger()
throws Exception
{
FreeStyleProject projectA = jenkins.createFreeStyleProject("A");
FreeStyleProject projectB = jenkins.createFreeStyleProject("B");
projectA.getPublishersList().add(new BuildPipelineTrigger("B", null));
jenkins.getInstance().rebuildDependencyGraph();

BuildPipelineView view = new BuildPipelineView("Pipeline", "Title", new DownstreamProjectGridBuilder("A"), "1", false, "");

jenkins.buildAndAssertSuccess(projectA);

view.triggerManualBuild(1, "B", "A");
jenkins.waitUntilNoActivity();

assertNotNull(projectB.getLastBuild());
FreeStyleBuild build = projectB.getLastBuild();
Cause.UserIdCause cause = build.getCause(Cause.UserIdCause.class);
assertNotNull(cause);
//Check that cause is of core class Cause.UserIdCause and not MyUserIdCause
assertEquals(Cause.UserIdCause.class.getName(), cause.getClass().getName());
Cause.UpstreamCause upstreamCause = build.getCause(Cause.UpstreamCause.class);
assertNotNull(upstreamCause);

// re-triggering the build should preserve upstream context (JENKINS-24883
view.rerunBuild(projectB.getLastBuild().getExternalizableId());
jenkins.waitUntilNoActivity();
build = projectB.getLastBuild();
upstreamCause = build.getCause(Cause.UpstreamCause.class);
assertNotNull(upstreamCause);
}
}

0 comments on commit 702b877

Please sign in to comment.