From 00f35d28162ce035ff1d7c4ecb492a77049417d7 Mon Sep 17 00:00:00 2001 From: syl20bnr Date: Thu, 6 Dec 2012 23:27:06 -0500 Subject: [PATCH] Added new configuration option to colorize sent messages depending on the current and previous states of job. --- .../hudson/plugins/ircbot/IrcPublisher.java | 18 ++++++-- .../hudson/plugins/ircbot/v2/IRCColor.java | 46 +++++++++++++++++++ .../plugins/ircbot/v2/IRCConnection.java | 4 ++ .../plugins/ircbot/IrcPublisher/global.jelly | 3 ++ .../webapp/help-globalConfigUseColors.html | 25 ++++++++++ 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/main/java/hudson/plugins/ircbot/v2/IRCColor.java create mode 100644 src/main/webapp/help-globalConfigUseColors.html diff --git a/src/main/java/hudson/plugins/ircbot/IrcPublisher.java b/src/main/java/hudson/plugins/ircbot/IrcPublisher.java index 18358f90..7b74e967 100644 --- a/src/main/java/hudson/plugins/ircbot/IrcPublisher.java +++ b/src/main/java/hudson/plugins/ircbot/IrcPublisher.java @@ -141,6 +141,7 @@ public static final class DescriptorImpl extends BuildStepDescriptor private static final String PREFIX = "irc_publisher."; public static final String PARAMETERNAME_USE_NOTICE = PREFIX + "useNotice"; + public static final String PARAMETERNAME_USE_COLORS = PREFIX + "useColors"; public static final String PARAMETERNAME_NICKSERV_PASSWORD = PREFIX + "nickServPassword"; public static final String[] CHARSETS; @@ -198,7 +199,9 @@ public static final class DescriptorImpl extends BuildStepDescriptor private boolean useNotice; private String charset; - + + private boolean useColors; + DescriptorImpl() { super(IrcPublisher.class); load(); @@ -270,9 +273,11 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc this.hudsonLogin = req.getParameter(getParamNames().getJenkinsLogin()); this.useNotice = "on".equals(req.getParameter(PARAMETERNAME_USE_NOTICE)); - + this.charset = req.getParameter("irc_publisher.charset"); - + + this.useColors = "on".equals(req.getParameter(PARAMETERNAME_USE_COLORS)); + // try to establish the connection try { IRCConnectionProvider.setDesc(this); @@ -478,6 +483,13 @@ public IMMessageTargetConverter getIMMessageTargetConverter() { public boolean isUseNotice() { return this.useNotice; } + + /** + * Specifies if the bot should send message with colors. + */ + public boolean isUseColors() { + return this.useColors; + } public String getCharset() { return this.charset; diff --git a/src/main/java/hudson/plugins/ircbot/v2/IRCColor.java b/src/main/java/hudson/plugins/ircbot/v2/IRCColor.java new file mode 100644 index 00000000..47c4f19e --- /dev/null +++ b/src/main/java/hudson/plugins/ircbot/v2/IRCColor.java @@ -0,0 +1,46 @@ +package hudson.plugins.ircbot.v2; + +import org.pircbotx.Colors; + +/** + * Simple support for IRC colors. + * + * @author syl20bnr + */ +public class IRCColor { + + private final String message; + + public IRCColor(String message) { + this.message = message; + } + + public String colorize(){ + String color = Colors.DARK_GRAY; + if(this.message.startsWith("Starting")){ + if (this.message.contains("FAIL")){ + color = Colors.BROWN; + } + else if (this.message.contains("FIXED")){ + color = Colors.OLIVE; + } + else{ + color = Colors.DARK_GREEN; + } + } + else if(this.message.contains("FIXED")){ + color = Colors.REVERSE + Colors.BOLD + Colors.GREEN; + } + else if(this.message.contains("SUCCESS")){ + color = Colors.BOLD + Colors.GREEN; + } + else if(this.message.contains("FAILURE")){ + color = Colors.REVERSE + Colors.BOLD + Colors.RED; + } + else if(this.message.contains("STILL FAILING")){ + color = Colors.BOLD + Colors.RED; + } + return color + this.message; + } + +} diff --git a/src/main/java/hudson/plugins/ircbot/v2/IRCConnection.java b/src/main/java/hudson/plugins/ircbot/v2/IRCConnection.java index c101b42a..cebc0789 100644 --- a/src/main/java/hudson/plugins/ircbot/v2/IRCConnection.java +++ b/src/main/java/hudson/plugins/ircbot/v2/IRCConnection.java @@ -267,6 +267,10 @@ public void send(String target, String text) throws IMException { if (this.descriptor.isUseNotice()) { this.pircConnection.sendNotice(channel, line); } else { + if (this.descriptor.isUseColors()){ + IRCColor cline = new IRCColor(line); + line = cline.colorize(); + } this.pircConnection.sendMessage(channel, line); } } diff --git a/src/main/resources/hudson/plugins/ircbot/IrcPublisher/global.jelly b/src/main/resources/hudson/plugins/ircbot/IrcPublisher/global.jelly index 921f3543..60cd23ce 100644 --- a/src/main/resources/hudson/plugins/ircbot/IrcPublisher/global.jelly +++ b/src/main/resources/hudson/plugins/ircbot/IrcPublisher/global.jelly @@ -76,6 +76,9 @@ + + + diff --git a/src/main/webapp/help-globalConfigUseColors.html b/src/main/webapp/help-globalConfigUseColors.html new file mode 100644 index 00000000..f7f024f5 --- /dev/null +++ b/src/main/webapp/help-globalConfigUseColors.html @@ -0,0 +1,25 @@ +
+

+ Checking this box allows the bot to send colorized messages depending on the + current and previous states of a job. + + The bot tries to send green messages for successful jobs + and red messages for failures but this may not be right + if your IRC client has defined a custom theme of colors. +

+

+ Here are the rules for coloring messages: +

    +
  • Starting previously successful job: green
  • +
  • Starting previously fixed job: olive
  • +
  • Starting previously failed job: red
  • +
  • First successful build (fixed job): green background
  • +
  • First failure: red background
  • +
  • Still successful job: bold green
  • +
  • Still failing job: bold red
  • +
  • Default message: bold gray
  • +
+ + There is no support for colorblind theme for now. +

+
\ No newline at end of file