Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
JENKINS-54701 Support branch indexing/event as trigger cause
Browse files Browse the repository at this point in the history
* Rename SCM label (source control management) to VCS (version control system)
  • Loading branch information
tommysdk authored Nov 21, 2018
1 parent 2683ec8 commit e9b9959
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -71,5 +79,4 @@ protected static String getDisplayName(String userName) {
return "anonymous";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public List<AbstractProject> getDownstreamProjects(AbstractProject<?, ?> projec
List<AbstractProject> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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));
}

Expand Down

0 comments on commit e9b9959

Please sign in to comment.