AbstractBuild.java 50.8 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 33
import hudson.Launcher;
import hudson.Util;
34 35
import hudson.console.AnnotatedLargeText;
import hudson.console.ExpandableDetailsNote;
36
import hudson.console.ModelHyperlinkNote;
37
import hudson.matrix.MatrixConfiguration;
38 39
import hudson.model.Fingerprint.BuildPtr;
import hudson.model.Fingerprint.RangeSet;
40
import hudson.model.listeners.RunListener;
K
kohsuke 已提交
41
import hudson.model.listeners.SCMListener;
42 43 44
import hudson.scm.ChangeLogParser;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
45
import hudson.scm.NullChangeLogParser;
46 47 48 49
import hudson.scm.SCM;
import hudson.slaves.NodeProperty;
import hudson.slaves.WorkspaceList;
import hudson.slaves.WorkspaceList.Lease;
50
import hudson.tasks.BuildStep;
51 52
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.BuildTrigger;
53
import hudson.tasks.BuildWrapper;
54 55
import hudson.tasks.Builder;
import hudson.tasks.Fingerprinter.FingerprintAction;
56
import hudson.tasks.Publisher;
57
import hudson.tasks.test.AbstractTestResultAction;
58
import hudson.tasks.test.AggregatedTestResultAction;
59
import hudson.util.*;
60
import jenkins.model.Jenkins;
61
import jenkins.model.lazy.AbstractLazyLoadRunMap.Direction;
62
import jenkins.model.lazy.BuildReference;
63
import org.kohsuke.stapler.HttpResponse;
64
import org.kohsuke.stapler.Stapler;
65 66
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
67
import org.kohsuke.stapler.export.Exported;
68
import org.xml.sax.SAXException;
69

70
import javax.servlet.ServletException;
71 72
import java.io.File;
import java.io.IOException;
73
import java.io.InterruptedIOException;
74
import java.io.StringWriter;
75
import java.lang.ref.WeakReference;
76
import java.text.MessageFormat;
77 78 79 80 81 82 83 84 85 86 87
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;
88 89
import java.util.logging.Level;
import java.util.logging.Logger;
90
import javax.annotation.CheckForNull;
J
Jesse Glick 已提交
91
import org.kohsuke.stapler.interceptor.RequirePOST;
K
kohsuke 已提交
92

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

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

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

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

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

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

127 128 129 130 131 132 133 134 135
    /**
     * 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 已提交
136
    private volatile transient WeakReference<ChangeLogSet<? extends Entry>> changeSet;
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151
    /**
     * 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;
152

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

159 160 161 162 163 164 165 166 167 168 169 170 171 172
    /**
     * 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
     * fields and point to {@link #selfReference} of adjacent builds.
     */
    private volatile transient BuildReference<R> previousBuild, nextBuild;

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



173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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();
    }

189 190 191 192 193 194
    @Override
    void dropLinks() {
        super.dropLinks();

        if(nextBuild!=null) {
            AbstractBuild nb = nextBuild.get();
195 196 197 198 199 200 201
            if (nb!=null) {
                // remove the oldest build
                if (previousBuild == selfReference) 
                    nb.previousBuild = nextBuild;
                else 
                    nb.previousBuild = previousBuild;
            }
202 203 204 205 206 207
        }
        if(previousBuild!=null) {
            AbstractBuild pb = previousBuild.get();
            if (pb!=null)   pb.nextBuild = nextBuild;
        }
    }
208 209 210

    @Override
    public R getPreviousBuild() {
211 212 213 214 215
        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 已提交
216 217
                P _parent = getParent();
                if (_parent == null) {
218
                    throw new IllegalStateException("no parent for " + number + " in " + workspace);
J
Jesse Glick 已提交
219 220
                }
                R pb = _parent._getRuns().search(number-1, Direction.DESC);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                if (pb!=null) {
                    ((AbstractBuild)pb).nextBuild = selfReference;   // establish bi-di link
                    this.previousBuild = pb.selfReference;
                    return pb;
                } else {
                    // this indicates that we know there's no previous build
                    // (as opposed to we don't know if/what our previous build is.
                    this.previousBuild = selfReference;
                    return null;
                }
            }
            if (r==selfReference)
                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;
