Project.java 11.6 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6
package hudson.model;

import hudson.model.Descriptor.FormException;
import hudson.model.Fingerprint.RangeSet;
import hudson.tasks.BuildStep;
import hudson.tasks.BuildTrigger;
K
kohsuke 已提交
7 8
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrappers;
K
kohsuke 已提交
9 10 11 12
import hudson.tasks.Builder;
import hudson.tasks.Fingerprinter;
import hudson.tasks.Publisher;
import hudson.triggers.Trigger;
K
kohsuke 已提交
13
import hudson.util.EditDistance;
K
kohsuke 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;

/**
 * Buildable software project.
 *
 * @author Kohsuke Kawaguchi
 */
38
public class Project extends AbstractProject<Project,Build> implements TopLevelItem {
K
kohsuke 已提交
39 40 41 42 43 44 45 46 47 48 49

    /**
     * List of active {@link Builder}s configured for this project.
     */
    private List<Builder> builders = new Vector<Builder>();

    /**
     * List of active {@link Publisher}s configured for this project.
     */
    private List<Publisher> publishers = new Vector<Publisher>();

50 51 52
    /**
     * List of active {@link BuildWrapper}s configured for this project.
     */
53
    private List<BuildWrapper> buildWrappers = new Vector<BuildWrapper>();
54

K
kohsuke 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    /**
     * {@link Action}s contributed from {@link #triggers}, {@link #builders},
     * and {@link #publishers}.
     *
     * We don't want to persist them separately, and these actions
     * come and go as configuration change, so it's kept separate.
     */
    private transient /*final*/ List<Action> transientActions = new Vector<Action>();

    /**
     * Creates a new project.
     */
    public Project(Hudson parent,String name) {
        super(parent,name);
    }

71 72
    public void onLoad(String name) throws IOException {
        super.onLoad(name);
K
kohsuke 已提交
73

K
kohsuke 已提交
74 75 76
        if(buildWrappers==null)
            // it didn't exist in < 1.64
            buildWrappers = new Vector<BuildWrapper>();
K
kohsuke 已提交
77 78 79 80 81

        updateTransientActions();
    }

    @Override
82
    public BallColor getIconColor() {
K
kohsuke 已提交
83 84
        if(isDisabled())
            // use grey to indicate that the build is disabled
85
            return BallColor.GREY;
K
kohsuke 已提交
86 87 88 89 90 91 92 93 94 95 96 97
        else
            return super.getIconColor();
    }

    public synchronized Map<Descriptor<Builder>,Builder> getBuilders() {
        return Descriptor.toMap(builders);
    }

    public synchronized Map<Descriptor<Publisher>,Publisher> getPublishers() {
        return Descriptor.toMap(publishers);
    }

98 99 100 101
    public synchronized Map<Descriptor<BuildWrapper>,BuildWrapper> getBuildWrappers() {
        return Descriptor.toMap(buildWrappers);
    }

102 103 104 105 106 107 108 109 110 111 112 113 114 115
    /**
     * Adds a new {@link BuildStep} to this {@link Project} and saves the configuration.
     */
    private void addPublisher(Publisher buildStep) throws IOException {
        addToList(buildStep,publishers);
    }

    /**
     * Removes a publisher from this project, if it's active.
     */
    private void removePublisher(Descriptor<Publisher> descriptor) throws IOException {
        removeFromList(descriptor, publishers);
    }

116
    @Override
K
kohsuke 已提交
117 118 119 120 121 122
    public Build newBuild() throws IOException {
        Build lastBuild = new Build(this);
        builds.put(lastBuild);
        return lastBuild;
    }

123 124 125 126 127
    @Override
    protected Build loadBuild(File dir) throws IOException {
        return new Build(this,dir);
    }

K
kohsuke 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    /**
     * Gets the dependency relationship map between this project (as the source)
     * and that project (as the sink.)
     *
     * @return
     *      can be empty but not null. build number of this project to the build
     *      numbers of that project.
     */
    public SortedMap<Integer,RangeSet> getRelationship(Project that) {
        TreeMap<Integer,RangeSet> r = new TreeMap<Integer,RangeSet>(REVERSE_INTEGER_COMPARATOR);

        checkAndRecord(that, r, this.getBuilds());
        // checkAndRecord(that, r, that.getBuilds());

        return r;
    }

    public List<Project> getDownstreamProjects() {
        BuildTrigger buildTrigger = (BuildTrigger) getPublishers().get(BuildTrigger.DESCRIPTOR);
        if(buildTrigger==null)
            return new ArrayList<Project>();
        else
            return buildTrigger.getChildProjects();
    }

    public List<Project> getUpstreamProjects() {
        List<Project> r = new ArrayList<Project>();
        for( Project p : Hudson.getInstance().getProjects() ) {
            synchronized(p) {
                for (BuildStep step : p.publishers) {
                    if (step instanceof BuildTrigger) {
                        BuildTrigger trigger = (BuildTrigger) step;
                        if(trigger.getChildProjects().contains(this))
                            r.add(p);
                    }
                }
            }
        }
        return r;
    }

    /**
     * Helper method for getDownstreamRelationship.
     *
     * For each given build, find the build number range of the given project and put that into the map.
     */
    private void checkAndRecord(Project that, TreeMap<Integer, RangeSet> r, Collection<? extends Build> builds) {
        for (Build build : builds) {
            RangeSet rs = build.getDownstreamRelationship(that);
            if(rs==null || rs.isEmpty())
                continue;

            int n = build.getNumber();

            RangeSet value = r.get(n);
            if(value==null)
                r.put(n,rs);
            else
                value.add(rs);
        }
    }

