BuildTrigger.java 4.2 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6
package hudson.tasks;

import hudson.Launcher;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
7
import hudson.model.Hudson;
K
kohsuke 已提交
8 9 10
import hudson.model.Job;
import hudson.model.Project;
import hudson.model.Result;
11 12 13
import hudson.util.FormFieldValidator;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
K
kohsuke 已提交
14

15 16
import javax.servlet.ServletException;
import java.io.IOException;
K
kohsuke 已提交
17
import java.util.List;
18
import java.util.StringTokenizer;
K
kohsuke 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

/**
 * Triggers builds of other projects.
 *
 * @author Kohsuke Kawaguchi
 */
public class BuildTrigger extends Publisher {

    /**
     * Comma-separated list of other projects to be scheduled.
     */
    private String childProjects;

    public BuildTrigger(String childProjects) {
        this.childProjects = childProjects;
    }

    public BuildTrigger(List<Project> childProjects) {
        this(Project.toNameList(childProjects));
    }

    public String getChildProjectsValue() {
        return childProjects;
    }

    public List<Project> getChildProjects() {
        return Project.fromNameList(childProjects);
    }

    public boolean perform(Build build, Launcher launcher, BuildListener listener) {
        if(build.getResult()== Result.SUCCESS) {
            for (Project p : getChildProjects()) {
                listener.getLogger().println("Triggering a new build of "+p.getName());
                p.scheduleBuild();
            }
        }

        return true;
    }

    /**
     * Called from {@link Job#renameTo(String)} when a job is renamed.
     *
     * @return true
     *      if this {@link BuildTrigger} is changed and needs to be saved.
     */
    public boolean onJobRenamed(String oldName, String newName) {
        // quick test
        if(!childProjects.contains(oldName))
            return false;

        boolean changed = false;

        // we need to do this per string, since old Project object is already gone.
        String[] projects = childProjects.split(",");
        for( int i=0; i<projects.length; i++ ) {
            if(projects[i].trim().equals(oldName)) {
                projects[i] = newName;
                changed = true;
            }
        }

        if(changed) {
            StringBuilder b = new StringBuilder();
            for (String p : projects) {
                if(b.length()>0)    b.append(',');
                b.append(p);
            }
            childProjects = b.toString();
        }

        return changed;
    }

    public Descriptor<Publisher> getDescriptor() {
        return DESCRIPTOR;
    }


98 99 100 101 102 103 104
    public static final Descriptor<Publisher> DESCRIPTOR = new DescriptorImpl();

    public static class DescriptorImpl extends Descriptor<Publisher> {
        public DescriptorImpl() {
            super(BuildTrigger.class);
        }

K
kohsuke 已提交
105 106 107 108 109 110 111
        public String getDisplayName() {
            return "Build other projects";
        }

        public Publisher newInstance(StaplerRequest req) {
            return new BuildTrigger(req.getParameter("childProjects"));
        }
112 113 114 115 116 117 118 119 120 121 122 123 124 125

        /**
         * Form validation method.
         */
        public void doCheck( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
            new FormFieldValidator(req,rsp,true) {
                protected void check() throws IOException, ServletException {
                    String list = request.getParameter("value");

                    StringTokenizer tokens = new StringTokenizer(list,",");
                    while(tokens.hasMoreTokens()) {
                        String projectName = tokens.nextToken().trim();
                        Job job = Hudson.getInstance().getJob(projectName);
                        if(job==null) {
K
kohsuke 已提交
126 127
                            error("No such project '"+projectName+"'. Did you mean '"+
                                Project.findNearest(projectName).getName()+"'?");
128 129 130 131 132 133 134 135 136 137 138 139 140
                            return;
                        }
                        if(!(job instanceof Project)) {
                            error(projectName+" is not a software build");
                            return;
                        }
                    }

                    ok();
                }
            }.process();
        }
    }
K
kohsuke 已提交
141
}