diff --git a/src/main/java/jenkins/plugins/slack/ActiveNotifier.java b/src/main/java/jenkins/plugins/slack/ActiveNotifier.java index 0018aef8..fb90a2fa 100755 --- a/src/main/java/jenkins/plugins/slack/ActiveNotifier.java +++ b/src/main/java/jenkins/plugins/slack/ActiveNotifier.java @@ -28,6 +28,7 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.logging.Logger; import java.util.regex.Matcher; @@ -199,7 +200,7 @@ String getChanges(AbstractBuild r, boolean includeCustomMessage) { message.append(" file(s) changed)"); message.appendOpenLink(); if (includeCustomMessage) { - message.appendCustomMessage(); + message.appendCustomMessage(r.getResult()); } return message.toString(); } @@ -265,7 +266,7 @@ String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary, boolea message.appendFailedTests(); } if (includeCustomMessage) { - message.appendCustomMessage(); + message.appendCustomMessage(r.getResult()); } return message.toString(); } @@ -446,6 +447,21 @@ public MessageBuilder appendCustomMessage() { message.append(envVars.expand(customMessage)); return this; } + + public MessageBuilder appendCustomMessage(Result buildResult) { + String customMessage = notifier.getCustomMessage(buildResult); + EnvVars envVars = new EnvVars(); + try { + envVars = 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(envVars.expand(customMessage)); + return this; + } private String createBackToNormalDurationString(){ // This status code guarantees that the previous build fails and has been successful before diff --git a/src/main/java/jenkins/plugins/slack/SlackNotifier.java b/src/main/java/jenkins/plugins/slack/SlackNotifier.java index dd35cc5b..0ce5187c 100755 --- a/src/main/java/jenkins/plugins/slack/SlackNotifier.java +++ b/src/main/java/jenkins/plugins/slack/SlackNotifier.java @@ -11,6 +11,7 @@ import hudson.model.Item; import hudson.model.Descriptor; import hudson.model.listeners.ItemListener; +import hudson.model.Result; import hudson.security.ACL; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepMonitor; @@ -32,6 +33,8 @@ import org.kohsuke.stapler.export.Exported; import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.logging.Logger; @@ -62,6 +65,7 @@ public class SlackNotifier extends Notifier { private CommitInfoChoice commitInfoChoice; private boolean includeCustomMessage; private String customMessage; + private Map customMessageMap = new HashMap(); @Override public DescriptorImpl getDescriptor() { @@ -245,6 +249,38 @@ public void setIncludeCustomMessage(boolean includeCustomMessage) { @DataBoundSetter public void setCustomMessage(String customMessage) { this.customMessage = customMessage; + } + + /** + * Returns a custom message for a specific build type + * @param buildType + * @return String + */ + public String getCustomMessage(Result buildType) { + return this.getCustomMessage(buildType.toString()); + } + + /** + * Returns a custom message for a specific build type + * @param buildType + * @return String + */ + public String getCustomMessage(String buildType) { + String message = null; + + for (Map.Entry pair : customMessageMap.entrySet()) { + if ( pair.getKey().toString().equals(buildType) ) + { + message = pair.getValue().toString(); + } + } + + // If there's no specific message, use the generic custom message + if ( message == null || message.equals("") ) + { + message = getCustomMessage(); + } + return message; } @DataBoundConstructor @@ -256,7 +292,7 @@ public SlackNotifier(final String baseUrl, final String teamDomain, final String final String sendAs, final boolean startNotification, final boolean notifyAborted, final boolean notifyFailure, final boolean notifyNotBuilt, final boolean notifySuccess, final boolean notifyUnstable, final boolean notifyRegression, final boolean notifyBackToNormal, final boolean notifyRepeatedFailure, final boolean includeTestSummary, final boolean includeFailedTests, - CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage) { + CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage, HashMap customMessageMap) { super(); this.baseUrl = baseUrl; if(this.baseUrl != null && !this.baseUrl.isEmpty() && !this.baseUrl.endsWith("/")) { @@ -282,6 +318,7 @@ public SlackNotifier(final String baseUrl, final String teamDomain, final String this.commitInfoChoice = commitInfoChoice; this.includeCustomMessage = includeCustomMessage; this.customMessage = customMessage; + this.customMessageMap = customMessageMap; } public BuildStepMonitor getRequiredMonitorService() { @@ -443,9 +480,17 @@ public SlackNotifier newInstance(StaplerRequest sr, JSONObject json) { CommitInfoChoice commitInfoChoice = CommitInfoChoice.forDisplayName(sr.getParameter("slackCommitInfoChoice")); boolean includeCustomMessage = "on".equals(sr.getParameter("includeCustomMessage")); String customMessage = sr.getParameter("customMessage"); + + HashMap customMessageMap = new HashMap(); + customMessageMap.put("SUCCESS", sr.getParameter("customMessageBuildSuccess")); + customMessageMap.put("FAILURE", sr.getParameter("customMessageBuildFailure")); + customMessageMap.put("ABORTED", sr.getParameter("customMessageBuildAborted")); + customMessageMap.put("NOT_BUILT", sr.getParameter("customMessageBuildNotBuilt")); + customMessageMap.put("UNSTABLE", sr.getParameter("customMessageBuildUnstable")); + return new SlackNotifier(baseUrl, teamDomain, token, botUser, room, tokenCredentialId, sendAs, startNotification, notifyAborted, notifyFailure, notifyNotBuilt, notifySuccess, notifyUnstable, notifyRegression, notifyBackToNormal, notifyRepeatedFailure, - includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage); + includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage, customMessageMap); } @Override diff --git a/src/main/resources/jenkins/plugins/slack/SlackNotifier/config.jelly b/src/main/resources/jenkins/plugins/slack/SlackNotifier/config.jelly index 91dd4959..1b0b4cdb 100755 --- a/src/main/resources/jenkins/plugins/slack/SlackNotifier/config.jelly +++ b/src/main/resources/jenkins/plugins/slack/SlackNotifier/config.jelly @@ -49,6 +49,21 @@ + + + + + + + + + + + + + + + diff --git a/src/test/java/jenkins/plugins/slack/SlackNotifierStub.java b/src/test/java/jenkins/plugins/slack/SlackNotifierStub.java index 84150762..a1441689 100644 --- a/src/test/java/jenkins/plugins/slack/SlackNotifierStub.java +++ b/src/test/java/jenkins/plugins/slack/SlackNotifierStub.java @@ -1,15 +1,18 @@ package jenkins.plugins.slack; +import hudson.model.Result; +import java.util.HashMap; + public class SlackNotifierStub extends SlackNotifier { public SlackNotifierStub(String baseUrl, String teamDomain, String authToken, boolean botUser, String room, String authTokenCredentialId, String sendAs, boolean startNotification, boolean notifyAborted, boolean notifyFailure, boolean notifyNotBuilt, boolean notifySuccess, boolean notifyUnstable, boolean notifyRegression, boolean notifyBackToNormal, boolean notifyRepeatedFailure, boolean includeTestSummary, boolean includeFailedTests, - CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage) { + CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage, HashMap customMessageMap) { super(baseUrl, teamDomain, authToken, botUser, room, authTokenCredentialId, sendAs, startNotification, notifyAborted, notifyFailure, notifyNotBuilt, notifySuccess, notifyUnstable, notifyRegression, notifyBackToNormal, notifyRepeatedFailure, - includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage); + includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage, customMessageMap); } public static class DescriptorImplStub extends SlackNotifier.DescriptorImpl { diff --git a/src/test/java/jenkins/plugins/slack/SlackNotifierTest.java b/src/test/java/jenkins/plugins/slack/SlackNotifierTest.java index f929b118..900c2323 100644 --- a/src/test/java/jenkins/plugins/slack/SlackNotifierTest.java +++ b/src/test/java/jenkins/plugins/slack/SlackNotifierTest.java @@ -1,6 +1,7 @@ package jenkins.plugins.slack; import hudson.model.Descriptor; +import hudson.model.Result; import hudson.util.FormValidation; import junit.framework.TestCase; import net.sf.json.JSONArray; @@ -13,6 +14,7 @@ import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; @RunWith(Parameterized.class) public class SlackNotifierTest extends TestCase { @@ -21,6 +23,7 @@ public class SlackNotifierTest extends TestCase { private SlackServiceStub slackServiceStub; private boolean response; private FormValidation.Kind expectedResult; + private SlackNotifierStub slackNotifier; @Rule public final JenkinsRule rule = new JenkinsRule(); @@ -35,6 +38,16 @@ public SlackNotifierTest(SlackServiceStub slackServiceStub, boolean response, Fo this.slackServiceStub = slackServiceStub; this.response = response; this.expectedResult = expectedResult; + + HashMap customMessageMap = new HashMap(); + customMessageMap.put("SUCCESS", "This is the Success Result Message"); + customMessageMap.put("UNSTABLE", "This is the Unstable Result Message"); + customMessageMap.put("NOT_BUILT", "This is the Not Built Result Message"); + customMessageMap.put("ABORTED", "This is the Aborted Result Message"); + customMessageMap.put("FAILURE", "This is the Failure Result Message"); + + this.slackNotifier = new SlackNotifierStub("baseUrl", "teamDomain", "authToken", true, "room", "buildServerUrl", "sendAs", true, true, + true, true, true, true, false, true, true, false, false, CommitInfoChoice.NONE, true, "Custom Message String", customMessageMap); } @Parameterized.Parameters @@ -61,6 +74,57 @@ public void testDoTestConnection() { } } + /** + * Test's the default custom message functionality + */ + @Test + public void testSlackNotifierCustomMessage() { + String message = slackNotifier.getCustomMessage(); + assertEquals("Custom Message String", message); + } + + /** + * Test's enhanced Custom Message functionality + */ + @Test + public void testSlackNotifierAdvancedCustomMessageResults() { + String message = slackNotifier.getCustomMessage( Result.SUCCESS ); + assertEquals("This is the Success Result Message", message); + + message = slackNotifier.getCustomMessage( Result.ABORTED ); + assertEquals("This is the Aborted Result Message", message); + + message = slackNotifier.getCustomMessage( Result.FAILURE ); + assertEquals("This is the Failure Result Message", message); + + message = slackNotifier.getCustomMessage( Result.NOT_BUILT ); + assertEquals("This is the Not Built Result Message", message); + + message = slackNotifier.getCustomMessage( Result.UNSTABLE ); + assertEquals("This is the Unstable Result Message", message); + } + + /** + * Test's enhanced Custom Message functionality + */ + @Test + public void testSlackNotifierAdvancedCustomMessageString() { + String message = slackNotifier.getCustomMessage( "SUCCESS" ); + assertEquals("This is the Success Result Message", message); + + message = slackNotifier.getCustomMessage( "ABORTED" ); + assertEquals("This is the Aborted Result Message", message); + + message = slackNotifier.getCustomMessage( "FAILURE" ); + assertEquals("This is the Failure Result Message", message); + + message = slackNotifier.getCustomMessage( "NOT_BUILT" ); + assertEquals("This is the Not Built Result Message", message); + + message = slackNotifier.getCustomMessage( "UNSTABLE" ); + assertEquals("This is the Unstable Result Message", message); + } + public static class SlackServiceStub implements SlackService { private boolean response;