AbstractBuild.java 50.3 KB
Newer Older
K
kohsuke 已提交
1 2
/*
 * The MIT License
3
 *
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc., CloudBees, Inc.
5
 *
K
kohsuke 已提交
6 7 8 9 10 11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
12
 *
K
kohsuke 已提交
13 14
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
15
 *
K
kohsuke 已提交
16 17 18 19 20 21 22 23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
package hudson.model;

K
Kohsuke Kawaguchi 已提交
26
import com.google.common.collect.ImmutableList;
27
import com.google.common.collect.ImmutableSortedSet;
28
import hudson.AbortException;
29
import hudson.EnvVars;
30
import hudson.FilePath;
31
import hudson.Functions;
32
import hudson.Launcher;
33 34
import hudson.console.AnnotatedLargeText;
import hudson.console.ExpandableDetailsNote;
35
import hudson.console.ModelHyperlinkNote;
36
import hudson.matrix.MatrixConfiguration;
37 38
import hudson.model.Fingerprint.BuildPtr;
import hudson.model.Fingerprint.RangeSet;
39
import hudson.model.listeners.RunListener;
K
kohsuke 已提交
40
import hudson.model.listeners.SCMListener;
41 42 43
import hudson.scm.ChangeLogParser;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
44
import hudson.scm.NullChangeLogParser;
45 46 47 48
import hudson.scm.SCM;
import hudson.slaves.NodeProperty;
import hudson.slaves.WorkspaceList;
import hudson.slaves.WorkspaceList.Lease;
49
import hudson.tasks.BuildStep;
50 51
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.BuildTrigger;
52
import hudson.tasks.BuildWrapper;
53 54
import hudson.tasks.Builder;
import hudson.tasks.Fingerprinter.FingerprintAction;
55
import hudson.tasks.Publisher;
56
import hudson.tasks.test.AbstractTestResultAction;
57
import hudson.tasks.test.AggregatedTestResultAction;
58
import hudson.util.*;
59
import jenkins.model.Jenkins;
60
import jenkins.model.lazy.AbstractLazyLoadRunMap.Direction;
61
import jenkins.model.lazy.BuildReference;
62
import org.kohsuke.stapler.HttpResponse;
63
import org.kohsuke.stapler.Stapler;
64 65
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
66
import org.kohsuke.stapler.export.Exported;
67
import org.xml.sax.SAXException;
68

69
import javax.servlet.ServletException;
70 71
import java.io.File;
import java.io.IOException;
72
import java.io.InterruptedIOException;
73
import java.io.StringWriter;
74
import java.lang.ref.WeakReference;
75
import java.text.MessageFormat;
76 77 78 79 80 81 82 83 84 85 86
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
87 88
import java.util.logging.Level;
import java.util.logging.Logger;
89
import javax.annotation.CheckForNull;
J
Jesse Glick 已提交
90
import org.kohsuke.stapler.interceptor.RequirePOST;
K
kohsuke 已提交
91

92 93
import static java.util.logging.Level.WARNING;

94 95 96
/**
 * Base implementation of {@link Run}s that build software.
 *
97
 * For now this is primarily the common part of {@link Build} and MavenBuild.
98 99 100 101
 *
 * @author Kohsuke Kawaguchi
 * @see AbstractProject
 */
102
public abstract class AbstractBuild<P extends AbstractProject<P,R>,R extends AbstractBuild<P,R>> extends Run<P,R> implements Queue.Executable {
103

104 105 106 107 108
    /**
     * Set if we want the blame information to flow from upstream to downstream build.
     */
    private static final boolean upstreamCulprits = Boolean.getBoolean("hudson.upstreamCulprits");

109
    /**
K
kohsuke 已提交
110
     * Name of the slave this project was built on.
K
kohsuke 已提交
111
     * Null or "" if built by the master. (null happens when we read old record that didn't have this information.)
112 113 114
     */
    private String builtOn;

K
kohsuke 已提交
115 116
    /**
     * The file path on the node that performed a build. Kept as a string since {@link FilePath} is not serializable into XML.
K
kohsuke 已提交
117
     * @since 1.319
K
kohsuke 已提交
118 119 120
     */
    private String workspace;

121 122 123 124 125
    /**
     * Version of Hudson that built this.
     */
    private String hudsonVersion;

126 127 128 129 130 131 132 133 134
    /**
     * SCM used for this build.
     * Maybe null, for historical reason, in which case CVS is assumed.
     */
    private ChangeLogParser scm;

    /**
     * Changes in this build.
     */
J
John Pederzolli 已提交
135
    private volatile transient WeakReference<ChangeLogSet<? extends Entry>> changeSet;
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150
    /**
     * Cumulative list of people who contributed to the build problem.
     *
     * <p>
     * This is a list of {@link User#getId() user ids} who made a change
     * since the last non-broken build. Can be null (which should be
     * treated like empty set), because of the compatibility.
     *
     * <p>
     * This field is semi-final --- once set the value will never be modified.
     *
     * @since 1.137
     */
    private volatile Set<String> culprits;
151

K
kohsuke 已提交
152
    /**
153
     * During the build this field remembers {@link hudson.tasks.BuildWrapper.Environment}s created by
K
kohsuke 已提交
154 155
     * {@link BuildWrapper}. This design is bit ugly but forced due to compatibility.
     */
156
    protected transient List<Environment> buildEnvironments;
157

158 159 160 161 162 163
    /**
     * Pointers to form bi-directional link between adjacent {@link AbstractBuild}s.
     *
     * <p>
     * Unlike {@link Run}, {@link AbstractBuild}s do lazy-loading, so we don't use
     * {@link Run#previousBuild} and {@link Run#nextBuild}, and instead use these
164
     * fields and point to {@link #selfReference} (or {@link #none}) of adjacent builds.
165 166 167
     */
    private volatile transient BuildReference<R> previousBuild, nextBuild;

168 169 170
    @SuppressWarnings({"unchecked", "rawtypes"}) private static final BuildReference NONE = new BuildReference("NONE", null);
    @SuppressWarnings("unchecked") private BuildReference<R> none() {return NONE;}

171 172 173 174
    /*package*/ final transient BuildReference<R> selfReference = new BuildReference<R>(getId(),_this());



175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    protected AbstractBuild(P job) throws IOException {
        super(job);
    }

    protected AbstractBuild(P job, Calendar timestamp) {
        super(job, timestamp);
    }

    protected AbstractBuild(P project, File buildDir) throws IOException {
        super(project, buildDir);
    }

