SCMListener.java 6.3 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 * 
 * 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:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * 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.listeners;

26
import hudson.Extension;
27
import hudson.ExtensionPoint;
J
Jesse Glick 已提交
28 29
import hudson.FilePath;
import hudson.Launcher;
30
import hudson.Util;
31
import hudson.model.AbstractBuild;
K
kohsuke 已提交
32
import hudson.model.Action;
K
kohsuke 已提交
33
import hudson.model.BuildListener;
34 35
import hudson.model.Run;
import hudson.model.TaskListener;
36
import hudson.scm.ChangeLogSet;
K
kohsuke 已提交
37
import hudson.scm.SCM;
J
Jesse Glick 已提交
38 39
import hudson.scm.SCMRevisionState;
import java.io.File;
40 41 42 43
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
J
Jesse Glick 已提交
44
import javax.annotation.CheckForNull;
45
import jenkins.model.Jenkins;
46 47 48 49 50 51 52 53

/**
 * Receives notifications about SCM activities in Hudson.
 *
 * <p>
 * This is an abstract class so that methods added in the future won't break existing listeners.
 *
 * <p>
54
 * Once instantiated, use the {@link #register()} method to start receiving events. 
55 56
 *
 * @author Kohsuke Kawaguchi
57
 * @see jenkins.model.Jenkins#getSCMListeners()
58 59
 * @since 1.70
 */
K
kohsuke 已提交
60
public abstract class SCMListener implements ExtensionPoint {
J
Jesse Glick 已提交
61 62

    /**
J
Jesse Glick 已提交
63
     * Should be called immediately after {@link SCM#checkout(Run, Launcher, FilePath, TaskListener, File, SCMRevisionState)} is called.
J
Jesse Glick 已提交
64 65
     * @param pollingBaseline information about what actually was checked out, if that is available, and this checkout is intended to be included in the build’s polling (if it does any at all)
     * @throws Exception if the checkout should be considered failed
66
     * @since 1.568
J
Jesse Glick 已提交
67 68 69
     */
    public void onCheckout(Run<?,?> build, SCM scm, FilePath workspace, TaskListener listener, @CheckForNull File changelogFile, @CheckForNull SCMRevisionState pollingBaseline) throws Exception {}

70 71 72 73
    /**
     * Called once the changelog is determined.
     *
     * <p>
K
Kohsuke Kawaguchi 已提交
74
     * During a build, Jenkins fetches the update of the workspace from SCM,
75 76 77 78 79 80 81 82 83 84
     * and determines the changelog (see {@link SCM#checkout}). Immediately
     * after that, a build will invoke this method on all registered
     * {@link SCMListener}s.
     *
     * <p>
     * If a build failed before we successfully determine changelog, this method
     * will not be invoked (for example, if "cvs update" failed.) OTOH, this method
     * is invoked before the actual build (like ant invocation) happens. 
     *
     * <p>
K
kohsuke 已提交
85 86 87 88 89 90 91
     * This is an opportunity for SCM-related plugins to act on changelog.
     * A typical usage includes parsing commit messages and do cross-referencing
     * between other systems. Implementations can also contribute {@link Action}
     * to {@link AbstractBuild} (by {@code build.getActions().add(...)} to
     * display additional data on build views.
     *
     * <p>
92 93 94 95 96
     * TODO: once we have cvsnews plugin, refer to its usage.
     *
     * @param build
     *      The build in progress, which just finished determining changelog.
     *      At this point this build is still in progress. Never null.
K
kohsuke 已提交
97 98 99 100
     * @param listener
     *      {@link BuildListener} for on-going build. This can be used to report
     *      any errors or the general logging of what's going on. This will show
     *      up in the "console output" of the build. Never null.
101 102 103 104
     * @param changelog
     *      Set of changes detected in this build. This is the same value
     *      returned from {@link AbstractBuild#getChangeSet()} but passed
     *      separately for convenience.
K
kohsuke 已提交
105 106 107 108
     *
     * @throws Exception
     *      If any exception is thrown from this method, it will be recorded
     *      and causes the build to fail. 
109
     * @since 1.568
110
     */
J
Jesse Glick 已提交
111
    public void onChangeLogParsed(Run<?,?> build, SCM scm, TaskListener listener, ChangeLogSet<?> changelog) throws Exception {
112 113 114 115 116 117
        if (build instanceof AbstractBuild && listener instanceof BuildListener && Util.isOverridden(SCMListener.class, getClass(), "onChangeLogParsed", AbstractBuild.class, BuildListener.class, ChangeLogSet.class)) {
            onChangeLogParsed((AbstractBuild) build, (BuildListener) listener, changelog);
        }
    }

    @Deprecated
K
kohsuke 已提交
118
    public void onChangeLogParsed(AbstractBuild<?,?> build, BuildListener listener, ChangeLogSet<?> changelog) throws Exception {
119 120 121
        if (Util.isOverridden(SCMListener.class, getClass(), "onChangeLogParsed", Run.class, SCM.class, TaskListener.class, ChangeLogSet.class)) {
            onChangeLogParsed((Run) build, build.getProject().getScm(), listener, changelog);
        }
122 123
    }

124 125 126
    /**
     * @since 1.568
     */
127 128
    @SuppressWarnings("deprecation")
    public static Collection<? extends SCMListener> all() {
129
        Jenkins j = Jenkins.getInstanceOrNull();
130
        if (j == null) { // TODO use !Functions.isExtensionsAvailable() once JENKINS-33377
131 132 133 134 135 136 137 138 139 140 141
            return Collections.emptySet();
        }
        List<SCMListener> r = new ArrayList<SCMListener>(j.getExtensionList(SCMListener.class));
        for (SCMListener l : j.getSCMListeners()) {
            r.add(l);
        }
        return r;
    }

    /** @deprecated Use {@link Extension} instead. */
    @Deprecated
142
    public final void register() {
143
        Jenkins.getInstance().getSCMListeners().add(this);
144 145
    }

146 147
    /** @deprecated Use {@link Extension} instead. */
    @Deprecated
148
    public final boolean unregister() {
149
        return Jenkins.getInstance().getSCMListeners().remove(this);
150 151
    }
}