Skip to content

Commit

Permalink
Custom Message based on Build Result
Browse files Browse the repository at this point in the history
Original idea from jenkinsci#169
(Enhance Custom Message Functionality by @csogilvie)

Discussion in jenkinsci#385

I re-wrote it almost completely on top of the current master
  • Loading branch information
KreAch3R committed Dec 16, 2018
1 parent bdb3b08 commit 9df2862
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 12 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@
<compatibleSinceVersion>2.0</compatibleSinceVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>0</forkCount>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
26 changes: 19 additions & 7 deletions src/main/java/jenkins/plugins/slack/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void started(AbstractBuild build) {
message.append(causeAction.getCauses().get(0).getShortDescription());
message.appendOpenLink();
if (notifier.getIncludeCustomMessage()) {
message.appendCustomMessage();
message.appendCustomMessage(build.getResult());
}
notifyStart(build, message.toString());
// Cause was found, exit early to prevent double-message
Expand Down Expand Up @@ -207,7 +207,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 @@ -275,7 +275,7 @@ String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary, boolea
message.appendFailedTests();
}
if (includeCustomMessage) {
message.appendCustomMessage();
message.appendCustomMessage(r.getResult());
}
return message.toString();
}
Expand All @@ -293,7 +293,7 @@ public static class MessageBuilder {
UNSTABLE_STATUS_MESSAGE = "Unstable",
REGRESSION_STATUS_MESSAGE = "Regression",
UNKNOWN_STATUS_MESSAGE = "Unknown";

private StringBuffer message;
private SlackNotifier notifier;
private AbstractBuild build;
Expand Down Expand Up @@ -443,9 +443,21 @@ public MessageBuilder appendFailedTests() {
return this;
}

public MessageBuilder appendCustomMessage() {
public MessageBuilder appendCustomMessage(Result buildResult) {
String customMessage = notifier.getCustomMessage();

if (buildResult != null) {
if (buildResult == Result.SUCCESS) {
customMessage = notifier.getCustomMessageSuccess();
} else if (buildResult == Result.ABORTED) {
customMessage = notifier.getCustomMessageAborted();
} else if (buildResult == Result.NOT_BUILT) {
customMessage = notifier.getCustomMessageNotBuilt();
} else if (buildResult == Result.UNSTABLE) {
customMessage = notifier.getCustomMessageUnstable();
} else if (buildResult == Result.FAILURE) {
customMessage = notifier.getCustomMessageFailure();
}
}
try {
String replaced = TokenMacro.expandAll(build, new LogTaskListener(logger, INFO), customMessage);
message.append("\n");
Expand All @@ -462,7 +474,7 @@ public MessageBuilder appendCustomMessage() {
}
return this;
}

private String createBackToNormalDurationString(){
// This status code guarantees that the previous build fails and has been successful before
// The back to normal time is the time since the build first broke
Expand Down
103 changes: 101 additions & 2 deletions src/main/java/jenkins/plugins/slack/SlackNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class SlackNotifier extends Notifier {
private CommitInfoChoice commitInfoChoice;
private boolean includeCustomMessage;
private String customMessage;
private String customMessageSuccess;
private String customMessageAborted;
private String customMessageNotBuilt;
private String customMessageUnstable;
private String customMessageFailure;

/** @deprecated use {@link #tokenCredentialId} */
private transient String authTokenCredentialId;
Expand Down Expand Up @@ -196,6 +201,26 @@ public String getCustomMessage() {
return customMessage;
}

public String getCustomMessageSuccess() {
return customMessageSuccess;
}

public String getCustomMessageAborted() {
return customMessageAborted;
}

public String getCustomMessageNotBuilt() {
return customMessageNotBuilt;
}

public String getCustomMessageUnstable() {
return customMessageUnstable;
}

public String getCustomMessageFailure() {
return customMessageFailure;
}

@DataBoundSetter
public void setStartNotification(boolean startNotification) {
this.startNotification = startNotification;
Expand Down Expand Up @@ -266,12 +291,38 @@ public void setCustomMessage(String customMessage) {
this.customMessage = customMessage;
}

@DataBoundSetter
public void setCustomMessageSuccess(String customMessageSuccess) {
this.customMessageSuccess = customMessageSuccess;
}

@DataBoundSetter
public void setCustomMessageAborted(String customMessageAborted) {
this.customMessageAborted = customMessageAborted;
}

@DataBoundSetter
public void setCustomMessageNotBuilt(String customMessageNotBuilt) {
this.customMessageNotBuilt = customMessageNotBuilt;
}

@DataBoundSetter
public void setCustomMessageUnstable(String customMessageUnstable) {
this.customMessageUnstable = customMessageUnstable;
}

@DataBoundSetter
public void setCustomMessageFailure(String customMessageFailure) {
this.customMessageFailure = customMessageFailure;
}

@DataBoundConstructor
public SlackNotifier(final String baseUrl, final String teamDomain, final String authToken, final boolean botUser, final String room, final String tokenCredentialId,
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, String customMessageSuccess,
String customMessageAborted, String customMessageNotBuilt, String customMessageUnstable, String customMessageFailure) {
super();
this.baseUrl = baseUrl;
if(this.baseUrl != null && !this.baseUrl.isEmpty() && !this.baseUrl.endsWith("/")) {
Expand All @@ -298,6 +349,11 @@ public SlackNotifier(final String baseUrl, final String teamDomain, final String
this.includeCustomMessage = includeCustomMessage;
if (includeCustomMessage) {
this.customMessage = customMessage;
this.customMessageSuccess = customMessageSuccess;
this.customMessageAborted = customMessageAborted;
this.customMessageNotBuilt = customMessageNotBuilt;
this.customMessageUnstable = customMessageUnstable;
this.customMessageFailure = customMessageFailure;
} else {
this.customMessage = null;
}
Expand Down Expand Up @@ -576,6 +632,12 @@ public static class SlackJobProperty extends hudson.model.JobProperty<AbstractPr
private boolean showCommitList;
private boolean includeCustomMessage;
private String customMessage;
private String customMessageSuccess;
private String customMessageAborted;
private String customMessageNotBuilt;
private String customMessageUnstable;
private String customMessageRegression;
private String customMessageFailure;

@DataBoundConstructor
public SlackJobProperty(String teamDomain,
Expand All @@ -594,7 +656,13 @@ public SlackJobProperty(String teamDomain,
boolean includeTestSummary,
boolean showCommitList,
boolean includeCustomMessage,
String customMessage) {
String customMessage,
String customMessageSuccess,
String customMessageAborted,
String customMessageNotBuilt,
String customMessageUnstable,
String customMessageRegression,
String customMessageFailure) {
this.teamDomain = teamDomain;
this.token = token;
this.botUser = botUser;
Expand All @@ -612,6 +680,12 @@ public SlackJobProperty(String teamDomain,
this.showCommitList = showCommitList;
this.includeCustomMessage = includeCustomMessage;
this.customMessage = customMessage;
this.customMessageSuccess = customMessageSuccess;
this.customMessageFailure = customMessageAborted;
this.customMessageSuccess = customMessageNotBuilt;
this.customMessageFailure = customMessageUnstable;
this.customMessageSuccess = customMessageRegression;
this.customMessageFailure = customMessageFailure;
}

@Exported
Expand Down Expand Up @@ -704,6 +778,31 @@ public String getCustomMessage() {
return customMessage;
}

@Exported
public String getCustomMessageSuccess() {
return customMessageSuccess;
}

@Exported
public String getCustomMessageAborted() {
return customMessageAborted;
}

@Exported
public String getCustomMessageNotBuilt() {
return customMessageNotBuilt;
}

@Exported
public String getCustomMessageUnstable() {
return customMessageUnstable;
}

@Exported
public String getCustomMessageFailure() {
return customMessageFailure;
}

}

@Extension(ordinal = 100) public static final class Migrator extends ItemListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@
<f:entry title="Custom Message" field="customMessage" help="/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea />
</f:entry>
<f:entry title="Custom Message - Build Unstable" help="/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea field="customMessageUnstable" />
</f:entry>
<f:entry title="Custom Message - Build Aborted" help="/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea field="customMessageAborted" />
</f:entry>
<f:entry title="Custom Message - Build Failure" help="/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea field="customMessageFailure" />
</f:entry>
<f:entry title="Custom Message - Build Not Built" help="/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea field="customMessageNotBuilt" />
</f:entry>
<f:entry title="Custom Message - Success" help="/plugin/slack/help-projectConfig-slackCustomMessage.html">
<f:textarea field="customMessageSuccess" />
</f:entry>
</f:optionalBlock>


Expand Down
8 changes: 5 additions & 3 deletions src/test/java/jenkins/plugins/slack/SlackNotifierStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ 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) {
boolean notifyRepeatedFailure, boolean includeTestSummary, boolean includeFailedTests,
CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage, String customMessageSuccess,
String customMessageAborted, String customMessageNotBuilt, String customMessageUnstable, String customMessageFailure) {
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,customMessageSuccess,
customMessageAborted, customMessageNotBuilt, customMessageUnstable, customMessageFailure);
}

public static class DescriptorImplStub extends SlackNotifier.DescriptorImpl {
Expand Down

0 comments on commit 9df2862

Please sign in to comment.