Notification Templates

This page lists notification template scripts to be used in conjunction with notification schemes. 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.

Embed the content of a report into a notification

Script notes:

  • No notes

AHPSCRIPTS-23

import com.urbancode.anthill3.domain.jobtrace.*;
import com.urbancode.anthill3.domain.workflow.*;
import com.urbancode.anthill3.runtime.paths.*;
import com.urbancode.devilfish.services.var.VarService;
import java.io.*;

private String getFileAsString(File file)
    throws IOException {
        StringBuffer result = new StringBuffer();
        
        if (file.exists() && file.isFile()) {
            InputStream inStream = new FileInputStream(file);
            try {
                byte[] buffer = new byte[4096];
                int length = 0;
                while ((length = inStream.read(buffer)) > 0) {
                    result.append(new String(buffer, 0, length));
                }
            }
            finally {
                try {
                    inStream.close();
                }
                catch (Exception e) {}
            }
        }
        return result.toString();
    }


String REPORT_NAME = "Fitnesse Reports"; // change to the name of your reports

WorkflowCase workflow = (WorkflowCase) context.get("workflow");
JobTrace[] jobTraceArray = workflow.getJobTraceArray();

// find the job trace you want in some meaningful way
JobTrace jobTrace = jobTraceArray[0];

VarService vs = VarService.getInstance();
String reportsPath = PublishPathHelper.getInstance().getPublishPath(jobTrace,REPORT_NAME);
File reportDir = new File(vs.resolve(reportsPath));

if (reportDir.exists() && reportDir.isDirectory()) {
    File[] reportFiles = reportDir.listFiles();
    String[] reports = new String[reportFiles.length];
    for (int f=0; f<reportFiles.length; f++) {
        reports[f] = getFileAsString(reportFiles[f]);
    }
    context.put("reports", reports);
}


Retrieves Step Trace on Job Trace and Sends Output Via E-mail

  • The following needs to go into the Context Script of a notification template. Then, reference the log in the template text as $output for unformatted text, or <ppre>$output</ppre> if you want it formatted.

AHPSCRIPTS-138

import com.urbancode.anthill3.domain.jobtrace.*; 
 import com.urbancode.anthill3.domain.workflow.*; 
 import com.urbancode.anthill3.runtime.paths.*; 
 import com.urbancode.anthill3.services.file.*; 
 import com.urbancode.devilfish.services.file.FileInfo; 
 import com.urbancode.devilfish.services.var.VarService; 
 import com.urbancode.anthill3.domain.workflow.WorkflowCase; 
 import java.io.*; 

 private String getStreamAsString(InputStream inStream) 
  throws IOException { 
      StringBuffer result = new StringBuffer(); 
      try { 
          byte[] buffer = new byte[4096]; 
          int length = 0; 
          while ((length = inStream.read(buffer)) > 0) { 
              result.append(new String(buffer, 0, length)); 
          } 
      } 
      finally { 
          try { 
              inStream.close(); 
          } 
          catch (Exception e) {} 
      } 
      return result.toString(); 
  } 

 WorkflowCase workflow = (WorkflowCase)context.get("workflow"); 

 String STEP_NAME = "echoer"; // change to the name of the step 
 jobTraces = workflow.getJobTraceArray(); 

 for (int j=0; j<jobTraces.length; j++){ 
 StepTrace[] stepTraces = jobTraces[j].getStepTraceArray(); 
 for (int s=0; s<stepTraces.length; s++) { 
      if (STEP_NAME.equals(stepTraces[s].getName())) { 
         // found the step 
         CommandTrace cmdTrace = stepTraces[s].getCommandTraceArray()[0]; 
         FileInfo outputFile = LogPathHelper.getInstance().getLogFileInfoArray(cmdTrace)[0]; 
         InputStream inStream = FileInfoService.getInstance().getFileInfoAsStream(outputFile); 
         String output = getStreamAsString(inStream); 
           
         context.put("output", output); 
           
         break; 
     } 
 } 
 }

