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

Enhance Custom Message Functionality (rebased from #169) #385

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 18 additions & 2 deletions src/main/java/jenkins/plugins/slack/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -265,7 +266,7 @@ String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary, boolea
message.appendFailedTests();
}
if (includeCustomMessage) {
message.appendCustomMessage();
message.appendCustomMessage(r.getResult());
}
return message.toString();
}
Expand Down Expand Up @@ -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
Expand Down
49 changes: 47 additions & 2 deletions src/main/java/jenkins/plugins/slack/SlackNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -62,6 +65,7 @@ public class SlackNotifier extends Notifier {
private CommitInfoChoice commitInfoChoice;
private boolean includeCustomMessage;
private String customMessage;
private Map<String, String> customMessageMap = new HashMap<String, String>();

@Override
public DescriptorImpl getDescriptor() {
Expand Down Expand Up @@ -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
Expand All @@ -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<String, String> customMessageMap) {
super();
this.baseUrl = baseUrl;
if(this.baseUrl != null && !this.baseUrl.isEmpty() && !this.baseUrl.endsWith("/")) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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<String,String> customMessageMap = new HashMap<String,String>();
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@
<f:entry title="Custom Message" help="/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea name="customMessage" value="${instance.getCustomMessage()}"/>
</f:entry>
<f:entry title="Custom Message - Build Unstable" help="${rootURL}/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea name="customMessageBuildUnstable" value="${instance.getCustomMessage('UNSTABLE')}"/>
</f:entry>
<f:entry title="Custom Message - Build Aborted" help="${rootURL}/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea name="customMessageBuildAborted" value="${instance.getCustomMessage('ABORTED')}"/>
</f:entry>
<f:entry title="Custom Message - Build Failure" help="${rootURL}/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea name="customMessageBuildFailure" value="${instance.getCustomMessage('FAILURE')}"/>
</f:entry>
<f:entry title="Custom Message - Build Not Built" help="${rootURL}/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea name="customMessageBuildNotBuilt" value="${instance.getCustomMessage('NOT_BUILT')}"/>
</f:entry>
<f:entry title="Custom Message - Success" help="${rootURL}/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea name="customMessageBuildSuccess" value="${instance.getCustomMessage('SUCCESS')}"/>
</f:entry>
</f:optionalBlock>

<f:entry title="Notification message includes" description="What commit information to include into notification message">
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/jenkins/plugins/slack/SlackNotifierStub.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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 {
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/jenkins/plugins/slack/SlackNotifierTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,6 +14,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;

@RunWith(Parameterized.class)
public class SlackNotifierTest extends TestCase {
Expand All @@ -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();
Expand All @@ -35,6 +38,16 @@ public SlackNotifierTest(SlackServiceStub slackServiceStub, boolean response, Fo
this.slackServiceStub = slackServiceStub;
this.response = response;
this.expectedResult = expectedResult;

HashMap<String, String> customMessageMap = new HashMap<String, String>();
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
Expand All @@ -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;
Expand Down