Dashboard > CruiseControl > ... > 3rdPartyCCStuff > IRCPublisher
IRCPublisher Log In | Sign Up   View a printable version of the current page.

Added by Ilkka Laukkanen , last edited by Santeri Vesalainen on Jan 15, 2009  (view change) show comment
Labels: 
(None)

Introduction

Here is a very simple IRC publisher CC plugin. When invoked, it connects to the IRC server, joins the channel and simply sends a message indicating what project was built and whether it failed, was fixed or was successful.

Usage

Compile the code by simply dropping the new sourcefile into main/src/net/sourceforge/cruisecontrol/publishers in your CruiseControl source distribution. Then run ant in the main directory and the new publisher gets built in.

It is configured like so:

<!-- load the plugin -->
<plugin name="irc" classname="net.sourceforge.cruisecontrol.publishers.IRCPublisher"/>

...

<!-- publish to IRC -->
<irc host="irc.example.com"
   port="6667"
   username="CruiseControl"
   nickname="buildbot"
   realname="CruiseControl build loop"
   channel="#cruisecontrol"
   buildresultsurl="http://www.example.com/cc"/>

Aknowledgements

The buildresultsURL stuff is pilfered from EmailPublisher.

Source code

IRCPublisher.java
/**
 * IRC publisher for CruiseControl using the moepii irc library
 * (http://moepii.sourceforge.net/)
 */
package net.sourceforge.cruisecontrol.publishers;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import net.sourceforge.cruisecontrol.CruiseControlException;
import net.sourceforge.cruisecontrol.Publisher;
import net.sourceforge.cruisecontrol.util.ValidationHelper;
import net.sourceforge.cruisecontrol.util.XMLLogHelper;

import org.apache.log4j.Logger;
import org.jdom.Element;
import org.schwering.irc.lib.IRCConnection;
import org.schwering.irc.lib.IRCEventListener;
import org.schwering.irc.lib.IRCModeParser;
import org.schwering.irc.lib.IRCUser;

/**
 * @author Ilkka Poutanen <PoutanenIlkka@JohnDeere.com>
 *
 */
public class IRCPublisher implements Publisher {

    /**
     * Our logger object.
     */
    private static final Logger LOG = Logger.getLogger(IRCPublisher.class);

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * The IRC connection.
     */
    private IRCConnection conn;

    /**
     * The IRC server
     */
    private String host;

    /**
     * The IRC server's port
     */
    private int port = 6667;

    /**
     * The nickname to use.
     */
    private String nickname = "buildbot";

    /**
     * The username to use.
     */
    private String username = "CruiseControl";

    /**
     * The realname to use.
     */
    private String realname = "CruiseControl build daemon";

    /**
     * The channel to use.
     */
    private String channel;

    /**
     * The quit message.
     */
    private String quitmsg = "Buh-bye";

    /**
     * The URL to the build results.
     */
    private String buildResultsURL;

    /*
     * (non-Javadoc)
     *
     * @see net.sourceforge.cruisecontrol.Publisher#publish(org.jdom.Element)
     */
    public void publish(Element cruisecontrolLog) throws CruiseControlException {
        // initialize
        init();

        // connect
        try {
            conn.connect();
        } catch (IOException ioe) {
            LOG.error("Couldn't connect to IRC server", ioe);
        }

        // join the channel
        conn.doJoin(channel);

        // generate message
        XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
        String msg = "";
        if (helper.isBuildSuccessful()) {
            msg += "Build successful for " + helper.getProjectName();
        } else if (helper.isBuildFix()) {
            msg += "Build fixed for " + helper.getProjectName();
        } else {
            msg += "Build FAILURE for " + helper.getProjectName() + ". ";
            msg += "Includes modifications by ";
            Set modificationset = helper.getBuildParticipants();
            Iterator iter = modificationset.iterator();
            while (iter.hasNext()) {
                String modname = (String) iter.next();
                msg += modname;
                if (iter.hasNext()) {
                    msg += ", ";
                }
            }
        }
        msg += ". Result URL: " + getLogfileURL(helper);

        // send the message
        conn.doPrivmsg(channel, msg);

        // quit irc
        conn.doQuit(quitmsg);
    }

    /**
     * @return the channel
     */
    public String getChannel() {
        return channel;
    }

    /**
     * @param channel
     *            the channel to set
     */
    public void setChannel(String channel) {
        this.channel = channel;
    }

    /**
     * @return the host
     */
    public String getHost() {
        return host;
    }

    /**
     * @param host
     *            the host to set
     */
    public void setHost(String host) {
        this.host = host;
    }

    /**
     * @return the nickname
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * @param nickname
     *            the nickname to set
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * @return the port
     */
    public int getPort() {
        return port;
    }

