Skip to content
Permalink
Browse files

[FIXED JENKINS-48090] Merge CauseActions when scheduling

- Should have been specified that SCMSource.fetchActions should not return CauseAction, but we missed that so let's just handle the merge as it is not a major crime
  • Loading branch information...
stephenc committed Dec 4, 2017
1 parent 5f1a1af commit 0fa29726b84dc1f1cf1c069240a5803e911627f0
Showing with 73 additions and 4 deletions.
  1. +29 −3 src/main/java/jenkins/branch/MultiBranchProject.java
  2. +44 −1 src/test/java/integration/BrandingTest.java
@@ -653,9 +653,35 @@ private void scheduleBuild(BranchProjectFactory<P, R> factory, final P item, SCM
name, getDisplayName());
return;
}
Action[] _actions = new Action[actions.length + 1];
_actions[0] = new CauseAction(cause);
System.arraycopy(actions, 0, _actions, 1, actions.length);
// JENKINS-48090 see Queue.Item.getCauses() which only operates on the first CauseAction
// We need to merge any additional causes with the causes we are supplying
int causeCount = 0;
for (Action action : actions) {
if (action instanceof CauseAction) {
causeCount++;
}
}
Action[] _actions;
if (causeCount == 0) {
// no existing causes, insert new one at start
_actions = new Action[actions.length + 1];
_actions[0] = new CauseAction(cause);
System.arraycopy(actions, 0, _actions, 1, actions.length);
} else {
// existing causes, filter out and insert aggregate at start
_actions = new Action[actions.length + 1 - causeCount];
int i = 1;
List<Cause> causeList = new ArrayList<>();
Collections.addAll(causeList, cause);
for (Action a: actions) {
if (a instanceof CauseAction) {
causeList.addAll(((CauseAction) a).getCauses());
} else {
_actions[i++] = a;
}
}
_actions[0] = new CauseAction(causeList);
}
if (ParameterizedJobMixIn.scheduleBuild2(item, 0, _actions) != null) {
listener.getLogger().println("Scheduled build for branch: " + name);
try {
@@ -27,25 +27,35 @@

import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.Action;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.FreeStyleProject;
import hudson.model.ListView;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.views.JobColumn;
import hudson.views.StatusColumn;
import hudson.views.WeatherColumn;
import integration.harness.BasicMultiBranchProject;
import integration.harness.BasicMultiBranchProjectFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import jenkins.branch.Branch;
import jenkins.branch.BranchIndexingCause;
import jenkins.branch.BranchSource;
import jenkins.branch.DescriptionColumn;
import jenkins.branch.MultiBranchProject;
import jenkins.branch.NameMangler;
import jenkins.branch.OrganizationFolder;
import jenkins.scm.api.SCMEvent;
import jenkins.scm.api.SCMEvents;
import jenkins.scm.api.SCMHeadEvent;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSourceEvent;
import jenkins.scm.api.metadata.ObjectMetadataAction;
import jenkins.scm.impl.mock.MockSCMController;
@@ -57,12 +67,14 @@
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
@@ -154,6 +166,37 @@ public void given_multibranch_when_branches_then_runBrandingPresent()
}
}

@Test
@Issue("JENKINS-48090")
public void given_multibranch_when_runBrandingIncludesAdditionalCauses_then_causesMerged() throws Exception {
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("foo");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches()){
@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMRevision revision, SCMHeadEvent event,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
List<Action> result = new ArrayList<>(super.retrieveActions(revision, event, listener));
result.add(new CauseAction(new Cause.UserIdCause()));
result.add(new CauseAction(new Cause.RemoteCause("test", "data")));
return result;
}
}));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
CauseAction causeAction = prj.getItem("master").getBuildByNumber(1).getAction(CauseAction.class);
assertThat(causeAction.getCauses(), containsInAnyOrder(
instanceOf(BranchIndexingCause.class),
instanceOf(Cause.UserIdCause.class),
instanceOf(Cause.RemoteCause.class)
));
}
}

@Test
public void given_orgFolder_when_noNavigatorsDefined_then_noNavigatorBrandingPresent() throws Exception {
try (MockSCMController c = MockSCMController.create()) {

0 comments on commit 0fa2972

Please sign in to comment.
You can’t perform that action at this time.