diff --git a/src/main/java/se/diabol/jenkins/pipeline/resolver/CoreCauseResolver.java b/src/main/java/se/diabol/jenkins/pipeline/resolver/CoreCauseResolver.java index 9cdf3c150..fb03318dd 100644 --- a/src/main/java/se/diabol/jenkins/pipeline/resolver/CoreCauseResolver.java +++ b/src/main/java/se/diabol/jenkins/pipeline/resolver/CoreCauseResolver.java @@ -24,6 +24,8 @@ import hudson.model.User; import hudson.triggers.SCMTrigger; import hudson.triggers.TimerTrigger; +import jenkins.branch.BranchEventCause; +import jenkins.branch.BranchIndexingCause; import se.diabol.jenkins.pipeline.CauseResolver; import se.diabol.jenkins.pipeline.domain.TriggerCause; import se.diabol.jenkins.pipeline.util.JenkinsUtil; @@ -34,35 +36,41 @@ public class CoreCauseResolver extends CauseResolver { @Override public TriggerCause resolveCause(Cause cause) { if (cause instanceof Cause.UserIdCause) { - return new TriggerCause(TriggerCause.TYPE_MANUAL, "user " - + getDisplayName(((Cause.UserIdCause) cause).getUserName())); + return new TriggerCause(TriggerCause.TYPE_MANUAL, + "user " + getDisplayName(((Cause.UserIdCause) cause).getUserName())); } else if (cause instanceof Cause.RemoteCause) { return new TriggerCause(TriggerCause.TYPE_REMOTE, "remote trigger"); } else if (cause instanceof Cause.UpstreamCause) { - Cause.UpstreamCause upstreamCause = (Cause.UpstreamCause) cause; - AbstractProject upstreamProject = JenkinsUtil.getInstance().getItem(upstreamCause.getUpstreamProject(), - JenkinsUtil.getInstance(), AbstractProject.class); - StringBuilder causeString = new StringBuilder("upstream project"); - if (upstreamProject != null) { - - causeString.append(" ").append(upstreamProject.getDisplayName()); - AbstractBuild upstreamBuild = upstreamProject.getBuildByNumber(upstreamCause.getUpstreamBuild()); - if (upstreamBuild != null) { - causeString.append(" build ").append(upstreamBuild.getDisplayName()); - } - } - return new TriggerCause(TriggerCause.TYPE_UPSTREAM, causeString.toString()); + return new TriggerCause(TriggerCause.TYPE_UPSTREAM, getUpstreamCauseString((Cause.UpstreamCause) cause)); } else if (cause instanceof Cause.UpstreamCause.DeeplyNestedUpstreamCause) { return new TriggerCause(TriggerCause.TYPE_UPSTREAM, "upstream"); } else if (cause instanceof SCMTrigger.SCMTriggerCause) { - return new TriggerCause(TriggerCause.TYPE_SCM, "SCM"); + return new TriggerCause(TriggerCause.TYPE_SCM, "VCS"); } else if (cause instanceof TimerTrigger.TimerTriggerCause) { return new TriggerCause(TriggerCause.TYPE_TIMER, "timer"); + } else if (cause instanceof BranchEventCause) { + return new TriggerCause(TriggerCause.TYPE_SCM, "VCS"); + } else if (cause instanceof BranchIndexingCause) { + return new TriggerCause(TriggerCause.TYPE_SCM, "VCS indexing"); } else { return null; } } + private static String getUpstreamCauseString(Cause.UpstreamCause upstreamCause) { + AbstractProject upstreamProject = JenkinsUtil.getInstance().getItem(upstreamCause.getUpstreamProject(), + JenkinsUtil.getInstance(), AbstractProject.class); + String causeString = "upstream project"; + if (upstreamProject != null) { + causeString += " " + upstreamProject.getDisplayName(); + AbstractBuild upstreamBuild = upstreamProject.getBuildByNumber(upstreamCause.getUpstreamBuild()); + if (upstreamBuild != null) { + causeString += " build " + upstreamBuild.getDisplayName(); + } + } + return causeString; + } + protected static String getDisplayName(String userName) { User user = JenkinsUtil.getInstance().getUser(userName); if (user != null) { @@ -71,5 +79,4 @@ protected static String getDisplayName(String userName) { return "anonymous"; } } - } diff --git a/src/main/java/se/diabol/jenkins/pipeline/resolver/SubProjectRelationshipResolver.java b/src/main/java/se/diabol/jenkins/pipeline/resolver/SubProjectRelationshipResolver.java index 5d9a5ddcf..371242451 100644 --- a/src/main/java/se/diabol/jenkins/pipeline/resolver/SubProjectRelationshipResolver.java +++ b/src/main/java/se/diabol/jenkins/pipeline/resolver/SubProjectRelationshipResolver.java @@ -39,9 +39,7 @@ public List getDownstreamProjects(AbstractProject projec List result = new ArrayList<>(); for (SubProjectsAction action : Util.filter(project.getActions(), SubProjectsAction.class)) { for (BlockableBuildTriggerConfig config : action.getConfigs()) { - for (AbstractProject subProject : config.getProjectList(project.getParent(), null)) { - result.add(subProject); - } + result.addAll(config.getProjectList(project.getParent(), null)); } } return result; diff --git a/src/test/java/se/diabol/jenkins/pipeline/resolver/CoreCauseResolverTest.java b/src/test/java/se/diabol/jenkins/pipeline/resolver/CoreCauseResolverTest.java index 326c8cbd4..069a3e5c2 100644 --- a/src/test/java/se/diabol/jenkins/pipeline/resolver/CoreCauseResolverTest.java +++ b/src/test/java/se/diabol/jenkins/pipeline/resolver/CoreCauseResolverTest.java @@ -17,9 +17,6 @@ */ package se.diabol.jenkins.pipeline.resolver; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - import hudson.cli.BuildCommand; import hudson.model.Cause; import org.junit.Rule; @@ -28,6 +25,9 @@ import org.jvnet.hudson.test.WithoutJenkins; import se.diabol.jenkins.pipeline.domain.TriggerCause; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + public class CoreCauseResolverTest { @Rule @@ -37,21 +37,21 @@ public class CoreCauseResolverTest { @Test @WithoutJenkins - public void remote() { + public void shouldResolveRemoteCause() { TriggerCause triggerCause = resolver.resolveCause(new Cause.RemoteCause("hostname", "note")); assertNotNull(triggerCause); assertEquals(TriggerCause.TYPE_REMOTE, triggerCause.getType()); } @Test - public void cliCause() { + public void shouldResolveCliCause() { TriggerCause triggerCause = resolver.resolveCause(new BuildCommand.CLICause("username")); assertNotNull(triggerCause); assertEquals(TriggerCause.TYPE_MANUAL, triggerCause.getType()); } @Test - public void testNullUser() { + public void shouldYieldAnonymousForUnknownUser() { assertEquals("anonymous", CoreCauseResolver.getDisplayName(null)); }