240 241 242 243 244
        }
    }

    @Override
    public R getNextBuild() {
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        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 {
                    // this indicates that we know there's no next build
                    // (as opposed to we don't know if/what our next build is.
                    this.nextBuild = selfReference;
                    return null;
                }
            }
            if (r==selfReference)
                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;
270 271 272
        }
    }

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

    /**
M
mindless 已提交
287 288
     * 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.)
289
     */
K
kohsuke 已提交
290
    @Exported(name="builtOn")
291 292 293 294
    public String getBuiltOnStr() {
        return builtOn;
    }

295
    /**
296 297 298 299
     * 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.
     *
300 301
     * @since 1.429
     */
302
    protected void setBuiltOnStr( String builtOn ) {
303 304 305
        this.builtOn = builtOn;
    }

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    /**
     * 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 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    /**
     * 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 已提交
340 341 342 343 344
    /**
     * Gets the directory where this build is being built.
     *
     * <p>
     * Note to implementors: to control where the workspace is created, override
345
     * {@link AbstractBuildExecution#decideWorkspace(Node,WorkspaceList)}.
K
kohsuke 已提交
346 347 348 349 350
     *
     * @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 已提交
351
     * @since 1.319
K
kohsuke 已提交
352
     */
353
    public final @CheckForNull FilePath getWorkspace() {
354
        if (workspace==null) return null;
K
kohsuke 已提交
355
        Node n = getBuiltOn();
356
        if (n==null) return null;
K
kohsuke 已提交
357 358 359
        return n.createPath(workspace);
    }

360
    /**
361
     * Normally, a workspace is assigned by {@link hudson.model.Run.RunExecution}, but this lets you set the workspace in case
362 363 364 365 366 367
     * {@link AbstractBuild} is created without a build.
     */
    protected void setWorkspace(FilePath ws) {
        this.workspace = ws.getRemote();
    }

K
kohsuke 已提交
368 369 370 371 372 373 374 375
    /**
     * 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();
376
        if (ws==null)    return null;
377
        return getParent().getScm().getModuleRoot(ws, this);
K
kohsuke 已提交
378 379 380 381 382 383 384 385 386 387 388
    }

    /**
     * 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();
389
        if (ws==null)    return null;
390
        return getParent().getScm().getModuleRoots(ws, this);
K
kohsuke 已提交
391 392
    }

393 394 395 396
    /**
     * List of users who committed a change since the last non-broken build till now.
     *
     * <p>
K
kohsuke 已提交
397 398
     * 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.
399 400 401 402 403 404
     *
     * @return
     *      can be empty but never null.
     */
    @Exported
    public Set<User> getCulprits() {
405
        if (culprits==null) {
406
            Set<User> r = new HashSet<User>();
407
            R p = getPreviousCompletedBuild();
408
            if (p !=null && isBuilding()) {
K
kohsuke 已提交
409
                Result pr = p.getResult();
K
Kohsuke Kawaguchi 已提交
410
                if (pr!=null && pr.isWorseThan(Result.SUCCESS)) {
K
kohsuke 已提交
411 412 413 414 415 416
                    // 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());
                }
417
            }
418
            for (Entry e : getChangeSet())
419
                r.add(e.getAuthor());
420 421 422 423

            if (upstreamCulprits) {
                // If we have dependencies since the last successful build, add their authors to our list
                if (getPreviousNotFailedBuild() != null) {
M
marco 已提交
424
                    Map <AbstractProject,AbstractBuild.DependencyChange> depmap = getDependencyChanges(getPreviousSuccessfulBuild());
425 426 427 428 429 430 431 432 433 434
                    for (AbstractBuild.DependencyChange dep : depmap.values()) {
                        for (AbstractBuild<?,?> b : dep.getBuilds()) {
                            for (Entry entry : b.getChangeSet()) {
                                r.add(entry.getAuthor());
                            }
                        }
                    }
                }
            }

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
            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();
            }
        };
    }

