use of hudson.model.DependencyGraph in project hudson-2.x by hudson.
the class BuildTrigger method execute.
/**
* Convenience method to trigger downstream builds.
*
* @param build
* The current build. Its downstreams will be triggered.
* @param listener
* Receives the progress report.
*/
public static boolean execute(AbstractBuild build, BuildListener listener) {
PrintStream logger = listener.getLogger();
// Check all downstream Project of the project, not just those defined by BuildTrigger
final DependencyGraph graph = Hudson.getInstance().getDependencyGraph();
List<Dependency> downstreamProjects = new ArrayList<Dependency>(graph.getDownstreamDependencies(build.getProject()));
// Sort topologically
Collections.sort(downstreamProjects, new Comparator<Dependency>() {
public int compare(Dependency lhs, Dependency rhs) {
// Swapping lhs/rhs to get reverse sort:
return graph.compare(rhs.getDownstreamProject(), lhs.getDownstreamProject());
}
});
for (Dependency dep : downstreamProjects) {
AbstractProject p = dep.getDownstreamProject();
if (p.isDisabled()) {
logger.println(Messages.BuildTrigger_Disabled(p.getName()));
continue;
}
List<Action> buildActions = new ArrayList<Action>();
if (dep.shouldTriggerBuild(build, listener, buildActions)) {
// this is not completely accurate, as a new build might be triggered
// between these calls
String name = p.getName() + " #" + p.getNextBuildNumber();
if (p.scheduleBuild(p.getQuietPeriod(), new UpstreamCause((Run) build), buildActions.toArray(new Action[buildActions.size()]))) {
logger.println(Messages.BuildTrigger_Triggering(name));
} else {
logger.println(Messages.BuildTrigger_InQueue(name));
}
}
}
return true;
}
Aggregations