    public final P getProject() {
        return getParent();
    }

191 192 193 194 195 196
    @Override
    void dropLinks() {
        super.dropLinks();

        if(nextBuild!=null) {
            AbstractBuild nb = nextBuild.get();
197
            if (nb!=null) {
198
                nb.previousBuild = previousBuild;
199
            }
200 201 202 203 204 205
        }
        if(previousBuild!=null) {
            AbstractBuild pb = previousBuild.get();
            if (pb!=null)   pb.nextBuild = nextBuild;
        }
    }
206 207 208

    @Override
    public R getPreviousBuild() {
209 210 211 212 213
        while (true) {
            BuildReference<R> r = previousBuild;    // capture the value once

            if (r==null) {
                // having two neighbors pointing to each other is important to make RunMap.removeValue work
J
Jesse Glick 已提交
214 215
                P _parent = getParent();
                if (_parent == null) {
216
                    throw new IllegalStateException("no parent for " + number + " in " + workspace);
J
Jesse Glick 已提交
217 218
                }
                R pb = _parent._getRuns().search(number-1, Direction.DESC);
219 220 221 222 223
                if (pb!=null) {
                    ((AbstractBuild)pb).nextBuild = selfReference;   // establish bi-di link
                    this.previousBuild = pb.selfReference;
                    return pb;
                } else {
224
                    this.previousBuild = none();
225 226 227
                    return null;
                }
            }
228
            if (r==none())
229 230 231 232 233 234 235
                return null;

            R referent = r.get();
            if (referent!=null) return referent;

            // the reference points to a GC-ed object, drop the reference and do it again
            this.previousBuild = null;
236 237 238 239 240
        }
    }

    @Override
    public R getNextBuild() {
241 242 243 244 245 246 247 248 249 250 251
        while (true) {
            BuildReference<R> r = nextBuild;    // capture the value once

            if (r==null) {
                // having two neighbors pointing to each other is important to make RunMap.removeValue work
                R nb = getParent().builds.search(number+1, Direction.ASC);
                if (nb!=null) {
                    ((AbstractBuild)nb).previousBuild = selfReference;   // establish bi-di link
                    this.nextBuild = nb.selfReference;
                    return nb;
                } else {
252
                    this.nextBuild = none();
253 254 255
                    return null;
                }
            }
256
            if (r==none())
257 258 259 260 261 262 263
                return null;

            R referent = r.get();
            if (referent!=null) return referent;

            // the reference points to a GC-ed object, drop the reference and do it again
            this.nextBuild = null;
264 265 266
        }
    }

267 268
    /**
     * Returns a {@link Slave} on which this build was done.
K
kohsuke 已提交
269 270
     *
     * @return
M
mindless 已提交
271
     *      null, for example if the slave that this build run no longer exists.
272
     */
273
    public @CheckForNull Node getBuiltOn() {
274
        if (builtOn==null || builtOn.equals(""))
275
            return Jenkins.getInstance();
276
        else
277
            return Jenkins.getInstance().getNode(builtOn);
278 279 280
    }

    /**
M
mindless 已提交
281 282
     * Returns the name of the slave it was built on; null or "" if built by the master.
     * (null happens when we read old record that didn't have this information.)
283
     */
K
kohsuke 已提交
284
    @Exported(name="builtOn")
285 286 287 288
    public String getBuiltOnStr() {
        return builtOn;
    }

289
    /**
290 291 292 293
     * Allows subtypes to set the value of {@link #builtOn}.
     * This is used for those implementations where an {@link AbstractBuild} is made 'built' without
     * actually running its {@link #run()} method.
     *
294 295
     * @since 1.429
     */
296
    protected void setBuiltOnStr( String builtOn ) {
297 298 299
        this.builtOn = builtOn;
    }

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    /**
     * Gets the nearest ancestor {@link AbstractBuild} that belongs to
     * {@linkplain AbstractProject#getRootProject() the root project of getProject()} that
     * dominates/governs/encompasses this build.
     *
     * <p>
     * Some projects (such as matrix projects, Maven projects, or promotion processes) form a tree of jobs,
     * and still in some of them, builds of child projects are related/tied to that of the parent project.
     * In such a case, this method returns the governing build.
     *
     * @return never null. In the worst case the build dominates itself.
     * @since 1.421
     * @see AbstractProject#getRootProject()
     */
    public AbstractBuild<?,?> getRootBuild() {
        return this;
    }

K
kohsuke 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    /**
     * Used to render the side panel "Back to project" link.
     *
     * <p>
     * In a rare situation where a build can be reached from multiple paths,
     * returning different URLs from this method based on situations might
     * be desirable.
     *
     * <p>
     * If you override this method, you'll most likely also want to override
     * {@link #getDisplayName()}.
     */
    public String getUpUrl() {
        return Functions.getNearestAncestorUrl(Stapler.getCurrentRequest(),getParent())+'/';
    }

K
kohsuke 已提交
334 335 336 337 338
    /**
     * Gets the directory where this build is being built.
     *
     * <p>
     * Note to implementors: to control where the workspace is created, override
339
     * {@link AbstractBuildExecution#decideWorkspace(Node,WorkspaceList)}.
K
kohsuke 已提交
340 341 342 343 344
     *
     * @return
     *      null if the workspace is on a slave that's not connected. Note that once the build is completed,
     *      the workspace may be used to build something else, so the value returned from this method may
     *      no longer show a workspace as it was used for this build.
K
kohsuke 已提交
345
     * @since 1.319
K
kohsuke 已提交
346
     */
347
    public final @CheckForNull FilePath getWorkspace() {
348
        if (workspace==null) return null;
K
kohsuke 已提交
349
        Node n = getBuiltOn();
350
        if (n==null) return null;
K
kohsuke 已提交
351 352 353
        return n.createPath(workspace);
    }

354
    /**
355
     * Normally, a workspace is assigned by {@link hudson.model.Run.RunExecution}, but this lets you set the workspace in case
356 357 358 359 360 361
     * {@link AbstractBuild} is created without a build.
     */
    protected void setWorkspace(FilePath ws) {
        this.workspace = ws.getRemote();
    }

K
kohsuke 已提交
362 363 364 365 366 367 368 369
    /**
     * Returns the root directory of the checked-out module.
     * <p>
     * This is usually where <tt>pom.xml</tt>, <tt>build.xml</tt>
     * and so on exists.
     */
    public final FilePath getModuleRoot() {
        FilePath ws = getWorkspace();
370
        if (ws==null)    return null;
371
        return getParent().getScm().getModuleRoot(ws, this);
K
kohsuke 已提交
372 373 374 375 376 377 378 379 380 381 382
    }