    /**
     * @param port
     *            the port to set
     */
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username
     *            the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Initialize the connection.
     */
    protected void init() {
        // create the connection
        conn = new IRCConnection(host, new int[] { port }, null, nickname,
                username, realname);
        conn.addIRCEventListener(new Listener());
        conn.setEncoding("UTF-8");
        conn.setPong(true);
        conn.setDaemon(false);
        conn.setColors(true);
    }

    /*
     * (non-Javadoc)
     *
     * @see net.sourceforge.cruisecontrol.Publisher#validate()
     */
    public void validate() throws CruiseControlException {
        // Validate required parameters
        ValidationHelper.assertIsSet(host, "host", this.getClass());
        ValidationHelper.assertIsSet(nickname, "nickname", this.getClass());
        ValidationHelper.assertIsSet(username, "username", this.getClass());
        ValidationHelper.assertIsSet(channel, "channel", this.getClass());
        ValidationHelper.assertIsSet(buildResultsURL, "buildResultsURL", this.getClass());
    }

    /**
     * This method returns the build result URL as a string.
     * @param logHelper the log helper.
     * @return the URL as string.
     */
    protected String getLogfileURL(XMLLogHelper logHelper) {
        String logFileName = "";
        try {
            logFileName = logHelper.getLogFileName();
        } catch (CruiseControlException e) {
            LOG.error("Error getting logfile name from helper", e);
        }
        String baseLogFileName =
            logFileName.substring(
                logFileName.lastIndexOf(File.separator) + 1,
                logFileName.lastIndexOf("."));

        StringBuffer str = new StringBuffer();
        str.append(getBuildResultsURL());

        if (buildResultsURL.indexOf("?") == -1) {
            str.append("?");
        } else {
            str.append("&");
        }

        str.append("log=");
        str.append(baseLogFileName);

        return str.toString();
    }

    /**
     * @return the realname
     */
    public String getRealname() {
        return realname;
    }

    /**
     * @param realname
     *            the realname to set
     */
    public void setRealname(String realname) {
        this.realname = realname;
    }

    public class Listener implements IRCEventListener {

        public void onDisconnected() {
            LOG.info("Disconnected");
        }

        public void onError(String msg) {
            LOG.warn("Error: " + msg);
        }

        public void onError(int num, String msg) {
            LOG.warn("Error: " + num + " : " + msg);
        }

        public void onInvite(String chan, IRCUser user, String passiveNick) {
            LOG.info("Invite to " + chan + " from " + user);
        }

        public void onJoin(String chan, IRCUser user) {
            LOG.info("Joining " + chan);
        }

        public void onKick(String chan, IRCUser user, String passiveNick,
                String msg) {
            LOG.info("Kick on " + chan + ": " + user + " (" + msg + ")");
        }

        public void onMode(String chan, IRCUser user, IRCModeParser modeParser) {
            LOG.info("Mode " + modeParser.getLine() + " (" + user + "@" + chan
                    + ")");
        }

        public void onMode(IRCUser user, String passiveNick, String mode) {
            LOG.info("Mode " + mode + " (" + user + ")");
        }

        public void onNick(IRCUser user, String newNick) {
            LOG.info("Nick change for " + user + ": " + newNick);
        }

        public void onNotice(String target, IRCUser user, String msg) {
            LOG.info("Notice: " + target + " " + user + ": " + msg);
        }

        public void onPart(String chan, IRCUser user, String msg) {
            LOG.info("Part: " + chan + " " + user + " " + msg);
        }

        public void onPing(String ping) {
            LOG.info("Ping");
        }

        public void onPrivmsg(String target, IRCUser user, String msg) {
            LOG.info("PrivMsg: " + target + " " + user + " " + msg);
        }

        public void onQuit(IRCUser user, String msg) {
            LOG.info("Quite: " + user + " " + msg);
        }

        public void onRegistered() {
            LOG.info("Registered");
        }

        public void onReply(int num, String value, String msg) {
            LOG.info("Reply: " + num + " " + value + " " + msg);
        }

        public void onTopic(String chan, IRCUser user, String topic) {
            LOG.info("Topic: " + chan + " " + user + " " + topic);
        }

        public void unknown(String prefix, String command, String middle,
                String trailing) {
            LOG.warn("Unknown: " + command);
        }

    }

    /**
     * @return the quitmsg
     */
    public String getQuitmsg() {
        return quitmsg;
    }

    /**
     * @param quitmsg the quitmsg to set
     */
    public void setQuitmsg(String quitmsg) {
        this.quitmsg = quitmsg;
    }

    /**
     * @return the buildResultsURL
     */
    public String getBuildResultsURL() {
        return buildResultsURL;
    }

    /**
     * @param buildResultsURL the buildResultsURL to set
     */
    public void setBuildResultsURL(String buildResultsURL) {
        this.buildResultsURL = buildResultsURL;
    }
}
Powered by a free Atlassian Confluence Open Source Project License granted to ThoughtWorks, Inc.. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators