package hudson.plugins.emailext.plugins.trigger;

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

public class ImprovementTrigger extends EmailTrigger {
    
    public static final String TRIGGER_NAME = "Improvement";
    
     @Override
    public boolean trigger(AbstractBuild<?, ?> build) {

        if (build.getPreviousBuild() == null)
            return false;
        if (build.getTestResultAction() == null) return false;
        if (build.getPreviousBuild().getTestResultAction() == null)
            return false;
        
        // The first part of the condition avoids accidental triggering for
        // builds that aggregate downstream test results before those test
        // results are available...
        return build.getTestResultAction().getTotalCount() > 0 &&
            getNumFailures(build) < 
                getNumFailures(build.getPreviousBuild());
    }

    @Override
    public EmailTriggerDescriptor getDescriptor() {
        return DESCRIPTOR;
    }
    
    public static DescriptorImpl DESCRIPTOR = new DescriptorImpl();
    
    public static final class DescriptorImpl extends EmailTriggerDescriptor {

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

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

        @Override
        public String getHelpText() {
            return Messages.ImprovementTrigger_HelpText();
        }
        
    }

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

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

@@ -627,24 +627,86 @@
 e;%0A        %0A
+        int numCurrFailures = getNumFailures(build);%0A        %0A
         // T
@@ -954,38 +954,39 @@
             
-getNum
+numCurr
 Failures
 (build) %3C %0A 
@@ -977,34 +977,10 @@
 ures
-(build) %3C %0A              
  
+%3C
  get
@@ -1016,17 +1016,64 @@
 Build())
-;
+ &&%0A            numCurrFailures %3E 0;            
 %0A    %7D%0A%0A

Fix JENKINS-14500