    /**
     * Returns the root directories of all checked-out modules.
     * <p>
     * Some SCMs support checking out multiple modules into the same workspace.
     * In these cases, the returned array will have a length greater than one.
     * @return The roots of all modules checked out from the SCM.
     */
    public FilePath[] getModuleRoots() {
        FilePath ws = getWorkspace();
383
        if (ws==null)    return null;
384
        return getParent().getScm().getModuleRoots(ws, this);
K
kohsuke 已提交
385 386
    }

387 388 389 390
    /**
     * List of users who committed a change since the last non-broken build till now.
     *
     * <p>
K
kohsuke 已提交
391 392
     * This list at least always include people who made changes in this build, but
     * if the previous build was a failure it also includes the culprit list from there.
393 394 395 396 397 398
     *
     * @return
     *      can be empty but never null.
     */
    @Exported
    public Set<User> getCulprits() {
399
        if (culprits==null) {
400
            Set<User> r = new HashSet<User>();
401
            R p = getPreviousCompletedBuild();
402
            if (p !=null && isBuilding()) {
K
kohsuke 已提交
403
                Result pr = p.getResult();
K
Kohsuke Kawaguchi 已提交
404
                if (pr!=null && pr.isWorseThan(Result.SUCCESS)) {
K
kohsuke 已提交
405 406 407 408 409 410
                    // we are still building, so this is just the current latest information,
                    // but we seems to be failing so far, so inherit culprits from the previous build.
                    // isBuilding() check is to avoid recursion when loading data from old Hudson, which doesn't record
                    // this information
                    r.addAll(p.getCulprits());
                }
411
            }
412
            for (Entry e : getChangeSet())
413
                r.add(e.getAuthor());
414 415 416 417

            if (upstreamCulprits) {
                // If we have dependencies since the last successful build, add their authors to our list
                if (getPreviousNotFailedBuild() != null) {
K
Kohsuke Kawaguchi 已提交
418 419
                    Map <AbstractProject,DependencyChange> depmap = getDependencyChanges(getPreviousSuccessfulBuild());
                    for (DependencyChange dep : depmap.values()) {
420 421 422 423 424 425 426 427 428
                        for (AbstractBuild<?,?> b : dep.getBuilds()) {
                            for (Entry entry : b.getChangeSet()) {
                                r.add(entry.getAuthor());
                            }
                        }
                    }
                }
            }

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
            return r;
        }

        return new AbstractSet<User>() {
            public Iterator<User> iterator() {
                return new AdaptedIterator<String,User>(culprits.iterator()) {
                    protected User adapt(String id) {
                        return User.get(id);
                    }
                };
            }

            public int size() {
                return culprits.size();
            }
        };
    }

447 448 449 450 451 452 453
    /**
     * Returns true if this user has made a commit to this build.
     *
     * @since 1.191
     */
    public boolean hasParticipant(User user) {
        for (ChangeLogSet.Entry e : getChangeSet())
454 455 456
            try{
                if (e.getAuthor()==user)
                    return true;
457 458
            } catch (RuntimeException re) { 
                LOGGER.log(Level.INFO, "Failed to determine author of changelog " + e.getCommitId() + "for " + getParent().getDisplayName() + ", " + getDisplayName(), re);
459
            }
460 461 462
        return false;
    }

K
kohsuke 已提交
463 464 465 466 467 468 469 470 471
    /**
     * Gets the version of Hudson that was used to build this job.
     *
     * @since 1.246
     */
    public String getHudsonVersion() {
        return hudsonVersion;
    }

472 473
    /**
     * @deprecated as of 1.467
474
     *      Please use {@link hudson.model.Run.RunExecution}
475 476 477 478 479 480 481 482 483 484 485
     */
    public abstract class AbstractRunner extends AbstractBuildExecution {

    }

    public abstract class AbstractBuildExecution extends Runner {
        /*
            Some plugins might depend on this instance castable to Runner, so we need to use
            deprecated class here.
         */

486 487
        /**
         * Since configuration can be changed while a build is in progress,
488
         * create a launcher once and stick to it for the entire build duration.
489 490 491
         */
        protected Launcher launcher;

492 493 494 495 496
        /**
         * Output/progress of this build goes here.
         */
        protected BuildListener listener;

K
Kohsuke Kawaguchi 已提交
497 498 499 500 501
        /**
         * Lease of the workspace.
         */
        private Lease lease;

K
kohsuke 已提交
502
        /**
K
typo  
Kohsuke Kawaguchi 已提交
503
         * Returns the current {@link Node} on which we are building.
K
kohsuke 已提交
504 505 506 507 508
         */
        protected final Node getCurrentNode() {
            return Executor.currentExecutor().getOwner().getNode();
        }

509 510 511 512 513 514 515 516
        public Launcher getLauncher() {
            return launcher;
        }

