Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #118 from stephenc/jenkins-48090
[FIXED JENKINS-48090] Merge CauseActions when scheduling
  • Loading branch information
stephenc committed Dec 4, 2017
2 parents 2feeeac + 68baf70 commit 0454746
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
34 changes: 30 additions & 4 deletions src/main/java/jenkins/branch/MultiBranchProject.java
Expand Up @@ -647,15 +647,41 @@ protected void computeChildren(final ChildObserver<P> observer, final TaskListen
}

private void scheduleBuild(BranchProjectFactory<P, R> factory, final P item, SCMRevision revision,
TaskListener listener, String name, Cause[] cause, Action... actions) {
TaskListener listener, String name, Cause[] causes, Action... actions) {
if (!isBuildable()) {
listener.getLogger().printf("Did not schedule build for branch: %s (%s is disabled)%n",
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(causes);
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> _causes = new ArrayList<>();
Collections.addAll(_causes, causes);
for (Action a: actions) {
if (a instanceof CauseAction) {
_causes.addAll(((CauseAction) a).getCauses());
} else {
_actions[i++] = a;
}
}
_actions[0] = new CauseAction(_causes);
}
if (ParameterizedJobMixIn.scheduleBuild2(item, 0, _actions) != null) {
listener.getLogger().println("Scheduled build for branch: " + name);
try {
Expand Down
45 changes: 44 additions & 1 deletion src/test/java/integration/BrandingTest.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 0454746

Please sign in to comment.