Skip to content

Commit

Permalink
add "Include failed Tests"
Browse files Browse the repository at this point in the history
includes all failed tests when some tests failed. does nothing if no
failed tests were found
  • Loading branch information
Benjamin Neff authored and SuperTux88 committed Feb 26, 2017
1 parent 73ba19b commit 6b16777
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
23 changes: 20 additions & 3 deletions src/main/java/jenkins/plugins/slack/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import hudson.scm.ChangeLogSet.AffectedFile;
import hudson.scm.ChangeLogSet.Entry;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResult;
import hudson.triggers.SCMTrigger;
import hudson.util.LogTaskListener;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void started(AbstractBuild build) {
if (changes != null) {
notifyStart(build, changes);
} else {
notifyStart(build, getBuildStatusMessage(build, false, notifier.includeCustomMessage()));
notifyStart(build, getBuildStatusMessage(build, false, false, notifier.includeCustomMessage()));
}
}

Expand Down Expand Up @@ -114,7 +115,7 @@ public void completed(AbstractBuild r) {
|| (result == Result.SUCCESS && notifier.getNotifySuccess())
|| (result == Result.UNSTABLE && notifier.getNotifyUnstable())) {
getSlack(r).publish(getBuildStatusMessage(r, notifier.includeTestSummary(),
notifier.includeCustomMessage()), getBuildColor(r));
notifier.includeFailedTests(), notifier.includeCustomMessage()), getBuildColor(r));
if (notifier.getCommitInfoChoice().showAnything()) {
getSlack(r).publish(getCommitList(r), getBuildColor(r));
}
Expand Down Expand Up @@ -205,14 +206,17 @@ static String getBuildColor(AbstractBuild r) {
}
}

String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary, boolean includeCustomMessage) {
String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary, boolean includeFailedTests, boolean includeCustomMessage) {
MessageBuilder message = new MessageBuilder(notifier, r);
message.appendStatusMessage();
message.appendDuration();
message.appendOpenLink();
if (includeTestSummary) {
message.appendTestSummary();
}
if (includeFailedTests) {
message.appendFailedTests();
}
if (includeCustomMessage) {
message.appendCustomMessage();
}
Expand Down Expand Up @@ -363,6 +367,19 @@ public MessageBuilder appendTestSummary() {
return this;
}

public MessageBuilder appendFailedTests() {
AbstractTestResultAction<?> action = this.build
.getAction(AbstractTestResultAction.class);
if (action != null) {
int failed = action.getFailCount();
message.append("\n").append(failed).append(" failed Tests:\n");
for(TestResult result : action.getFailedTests()) {
message.append("\t").append(result.getName()).append("\n");
}
}
return this;
}

public MessageBuilder appendCustomMessage() {
String customMessage = notifier.getCustomMessage();
EnvVars envVars = new EnvVars();
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/jenkins/plugins/slack/SlackNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class SlackNotifier extends Notifier {
private boolean notifyBackToNormal;
private boolean notifyRepeatedFailure;
private boolean includeTestSummary;
private boolean includeFailedTests;
private CommitInfoChoice commitInfoChoice;
private boolean includeCustomMessage;
private String customMessage;
Expand Down Expand Up @@ -155,6 +156,10 @@ public boolean includeTestSummary() {
return includeTestSummary;
}

public boolean includeFailedTests() {
return includeFailedTests;
}

public boolean getNotifyRepeatedFailure() {
return notifyRepeatedFailure;
}
Expand Down Expand Up @@ -235,8 +240,8 @@ public SlackNotifier() {
public SlackNotifier(final String teamDomain, final String authToken, final boolean botUser, final String room, final String authTokenCredentialId,
final String sendAs, final boolean startNotification, final boolean notifyAborted, final boolean notifyFailure,
final boolean notifyNotBuilt, final boolean notifySuccess, final boolean notifyUnstable, final boolean notifyBackToNormal,
final boolean notifyRepeatedFailure, final boolean includeTestSummary, CommitInfoChoice commitInfoChoice,
boolean includeCustomMessage, String customMessage) {
final boolean notifyRepeatedFailure, final boolean includeTestSummary, final boolean includeFailedTests,
CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage) {
super();
this.teamDomain = teamDomain;
this.authToken = authToken;
Expand All @@ -253,6 +258,7 @@ public SlackNotifier(final String teamDomain, final String authToken, final bool
this.notifyBackToNormal = notifyBackToNormal;
this.notifyRepeatedFailure = notifyRepeatedFailure;
this.includeTestSummary = includeTestSummary;
this.includeFailedTests = includeFailedTests;
this.commitInfoChoice = commitInfoChoice;
this.includeCustomMessage = includeCustomMessage;
this.customMessage = customMessage;
Expand Down Expand Up @@ -396,12 +402,13 @@ public SlackNotifier newInstance(StaplerRequest sr, JSONObject json) {
boolean notifyBackToNormal = "true".equals(sr.getParameter("slackNotifyBackToNormal"));
boolean notifyRepeatedFailure = "true".equals(sr.getParameter("slackNotifyRepeatedFailure"));
boolean includeTestSummary = "true".equals(sr.getParameter("includeTestSummary"));
boolean includeFailedTests = "true".equals(sr.getParameter("includeFailedTests"));
CommitInfoChoice commitInfoChoice = CommitInfoChoice.forDisplayName(sr.getParameter("slackCommitInfoChoice"));
boolean includeCustomMessage = "on".equals(sr.getParameter("includeCustomMessage"));
String customMessage = sr.getParameter("customMessage");
return new SlackNotifier(teamDomain, token, botUser, room, tokenCredentialId, sendAs, startNotification, notifyAborted,
notifyFailure, notifyNotBuilt, notifySuccess, notifyUnstable, notifyBackToNormal, notifyRepeatedFailure,
includeTestSummary, commitInfoChoice, includeCustomMessage, customMessage);
includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<f:entry title="Include Test Summary">
<f:checkbox name="includeTestSummary" value="true" checked="${instance.includeTestSummary()}"/>
</f:entry>
<f:entry title="Include failed Tests">
<f:checkbox name="includeFailedTests" value="true" checked="${instance.includeFailedTests()}"/>
</f:entry>

<f:optionalBlock name="includeCustomMessage" title="Include Custom Message" checked="${instance.includeCustomMessage()}">
<f:entry title="Custom Message" help="/plugin/slack/help-projectConfig-slackCustomMessage.html">
Expand Down
6 changes: 3 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,11 @@ public class SlackNotifierStub extends SlackNotifier {
public SlackNotifierStub(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 notifyBackToNormal,
boolean notifyRepeatedFailure, boolean includeTestSummary, CommitInfoChoice commitInfoChoice,
boolean includeCustomMessage, String customMessage) {
boolean notifyRepeatedFailure, boolean includeTestSummary, boolean includeFailedTests,
CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage) {
super(teamDomain, authToken, botUser, room, authTokenCredentialId, sendAs, startNotification, notifyAborted, notifyFailure,
notifyNotBuilt, notifySuccess, notifyUnstable, notifyBackToNormal, notifyRepeatedFailure,
includeTestSummary, commitInfoChoice, includeCustomMessage, customMessage);
includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage);
}

public static class DescriptorImplStub extends SlackNotifier.DescriptorImpl {
Expand Down

0 comments on commit 6b16777

Please sign in to comment.