        public BuildListener getListener() {
            return listener;
        }

K
kohsuke 已提交
517 518 519 520 521 522 523 524
        /**
         * Allocates the workspace from {@link WorkspaceList}.
         *
         * @param n
         *      Passed in for the convenience. The node where the build is running.
         * @param wsl
         *      Passed in for the convenience. The returned path must be registered to this object.
         */
525
        protected Lease decideWorkspace(Node n, WorkspaceList wsl) throws InterruptedException, IOException {
526 527 528 529 530
            String customWorkspace = getProject().getCustomWorkspace();
            if (customWorkspace != null) {
                // we allow custom workspaces to be concurrently used between jobs.
                return Lease.createDummyLease(n.getRootPath().child(getEnvironment(listener).expand(customWorkspace)));
            }
K
kohsuke 已提交
531
            // TODO: this cast is indicative of abstraction problem
532
            return wsl.allocate(n.getWorkspaceFor((TopLevelItem)getProject()), getBuild());
K
kohsuke 已提交
533 534
        }

535
        public Result run(BuildListener listener) throws Exception {
536
            Node node = getCurrentNode();
537 538
            assert builtOn==null;
            builtOn = node.getNodeName();
539
            hudsonVersion = Jenkins.VERSION;
540
            this.listener = listener;
541

542
            launcher = createLauncher(listener);
543
            if (!Jenkins.getInstance().getNodes().isEmpty())
544
                listener.getLogger().print(node instanceof Jenkins ? Messages.AbstractBuild_BuildingOnMaster() : 
545
                    Messages.AbstractBuild_BuildingRemotely(ModelHyperlinkNote.encodeTo("/computer/" + builtOn, builtOn)));
546 547
            else
            	listener.getLogger().print(Messages.AbstractBuild_Building());
548
            
K
Kohsuke Kawaguchi 已提交
549
            lease = decideWorkspace(node, Computer.currentComputer().getWorkspaceList());
K
kohsuke 已提交
550

K
Kohsuke Kawaguchi 已提交
551 552 553
            workspace = lease.path.getRemote();
            listener.getLogger().println(Messages.AbstractBuild_BuildingInWorkspace(workspace));
            node.getFileSystemProvisioner().prepareWorkspace(AbstractBuild.this,lease.path,listener);
554

K
Kohsuke Kawaguchi 已提交
555 556 557
            for (WorkspaceListener wl : WorkspaceListener.all()) {
                wl.beforeUse(AbstractBuild.this, lease.path, listener);
            }
558

K
Kohsuke Kawaguchi 已提交
559 560
            getProject().getScmCheckoutStrategy().preCheckout(AbstractBuild.this, launcher, this.listener);
            getProject().getScmCheckoutStrategy().checkout(this);
561

K
Kohsuke Kawaguchi 已提交
562 563
            if (!preBuild(listener,project.getProperties()))
                return Result.FAILURE;
564

K
Kohsuke Kawaguchi 已提交
565
            Result result = doRun(listener);
566

K
Kohsuke Kawaguchi 已提交
567 568 569 570 571
            Computer c = node.toComputer();
            if (c==null || c.isOffline()) {
                // As can be seen in HUDSON-5073, when a build fails because of the slave connectivity problem,
                // error message doesn't point users to the slave. So let's do it here.
                listener.hyperlink("/computer/"+builtOn+"/log","Looks like the node went offline during the build. Check the slave log for the details.");
D
Dave Brosius 已提交
572

K
Kohsuke Kawaguchi 已提交
573 574 575 576 577 578 579 580 581
                if (c != null) {
                    // grab the end of the log file. This might not work very well if the slave already
                    // starts reconnecting. Fixing this requires a ring buffer in slave logs.
                    AnnotatedLargeText<Computer> log = c.getLogText();
                    StringWriter w = new StringWriter();
                    log.writeHtmlTo(Math.max(0,c.getLogFile().length()-10240),w);

                    listener.getLogger().print(ExpandableDetailsNote.encodeTo("details",w.toString()));
                    listener.getLogger().println();
582
                }
K
Kohsuke Kawaguchi 已提交
583
            }
584

K
Kohsuke Kawaguchi 已提交
585 586 587 588
            // kill run-away processes that are left
            // use multiple environment variables so that people can escape this massacre by overriding an environment
            // variable for some processes
            launcher.kill(getCharacteristicEnvVars());
589

K
Kohsuke Kawaguchi 已提交
590 591 592 593
            // this is ugly, but for historical reason, if non-null value is returned
            // it should become the final result.
            if (result==null)    result = getResult();
            if (result==null)    result = Result.SUCCESS;
594

K
Kohsuke Kawaguchi 已提交
595
            return result;
596 597
        }

598 599 600 601 602
        /**
         * Creates a {@link Launcher} that this build will use. This can be overridden by derived types
         * to decorate the resulting {@link Launcher}.
         *
         * @param listener
603
         *      Always non-null. Connected to the main build output.
604 605
         */
        protected Launcher createLauncher(BuildListener listener) throws IOException, InterruptedException {
606 607 608 609
            Launcher l = getCurrentNode().createLauncher(listener);

            if (project instanceof BuildableItemWithBuildWrappers) {
                BuildableItemWithBuildWrappers biwbw = (BuildableItemWithBuildWrappers) project;
610
                for (BuildWrapper bw : biwbw.getBuildWrappersList())
611 612 613 614 615
                    l = bw.decorateLauncher(AbstractBuild.this,l,listener);
            }

            buildEnvironments = new ArrayList<Environment>();

616 617 618 619 620 621 622
            for (RunListener rl: RunListener.all()) {
                Environment environment = rl.setUpEnvironment(AbstractBuild.this, l, listener);
                if (environment != null) {
                    buildEnvironments.add(environment);
                }
            }

623
            for (NodeProperty nodeProperty: Jenkins.getInstance().getGlobalNodeProperties()) {
624 625 626 627 628 629 630 631 632 633 634 635 636 637
                Environment environment = nodeProperty.setUp(AbstractBuild.this, l, listener);
                if (environment != null) {
                    buildEnvironments.add(environment);
                }
            }

            for (NodeProperty nodeProperty: Computer.currentComputer().getNode().getNodeProperties()) {
                Environment environment = nodeProperty.setUp(AbstractBuild.this, l, listener);
                if (environment != null) {
                    buildEnvironments.add(environment);
                }
            }

            return l;
638 639
        }

640 641 642
        public void defaultCheckout() throws IOException, InterruptedException {
            AbstractBuild<?,?> build = AbstractBuild.this;
            AbstractProject<?, ?> project = build.getProject();
643

644 645 646 647 648 649 650
            for (int retryCount=project.getScmCheckoutRetryCount(); ; retryCount--) {
                // for historical reasons, null in the scm field means CVS, so we need to explicitly set this to something
                // in case check out fails and leaves a broken changelog.xml behind.
                // see http://www.nabble.com/CVSChangeLogSet.parse-yields-SAXParseExceptions-when-parsing-bad-*AccuRev*-changelog.xml-files-td22213663.html
                build.scm = NullChangeLogParser.INSTANCE;

                try {
K
Kohsuke Kawaguchi 已提交
651
                    if (project.checkout(build, launcher,listener,new File(build.getRootDir(),"changelog.xml"))) {
652 653 654 655 656 657 658
                        // check out succeeded
                        SCM scm = project.getScm();

                        build.scm = scm.createChangeLogParser();
                        build.changeSet = new WeakReference<ChangeLogSet<? extends Entry>>(build.calcChangeSet());

                        for (SCMListener l : Jenkins.getInstance().getSCMListeners())
659 660 661
                            try {
                                l.onChangeLogParsed(build,listener,build.getChangeSet());
                            } catch (Exception e) {
662
                                throw new IOException("Failed to parse changelog",e);
663
                            }
664 665 666 667

                        // Get a chance to do something after checkout and changelog is done
                        scm.postCheckout( build, launcher, build.getWorkspace(), listener );

668 669 670 671 672 673 674 675 676
                        return;
                    }
                } catch (AbortException e) {
                    listener.error(e.getMessage());
                } catch (InterruptedIOException e) {
                    throw (InterruptedException)new InterruptedException().initCause(e);
                } catch (IOException e) {
                    // checkout error not yet reported
                    e.printStackTrace(listener.getLogger());
677
                }
678 679 680 681 682 683 684

                if (retryCount == 0)   // all attempts failed
                    throw new RunnerAbortedException();

                listener.getLogger().println("Retrying after 10 seconds");
                Thread.sleep(10000);
            }
685 686
        }

K
kohsuke 已提交
687 688 689 690 691 692 693 694 695
        /**
         * The portion of a build that is specific to a subclass of {@link AbstractBuild}
         * goes here.
         *
         * @return
         *      null to continue the build normally (that means the doRun method
         *      itself run successfully)
         *      Return a non-null value to abort the build right there with the specified result code.
         */
K
kohsuke 已提交
696
        protected abstract Result doRun(BuildListener listener) throws Exception, RunnerAbortedException;
697 698 699 700 701 702 703 704 705 706 707 708 709 710

