Search in sources :

Example 1 with DependencyGraph

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;
}
Also used : PrintStream(java.io.PrintStream) Action(hudson.model.Action) ArrayList(java.util.ArrayList) UpstreamCause(hudson.model.Cause.UpstreamCause) DependencyGraph(hudson.model.DependencyGraph) Run(hudson.model.Run) Dependency(hudson.model.DependencyGraph.Dependency) AbstractProject(hudson.model.AbstractProject)

Aggregations

AbstractProject (hudson.model.AbstractProject)1 Action (hudson.model.Action)1 UpstreamCause (hudson.model.Cause.UpstreamCause)1 DependencyGraph (hudson.model.DependencyGraph)1 Dependency (hudson.model.DependencyGraph.Dependency)1 Run (hudson.model.Run)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1