1 package hudson.tasks; 2 3 import hudson.Launcher; 4 import hudson.model.AbstractProject; 5 import hudson.model.Build; 6 import hudson.model.BuildListener; 7 import hudson.model.Descriptor; 8 import hudson.model.Hudson; 9 import hudson.model.Item; 10 import hudson.model.Items; 11 import hudson.model.Job; 12 import hudson.model.Result; 13 import hudson.util.FormFieldValidator; 14 import org.kohsuke.stapler.StaplerRequest; 15 import org.kohsuke.stapler.StaplerResponse; 16 17 import javax.servlet.ServletException ; 18 import java.io.IOException ; 19 import java.io.PrintStream ; 20 import java.util.List ; 21 import java.util.StringTokenizer ; 22 23 28 public class BuildTrigger extends Publisher { 29 30 33 private String childProjects; 34 35 public BuildTrigger(String childProjects) { 36 this.childProjects = childProjects; 37 } 38 39 public BuildTrigger(List <AbstractProject> childProjects) { 40 this(Items.toNameList(childProjects)); 41 } 42 43 public String getChildProjectsValue() { 44 return childProjects; 45 } 46 47 public List <AbstractProject> getChildProjects() { 48 return Items.fromNameList(childProjects,AbstractProject.class); 49 } 50 51 public boolean perform(Build build, Launcher launcher, BuildListener listener) { 52 if(build.getResult()== Result.SUCCESS) { 53 PrintStream logger = listener.getLogger(); 54 for (AbstractProject p : getChildProjects()) { 55 if(p.isDisabled()) { 56 logger.println(p.getName()+" is disabled. Triggering skiiped"); 57 continue; 58 } 59 60 String name = p.getName()+" #"+p.getNextBuildNumber(); 63 if(!p.scheduleBuild()) { 64 logger.println("Triggering a new build of "+name); 65 } else { 66 logger.println(name+" is already in the queue"); 67 } 68 } 69 } 70 71 return true; 72 } 73 74 80 public boolean onJobRenamed(String oldName, String newName) { 81 if(!childProjects.contains(oldName)) 83 return false; 84 85 boolean changed = false; 86 87 String [] projects = childProjects.split(","); 89 for( int i=0; i<projects.length; i++ ) { 90 if(projects[i].trim().equals(oldName)) { 91 projects[i] = newName; 92 changed = true; 93 } 94 } 95 96 if(changed) { 97 StringBuilder b = new StringBuilder (); 98 for (String p : projects) { 99 if(b.length()>0) b.append(','); 100 b.append(p); 101 } 102 childProjects = b.toString(); 103 } 104 105 return changed; 106 } 107 108 public Descriptor<Publisher> getDescriptor() { 109 return DESCRIPTOR; 110 } 111 112 113 public static final Descriptor<Publisher> DESCRIPTOR = new DescriptorImpl(); 114 115 public static class DescriptorImpl extends Descriptor<Publisher> { 116 public DescriptorImpl() { 117 super(BuildTrigger.class); 118 } 119 120 public String getDisplayName() { 121 return "Build other projects"; 122 } 123 124 public Publisher newInstance(StaplerRequest req) { 125 return new BuildTrigger(req.getParameter("childProjects")); 126 } 127 128 131 public void doCheck( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 132 new FormFieldValidator(req,rsp,true) { 133 protected void check() throws IOException , ServletException { 134 String list = request.getParameter("value"); 135 136 StringTokenizer tokens = new StringTokenizer (list,","); 137 while(tokens.hasMoreTokens()) { 138 String projectName = tokens.nextToken().trim(); 139 Item item = Hudson.getInstance().getItemByFullName(projectName,Item.class); 140 if(item==null) { 141 error("No such project '"+projectName+"'. Did you mean '"+ 142 AbstractProject.findNearest(projectName).getName()+"'?"); 143 return; 144 } 145 if(!(item instanceof AbstractProject)) { 146 error(projectName+" is not buildable"); 147 return; 148 } 149 } 150 151 ok(); 152 } 153 }.process(); 154 } 155 } 156 } 157 | Popular Tags |