        /**
         * @see #post(BuildListener)
         */
        protected abstract void post2(BuildListener listener) throws Exception;

        public final void post(BuildListener listener) throws Exception {
            try {
                post2(listener);
            } finally {
                // update the culprit list
                HashSet<String> r = new HashSet<String>();
                for (User u : getCulprits())
                    r.add(u.getId());
711
                culprits = ImmutableSortedSet.copyOf(r);
K
kohsuke 已提交
712
                CheckPoint.CULPRITS_DETERMINED.report();
713 714
            }
        }
715 716

        public void cleanUp(BuildListener listener) throws Exception {
K
Kohsuke Kawaguchi 已提交
717 718 719 720
            if (lease!=null) {
                lease.release();
                lease = null;
            }
721 722
            BuildTrigger.execute(AbstractBuild.this, listener);
            buildEnvironments = null;
723
        }
724

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
        /**
         * @deprecated as of 1.356
         *      Use {@link #performAllBuildSteps(BuildListener, Map, boolean)}
         */
        protected final void performAllBuildStep(BuildListener listener, Map<?,? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
            performAllBuildSteps(listener,buildSteps.values(),phase);
        }

        protected final boolean performAllBuildSteps(BuildListener listener, Map<?,? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
            return performAllBuildSteps(listener,buildSteps.values(),phase);
        }

        /**
         * @deprecated as of 1.356
         *      Use {@link #performAllBuildSteps(BuildListener, Iterable, boolean)}
         */
        protected final void performAllBuildStep(BuildListener listener, Iterable<? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
            performAllBuildSteps(listener,buildSteps,phase);
743 744 745 746 747 748 749 750
        }

        /**
         * Runs all the given build steps, even if one of them fail.
         *
         * @param phase
         *      true for the post build processing, and false for the final "run after finished" execution.
         */
751
        protected final boolean performAllBuildSteps(BuildListener listener, Iterable<? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
752
            boolean r = true;
753 754
            for (BuildStep bs : buildSteps) {
                if ((bs instanceof Publisher && ((Publisher)bs).needsToRunAfterFinalized()) ^ phase)
755
                    try {
756 757 758 759
                        if (!perform(bs,listener)) {
                            LOGGER.fine(MessageFormat.format("{0} : {1} failed", AbstractBuild.this.toString(), bs));
                            r = false;
                        }
760
                    } catch (Exception e) {
761 762
                        String msg = "Publisher " + bs.getClass().getName() + " aborted due to exception";
                        e.printStackTrace(listener.error(msg));
763
                        LOGGER.log(WARNING, msg, e);
764
                        setResult(Result.FAILURE);
765
                    }
K
kohsuke 已提交
766
            }
767
            return r;
K
kohsuke 已提交
768 769 770 771 772 773 774 775 776 777 778
        }

        /**
         * Calls a build step.
         */
        protected final boolean perform(BuildStep bs, BuildListener listener) throws InterruptedException, IOException {
            BuildStepMonitor mon;
            try {
                mon = bs.getRequiredMonitorService();
            } catch (AbstractMethodError e) {
                mon = BuildStepMonitor.BUILD;
779
            }
780
            Result oldResult = AbstractBuild.this.getResult();
781 782 783
            for (BuildStepListener bsl : BuildStepListener.all()) {
                bsl.started(AbstractBuild.this, bs, listener);
            }
784
            boolean canContinue = mon.perform(bs, AbstractBuild.this, launcher, listener);
785 786 787
            for (BuildStepListener bsl : BuildStepListener.all()) {
                bsl.finished(AbstractBuild.this, bs, listener, canContinue);
            }
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
            Result newResult = AbstractBuild.this.getResult();
            if (newResult != oldResult) {
                String buildStepName = getBuildStepName(bs);
                listener.getLogger().format("Build step '%s' changed build result to %s%n", buildStepName, newResult);
            }
            if (!canContinue) {
                String buildStepName = getBuildStepName(bs);
                listener.getLogger().format("Build step '%s' marked build as failure%n", buildStepName);
            }
            return canContinue;
        }

        private String getBuildStepName(BuildStep bs) {
            if (bs instanceof Describable<?>) {
                return ((Describable<?>) bs).getDescriptor().getDisplayName();
            } else {
                return bs.getClass().getSimpleName();
            }
806
        }
807 808

        protected final boolean preBuild(BuildListener listener,Map<?,? extends BuildStep> steps) {
809 810 811 812
            return preBuild(listener,steps.values());
        }

        protected final boolean preBuild(BuildListener listener,Collection<? extends BuildStep> steps) {
813 814 815 816
            return preBuild(listener,(Iterable<? extends BuildStep>)steps);
        }