Include Error Messages in Notifications

  • Post 3.4, it's become much easier to smartly include sections of the log that are interested in email notifications of failed builds or deployments. The API widgets that create those bright red error messages on the build life or job pages can be tapped into and used in the email. This example shows the changes to the context script you need to make, as well as a simple error reporting snippet that would work well in an html based email. It tries to be pretty minimal, but if you want more logging, you can remove some of the #if #end pairs.
  • Due to formatting limitations for the Wiki, the following needs changed in the script:
    • All "ppre" tags need changed to "pre"

AHPSCRIPTS-88

Context

import com.urbancode.anthill3.runtime.scripting.helpers.*; 


 workflow = context.get("workflow"); 
 WorkflowErrorSummary wes = ErrorHelper.getWorkflowErrorSummary(workflow); 
 int arrayLength = wes.getJobTraceErrorSummaryArray().length; 
 context.put("workflowErrorSummary", wes); 
 context.put("jtesal", arrayLength);

Template

## BEGIN SECTION Subject 

 Unit Testing 

 ## END SECTION Subject 

 ## BEGIN SECTION Body 
 ## PROPERTY Content-Type: text/html 
 ## PROPERTY X-Priority: 3 

 #if ($workflowErrorSummary.logErrors || 
 $jtesal > 0) 
  <h2> Workflow Error Log </h2> 
  #if ($jtesal == 0) 
    <ppre>$workflowErrorSummary.logErrors</ppre> 
  #end 
  #foreach($errorTrace in $workflowErrorSummary.jobTraceErrorSummaryArray) 
    <h3>$errorTrace.jobName</h3> 
    <ppre>$errorTrace.logErrors</ppre> 
    <ul> 
    #foreach($step in $errorTrace.stepTraceErrorSummaryArray) 
      <li> <b>$step.stepName</b><br> 
      #foreach($commandError in $step.commandTraceErrorSummaryArray) 
        #if ($commandError.outputErrors) 
          <ppre>$commandError.outputErrors</ppre> 
        #else 
          <ppre>$commandError.logError</ppre> 
        #end 
        <br><br> 
      #end 
      </li> 
    #end 
    </ul> 
  #end 
 #end 

 ## END SECTION Body

Property in Notification Template (E-Mail)

  • This is a most basic example, but can be expanded upon for more advanced uses of putting a property (from the BuildRequest) into an e-mail. Note that .getBuildRequest() can be subbed with things like getProject() if you want the project, etc.

AHPSCRIPTS-140

CONTEXT SCRIPT:

import com.urbancode.anthill3.domain.workflow.*; 
 WorkflowCase workflow = (WorkflowCase) context.get("workflow"); 
 context.put("testValue", workflow.getBuildRequest().getProperty("test.property"));

TEMPLATE TEXT:

  • Then, in your template text, simply type the following to return the value of the property from the BuildRequest:
$testValue

Include Issue Data in E-Mails

  • This snippet shows how to include issues from the build life tab in your build or deploy emails. And add this snippet into an existing email notification template (this is not stand-alone). Please feel free to stylize the table generated below to suite. This version is the very basic, very ugly version.

AHPSCRIPTS-108

CONTEXT

buildLife = workflow.getBuildLife(); 
 issues = IssueHelper.getIssueArray(buildLife); 
 issues = issues.length > 0 ? issues : null; 
 System.out.println("issues: " + issues); 
 context.put("issues", issues);

TEMPLATE

#if ($issues) 
 <h2>Issues</h2> 
 <table> 
  <tr> 
    <th>Issue ID</th> 
    <th>Name</th> 
    <th>Description</th> 
  </tr> 
 #foreach($issue in $issues) 
   <tr> 
     <td><a href="$issue.issueUrl">$issue.issueId</a></td> 
     <td>$issue.name</td> 
     <td>$issue.description</td> 
   </tr> 
 #end 
  </table> 
 #end

Emailing Dependencies

  • This is a snippet for the body section of an email. It could be built out to provide links to those builds, but as is, it's pretty simple and provides the name of the dependency as well as the most recent stamp (build number) applied to it.

AHPSCRIPTS-39

#set($buildLife = $workflow.BuildLife)

 Dependencies:
 #foreach($dep in $buildLife.dependencyBuildLifeArray)
   $dep.codestationProject.name ($dep.latestStampValue)
 #end