Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append custom message to notification, configurable per Project (includes var evaluation). #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>hipchat</artifactId>
<packaging>hpi</packaging>
<version>0.1.5-SNAPSHOT</version>
<version>0.1.6-SNAPSHOT</version>
<name>Jenkins HipChat Plugin</name>
<description>A Build status publisher that notifies channels on a HipChat server</description>
<url>http://wiki.jenkins-ci.org/display/JENKINS/HipChat+Plugin</url>
Expand Down
99 changes: 73 additions & 26 deletions src/main/java/jenkins/plugins/hipchat/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,13 @@ public void deleted(AbstractBuild r) {
}

public void started(AbstractBuild build) {
String changes = getChanges(build);
CauseAction cause = build.getAction(CauseAction.class);

if (changes != null) {
notifyStart(build, changes);
} else if (cause != null) {
MessageBuilder message = new MessageBuilder(notifier, build);
message.append(cause.getShortDescription());
notifyStart(build, message.appendOpenLink().toString());
} else {
notifyStart(build, getBuildStatusMessage(build));
AbstractProject<?, ?> project = build.getProject();
HipChatNotifier.HipChatJobProperty jobProperty = project.getProperty(HipChatNotifier.HipChatJobProperty.class);
if (MessageBuilder.shouldNotify(jobProperty.getStartNotification(), jobProperty.getConditionalNotify(), build)) {
getHipChat(build).publish(getBuildStatusMessage(build), "green");
}
}

private void notifyStart(AbstractBuild build, String message) {
getHipChat(build).publish(message, "green");
}

public void finalized(AbstractBuild r) {
}

Expand All @@ -62,12 +51,12 @@ public void completed(AbstractBuild r) {
Result result = r.getResult();
AbstractBuild<?, ?> previousBuild = project.getLastBuild().getPreviousBuild();
Result previousResult = (previousBuild != null) ? previousBuild.getResult() : Result.SUCCESS;
if ((result == Result.ABORTED && jobProperty.getNotifyAborted())
|| (result == Result.FAILURE && jobProperty.getNotifyFailure())
|| (result == Result.NOT_BUILT && jobProperty.getNotifyNotBuilt())
|| (result == Result.SUCCESS && previousResult == Result.FAILURE && jobProperty.getNotifyBackToNormal())
|| (result == Result.SUCCESS && jobProperty.getNotifySuccess())
|| (result == Result.UNSTABLE && jobProperty.getNotifyUnstable())) {
if ((result == Result.ABORTED && MessageBuilder.shouldNotify(jobProperty.getNotifyAborted(), jobProperty.getConditionalNotify(), r))
|| (result == Result.FAILURE && MessageBuilder.shouldNotify(jobProperty.getNotifyFailure(), jobProperty.getConditionalNotify(), r))
|| (result == Result.NOT_BUILT && MessageBuilder.shouldNotify(jobProperty.getNotifyNotBuilt(), jobProperty.getConditionalNotify(), r))
|| (result == Result.SUCCESS && previousResult == Result.FAILURE && MessageBuilder.shouldNotify(jobProperty.getNotifyBackToNormal(), jobProperty.getConditionalNotify(), r))
|| (result == Result.SUCCESS && MessageBuilder.shouldNotify(jobProperty.getNotifySuccess(), jobProperty.getConditionalNotify(), r))
|| (result == Result.UNSTABLE && MessageBuilder.shouldNotify(jobProperty.getNotifyUnstable(), jobProperty.getConditionalNotify(), r))) {
getHipChat(r).publish(getBuildStatusMessage(r), getBuildColor(r));
}
}
Expand Down Expand Up @@ -100,7 +89,17 @@ String getChanges(AbstractBuild r) {
message.append(" (");
message.append(files.size());
message.append(" file(s) changed)");
return message.appendOpenLink().toString();
return message.toString();
}

String getCulprits(AbstractBuild r) {
Set<User> culprits = r.getCulprits();
Set<String> culpritNames = new HashSet<String>();
for (User culprit : culprits) {
culpritNames.add(culprit.getFullName());
}

return "Committers since last Success: " + StringUtils.join(culpritNames, ", ");
}

static String getBuildColor(AbstractBuild r) {
Expand All @@ -114,11 +113,31 @@ static String getBuildColor(AbstractBuild r) {
}
}

String getBuildStatusMessage(AbstractBuild r) {
MessageBuilder message = new MessageBuilder(notifier, r);
String getBuildStatusMessage(AbstractBuild build) {
String changes = getChanges(build);
CauseAction cause = build.getAction(CauseAction.class);
AbstractProject<?, ?> project = build.getProject();
String customMessage = Util.fixEmpty(project.getProperty(HipChatNotifier.HipChatJobProperty.class).getCustomMessage());
MessageBuilder message = new MessageBuilder(notifier, build);

message.appendOpenLink().append("<br />");
message.appendStatusMessage();
message.appendDuration();
return message.appendOpenLink().toString();
if (!build.isBuilding()) message.appendDuration();

if (changes != null) {
message.append("<br />");
message.append(changes);
} else if (cause != null) {
message.append("<br />");
message.append(cause.getShortDescription());
}

if (build.getResult() == Result.FAILURE) {
message.append("<br />");
message.append(getCulprits(build));
}

return message.appendCustomMessage(customMessage, build).toString();
}

public static class MessageBuilder {
Expand All @@ -134,6 +153,7 @@ public MessageBuilder(HipChatNotifier notifier, AbstractBuild build) {
}

public MessageBuilder appendStatusMessage() {
message.append("Build status: ");
message.append(getStatusMessage(build));
return this;
}
Expand Down Expand Up @@ -184,8 +204,35 @@ public MessageBuilder appendDuration() {
return this;
}

public MessageBuilder appendCustomMessage(String customMessage, AbstractBuild build) {
if (customMessage != null) {
message.append("<br />");
message.append(getParameterString(customMessage, build));
}

return this;
}

public String toString() {
return message.toString();
}

public static boolean shouldNotify(boolean jobNotifyProperty, String conditionalProperty, AbstractBuild r) {
boolean blnConditionalProperty = true;
conditionalProperty = MessageBuilder.getParameterString(conditionalProperty, r);
if (conditionalProperty.equalsIgnoreCase("true") || conditionalProperty.equalsIgnoreCase("false")) {
blnConditionalProperty = Boolean.valueOf(conditionalProperty);
}
return jobNotifyProperty && blnConditionalProperty;
}

public static String getParameterString(String original, AbstractBuild<?, ?> r) {
ParametersAction parameters = r.getAction(ParametersAction.class);
if (parameters != null) {
original = parameters.substitute(r, original);
}

return original;
}
}
}
23 changes: 20 additions & 3 deletions src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public static class HipChatJobProperty extends hudson.model.JobProperty<Abstract
private boolean notifyUnstable;
private boolean notifyFailure;
private boolean notifyBackToNormal;

private String conditionalNotify;
private String customMessage;

@DataBoundConstructor
public HipChatJobProperty(String room,
Expand All @@ -154,7 +155,9 @@ public HipChatJobProperty(String room,
boolean notifyNotBuilt,
boolean notifySuccess,
boolean notifyUnstable,
boolean notifyBackToNormal) {
boolean notifyBackToNormal,
String conditionalNotify,
String customMessage) {
this.room = room;
this.startNotification = startNotification;
this.notifyAborted = notifyAborted;
Expand All @@ -163,6 +166,8 @@ public HipChatJobProperty(String room,
this.notifySuccess = notifySuccess;
this.notifyUnstable = notifyUnstable;
this.notifyBackToNormal = notifyBackToNormal;
this.conditionalNotify = conditionalNotify;
this.customMessage = customMessage;
}

@Exported
Expand Down Expand Up @@ -219,6 +224,16 @@ public boolean getNotifyBackToNormal() {
return notifyBackToNormal;
}

@Exported
public String getConditionalNotify() {
return conditionalNotify;
}

@Exported
public String getCustomMessage() {
return customMessage;
}

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {
public String getDisplayName() {
Expand All @@ -239,7 +254,9 @@ public HipChatJobProperty newInstance(StaplerRequest sr, JSONObject formData) th
sr.getParameter("hipChatNotifyNotBuilt") != null,
sr.getParameter("hipChatNotifySuccess") != null,
sr.getParameter("hipChatNotifyUnstable") != null,
sr.getParameter("hipChatNotifyBackToNormal") != null);
sr.getParameter("hipChatNotifyBackToNormal") != null,
sr.getParameter("hipChatConditionalNotify"),
sr.getParameter("hipChatCustomMessage"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="Project Room">
<f:textbox name="hipChatProjectRoom" value="${instance.getRoom()}"/>
</f:entry>

<f:section title="HipChat Notifications">
<f:entry title="Project Room">
<f:textbox name="hipChatProjectRoom" value="${instance.getRoom()}"/>
</f:entry>
<f:entry title="Notify Build Start">
<f:checkbox name="hipChatStartNotification" value="true" checked="${instance.getStartNotification()}"/>
</f:entry>

<f:entry title="Notify Build Start">
<f:checkbox name="hipChatStartNotification" value="true" checked="${instance.getStartNotification()}"/>
</f:entry>
<f:entry title="Notify Aborted">
<f:checkbox name="hipChatNotifyAborted" value="true" checked="${instance.getNotifyAborted()}"/>
</f:entry>

<f:entry title="Notify Aborted">
<f:checkbox name="hipChatNotifyAborted" value="true" checked="${instance.getNotifyAborted()}"/>
</f:entry>
<f:entry title="Notify Failure">
<f:checkbox name="hipChatNotifyFailure" value="true" checked="${instance.getNotifyFailure()}"/>
</f:entry>

<f:entry title="Notify Failure">
<f:checkbox name="hipChatNotifyFailure" value="true" checked="${instance.getNotifyFailure()}"/>
</f:entry>
<f:entry title="Notify Not Built">
<f:checkbox name="hipChatNotifyNotBuilt" value="true" checked="${instance.getNotifyNotBuilt()}"/>
</f:entry>

<f:entry title="Notify Not Built">
<f:checkbox name="hipChatNotifyNotBuilt" value="true" checked="${instance.getNotifyNotBuilt()}"/>
</f:entry>
<f:entry title="Notify Success">
<f:checkbox name="hipChatNotifySuccess" value="true" checked="${instance.getNotifySuccess()}"/>
</f:entry>

<f:entry title="Notify Success">
<f:checkbox name="hipChatNotifySuccess" value="true" checked="${instance.getNotifySuccess()}"/>
</f:entry>
<f:entry title="Notify Unstable">
<f:checkbox name="hipChatNotifyUnstable" value="true" checked="${instance.getNotifyUnstable()}"/>
</f:entry>

<f:entry title="Notify Unstable">
<f:checkbox name="hipChatNotifyUnstable" value="true" checked="${instance.getNotifyUnstable()}"/>
</f:entry>
<f:entry title="Notify Back To Normal">
<f:checkbox name="hipChatNotifyBackToNormal" value="true" checked="${instance.getNotifyBackToNormal()}"/>
</f:entry>

<f:entry title="Notify Back To Normal">
<f:checkbox name="hipChatNotifyBackToNormal" value="true" checked="${instance.getNotifyBackToNormal()}"/>
</f:entry>

</f:section>
<f:entry title="Conditional Notify" help="${rootURL}/plugin/hipchat/help-config-hipChatConditionalNotify.html">
<f:textbox name="hipChatConditionalNotify" value="${instance.getConditionalNotify()}"/>
</f:entry>

<f:entry title="Custom Message" help="${rootURL}/plugin/hipchat/help-config-hipChatCustomMessage.html">
<f:textbox name="hipChatCustomMessage" value="${instance.getCustomMessage()}"/>
</f:entry>
</j:jelly>
4 changes: 4 additions & 0 deletions src/main/webapp/help-config-hipChatConditionalNotify.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div xmlns="http://www.w3.org/1999/html">
<p>Specify weather notifications should be sent out based on build parameters. If parameters evaluate to true, then all notifications specified above will be sent. Otherwise, the notifications will not be sent.</p>
<p>One example of this parameter's use is to add a build parameter to your jenkins job named 'Send_Notification'. This parameter can then be used here to allow person running the job to determine if notifications can be sent. User would put ${Send_Notification} in this field.</p>
</div>
3 changes: 3 additions & 0 deletions src/main/webapp/help-config-hipChatCustomMessage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div xmlns="http://www.w3.org/1999/html">
<p>Optionally specify a custom message to append to your HipChat notification. You may use variables in your message using the standing format. e.g. ${var_name}</p>
</div>