        protected final boolean preBuild(BuildListener listener,Iterable<? extends BuildStep> steps) {
817
            for (BuildStep bs : steps)
818 819
                if (!bs.prebuild(AbstractBuild.this,listener)) {
                    LOGGER.fine(MessageFormat.format("{0} : {1} failed", AbstractBuild.this.toString(), bs));
820
                    return false;
821
                }
822 823
            return true;
        }
824
    }
K
kohsuke 已提交
825

826 827 828 829 830 831 832 833 834 835 836 837 838 839
    /**
     * get the fingerprints associated with this build
     *
     * @return never null
     */
    @Exported(name = "fingerprint", inline = true, visibility = -1)
    public Collection<Fingerprint> getBuildFingerprints() {
        FingerprintAction fingerprintAction = getAction(FingerprintAction.class);
        if (fingerprintAction != null) {
            return fingerprintAction.getFingerprints().values();
        }
        return Collections.<Fingerprint>emptyList();
    }

840 841 842 843 844
	/*
     * No need to to lock the entire AbstractBuild on change set calculcation
     */
    private transient Object changeSetLock = new Object();
    
845 846 847 848 849
    /**
     * Gets the changes incorporated into this build.
     *
     * @return never null.
     */
K
kohsuke 已提交
850
    @Exported
851
    public ChangeLogSet<? extends Entry> getChangeSet() {
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
        synchronized (changeSetLock) {
            if (scm==null) {
                // for historical reason, null means CVS.
                try {
                    Class<?> c = Jenkins.getInstance().getPluginManager().uberClassLoader.loadClass("hudson.scm.CVSChangeLogParser");
                    scm = (ChangeLogParser)c.newInstance();
                } catch (ClassNotFoundException e) {
                    // if CVS isn't available, fall back to something non-null.
                    scm = NullChangeLogParser.INSTANCE;
                } catch (InstantiationException e) {
                    scm = NullChangeLogParser.INSTANCE;
                    throw (Error)new InstantiationError().initCause(e);
                } catch (IllegalAccessException e) {
                    scm = NullChangeLogParser.INSTANCE;
                    throw (Error)new IllegalAccessError().initCause(e);
                }
868 869
            }
        }
870

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
        ChangeLogSet<? extends Entry> cs = null;
        if (changeSet!=null)
            cs = changeSet.get();

        if (cs==null)
            cs = calcChangeSet();

        // defensive check. if the calculation fails (such as through an exception),
        // set a dummy value so that it'll work the next time. the exception will
        // be still reported, giving the plugin developer an opportunity to fix it.
        if (cs==null)
            cs = ChangeLogSet.createEmpty(this);

        changeSet = new WeakReference<ChangeLogSet<? extends Entry>>(cs);
        return cs;
886 887 888 889 890 891 892 893 894 895 896 897
    }

    /**
     * Returns true if the changelog is already computed.
     */
    public boolean hasChangeSetComputed() {
        File changelogFile = new File(getRootDir(), "changelog.xml");
        return changelogFile.exists();
    }

    private ChangeLogSet<? extends Entry> calcChangeSet() {
        File changelogFile = new File(getRootDir(), "changelog.xml");
898
        if (!changelogFile.exists())
899
            return ChangeLogSet.createEmpty(this);
900 901 902 903

        try {
            return scm.parse(this,changelogFile);
        } catch (IOException e) {
904
            LOGGER.log(WARNING, "Failed to parse "+changelogFile,e);
905
        } catch (SAXException e) {
906
            LOGGER.log(WARNING, "Failed to parse "+changelogFile,e);
907
        }
908
        return ChangeLogSet.createEmpty(this);
909 910 911
    }

    @Override
K
kohsuke 已提交
912 913
    public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
        EnvVars env = super.getEnvironment(log);
914 915 916
        FilePath ws = getWorkspace();
        if (ws!=null)   // if this is done very early on in the build, workspace may not be decided yet. see HUDSON-3997
            env.put("WORKSPACE", ws.getRemote());
917

918
        project.getScm().buildEnvVars(this,env);
919

920 921 922
        if (buildEnvironments!=null)
            for (Environment e : buildEnvironments)
                e.buildEnvVars(env);
923

924
        for (EnvironmentContributingAction a : getActions(EnvironmentContributingAction.class))
925 926
            a.buildEnvVars(this,env);

927
        EnvVars.resolve(env);
928

929 930
        return env;
    }
K
kohsuke 已提交
931

K
Kohsuke Kawaguchi 已提交
932 933
    /**
     * During the build, expose the environments contributed by {@link BuildWrapper}s and others.
934 935 936 937 938 939 940
     * 
     * <p>
     * Since 1.444, executor thread that's doing the build can access mutable underlying list,
     * which allows the caller to add/remove environments. The recommended way of adding
     * environment is through {@link BuildWrapper}, but this might be handy for build steps
     * who wants to expose additional environment variables to the rest of the build.
     * 
K
Kohsuke Kawaguchi 已提交
941 942 943
     * @return can be empty list, but never null. Immutable.
     * @since 1.437
     */
944
    public EnvironmentList getEnvironments() {
945 946 947 948 949 950
        Executor e = Executor.currentExecutor();
        if (e!=null && e.getCurrentExecutable()==this) {
            if (buildEnvironments==null)    buildEnvironments = new ArrayList<Environment>();
            return new EnvironmentList(buildEnvironments); 
        }
        
951
        return new EnvironmentList(buildEnvironments==null ? Collections.<Environment>emptyList() : ImmutableList.copyOf(buildEnvironments));
K
Kohsuke Kawaguchi 已提交
952 953
    }

954
    public Calendar due() {
955
        return getTimestamp();
956
    }
957
      
958 959 960
    public List<Action> getPersistentActions(){
        return super.getActions();
    }
961

962 963
    /**
     * Builds up a set of variable names that contain sensitive values that
A
alanharder 已提交
964
     * should not be exposed. The expectation is that this set is populated with
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
     * keys returned by {@link #getBuildVariables()} that should have their
     * values masked for display purposes.
     *
     * @since 1.378
     */
    public Set<String> getSensitiveBuildVariables() {
        Set<String> s = new HashSet<String>();

        ParametersAction parameters = getAction(ParametersAction.class);
        if (parameters != null) {
            for (ParameterValue p : parameters) {
                if (p.isSensitive()) {
                    s.add(p.getName());
                }
            }
        }

        // Allow BuildWrappers to determine if any of their data is sensitive
        if (project instanceof BuildableItemWithBuildWrappers) {
            for (BuildWrapper bw : ((BuildableItemWithBuildWrappers) project).getBuildWrappersList()) {
                bw.makeSensitiveBuildVariables(this, s);
            }
        }
        
        return s;
    }

992 993 994 995 996 997
    /**
     * Provides additional variables and their values to {@link Builder}s.
     *
     * <p>
     * This mechanism is used by {@link MatrixConfiguration} to pass
     * the configuration values to the current build. It is up to
J
Jesse Glick 已提交
998
     * {@link Builder}s to decide whether they want to recognize the values
999 1000
     * or how to use them.
     *
1001 1002 1003 1004 1005
     * <p>
     * This also includes build parameters if a build is parameterized.
     *
     * @return
     *      The returned map is mutable so that subtypes can put more values.
1006 1007
     */
    public Map<String,String> getBuildVariables() {
1008 1009 1010
        Map<String,String> r = new HashMap<String, String>();

        ParametersAction parameters = getAction(ParametersAction.class);
1011
        if (parameters!=null) {
1012 1013 1014
            // this is a rather round about way of doing this...
            for (ParameterValue p : parameters) {
                String v = p.createVariableResolver(this).resolve(p.getName());
1015
                if (v!=null) r.put(p.getName(),v);
1016 1017
            }
        }
1018 1019 1020 1021 1022 1023 1024

        // allow the BuildWrappers to contribute additional build variables
        if (project instanceof BuildableItemWithBuildWrappers) {
            for (BuildWrapper bw : ((BuildableItemWithBuildWrappers) project).getBuildWrappersList())
                bw.makeBuildVariables(this,r);
        }

1025 1026 1027
        for (BuildVariableContributor bvc : BuildVariableContributor.all())
            bvc.buildVariablesFor(this,r);

1028
        return r;
1029
    }
