package hudson.plugins.emailext.plugins.trigger;

import hudson.Extension;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.EmailTriggerDescriptor;
import hudson.plugins.emailext.plugins.RecipientProvider;
import hudson.plugins.emailext.plugins.recipients.DevelopersRecipientProvider;
import hudson.plugins.emailext.plugins.recipients.ListRecipientProvider;
import java.util.List;

import hudson.tasks.junit.CaseResult;
import org.kohsuke.stapler.DataBoundConstructor;


public class RegressionTrigger extends EmailTrigger {
    
    public static final String TRIGGER_NAME = "Test Regression";
    
    @DataBoundConstructor
    public RegressionTrigger(List<RecipientProvider> recipientProviders, String recipientList, String replyTo, String subject, String body, String attachmentsPattern, int attachBuildLog, String contentType) {
        super(recipientProviders, recipientList, replyTo, subject, body, attachmentsPattern, attachBuildLog, contentType);
    }
    
    @Deprecated
    public RegressionTrigger(boolean sendToList, boolean sendToDevs, boolean sendToRequester, boolean sendToCulprits, String recipientList, String replyTo, String subject, String body, String attachmentsPattern, int attachBuildLog, String contentType) {
        super(sendToList, sendToDevs, sendToRequester, sendToCulprits,recipientList, replyTo, subject, body, attachmentsPattern, attachBuildLog, contentType);
    }
    
    @Override
    public boolean trigger(AbstractBuild<?, ?> build, TaskListener listener) {
        AbstractBuild<?,?> previousBuild = ExtendedEmailPublisher.getPreviousBuild(build, listener);
        if (previousBuild == null)
            return build.getResult() == Result.FAILURE;

        if (build.getTestResultAction() == null)
            return false;

        if (previousBuild.getTestResultAction() == null)
            return build.getTestResultAction().getFailCount() > 0;

        for (Object result : build.getTestResultAction().getFailedTests()){
            CaseResult res = (CaseResult)result;
            if (res.getAge() == 1)
                return true;
        }

        return build.getTestResultAction().getFailCount() > 
                previousBuild.getTestResultAction().getFailCount();
    }

    @Extension
    public static final class DescriptorImpl extends EmailTriggerDescriptor {

        public DescriptorImpl() {
            addTriggerNameToReplace(UnstableTrigger.TRIGGER_NAME);
            addTriggerNameToReplace(StillUnstableTrigger.TRIGGER_NAME);
            
            addDefaultRecipientProvider(new DevelopersRecipientProvider());
            addDefaultRecipientProvider(new ListRecipientProvider());
        }
        
        @Override
        public String getDisplayName() {
            return TRIGGER_NAME;
        }
    }    
}

@@ -1959,16 +1959,102 @@
 false;%0A%0A
+        // if previous run didn't have test results and this one does (with failures)%0A
         
@@ -2170,16 +2170,273 @@
 ) %3E 0;%0A%0A
+        // if more tests failed during this run%0A        if(build.getTestResultAction().getFailCount() %3E%0A                previousBuild.getTestResultAction().getFailCount())%0A            return true;%0A%0A        // if any test failed this time, but not last time%0A
         
@@ -2646,120 +2646,13 @@
 urn 
-build.getTestResultAction().getFailCount() %3E %0A                previousBuild.getTestResultAction().getFailCount()
+false
 ;%0A  

Fix JENKINS-22041