From 6b16777446543bdc8f16299589dde733e660dc2f Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 11 Dec 2015 15:24:07 +0100 Subject: [PATCH] add "Include failed Tests" includes all failed tests when some tests failed. does nothing if no failed tests were found --- .../jenkins/plugins/slack/ActiveNotifier.java | 23 ++++++++++++++++--- .../jenkins/plugins/slack/SlackNotifier.java | 13 ++++++++--- .../plugins/slack/SlackNotifier/config.jelly | 3 +++ .../plugins/slack/SlackNotifierStub.java | 6 ++--- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/main/java/jenkins/plugins/slack/ActiveNotifier.java b/src/main/java/jenkins/plugins/slack/ActiveNotifier.java index 7633eb35..b2e84a7c 100755 --- a/src/main/java/jenkins/plugins/slack/ActiveNotifier.java +++ b/src/main/java/jenkins/plugins/slack/ActiveNotifier.java @@ -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; @@ -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())); } } @@ -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)); } @@ -205,7 +206,7 @@ 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(); @@ -213,6 +214,9 @@ String getBuildStatusMessage(AbstractBuild r, boolean includeTestSummary, boolea if (includeTestSummary) { message.appendTestSummary(); } + if (includeFailedTests) { + message.appendFailedTests(); + } if (includeCustomMessage) { message.appendCustomMessage(); } @@ -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(); diff --git a/src/main/java/jenkins/plugins/slack/SlackNotifier.java b/src/main/java/jenkins/plugins/slack/SlackNotifier.java index c8d7e0f5..31d1f925 100755 --- a/src/main/java/jenkins/plugins/slack/SlackNotifier.java +++ b/src/main/java/jenkins/plugins/slack/SlackNotifier.java @@ -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; @@ -155,6 +156,10 @@ public boolean includeTestSummary() { return includeTestSummary; } + public boolean includeFailedTests() { + return includeFailedTests; + } + public boolean getNotifyRepeatedFailure() { return notifyRepeatedFailure; } @@ -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; @@ -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; @@ -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 diff --git a/src/main/resources/jenkins/plugins/slack/SlackNotifier/config.jelly b/src/main/resources/jenkins/plugins/slack/SlackNotifier/config.jelly index 61974cd3..28e4416e 100755 --- a/src/main/resources/jenkins/plugins/slack/SlackNotifier/config.jelly +++ b/src/main/resources/jenkins/plugins/slack/SlackNotifier/config.jelly @@ -36,6 +36,9 @@ + + + diff --git a/src/test/java/jenkins/plugins/slack/SlackNotifierStub.java b/src/test/java/jenkins/plugins/slack/SlackNotifierStub.java index ac84a56d..83c3bac0 100644 --- a/src/test/java/jenkins/plugins/slack/SlackNotifierStub.java +++ b/src/test/java/jenkins/plugins/slack/SlackNotifierStub.java @@ -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 {