453 454 455 456 457 458 459
    /**
     * 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())
460 461 462
            try{
                if (e.getAuthor()==user)
                    return true;
463 464
            } catch (RuntimeException re) { 
                LOGGER.log(Level.INFO, "Failed to determine author of changelog " + e.getCommitId() + "for " + getParent().getDisplayName() + ", " + getDisplayName(), re);
465
            }
466 467 468
        return false;
    }

K
kohsuke 已提交
469 470 471 472 473 474 475 476 477
    /**
     * Gets the version of Hudson that was used to build this job.
     *
     * @since 1.246
     */
    public String getHudsonVersion() {
        return hudsonVersion;
    }

478 479
    /**
     * @deprecated as of 1.467
480
     *      Please use {@link hudson.model.Run.RunExecution}
481 482 483 484 485 486 487 488 489 490 491
     */
    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.
         */

492 493
        /**
         * Since configuration can be changed while a build is in progress,
494
         * create a launcher once and stick to it for the entire build duration.
495 496 497
         */
        protected Launcher launcher;

498 499 500 501 502
        /**
         * Output/progress of this build goes here.
         */
        protected BuildListener listener;

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

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

        public BuildListener getListener() {
            return listener;
        }

K
kohsuke 已提交
518 519 520 521 522 523 524 525
        /**
         * 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.
         */
526
        protected Lease decideWorkspace(Node n, WorkspaceList wsl) throws InterruptedException, IOException {
527 528 529 530 531
            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 已提交
532
            // TODO: this cast is indicative of abstraction problem
533
            return wsl.allocate(n.getWorkspaceFor((TopLevelItem)getProject()), getBuild());
K
kohsuke 已提交
534 535
        }

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

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

            try {
553
                workspace = lease.path.getRemote();
554
                listener.getLogger().println(Messages.AbstractBuild_BuildingInWorkspace(workspace));
555
                node.getFileSystemProvisioner().prepareWorkspace(AbstractBuild.this,lease.path,listener);
556 557 558 559
                
                for (WorkspaceListener wl : WorkspaceListener.all()) {
                    wl.beforeUse(AbstractBuild.this, lease.path, listener);
                }
560

561 562
                getProject().getScmCheckoutStrategy().preCheckout(AbstractBuild.this, launcher, this.listener);
                getProject().getScmCheckoutStrategy().checkout(this);
563

564
                if (!preBuild(listener,project.getProperties()))
K
kohsuke 已提交
565
                    return Result.FAILURE;
566

K
kohsuke 已提交
567
                Result result = doRun(listener);
568

569 570
                Computer c = node.toComputer();
                if (c==null || c.isOffline()) {
571 572
                    // 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.
573
                    listener.hyperlink("/computer/"+builtOn+"/log","Looks like the node went offline during the build. Check the slave log for the details.");
574

D
Dave Brosius 已提交
575 576 577 578 579 580 581 582 583 584
                    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();
                    }
585 586
                }

K
kohsuke 已提交
587 588 589 590
                // 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());
591

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

K
kohsuke 已提交
597 598
                return result;
            } finally {
599
                lease.release();
600
                this.listener = null;
K
kohsuke 已提交
601
            }
602 603
        }

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

            if (project instanceof BuildableItemWithBuildWrappers) {
                BuildableItemWithBuildWrappers biwbw = (BuildableItemWithBuildWrappers) project;
616
                for (BuildWrapper bw : biwbw.getBuildWrappersList())
617 618 619 620 621
                    l = bw.decorateLauncher(AbstractBuild.this,l,listener);
            }

            buildEnvironments = new ArrayList<Environment>();