1030 1031 1032 1033 1034 1035 1036 1037

    /**
     * Creates {@link VariableResolver} backed by {@link #getBuildVariables()}.
     */
    public final VariableResolver<String> getBuildVariableResolver() {
        return new VariableResolver.ByMap<String>(getBuildVariables());
    }

1038 1039 1040
    /**
     * Gets {@link AbstractTestResultAction} associated with this build if any.
     */
1041 1042 1043
    public AbstractTestResultAction getTestResultAction() {
        return getAction(AbstractTestResultAction.class);
    }
1044

1045 1046 1047 1048 1049 1050 1051
    /**
     * Gets {@link AggregatedTestResultAction} associated with this build if any.
     */
    public AggregatedTestResultAction getAggregatedTestResultAction() {
        return getAction(AggregatedTestResultAction.class);
    }

K
kohsuke 已提交
1052 1053 1054 1055 1056
    /**
     * Invoked by {@link Executor} to performs a build.
     */
    public abstract void run();

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
//
//
// fingerprint related stuff
//
//

    @Override
    public String getWhyKeepLog() {
        // if any of the downstream project is configured with 'keep dependency component',
        // we need to keep this log
K
kohsuke 已提交
1067
        OUTER:
K
kohsuke 已提交
1068
        for (AbstractProject<?,?> p : getParent().getDownstreamProjects()) {
1069
            if (!p.isKeepDependencies()) continue;
K
kohsuke 已提交
1070

K
kohsuke 已提交
1071
            AbstractBuild<?,?> fb = p.getFirstBuild();
1072
            if (fb==null)        continue; // no active record
K
kohsuke 已提交
1073 1074

            // is there any active build that depends on us?
1075
            for (int i : getDownstreamRelationship(p).listNumbersReverse()) {
K
kohsuke 已提交
1076 1077 1078
                // TODO: this is essentially a "find intersection between two sparse sequences"
                // and we should be able to do much better.

1079
                if (i<fb.getNumber())
K
kohsuke 已提交
1080 1081
                    continue OUTER; // all the other records are younger than the first record, so pointless to search.

K
kohsuke 已提交
1082
                AbstractBuild<?,?> b = p.getBuildByNumber(i);
1083
                if (b!=null)
K
kohsuke 已提交
1084
                    return Messages.AbstractBuild_KeptBecause(b);
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
            }
        }

        return super.getWhyKeepLog();
    }

    /**
     * Gets the dependency relationship from this build (as the source)
     * and that project (as the sink.)
     *
     * @return
     *      range of build numbers that represent which downstream builds are using this build.
1097
     *      The range will be empty if no build of that project matches this (or there is no {@link FingerprintAction}), but it'll never be null.
1098
     */
1099 1100 1101 1102
    public RangeSet getDownstreamRelationship(AbstractProject that) {
        RangeSet rs = new RangeSet();

        FingerprintAction f = getAction(FingerprintAction.class);
1103
        if (f==null)     return rs;
1104 1105 1106

        // look for fingerprints that point to this build as the source, and merge them all
        for (Fingerprint e : f.getFingerprints().values()) {
1107 1108 1109 1110

            if (upstreamCulprits) {
                // With upstreamCulprits, we allow downstream relationships
                // from intermediate jobs
1111
                rs.add(e.getRangeSet(that));
1112 1113
            } else {
                BuildPtr o = e.getOriginal();
1114
                if (o!=null && o.is(this))
1115 1116
                    rs.add(e.getRangeSet(that));
            }
1117 1118 1119 1120
        }

        return rs;
    }
1121

K
kohsuke 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    /**
     * Works like {@link #getDownstreamRelationship(AbstractProject)} but returns
     * the actual build objects, in ascending order.
     * @since 1.150
     */
    public Iterable<AbstractBuild<?,?>> getDownstreamBuilds(final AbstractProject<?,?> that) {
        final Iterable<Integer> nums = getDownstreamRelationship(that).listNumbers();

        return new Iterable<AbstractBuild<?, ?>>() {
            public Iterator<AbstractBuild<?, ?>> iterator() {
1132 1133 1134 1135 1136 1137
                return Iterators.removeNull(
                    new AdaptedIterator<Integer,AbstractBuild<?,?>>(nums) {
                        protected AbstractBuild<?, ?> adapt(Integer item) {
                            return that.getBuildByNumber(item);
                        }
                    });
K
kohsuke 已提交
1138 1139 1140 1141
            }
        };
    }

1142 1143 1144 1145 1146 1147
    /**
     * Gets the dependency relationship from this build (as the sink)
     * and that project (as the source.)
     *
     * @return
     *      Build number of the upstream build that feed into this build,
1148
     *      or -1 if no record is available (for example if there is no {@link FingerprintAction}, even if there is an {@link Cause.UpstreamCause}).
1149
     */
1150 1151
    public int getUpstreamRelationship(AbstractProject that) {
        FingerprintAction f = getAction(FingerprintAction.class);
1152
        if (f==null)     return -1;
1153 1154 1155 1156 1157

        int n = -1;

        // look for fingerprints that point to the given project as the source, and merge them all
        for (Fingerprint e : f.getFingerprints().values()) {
1158 1159 1160 1161
            if (upstreamCulprits) {
                // With upstreamCulprits, we allow upstream relationships
                // from intermediate jobs
                Fingerprint.RangeSet rangeset = e.getRangeSet(that);
1162
                if (!rangeset.isEmpty()) {
1163 1164 1165 1166
                    n = Math.max(n, rangeset.listNumbersReverse().iterator().next());
                }
            } else {
                BuildPtr o = e.getOriginal();
1167
                if (o!=null && o.belongsTo(that))
1168 1169
                    n = Math.max(n,o.getNumber());
            }
1170 1171 1172 1173
        }

        return n;
    }
1174

K
kohsuke 已提交
1175 1176 1177 1178 1179 1180 1181 1182
    /**
     * Works like {@link #getUpstreamRelationship(AbstractProject)} but returns the
     * actual build object.
     *
     * @return
     *      null if no such upstream build was found, or it was found but the
     *      build record is already lost.
     */
