Skip to content

Commit

Permalink
Regression notification implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
prashant authored and samrocketman committed Mar 11, 2017
1 parent cf4adc0 commit ff85024
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 6 deletions.
44 changes: 44 additions & 0 deletions src/main/java/jenkins/plugins/slack/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.AffectedFile;
import hudson.scm.ChangeLogSet.Entry;
import hudson.tasks.junit.TestResultAction;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResult;
import hudson.triggers.SCMTrigger;
Expand Down Expand Up @@ -96,6 +97,20 @@ private void notifyStart(AbstractBuild build, String message) {
}

public void finalized(AbstractBuild r) {
AbstractProject<?, ?> project = r.getProject();
Result result = r.getResult();
AbstractBuild<?, ?> previousBuild = project.getLastBuild();
do {
previousBuild = previousBuild.getPreviousCompletedBuild();
} while (previousBuild != null && previousBuild.getResult() == Result.ABORTED);
Result previousResult = (previousBuild != null) ? previousBuild.getResult() : Result.SUCCESS;
if((result.isWorseThan(previousResult) || moreTestFailuresThanPreviousBuild(r, previousBuild)) && notifier.getNotifyRegression()) {
getSlack(r).publish(getBuildStatusMessage(r, notifier.includeTestSummary(),
notifier.includeFailedTests(), notifier.includeCustomMessage()), getBuildColor(r));
if (notifier.getCommitInfoChoice().showAnything()) {
getSlack(r).publish(getCommitList(r), getBuildColor(r));
}
}
}

public void completed(AbstractBuild r) {
Expand Down Expand Up @@ -127,6 +142,31 @@ public void completed(AbstractBuild r) {
}
}

private boolean moreTestFailuresThanPreviousBuild(AbstractBuild currentBuild, AbstractBuild<?, ?> previousBuild) {
if (getTestResult(currentBuild) != null && getTestResult(previousBuild) != null) {
if (getTestResult(currentBuild).getFailCount() > getTestResult(previousBuild).getFailCount())
return true;

// test if different tests failed.
return !getFailedTestIds(currentBuild).equals(getFailedTestIds(previousBuild));
}
return false;
}

private TestResultAction getTestResult(AbstractBuild build) {
return build.getAction(TestResultAction.class);
}

private Set<String> getFailedTestIds(AbstractBuild currentBuild) {
Set<String> failedTestIds = new HashSet<String>();
List<? extends TestResult> failedTests = getTestResult(currentBuild).getFailedTests();
for(TestResult result : failedTests) {
failedTestIds.add(result.getId());
}

return failedTestIds;
}