622 623 624 625 626 627 628
            for (RunListener rl: RunListener.all()) {
                Environment environment = rl.setUpEnvironment(AbstractBuild.this, l, listener);
                if (environment != null) {
                    buildEnvironments.add(environment);
                }
            }

629
            for (NodeProperty nodeProperty: Jenkins.getInstance().getGlobalNodeProperties()) {
630 631 632 633 634 635 636 637 638 639 640 641 642 643
                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;
644 645
        }

646 647 648
        public void defaultCheckout() throws IOException, InterruptedException {
            AbstractBuild<?,?> build = AbstractBuild.this;
            AbstractProject<?, ?> project = build.getProject();
649

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
            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 {
                    if (project.checkout(build,launcher,listener,new File(build.getRootDir(),"changelog.xml"))) {
                        // 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())
665 666 667 668 669
                            try {
                                l.onChangeLogParsed(build,listener,build.getChangeSet());
                            } catch (Exception e) {
                                throw new IOException2("Failed to parse changelog",e);
                            }
670 671 672 673

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

674 675 676 677 678 679 680 681 682
                        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());
683
                }
684 685 686 687 688 689 690

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

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

K
kohsuke 已提交
693 694 695 696 697 698 699 700 701
        /**
         * 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 已提交
702
        protected abstract Result doRun(BuildListener listener) throws Exception, RunnerAbortedException;
703 704 705 706 707 708 709 710 711 712 713 714 715 716

        /**
         * @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());
717
                culprits = ImmutableSortedSet.copyOf(r);
K
kohsuke 已提交
718
                CheckPoint.CULPRITS_DETERMINED.report();
719 720
            }
        }
721 722

        public void cleanUp(BuildListener listener) throws Exception {
723 724
            BuildTrigger.execute(AbstractBuild.this, listener);
            buildEnvironments = null;
725
        }
726

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
        /**
         * @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);
745 746 747 748 749 750 751 752
        }

        /**
         * 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.
         */
753
        protected final boolean performAllBuildSteps(BuildListener listener, Iterable<? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
754
            boolean r = true;
755 756
            for (BuildStep bs : buildSteps) {
                if ((bs instanceof Publisher && ((Publisher)bs).needsToRunAfterFinalized()) ^ phase)
757
                    try {
758 759 760 761
                        if (!perform(bs,listener)) {
                            LOGGER.fine(MessageFormat.format("{0} : {1} failed", AbstractBuild.this.toString(), bs));
                            r = false;
                        }
762
                    } catch (Exception e) {
763 764
                        String msg = "Publisher " + bs.getClass().getName() + " aborted due to exception";
                        e.printStackTrace(listener.error(msg));
765
                        LOGGER.log(WARNING, msg, e);
766
                        setResult(Result.FAILURE);
767
                    }
K
kohsuke 已提交
768
            }
769
            return r;
K
kohsuke 已提交
770 771 772 773 774 775 776 777 778 779 780
        }

        /**
         * 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;
781
            }
782
            Result oldResult = AbstractBuild.this.getResult();
783 784 785
            for (BuildStepListener bsl : BuildStepListener.all()) {
                bsl.started(AbstractBuild.this, bs, listener);
            }
786
            boolean canContinue = mon.perform(bs, AbstractBuild.this, launcher, listener);
787 788 789
            for (BuildStepListener bsl : BuildStepListener.all()) {
                bsl.finished(AbstractBuild.this, bs, listener, canContinue);
            }
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
            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();
            }
808
        }
809 810

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

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

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

828 829 830 831 832 833 834 835 836 837 838 839 840 841
    /**
     * 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();
    }

842 843 844 845 846
	/*
     * No need to to lock the entire AbstractBuild on change set calculcation
     */
    private transient Object changeSetLock = new Object();
    
847 848 849 850 851
    /**
     * Gets the changes incorporated into this build.
     *
     * @return never null.
     */
K
kohsuke 已提交
852
    @Exported
853
    public ChangeLogSet<? extends Entry> getChangeSet() {
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
        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);
                }
