Skip to content
Permalink
Browse files
Fix JENKINS-7859
- Added FirstFailureTrigger.java to handle the case when a build moves from Success to Failure (or on first build run + Failure)
  • Loading branch information
slide committed Jul 18, 2012
1 parent 86f5f9e commit d072f821f5d545558cc5ddf17b68e1e23ad9d02d
@@ -54,6 +54,7 @@
import hudson.plugins.emailext.plugins.content.WorkspaceFileContent;
import hudson.plugins.emailext.plugins.trigger.AbortedTrigger;
import hudson.plugins.emailext.plugins.trigger.FailureTrigger;
import hudson.plugins.emailext.plugins.trigger.FirstFailureTrigger;
import hudson.plugins.emailext.plugins.trigger.FixedTrigger;
import hudson.plugins.emailext.plugins.trigger.ImprovementTrigger;
import hudson.plugins.emailext.plugins.trigger.NotBuiltTrigger;
@@ -119,6 +120,7 @@ public void start() throws Exception {
addEmailTriggerPlugin(NotBuiltTrigger.DESCRIPTOR);
addEmailTriggerPlugin(ImprovementTrigger.DESCRIPTOR);
addEmailTriggerPlugin(RegressionTrigger.DESCRIPTOR);
addEmailTriggerPlugin(FirstFailureTrigger.DESCRIPTOR);
}

private void addEmailContentPlugin(EmailContent content) {
@@ -0,0 +1,69 @@
package hudson.plugins.emailext.plugins.trigger;

import hudson.model.AbstractBuild;
import hudson.model.Result;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.EmailTriggerDescriptor;

public class FirstFailureTrigger extends EmailTrigger {

public static final String TRIGGER_NAME = "First Failure";

@Override
public boolean trigger(AbstractBuild<?,?> build) {

Result buildResult = build.getResult();

if (buildResult == Result.FAILURE) {
AbstractBuild<?,?> prevBuild = build.getPreviousBuild();
// if there is no previous build, this is a first failure
// if there is a previous build and it's result was success, this is first failure
if (prevBuild == null || (prevBuild.getResult() == Result.SUCCESS)) {
return true;
}
}

return false;
}

@Override
public EmailTriggerDescriptor getDescriptor() {
return DESCRIPTOR;
}

public static DescriptorImpl DESCRIPTOR = new DescriptorImpl();

public static final class DescriptorImpl extends EmailTriggerDescriptor {

public DescriptorImpl() {
addTriggerNameToReplace(SuccessTrigger.TRIGGER_NAME);
}

@Override
public String getTriggerName() {
return TRIGGER_NAME;
}

@Override
public EmailTrigger newInstance() {
return new FirstFailureTrigger();
}

@Override
public String getHelpText() {
return "An email will be sent when the build status changes from \"Success\" " +
"to \"Failure\"";
}

}

@Override
public boolean getDefaultSendToDevs() {
return true;
}

@Override
public boolean getDefaultSendToList() {
return true;
}
}
@@ -12,6 +12,7 @@
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.trigger.AbortedTrigger;
import hudson.plugins.emailext.plugins.trigger.FailureTrigger;
import hudson.plugins.emailext.plugins.trigger.FirstFailureTrigger;
import hudson.plugins.emailext.plugins.trigger.FixedTrigger;
import hudson.plugins.emailext.plugins.trigger.NotBuiltTrigger;
import hudson.plugins.emailext.plugins.trigger.PreBuildTrigger;
@@ -126,6 +127,29 @@ public void testSuccessTriggerShouldNotSendEmailWhenBuildFails()
assertEquals( 0, Mailbox.get( "ashlux@gmail.com" ).size() );
}

public void testFirstFailureTriggerShouldNotSendEmailOnSecondFail()
throws Exception
{
project.getBuildersList().add( new FailureBuilder() );

FirstFailureTrigger trigger = new FirstFailureTrigger();
addEmailType( trigger );
publisher.getConfiguredTriggers().add( trigger );

FreeStyleBuild build = project.scheduleBuild2( 0 ).get();
assertBuildStatus( Result.FAILURE, build );

FreeStyleBuild build2 = project.scheduleBuild2( 1 ).get();
assertBuildStatus( Result.FAILURE, build2 );

assertThat( "Email should have been triggered for build 0, so we should see it in the logs.", build.getLog( 100 ),
hasItems( "Email was triggered for: " + FirstFailureTrigger.TRIGGER_NAME ) );

assertThat( "Email should NOT have been triggered for build 1, so we shouldn't see it in the logs.", build2.getLog( 100 ),
not( hasItems( "Email was triggered for: " + FailureTrigger.TRIGGER_NAME ) ) );
assertEquals( 1, Mailbox.get( "ashlux@gmail.com" ).size() );
}

public void testFixedTriggerShouldNotSendEmailWhenBuildFirstFails()
throws Exception
{

0 comments on commit d072f82

Please sign in to comment.