Skip to content

WebHook Templates : The Velocity Templating Engine

netwolfuk edited this page Jul 9, 2023 · 10 revisions

Since tcWebHooks 1.2.0-alpha1

The Velocity Template engine adds conditional logic, loops and object accessing to WebHook Templates.

When creating or editing a WebHook Template, choose a Payload Format which contains the name "Velocity Template". Any templates using this format will be parsed by the Velocity Template engine as they are rendered by the webhook.

WebHook Velocity Extensions

Velocity allows additional commands to be added by way of Velocity Directives. The following directives are available for use in webhook templates.

  • #escapejson(String) - Escapes a string into valid JSON.
  • #tojson(Object) - Converts an object into a JSON structure using the GSON library.
  • #capitalise(String), #capitalize(String) - Converts the first letter of each word to a capital letter.
  • #now(String) - Return the date/time using the format provided (uses SimpleDateFormat).
  • #substr(text, startIndex, endIndex, minLength) - Returns the sub string of text starting at startIndex and finishing at endIndex, if text is longer than minLength.
  • #sanitise(String), sanitize(String) - Converts some problematic characters into underscores.
  • $jsonTool.jsonToMap(String) - Converts a JSON string into a Java Map instance. - Since 1.2.0-alpha.4
  • $nullUtils.toUnResolved(Object) - Returns the object, or the string UNRESOLVED if the object is null - Since 1.2.0-alpha.6
  • $nullUtils.toUnResolved(Object, boolean) - Returns the object, or the string "UNRESOLVED" or UNRESOLVED. The UNRESOLVED is inside double quotes if the boolean is true. - Since 1.2.0-alpha.6

Velocity templating examples

Get the 7 digit githash from a 32 digit commit hash

#substr($build_vcs_number,0,7,32)

Iterate over a change set in a loop

Uses a Velocity macro.

## Define macro called "showchanges"
#macro( showchanges $mychanges)
  #if ( $mychanges.size() > 0 ) 
    #foreach( $change in $mychanges )
      #substr($change.version,0,7,32) :: $change.change.username :: #escapejson($change.change.comment)
    #end
  #else No Changes found #end
#end

## Access the macro defined above
#showchanges( $changes )

Print test counts and then list all Ignored Tests

Note: This makes use of the new build, project and buildType objects since 1.2.0-alpha3

## Create a variable to hold the result of build.getFullStatistics()
#set ($buildStatistics = $build.fullStatistics)

## Loop over tests and print name and status.
#foreach ($test in $buildStatistics.allTests)
    #if( $test.status.isIgnored() )
       "test-$test.testRunId" : "$test.test.name - $test.status",
    #end
#end

Print the build's statistics.

These are typically items like code coverage, etc. Note: This makes use of the new build, project and buildType objects since 1.2.0-alpha3

## Create a variable to hold the result of build.getStatisticValues()
#set ($statsValues = $build.statisticValues)

## Loop over stats and print name (key) and value (converted from bigDecimal to integer).
#foreach ($mapEntry in $statsValues.entrySet())
  #if ( $mapEntry.value )
    "$mapEntry.key": $mapEntry.value.intValue(),
  #end
#end

Print all Ignored tests as JSON

This could be modified to print failed tests, or all tests. Note: This makes use of the new build, project and buildType objects since 1.2.0-alpha3

#set ($buildStatistics = $build.fullStatistics)
{
    "buildName": "${buildType.name}",
    "projectName" : "${project.externalId}",
    "successfulTests": $buildStatistics.passedTestCount, 
    "failedTests" : $buildStatistics.failedTestCount,
    "ignoredTests": $buildStatistics.ignoredTestCount,
    "mutedTests"  : $buildStatistics.mutedTestsCount,
#foreach ($test in $buildStatistics.allTests)
    #if( $test.status.isIgnored() )
       "test-$test.testRunId" : "$test.test.name - $test.status",
    #end
#end 
## Add an extra field here on the end, so that we can have a line without a trailing comma (otherwise JSON is invalid).
    "buildResult" : "${buildResult}"
}

Print test details for mute/unmute as JSON

Note: This relies on the TestMuted and TestUnMuted event support in v1.2.3 and v2.0.0rc2

{
    "event": "$buildEventType",
    "tests": [
#foreach( $entry in $mutedOrUnmutedTests.entrySet() )##
#foreach( $test in $entry.value )##
      {
        "name": "#escapejson($test.name)",
        "id": "$test.testNameId",
        "user": "$entry.key.mutingUser.username"
      }#if( $foreach.hasNext ),#end
#end
#end
    ]
}
Clone this wiki locally