870 871
            }
        }
872

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
        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;
888 889 890 891 892 893 894 895 896 897 898 899
    }

    /**
     * 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");
900
        if (!changelogFile.exists())
901
            return ChangeLogSet.createEmpty(this);
902 903 904 905

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

    @Override
K
kohsuke 已提交
914 915
    public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
        EnvVars env = super.getEnvironment(log);
916 917 918
        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());
919

920
        project.getScm().buildEnvVars(this,env);
921

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

926 927 928
        for (EnvironmentContributingAction a : Util.filter(getActions(),EnvironmentContributingAction.class))
            a.buildEnvVars(this,env);

929
        EnvVars.resolve(env);
930

931 932
        return env;
    }
K
kohsuke 已提交
933

K
Kohsuke Kawaguchi 已提交
934 935
    /**
     * During the build, expose the environments contributed by {@link BuildWrapper}s and others.
936 937 938 939 940 941 942
     * 
     * <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 已提交
943 944 945
     * @return can be empty list, but never null. Immutable.
     * @since 1.437
     */
946
    public EnvironmentList getEnvironments() {
947 948 949 950 951 952
        Executor e = Executor.currentExecutor();
        if (e!=null && e.getCurrentExecutable()==this) {
            if (buildEnvironments==null)    buildEnvironments = new ArrayList<Environment>();
            return new EnvironmentList(buildEnvironments); 
        }
        
953
        return new EnvironmentList(buildEnvironments==null ? Collections.<Environment>emptyList() : ImmutableList.copyOf(buildEnvironments));
K
Kohsuke Kawaguchi 已提交
954 955
    }

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

964 965
    /**
     * Builds up a set of variable names that contain sensitive values that
A
alanharder 已提交
966
     * should not be exposed. The expectation is that this set is populated with
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 992 993
     * 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;
    }

994 995 996 997 998 999
    /**
     * 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 已提交
1000
     * {@link Builder}s to decide whether they want to recognize the values
1001 1002
     * or how to use them.
     *
1003 1004 1005 1006 1007
     * <p>
     * This also includes build parameters if a build is parameterized.
     *
     * @return
     *      The returned map is mutable so that subtypes can put more values.
1008 1009
     */
    public Map<String,String> getBuildVariables() {
1010 1011 1012
        Map<String,String> r = new HashMap<String, String>();

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

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

1027 1028 1029
        for (BuildVariableContributor bvc : BuildVariableContributor.all())
            bvc.buildVariablesFor(this,r);

1030
        return r;
1031
    }
1032 1033 1034 1035 1036 1037 1038 1039

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

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

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

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

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
//
//
// 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 已提交
1069
        OUTER:
K
kohsuke 已提交
1070
        for (AbstractProject<?,?> p : getParent().getDownstreamProjects()) {
1071
            if (!p.isKeepDependencies()) continue;
K
kohsuke 已提交
1072

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

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

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

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

        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.
1099
     *      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.
1100
     */
1101 1102 1103 1104
    public RangeSet getDownstreamRelationship(AbstractProject that) {
        RangeSet rs = new RangeSet();

        FingerprintAction f = getAction(FingerprintAction.class);
1105
        if (f==null)     return rs;
1106 1107 1108

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

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

        return rs;
    }
1123

K
kohsuke 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    /**
     * 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() {
1134 1135 1136 1137 1138 1139
                return Iterators.removeNull(
                    new AdaptedIterator<Integer,AbstractBuild<?,?>>(nums) {
                        protected AbstractBuild<?, ?> adapt(Integer item) {
                            return that.getBuildByNumber(item);
                        }
                    });
K
kohsuke 已提交
1140 1141 1142 1143
            }
        };
    }

1144 1145 1146 1147 1148 1149
    /**
     * 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,
1150
     *      or -1 if no record is available (for example if there is no {@link FingerprintAction}, even if there is an {@link Cause.UpstreamCause}).
1151
     */
1152 1153
    public int getUpstreamRelationship(AbstractProject that) {
        FingerprintAction f = getAction(FingerprintAction.class);
1154
        if (f==null)     return -1;
1155 1156 1157 1158 1159

        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()) {
1160 1161 1162 1163
            if (upstreamCulprits) {
                // With upstreamCulprits, we allow upstream relationships
                // from intermediate jobs
                Fingerprint.RangeSet rangeset = e.getRangeSet(that);
1164
                if (!rangeset.isEmpty()) {
1165 1166 1167 1168
                    n = Math.max(n, rangeset.listNumbersReverse().iterator().next());
                }
            } else {
                BuildPtr o = e.getOriginal();
1169
                if (o!=null && o.belongsTo(that))
1170 1171
                    n = Math.max(n,o.getNumber());
            }
1172 1173 1174 1175
        }

        return n;
    }