1183
    public AbstractBuild<?,?> getUpstreamRelationshipBuild(AbstractProject<?,?> that) {
K
kohsuke 已提交
1184
        int n = getUpstreamRelationship(that);
1185
        if (n==-1)   return null;
K
kohsuke 已提交
1186 1187 1188
        return that.getBuildByNumber(n);
    }

1189 1190 1191 1192 1193 1194
    /**
     * Gets the downstream builds of this build, which are the builds of the
     * downstream projects that use artifacts of this build.
     *
     * @return
     *      For each project with fingerprinting enabled, returns the range
1195
     *      of builds (which can be empty if no build uses the artifact from this build or downstream is not {@link AbstractProject#isFingerprintConfigured}.)
1196 1197 1198 1199
     */
    public Map<AbstractProject,RangeSet> getDownstreamBuilds() {
        Map<AbstractProject,RangeSet> r = new HashMap<AbstractProject,RangeSet>();
        for (AbstractProject p : getParent().getDownstreamProjects()) {
1200
            if (p.isFingerprintConfigured())
1201 1202 1203 1204
                r.put(p,getDownstreamRelationship(p));
        }
        return r;
    }
1205

1206 1207 1208
    /**
     * Gets the upstream builds of this build, which are the builds of the
     * upstream projects whose artifacts feed into this build.
1209
     * @return empty if there is no {@link FingerprintAction} (even if there is an {@link Cause.UpstreamCause})
K
kohsuke 已提交
1210
     * @see #getTransitiveUpstreamBuilds()
1211 1212
     */
    public Map<AbstractProject,Integer> getUpstreamBuilds() {
K
kohsuke 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
        return _getUpstreamBuilds(getParent().getUpstreamProjects());
    }

    /**
     * Works like {@link #getUpstreamBuilds()}  but also includes all the transitive
     * dependencies as well.
     */
    public Map<AbstractProject,Integer> getTransitiveUpstreamBuilds() {
        return _getUpstreamBuilds(getParent().getTransitiveUpstreamProjects());
    }

    private Map<AbstractProject, Integer> _getUpstreamBuilds(Collection<AbstractProject> projects) {
1225
        Map<AbstractProject,Integer> r = new HashMap<AbstractProject,Integer>();
K
kohsuke 已提交
1226
        for (AbstractProject p : projects) {
1227
            int n = getUpstreamRelationship(p);
1228
            if (n>=0)
1229 1230 1231 1232 1233
                r.put(p,n);
        }
        return r;
    }

1234 1235
    /**
     * Gets the changes in the dependency between the given build and this build.
1236
     * @return empty if there is no {@link FingerprintAction}
1237 1238
     */
    public Map<AbstractProject,DependencyChange> getDependencyChanges(AbstractBuild from) {
1239
        if (from==null)             return Collections.emptyMap(); // make it easy to call this from views
1240 1241
        FingerprintAction n = this.getAction(FingerprintAction.class);
        FingerprintAction o = from.getAction(FingerprintAction.class);
1242
        if (n==null || o==null)     return Collections.emptyMap();
1243

1244 1245
        Map<AbstractProject,Integer> ndep = n.getDependencies(true);
        Map<AbstractProject,Integer> odep = o.getDependencies(true);
1246 1247 1248 1249 1250 1251 1252

        Map<AbstractProject,DependencyChange> r = new HashMap<AbstractProject,DependencyChange>();

        for (Map.Entry<AbstractProject,Integer> entry : odep.entrySet()) {
            AbstractProject p = entry.getKey();
            Integer oldNumber = entry.getValue();
            Integer newNumber = ndep.get(p);
1253
            if (newNumber!=null && oldNumber.compareTo(newNumber)<0) {
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
                r.put(p,new DependencyChange(p,oldNumber,newNumber));
            }
        }

        return r;
    }

    /**
     * Represents a change in the dependency.
     */
    public static final class DependencyChange {
        /**
         * The dependency project.
         */
        public final AbstractProject project;
        /**
         * Version of the dependency project used in the previous build.
         */
        public final int fromId;
        /**
         * {@link Build} object for {@link #fromId}. Can be null if the log is gone.
         */
        public final AbstractBuild from;
        /**
         * Version of the dependency project used in this build.
         */
        public final int toId;

        public final AbstractBuild to;

        public DependencyChange(AbstractProject<?,?> project, int fromId, int toId) {
            this.project = project;
            this.fromId = fromId;
            this.toId = toId;
            this.from = project.getBuildByNumber(fromId);
            this.to = project.getBuildByNumber(toId);
        }
1291 1292 1293 1294 1295

        /**
         * Gets the {@link AbstractBuild} objects (fromId,toId].
         * <p>
         * This method returns all such available builds in the ascending order
1296
         * of IDs, but due to log rotations, some builds may be already unavailable.
1297 1298 1299 1300 1301
         */
        public List<AbstractBuild> getBuilds() {
            List<AbstractBuild> r = new ArrayList<AbstractBuild>();

            AbstractBuild<?,?> b = (AbstractBuild)project.getNearestBuild(fromId);
1302
            if (b!=null && b.getNumber()==fromId)
1303 1304
                b = b.getNextBuild(); // fromId exclusive

1305
            while (b!=null && b.getNumber()<=toId) {
1306 1307 1308 1309 1310 1311
                r.add(b);
                b = b.getNextBuild();
            }

            return r;
        }
1312
    }
1313

1314 1315 1316 1317
    //
    // web methods
    //

1318 1319 1320 1321 1322 1323 1324 1325
    /**
     * @deprecated as of 1.489
     *      Use {@link #doStop()}
     */
    public void doStop(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
        doStop().generateResponse(req,rsp,this);
    }

K
kohsuke 已提交
1326 1327 1328 1329 1330
    /**
     * Stops this build if it's still going.
     *
     * If we use this/executor/stop URL, it causes 404 if the build is already killed,
     * as {@link #getExecutor()} returns null.
C
Christoph Kutzinski 已提交
1331 1332
     * 
     * @since 1.489
K
kohsuke 已提交
1333
     */
J
Jesse Glick 已提交
1334
    @RequirePOST
1335
    public synchronized HttpResponse doStop() throws IOException, ServletException {
K
kohsuke 已提交
1336
        Executor e = getExecutor();
1337 1338
        if (e==null)
            e = getOneOffExecutor();
1339
        if (e!=null)
1340
            return e.doStop();
K
kohsuke 已提交
1341 1342
        else
            // nothing is building
1343
            return HttpResponses.forwardToPreviousPage();
K
kohsuke 已提交
1344
    }
1345 1346

    private static final Logger LOGGER = Logger.getLogger(AbstractBuild.class.getName());
1347
}
1348 1349