Skip to content

Commit

Permalink
Merge pull request #4 from syl20bnr/master
Browse files Browse the repository at this point in the history
Added new configuration option to colorize sent messages depending on the current and previous states of job.
Thanks !
  • Loading branch information
olamy committed Dec 7, 2012
2 parents 928da8e + 00f35d2 commit df3fc81
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/main/java/hudson/plugins/ircbot/IrcPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Publisher>

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;
Expand Down Expand Up @@ -198,7 +199,9 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Publisher>
private boolean useNotice;

private String charset;


private boolean useColors;

DescriptorImpl() {
super(IrcPublisher.class);
load();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/hudson/plugins/ircbot/v2/IRCColor.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
4 changes: 4 additions & 0 deletions src/main/java/hudson/plugins/ircbot/v2/IRCConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<f:entry title="Use /notice command" description="Use /notice command instead of /msg (default in ircbot &lt;= 2.0)">
<f:checkbox name="${descriptor.PARAMETERNAME_USE_NOTICE}" checked="${descriptor.useNotice}"/>
</f:entry>
<f:entry title="Use colors" description="If checked, the bot will send colorized messages depending on the current and previous states of a job." help="/plugin/ircbot/help-globalConfigUseColors.html">
<f:checkbox name="${descriptor.PARAMETERNAME_USE_COLORS}" checked="${descriptor.useColors}"/>
</f:entry>

</f:advanced>
</f:optionalBlock>
Expand Down
25 changes: 25 additions & 0 deletions src/main/webapp/help-globalConfigUseColors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div>
<p>
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 <font color="green">green messages</font> for successful jobs
and <font color="red">red messages</font> for failures <em> but this may not be right
if your IRC client has defined a custom theme of colors.</em>
</p>
<p>
Here are the rules for coloring messages:
<ul>
<li>Starting previously successful job: <font color="green">green</font></li>
<li>Starting previously fixed job: <font color="olive">olive</font></li>
<li>Starting previously failed job: <font color="red">red</font></li>
<li>First successful build (fixed job): <font color="black" style="background-color: green"><strong>green background</strong></font></li>
<li>First failure: <font color="black" style="background-color: red"><strong>red background</strong></font></li>
<li>Still successful job: <font color="green"><strong>bold green</strong></font></li>
<li>Still failing job: <font color="red"><strong>bold red</strong></font></li>
<li>Default message: <font color="darkgray"><strong>bold gray</strong></font></li>
</ul>

There is no support for colorblind theme for now.
</p>
</div>

0 comments on commit df3fc81

Please sign in to comment.