    /**
     * Returns true if the fingerprint record is configured in this project.
     */
    public boolean isFingerprintConfigured() {
        synchronized(publishers) {
            for (Publisher p : publishers) {
                if(p instanceof Fingerprinter)
                    return true;
            }
        }
        return false;
    }



//
//
// actions
//
//
    /**
     * Accepts submission from the configuration page.
     */
    public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {

J
jglick 已提交
215
        Set<Project> upstream = Collections.emptySet();
K
kohsuke 已提交
216 217 218 219 220 221 222 223

        synchronized(this) {
            try {
                if(!Hudson.adminCheck(req,rsp))
                    return;

                req.setCharacterEncoding("UTF-8");

224
                buildDescribable(req, BuildWrappers.WRAPPERS, buildWrappers, "wrapper");
K
kohsuke 已提交
225 226 227
                buildDescribable(req, BuildStep.BUILDERS, builders, "builder");
                buildDescribable(req, BuildStep.PUBLISHERS, publishers, "publisher");

228
                super.doConfigSubmit(req,rsp);
K
kohsuke 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

                updateTransientActions();
            } catch (FormException e) {
                sendError(e,req,rsp);
            }
        }

        if(req.getParameter("pseudoUpstreamTrigger")!=null) {
            upstream = new HashSet<Project>(Project.fromNameList(req.getParameter("upstreamProjects")));
        }

        // this needs to be done after we release the lock on this,
        // or otherwise we could dead-lock
        for (Project p : Hudson.getInstance().getProjects()) {
            boolean isUpstream = upstream.contains(p);
            synchronized(p) {
                List<Project> newChildProjects = p.getDownstreamProjects();

                if(isUpstream) {
                    if(!newChildProjects.contains(this))
                        newChildProjects.add(this);
                } else {
                    newChildProjects.remove(this);
                }

                if(newChildProjects.isEmpty()) {
                    p.removePublisher(BuildTrigger.DESCRIPTOR);
                } else {
                    p.addPublisher(new BuildTrigger(newChildProjects));
                }
            }
        }
K
kohsuke 已提交
261

262 263
        save();

K
kohsuke 已提交
264 265
        // notify the queue as the project might be now tied to different node
        Hudson.getInstance().getQueue().scheduleMaintenance();  
K
kohsuke 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    }

    private void updateTransientActions() {
        if(transientActions==null)
            transientActions = new Vector<Action>();    // happens when loaded from disk
        synchronized(transientActions) {
            transientActions.clear();
            for (BuildStep step : builders) {
                Action a = step.getProjectAction(this);
                if(a!=null)
                    transientActions.add(a);
            }
            for (BuildStep step : publishers) {
                Action a = step.getProjectAction(this);
                if(a!=null)
                    transientActions.add(a);
            }
            for (Trigger trigger : triggers) {
                Action a = trigger.getProjectAction();
                if(a!=null)
                    transientActions.add(a);
            }
        }
    }

    public synchronized List<Action> getActions() {
        // add all the transient actions, too
        List<Action> actions = new Vector<Action>(super.getActions());
        actions.addAll(transientActions);
        return actions;
    }

    public List<ProminentProjectAction> getProminentActions() {
        List<Action> a = getActions();
        List<ProminentProjectAction> pa = new Vector<ProminentProjectAction>();
        for (Action action : a) {
            if(action instanceof ProminentProjectAction)
                pa.add((ProminentProjectAction) action);
        }
        return pa;
    }

    /**
     * @deprecated
     *      left for legacy config file compatibility
     */
J
jglick 已提交
312
    @Deprecated
K
kohsuke 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    private transient String slave;

    /**
     * Converts a list of projects into a camma-separated names.
     */
    public static String toNameList(Collection<? extends Project> projects) {
        StringBuilder buf = new StringBuilder();
        for (Project project : projects) {
            if(buf.length()>0)
                buf.append(", ");
            buf.append(project.getName());
        }
        return buf.toString();
    }

    /**
     * Does the opposite of {@link #toNameList(Collection)}.
     */
    public static List<Project> fromNameList(String list) {
        Hudson hudson = Hudson.getInstance();

        List<Project> r = new ArrayList<Project>();
        StringTokenizer tokens = new StringTokenizer(list,",");
        while(tokens.hasMoreTokens()) {
            String projectName = tokens.nextToken().trim();
            Job job = hudson.getJob(projectName);
            if(!(job instanceof Project)) {
                continue; // ignore this token
            }
            r.add((Project) job);
        }
        return r;
    }

K
kohsuke 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359
    /**
     * Finds a {@link Project} that has the name closest to the given name.
     */
    public static Project findNearest(String name) {
        List<Project> projects = Hudson.getInstance().getProjects();
        String[] names = new String[projects.size()];
        for( int i=0; i<projects.size(); i++ )
            names[i] = projects.get(i).getName();

        String nearest = EditDistance.findNearest(name, names);
        return (Project)Hudson.getInstance().getJob(nearest);
    }

K
kohsuke 已提交
360 361 362 363 364 365
    private static final Comparator<Integer> REVERSE_INTEGER_COMPARATOR = new Comparator<Integer>() {
        public int compare(Integer o1, Integer o2) {
            return o2-o1;
        }
    };

366
    public TopLevelItemDescriptor getDescriptor() {
K
kohsuke 已提交
367 368 369
        return DESCRIPTOR;
    }

370
    public static final TopLevelItemDescriptor DESCRIPTOR = new TopLevelItemDescriptor(Project.class) {
K
kohsuke 已提交
371 372 373 374 375 376 377 378
        public String getDisplayName() {
            return "Building a software project";
        }

        public Project newInstance(String name) {
            return new Project(Hudson.getInstance(),name);
        }
    };
379
}