String getChanges(AbstractBuild r, boolean includeCustomMessage) {
if (!r.hasChangeSetComputed()) {
logger.info("No change set computed...");
Expand Down Expand Up @@ -242,6 +282,7 @@ public static class MessageBuilder {
ABORTED_STATUS_MESSAGE = "Aborted",
NOT_BUILT_STATUS_MESSAGE = "Not built",
UNSTABLE_STATUS_MESSAGE = "Unstable",
REGRESSION_STATUS_MESSAGE = "Regression",
UNKNOWN_STATUS_MESSAGE = "Unknown";

private StringBuffer message;
Expand Down Expand Up @@ -318,6 +359,9 @@ private String getStatusMessage(AbstractBuild r) {
if (result == Result.UNSTABLE) {
return UNSTABLE_STATUS_MESSAGE;
}
if (lastNonAbortedBuild != null && result.isWorseThan(previousResult)) {
return REGRESSION_STATUS_MESSAGE;
}
return UNKNOWN_STATUS_MESSAGE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jenkins/plugins/slack/SlackListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void onDeleted(AbstractBuild r) {

@Override
public void onFinalized(AbstractBuild r) {
// getNotifier(r.getProject()).finalized(r);
// super.onFinalized(r);
getNotifier(r.getProject(), null).finalized(r);
super.onFinalized(r);
}

@SuppressWarnings("unchecked")
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/jenkins/plugins/slack/SlackNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class SlackNotifier extends Notifier {
private boolean notifyAborted;
private boolean notifyNotBuilt;
private boolean notifyUnstable;
private boolean notifyRegression;
private boolean notifyFailure;
private boolean notifyBackToNormal;
private boolean notifyRepeatedFailure;
Expand Down Expand Up @@ -153,6 +154,10 @@ public boolean getNotifyUnstable() {
return notifyUnstable;
}

public boolean getNotifyRegression() {
return notifyRegression;
}

public boolean getNotifyBackToNormal() {
return notifyBackToNormal;
}
Expand Down Expand Up @@ -212,6 +217,11 @@ public void setNotifyUnstable(boolean notifyUnstable) {
this.notifyUnstable = notifyUnstable;
}

@DataBoundSetter
public void setNotifyRegression(boolean notifyRegression) {
this.notifyRegression = notifyRegression;
}

@DataBoundSetter
public void setNotifyBackToNormal(boolean notifyBackToNormal) {
this.notifyBackToNormal = notifyBackToNormal;
Expand Down Expand Up @@ -244,7 +254,7 @@ public SlackNotifier() {

public SlackNotifier(final String baseUrl, 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 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) {
super();
Expand All @@ -264,6 +274,7 @@ public SlackNotifier(final String baseUrl, final String teamDomain, final String
this.notifyNotBuilt = notifyNotBuilt;
this.notifySuccess = notifySuccess;
this.notifyUnstable = notifyUnstable;
this.notifyRegression = notifyRegression;
this.notifyBackToNormal = notifyBackToNormal;
this.notifyRepeatedFailure = notifyRepeatedFailure;
this.includeTestSummary = includeTestSummary;
Expand Down Expand Up @@ -423,6 +434,7 @@ public SlackNotifier newInstance(StaplerRequest sr, JSONObject json) {
boolean notifyAborted = "true".equals(sr.getParameter("slackNotifyAborted"));
boolean notifyNotBuilt = "true".equals(sr.getParameter("slackNotifyNotBuilt"));
boolean notifyUnstable = "true".equals(sr.getParameter("slackNotifyUnstable"));
boolean notifyRegression = "true".equals(sr.getParameter("slackNotifyRegression"));
boolean notifyFailure = "true".equals(sr.getParameter("slackNotifyFailure"));
boolean notifyBackToNormal = "true".equals(sr.getParameter("slackNotifyBackToNormal"));
boolean notifyRepeatedFailure = "true".equals(sr.getParameter("slackNotifyRepeatedFailure"));
Expand All @@ -432,7 +444,7 @@ public SlackNotifier newInstance(StaplerRequest sr, JSONObject json) {
boolean includeCustomMessage = "on".equals(sr.getParameter("includeCustomMessage"));
String customMessage = sr.getParameter("customMessage");
return new SlackNotifier(baseUrl, teamDomain, token, botUser, room, tokenCredentialId, sendAs, startNotification, notifyAborted,
notifyFailure, notifyNotBuilt, notifySuccess, notifyUnstable, notifyBackToNormal, notifyRepeatedFailure,
notifyFailure, notifyNotBuilt, notifySuccess, notifyUnstable, notifyRegression, notifyBackToNormal, notifyRepeatedFailure,
includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage);
}

Expand Down Expand Up @@ -515,6 +527,7 @@ public static class SlackJobProperty extends hudson.model.JobProperty<AbstractPr
private boolean notifyAborted;
private boolean notifyNotBuilt;
private boolean notifyUnstable;
private boolean notifyRegression;
private boolean notifyFailure;
private boolean notifyBackToNormal;
private boolean notifyRepeatedFailure;
Expand All @@ -534,6 +547,7 @@ public SlackJobProperty(String teamDomain,
boolean notifyNotBuilt,
boolean notifySuccess,
boolean notifyUnstable,
boolean notifyRegression,
boolean notifyBackToNormal,
boolean notifyRepeatedFailure,
boolean includeTestSummary,
Expand All @@ -550,6 +564,7 @@ public SlackJobProperty(String teamDomain,
this.notifyNotBuilt = notifyNotBuilt;
this.notifySuccess = notifySuccess;
this.notifyUnstable = notifyUnstable;
this.notifyRegression = notifyRegression;
this.notifyBackToNormal = notifyBackToNormal;
this.notifyRepeatedFailure = notifyRepeatedFailure;
this.includeTestSummary = includeTestSummary;
Expand Down Expand Up @@ -618,6 +633,11 @@ public boolean getNotifyUnstable() {
return notifyUnstable;
}

@Exported
public boolean getNotifyRegression() {
return notifyRegression;
}

@Exported
public boolean getNotifyBackToNormal() {
return notifyBackToNormal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private void updateSlackNotifier(final SlackNotifier slackNotifier,
slackNotifier.setNotifyNotBuilt(slackJobProperty.getNotifyNotBuilt());
slackNotifier.setNotifySuccess(slackJobProperty.getNotifySuccess());
slackNotifier.setNotifyUnstable(slackJobProperty.getNotifyUnstable());
slackNotifier.setNotifyRegression(slackJobProperty.getNotifyRegression());
slackNotifier.setNotifyBackToNormal(slackJobProperty.getNotifyBackToNormal());
slackNotifier.setNotifyRepeatedFailure(slackJobProperty.getNotifyRepeatedFailure());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<f:checkbox name="slackNotifyUnstable" value="true" checked="${instance.getNotifyUnstable()}"/>
</f:entry>

<f:entry title="Notify Regression">
<f:checkbox name="slackNotifyRegression" value="true" checked="${instance.getNotifyRegression()}"/>
</f:entry>


<f:entry title="Notify Back To Normal">
<f:checkbox name="slackNotifyBackToNormal" value="true" checked="${instance.getNotifyBackToNormal()}"/>
</f:entry>
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/jenkins/plugins/slack/SlackNotifierStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ 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 notifyBackToNormal,
boolean notifyNotBuilt, boolean notifySuccess, boolean notifyUnstable, boolean notifyRegression, boolean notifyBackToNormal,
boolean notifyRepeatedFailure, boolean includeTestSummary, boolean includeFailedTests,
CommitInfoChoice commitInfoChoice, boolean includeCustomMessage, String customMessage) {
super(baseUrl, teamDomain, authToken, botUser, room, authTokenCredentialId, sendAs, startNotification, notifyAborted, notifyFailure,
notifyNotBuilt, notifySuccess, notifyUnstable, notifyBackToNormal, notifyRepeatedFailure,
notifyNotBuilt, notifySuccess, notifyUnstable, notifyRegression, notifyBackToNormal, notifyRepeatedFailure,
includeTestSummary, includeFailedTests, commitInfoChoice, includeCustomMessage, customMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void testBasicMigration() {
assertFalse(notifier.getNotifyAborted());
assertFalse(notifier.getNotifyNotBuilt());
assertFalse(notifier.getNotifyUnstable());
assertFalse(notifier.getNotifyRegression());
assertTrue(notifier.getNotifyFailure());
assertFalse(notifier.getNotifyBackToNormal());
assertFalse(notifier.getNotifyRepeatedFailure());
Expand Down Expand Up @@ -77,6 +78,7 @@ public void testGlobalSettingsOverriden() {
assertFalse(notifier.getNotifyAborted());
assertFalse(notifier.getNotifyNotBuilt());
assertFalse(notifier.getNotifyUnstable());
assertFalse(notifier.getNotifyRegression());
assertTrue(notifier.getNotifyFailure());
assertFalse(notifier.getNotifyBackToNormal());
assertFalse(notifier.getNotifyRepeatedFailure());
Expand Down Expand Up @@ -104,6 +106,7 @@ public void testGlobalSettingsNotOverridden() throws IOException {
assertFalse(notifier.getNotifyAborted());
assertFalse(notifier.getNotifyNotBuilt());
assertFalse(notifier.getNotifyUnstable());
assertFalse(notifier.getNotifyRegression());
assertTrue(notifier.getNotifyFailure());
assertFalse(notifier.getNotifyBackToNormal());
assertFalse(notifier.getNotifyRepeatedFailure());
Expand Down Expand Up @@ -159,6 +162,7 @@ public void testMigrationOfSomeJobs() throws IOException {
assertTrue(notifier.getNotifyAborted());
assertTrue(notifier.getNotifyNotBuilt());
assertTrue(notifier.getNotifyUnstable());
assertTrue(notifier.getNotifyRegression());
assertTrue(notifier.getNotifyFailure());
assertTrue(notifier.getNotifyBackToNormal());
assertTrue(notifier.getNotifyRepeatedFailure());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<notifyAborted>false</notifyAborted>
<notifyNotBuilt>false</notifyNotBuilt>
<notifyUnstable>false</notifyUnstable>
<notifyRegression>false</notifyRegression>
<notifyFailure>true</notifyFailure>
<notifyBackToNormal>false</notifyBackToNormal>
<notifyRepeatedFailure>false</notifyRepeatedFailure>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<notifyAborted>false</notifyAborted>
<notifyNotBuilt>false</notifyNotBuilt>
<notifyUnstable>false</notifyUnstable>
<notifyRegration>false</notifyRegration>
<notifyFailure>true</notifyFailure>
<notifyBackToNormal>false</notifyBackToNormal>
<notifyRepeatedFailure>false</notifyRepeatedFailure>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<notifyAborted>false</notifyAborted>
<notifyNotBuilt>false</notifyNotBuilt>
<notifyUnstable>false</notifyUnstable>
<notifyRegression>false</notifyRegression>
<notifyFailure>true</notifyFailure>
<notifyBackToNormal>false</notifyBackToNormal>
<notifyRepeatedFailure>false</notifyRepeatedFailure>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<notifyAborted>true</notifyAborted>
<notifyNotBuilt>true</notifyNotBuilt>
<notifyUnstable>true</notifyUnstable>
<notifyRegression>true</notifyRegression>
<notifyFailure>true</notifyFailure>
<notifyBackToNormal>true</notifyBackToNormal>
<notifyRepeatedFailure>true</notifyRepeatedFailure>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<notifyAborted>false</notifyAborted>
<notifyNotBuilt>false</notifyNotBuilt>
<notifyUnstable>false</notifyUnstable>
<notifyRegression>false</notifyRegression>
<notifyFailure>false</notifyFailure>
<notifyBackToNormal>false</notifyBackToNormal>
<notifyRepeatedFailure>false</notifyRepeatedFailure>
Expand Down

0 comments on commit ff85024

Please sign in to comment.