Stamping

This page lists stamping scripts 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.

One-Liner Stamps

These stamps can be put directly into the stamp configuration screen on a workflow configuration page. They do not require any stamp context, and are simple calls made to the API to get basic things about a build.

Current Build Life

${bsh:BuildLifeLookup.getCurrent().getId();}

Current Build Request

${bsh:BuildRequestLookup.getCurrent().getId();}

Current Time (Unix)

${bsh:System.currentTimeMillis();}

Stamping w/Time Stamp

Script Notes:

  • This script goes in System --> Stamping

AHPSCRIPTS-6

import com.urbancode.vcsdriver3.*;
import com.urbancode.anthill3.runtime.scripting.helpers.*;
import java.text.SimpleDateFormat;

SimpleDateFormat timestampformat = new SimpleDateFormat("yyyyMMddHHmmss");

buildLife = BuildLifeLookup.getCurrent();
checkoutDate = buildLife.getActualWorkspaceDate();

stamp = timestampformat.format(checkoutDate);

stampContext.put("timestamp", ""+stamp);

Perforce: Read File From Depot Without Syncing and Parse Using Regex

This script will read a file from the Perforce depot without syncing, and will parse it on a line-by-line basis, using regex matching to get values.

AHPSCRIPTS-31

import java.io.*;
import java.util.*;
import java.util.regex.*;

org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("script");
/*
Two possible paths:
Release Branch:
//depot/R99/mtt/version.inc
FeatureSet (Development) Branch:
//depot/projects/FS/FS00999/mtt/version.inc
And the name of the AntHill project will be the branch name
*/
String sProjectName = (String)ProjectLookup.getCurrent().getName();
String depotPath = "//depot/";
if (sProjectName.matches("\\s*R\\d+.*")) {
        depotPath += sProjectName;
}
else if (sProjectName.matches("\\s*FS\\d+.*")) {
        depotPath += "projects/FS/" + sProjectName;
}
depotPath += "/mtt/version.inc";

//Use exec of "p4 print" to read file directly from depot server to stdout
String[] cmdarray = {
        "p4.exe",
        "-Q", "winansi",
        "-C", "utf16le-bom",
        "-p", "server:9999",
        "-u", "account",
        "-P", "password",
        "print",
        depotPath};

log.warn("Calling command: " + Arrays.toString(cmdarray));

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmdarray);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

//Parse lines from file looking for WebRelease and Hotfix values
Pattern webrelPattern
        = Pattern.compile("^\\s*CURRENT_WEBREL\\s*=\\s*([0-9]+)");
Pattern hotfixPattern
        = Pattern.compile("^\\s*BUILD_HOTFIX_VERSION\\s*=\\s*([0-9]+)");

String line; String sWebRel; String sHotFix;
while ((line = br.readLine()) != null) {
        Matcher webrelMatch = webrelPattern.matcher(line);
        Matcher hotfixMatch = hotfixPattern.matcher(line);
        if (webrelMatch.matches()) {
                sWebRel = (String)webrelMatch.group(1);
                log.warn("WebRel = " + sWebRel);
        }
        if (hotfixMatch.matches()) {
                sHotFix = (String)hotfixMatch.group(1);
                log.warn("HotFix = " + sHotFix);
        }
}

String value = "NF";
if ((null != sWebRel) && (null != sHotFix)) {
        value = sWebRel + "_" + sHotFix;
}

stampContext.put("Revision", "" + value);


Get Latest Published Change ID

  • ChangeSetHelper is only available in 3.6+

AHPSCRIPTS-82

${bsh:changes=ChangeSetHelper.getChangeSetArray(); if (changes != null && changes.length > 0) { return changes[0].getChangeId(); \} else { return "no revision"; \}}

Add User Who Requested the Build to the Stamp

  • This script below will output "DEV_anthillLogin" where anthillLogin is the the login name of who requested the build.

AHPSCRIPTS-22

import com.urbancode.anthill3.runtime.scripting.helpers.*;
 import com.urbancode.anthill3.domain.buildrequest.*;
 String name = BuildRequestLookup.getCurrent().getUser().getName();

 String value = "DEV_"+name;
 return value;

Incrementing Stamp Script Allowing For Manual Input

  • This script depends on a workflow property called "inputversion" where a user can specify a stamp at the time a build is being started. If inputversion is left blank, it uses the project property "my-stamp-property" as the stamp, and increments it. If "inputversion" is not null, and a value is passed, that value is used as the stamp, and becomes the new value of the "my-stamp-property" .

AHPSCRIPTS-32

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

   final int PREFIX = 1; 
   final int DIGIT = 2; 
   final int POSTFIX = 3; 
    
   UnitOfWork uow = UnitOfWork.getCurrent(); 
   String input = PropertyLookup.getValue("inputversion"); 
   String inc = PropertyLookup.getValue("my-stamp-property"); 

   static final private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ReportScript"); 
    
 public String incrementVersion(String versionStr) 
    throws Exception { 
        // incrementing only the version number 
        char c; 
        StringBuffer prefix = new StringBuffer(); 
        StringBuffer digits = new StringBuffer(); 
        StringBuffer postfix = new StringBuffer(); 
 log.warn("im in the method"); 
        int state = POSTFIX; 
 log.warn("im in the method2 "+versionStr.length()); 
        for (int i = versionStr.length() - 1; i >= 0; i--) { 
        log.warn("im in the method3"); 
            c = versionStr.charAt(i); 
            if (!Character.isDigit(c)) { 
            log.warn("im in the method4"); 
                if (state == POSTFIX) { 
                log.warn("im in the method5"); 
                    postfix.insert(0, c); 
                } 
                else { 
                    if (state == DIGIT) { 
                    log.warn("im in the method6"); 
                        state = PREFIX; 
                    } 
                    prefix.insert(0, c); 
                    log.warn("im in the method7"); 
                } 
            } 
            else { 
                if (state == POSTFIX || state == DIGIT) { 
                log.warn("im in the method8"); 
                    state = DIGIT; 
                    digits.insert(0, c); 
                } 
                else { 
                log.warn("im in the method9"); 
                    prefix.insert(0, c); 
                } 
            } 
        } 
 log.warn("im in the method10"); 
        int version = 0; 
        try { 
            version = Integer.parseInt(digits.toString()); 
            log.warn("im in the method11"); 
        } 
        catch (NumberFormatException nfe) { 
            throw new Exception("Version String illegal. " + 
                                          "Can't parse version number. " + 
                                          "versionStr: " + versionStr); 
        } 
        version++; 
 log.warn("im in the method12"); 
        return prefix.toString() + String.valueOf(version) + postfix.toString(); 
        log.warn("im in the method13"); 
 }//end method 



 log.warn("inputversion :" + input); 
 log.warn("####"+ProjectLookup.getCurrent().getProperty("my-stamp-property")); 
 log.warn("mystampprop :" + inc); 

 String stampValue = null; 

 if(input == null || input.length() == 0){ 
 stampValue = incrementVersion(inc); 
 }else{ 
 stampValue = input; 
 } 
 ProjectLookup.getCurrent().getProperty("my-stamp-property").setValue(stampValue); 
 uow.commit(); 
 stampContext.put("stampValue", ""+stampValue );