Skip to content

Commit

Permalink
Add a custom message
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil West authored and Phil West committed Apr 7, 2015
1 parent d89e5dc commit fe18d04
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 28 deletions.
48 changes: 38 additions & 10 deletions src/main/java/jenkins/plugins/slack/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
import hudson.scm.ChangeLogSet.AffectedFile;
import hudson.scm.ChangeLogSet.Entry;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.util.LogTaskListener;
import hudson.util.VariableResolver;
import org.apache.commons.lang.StringUtils;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;

import static hudson.Util.replaceMacro;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;

@SuppressWarnings("rawtypes")
public class ActiveNotifier implements FineGrainedNotifier {

Expand All @@ -44,6 +48,8 @@ public void deleted(AbstractBuild r) {
public void started(AbstractBuild build) {
String changes = getChanges(build);
CauseAction cause = build.getAction(CauseAction.class);
AbstractProject<?, ?> project = build.getProject();
SlackNotifier.SlackJobProperty jobProperty = project.getProperty(SlackNotifier.SlackJobProperty.class);

if (changes != null) {
notifyStart(build, changes);
Expand All @@ -52,7 +58,7 @@ public void started(AbstractBuild build) {
message.append(cause.getShortDescription());
notifyStart(build, message.appendOpenLink().toString());
} else {
notifyStart(build, getBuildStatusMessage(build, false));
notifyStart(build, getBuildStatusMessage(build, false, jobProperty.includeCustomMessage()));
}
}

Expand Down Expand Up @@ -92,7 +98,8 @@ public void completed(AbstractBuild r) {
&& jobProperty.getNotifyBackToNormal())
|| (result == Result.SUCCESS && jobProperty.getNotifySuccess())
|| (result == Result.UNSTABLE && jobProperty.getNotifyUnstable())) {
getSlack(r).publish(getBuildStatusMessage(r, jobProperty.includeTestSummary()),
getSlack(r).publish(getBuildStatusMessage(r, jobProperty.includeTestSummary(),
jobProperty.includeCustomMessage()),
getBuildColor(r));
if (jobProperty.getShowCommitList()) {
getSlack(r).publish(getCommitList(r), getBuildColor(r));
Expand Down Expand Up @@ -171,15 +178,18 @@ static String getBuildColor(AbstractBuild r) {
}
}

String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary) {
String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary, boolean includeCustomMessage) {
MessageBuilder message = new MessageBuilder(notifier, r);
message.appendStatusMessage();
message.appendDuration();
message.appendOpenLink();
if (!includeTestSummary) {
return message.toString();
if (includeTestSummary) {
message.appendTestSummary();
}
if (includeCustomMessage) {
message.appendCustomMessage();
}
return message.appendTestSummary().toString();
return message.toString();
}

public static class MessageBuilder {
Expand Down Expand Up @@ -275,6 +285,24 @@ public MessageBuilder appendTestSummary() {
return this;
}

public MessageBuilder appendCustomMessage() {
AbstractProject<?, ?> project = build.getProject();
String customMessage = Util.fixEmpty(project.getProperty(SlackNotifier.SlackJobProperty.class)
.getCustomMessage());
Map<String, String> buildVariables = new HashMap();
buildVariables.putAll(build.getBuildVariables());
try {
buildVariables.putAll(build.getEnvironment(new LogTaskListener(logger, INFO)));
} catch (IOException e) {
logger.log(SEVERE, e.getMessage(), e);
} catch (InterruptedException e) {
logger.log(SEVERE, e.getMessage(), e);
}
message.append("\n");
message.append(replaceMacro(customMessage, new VariableResolver.ByMap<String>(buildVariables)));
return this;
}

public String escape(String string) {
string = string.replace("&", "&amp;");
string = string.replace("<", "&lt;");
Expand Down
54 changes: 36 additions & 18 deletions src/main/java/jenkins/plugins/slack/SlackNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public String getDisplayName() {
}

public FormValidation doTestConnection(@QueryParameter("slackTeamDomain") final String teamDomain,
@QueryParameter("slackToken") final String authToken,
@QueryParameter("slackRoom") final String room,
@QueryParameter("slackBuildServerUrl") final String buildServerUrl) throws FormException {
@QueryParameter("slackToken") final String authToken,
@QueryParameter("slackRoom") final String room,
@QueryParameter("slackBuildServerUrl") final String buildServerUrl) throws FormException {
try {
SlackService testSlackService = new StandardSlackService(teamDomain, authToken, room);
String message = "Slack/Jenkins plugin: you're all set on " + buildServerUrl;
Expand All @@ -209,21 +209,25 @@ public static class SlackJobProperty extends hudson.model.JobProperty<AbstractPr
private boolean notifyRepeatedFailure;
private boolean includeTestSummary;
private boolean showCommitList;
private boolean includeCustomMessage;
private String customMessage;

@DataBoundConstructor
public SlackJobProperty(String teamDomain,
String token,
String room,
boolean startNotification,
boolean notifyAborted,
boolean notifyFailure,
boolean notifyNotBuilt,
boolean notifySuccess,
boolean notifyUnstable,
boolean notifyBackToNormal,
boolean notifyRepeatedFailure,
boolean includeTestSummary,
boolean showCommitList) {
String token,
String room,
boolean startNotification,
boolean notifyAborted,
boolean notifyFailure,
boolean notifyNotBuilt,
boolean notifySuccess,
boolean notifyUnstable,
boolean notifyBackToNormal,
boolean notifyRepeatedFailure,
boolean includeTestSummary,
boolean showCommitList,
boolean includeCustomMessage,
String customMessage) {
this.teamDomain = teamDomain;
this.token = token;
this.room = room;
Expand All @@ -237,6 +241,8 @@ public SlackJobProperty(String teamDomain,
this.notifyRepeatedFailure = notifyRepeatedFailure;
this.includeTestSummary = includeTestSummary;
this.showCommitList = showCommitList;
this.includeCustomMessage = includeCustomMessage;
this.customMessage = customMessage;
}

@Exported
Expand Down Expand Up @@ -319,6 +325,16 @@ public boolean getNotifyRepeatedFailure() {
return notifyRepeatedFailure;
}

@Exported
public boolean includeCustomMessage() {
return includeCustomMessage;
}

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

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {

Expand Down Expand Up @@ -346,12 +362,14 @@ public SlackJobProperty newInstance(StaplerRequest sr, JSONObject formData) thro
sr.getParameter("slackNotifyBackToNormal") != null,
sr.getParameter("slackNotifyRepeatedFailure") != null,
sr.getParameter("includeTestSummary") != null,
sr.getParameter("slackShowCommitList") != null);
sr.getParameter("slackShowCommitList") != null,
sr.getParameter("includeCustomMessage") != null,
sr.getParameter("customMessage"));
}

public FormValidation doTestConnection(@QueryParameter("slackTeamDomain") final String teamDomain,
@QueryParameter("slackToken") final String authToken,
@QueryParameter("slackProjectRoom") final String room) throws FormException {
@QueryParameter("slackToken") final String authToken,
@QueryParameter("slackProjectRoom") final String room) throws FormException {
try {
SlackService testSlackService = new StandardSlackService(teamDomain, authToken, room);
String message = "Slack/Jenkins plugin: you're all set.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<f:checkbox name="includeTestSummary" value="true" checked="${instance.includeTestSummary()}"/>
</f:entry>

<f:optionalBlock name="includeCustomMessage" title="Include Custom Message" checked="${instance.includeCustomMessage()}">
<f:entry title="Custom Message" help="${rootURL}/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textbox name="customMessage" value="${instance.getCustomMessage()}"/>
</f:entry>
</f:optionalBlock>

<f:entry title="Show Commit List with Titles and Authors">
<f:checkbox name="slackShowCommitList" value="true" checked="${instance.getShowCommitList()}"/>
</f:entry>
Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/help-projectConfig-slackCustomMessage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<p>
Enter a custom message that will be included with the notifications. Include build variables in the form <em>$VAR_NAME</em>.
</p>
</div>

0 comments on commit fe18d04

Please sign in to comment.