Status Scripts

This page lists assign status scripts (used for status selectors) listed in the scripting site. These scripts were taken straight from the public JIRA site. Please note that some scripts may be snippets and probably WILL need modification to work properly for your situation. Treat these as templates that you can modify from.

Assign Status or Failure in One Step

AHPSCRIPTS-45

import com.urbancode.anthill3.services.jobs.JobStatusEnum; 
 project = ProjectLookup.getCurrent(); 
 success = project.getStatusGroup().SUCCESS(); 
 failure = project.getStatusGroup().FAILURE(); 
 result = success; 
 workflow = WorkflowLookup.getCurrentCase(); 
 jobArray = workflow.getJobTraceArray(); 
 for (int j=0; j<jobArray.length; j++) { 
     stepArray = jobArray[j].getStepTraceArray(); 
     for (int s = 0; s < stepArray.length; s++) { 
         if (!stepArray[s].isIgnoreMyFailures() && 
             JobStatusEnum.FAILED.equals(stepArray[s].getStatus())) { 
             result = failure; 
             break; 
         } 
     } 
     if (failure.equals(result)) { 
         break; 
     } 
 } 
 return result;

Script to Set a Build Life Status if Any Tests Fail

Script Notes:

  • This script goes in an Assign Status step.

AHPSCRIPTS-37

import com.urbancode.anthill3.domain.test.*;

 reports = TestReportFactory.getInstance().restoreAllForJobTrace(JobTraceLookup.getCurrent());
 for (int i=0; i<reports.length; i++) {
     if (reports[i].getNumberOfFailures() > 0) {
         return "Failed Tests";
     }
 }
 return null;

Assign a Status Based on the Results of JUnit Tests

Script Notes:

  • This script goes in an Assign Status step.

AHPSCRIPTS-36

import com.urbancode.anthill3.domain.singleton.serversettings.*;
import com.urbancode.anthill3.domain.buildlife.*;
import com.urbancode.anthill3.domain.jobtrace.*;
import com.urbancode.anthill3.domain.jobtrace.buildlife.*;
import com.urbancode.anthill3.runtime.scripting.helpers.*;

// BUILD_STEP_NAME: Name of the step within the job that should be inspected
String BUILD_STEP_NAME = "build";

BuildLifeJobTrace jobTrace = JobTraceLookup.getCurrent();
StepTrace[] stepTraceArray = jobTrace.getStepTraceArray();
StepTrace stepTrace = null;
for (int j = 0; j < stepTraceArray.length && stepTrace == null; j++) {
    if (stepTraceArray[j].getName().equalsIgnoreCase(BUILD_STEP_NAME)) {
        stepTrace = stepTraceArray[j];
        break;
    }
}


String outputLog = LogHelper.getOutput(stepTrace.getCommandTraceArray()[0]);
String failures = "Failures:";
String errors = "Errors:";
boolean hasFailures = false;
StringTokenizer st = new StringTokenizer(outputLog);
String token = null;
BuildLife bl = BuildLifeLookup.getCurrent();

while(st.hasMoreTokens()){
    token = st.nextToken();
    if(token.equals(failures)){
        String temp = st.nextToken();
        if(temp != null){
            char a = temp.charAt(0);
            String s = a.toString();
            int i = Integer.parseInt(s);
            if(i > 0){
                hasFailures = true;
            }//end if
        }//end if
    }//end if
    else{
        if(token.equals(errors)){
            String temp = st.nextToken();
            if(temp != null){ 
                char a = temp.charAt(0);
                String s = a.toString();
                int i = Integer.parseInt(s); 
                    if(i > 0){ 
                        return StatusLookup.getStatusByName("ORANGE");
                    }else{
                    if(hasFailures){ 
                        return StatusLookup.getStatusByName("YELLOW");
                    }else{
                        return StatusLookup.getStatusByName("Success");
                        }//end else
                    }//end else 
            }//end if
        }//end if
    }//end else 
}//end while

Set a Build Life Status on Any Failed Test (Assign Status Step)

Script Notes:

  • This script goes in an Assign Status step.
  • A combination of the practices in AHPSCRIPTS-36 & AHPSCRIPTS-37 are being used. In this script (which is placed in an Assign Status step), the tests on the build life will be analyzed, and any that are found to have failures will cause a failure status to be assigned; otherwise a success will appear.
  • It should be noted that post-processing scripts will still complete successfully if the status was assigned. Therefore, it may be necessary to follow-up with an additional evaluate script step to check the output of the previous step:
    • import com.urbancode.anthill3.runtime.scripting.helpers.*;
      import com.urbancode.anthill3.domain.status.*;
      import com.urbancode.anthill3.domain.buildlife.*;
      
      Status fail = StatusLookup.getStatusByName("Failed Tests");
      BuildLife current = BuildLifeLookup.getCurrent();
      
      if(current.hasStatus(fail)){
          commandOutput.println("Failed Status detected!!!");
          throw new Exception();
      } else return null;
      
      

AHPSCRIPTS-37 & AHPSCRIPTS-36 Combo

import com.urbancode.anthill3.domain.buildlife.BuildLife;
import com.urbancode.anthill3.domain.jobtrace.JobTrace;
import com.urbancode.anthill3.domain.test.TestReport;
import com.urbancode.anthill3.domain.test.TestReportFactory;

BuildLife currentBuildLife = BuildLifeLookup.getCurrent();
TestReport[] reports = TestReportFactory.getInstance().restoreAllForBuildLife(currentBuildLife);


for (int i=0; i<reports.length; i++) {
    //System.out.println("reports.length:  " + reports.length);
    //System.out.println("numFailures:  " + reports[i].getNumberOfFailures());
    if (reports[i].getNumberOfFailures() > 0) {
        return StatusLookup.getStatusByName("failure");
    }
}

return StatusLookup.getStatusByName("success");

SCREENSHOT

Screenshot of the build life status being assigned to "failure" since a failed test exists.