1176

K
kohsuke 已提交
1177 1178 1179 1180 1181 1182 1183 1184
    /**
     * 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.
     */
1185
    public AbstractBuild<?,?> getUpstreamRelationshipBuild(AbstractProject<?,?> that) {
K
kohsuke 已提交
1186
        int n = getUpstreamRelationship(that);
1187
        if (n==-1)   return null;
K
kohsuke 已提交
1188 1189 1190
        return that.getBuildByNumber(n);
    }

1191 1192 1193 1194 1195 1196
    /**
     * 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
1197
     *      of builds (which can be empty if no build uses the artifact from this build or downstream is not {@link AbstractProject#isFingerprintConfigured}.)
1198 1199 1200 1201
     */
    public Map<AbstractProject,RangeSet> getDownstreamBuilds() {
        Map<AbstractProject,RangeSet> r = new HashMap<AbstractProject,RangeSet>();
        for (AbstractProject p : getParent().getDownstreamProjects()) {
1202
            if (p.isFingerprintConfigured())
1203 1204 1205 1206
                r.put(p,getDownstreamRelationship(p));
        }
        return r;
    }
1207

1208 1209 1210
    /**
     * Gets the upstream builds of this build, which are the builds of the
     * upstream projects whose artifacts feed into this build.
1211
     * @return empty if there is no {@link FingerprintAction} (even if there is an {@link Cause.UpstreamCause})
K
kohsuke 已提交
1212
     * @see #getTransitiveUpstreamBuilds()
1213 1214
     */
    public Map<AbstractProject,Integer> getUpstreamBuilds() {
K
kohsuke 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
        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) {
1227
        Map<AbstractProject,Integer> r = new HashMap<AbstractProject,Integer>();
K
kohsuke 已提交
1228
        for (AbstractProject p : projects) {
1229
            int n = getUpstreamRelationship(p);
1230
            if (n>=0)
1231 1232 1233 1234 1235
                r.put(p,n);
        }
        return r;
    }

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

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

        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);
1255
            if (newNumber!=null && oldNumber.compareTo(newNumber)<0) {
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 1291 1292
                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);
        }
1293 1294 1295 1296 1297

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

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

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

            return r;
        }
1314
    }
1315

1316 1317 1318 1319
    //
    // web methods
    //

1320 1321 1322 1323 1324 1325 1326 1327
    /**
     * @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 已提交
1328 1329 1330 1331 1332
    /**
     * 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 已提交
1333 1334
     * 
     * @since 1.489
K
kohsuke 已提交
1335
     */
J
Jesse Glick 已提交
1336
    @RequirePOST
1337
    public synchronized HttpResponse doStop() throws IOException, ServletException {
K
kohsuke 已提交
1338
        Executor e = getExecutor();
1339 1340
        if (e==null)
            e = getOneOffExecutor();
1341
        if (e!=null)
1342
            return e.doStop();
K
kohsuke 已提交
1343 1344
        else
            // nothing is building
1345
            return HttpResponses.forwardToPreviousPage();
K
kohsuke 已提交
1346
    }
1347 1348

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