This project has moved. For the latest updates, please go here.

Sample DSL Script

The web service is configured to run a script (via the web.config) when the service end point is called. The path to this script file path is define as for the email template .

The script is written in IronPython if the general form

import sys
# Expect 2 args the event type and a value unique ID
LogInfoMessage( "The following arguments were passed" )
LogInfoMessage( sys.argv )
if sys.argv[0] == "BuildEvent" :
         LogInfoMessage ("A build event " + sys.argv[1])
          # a sample for using the DSL to create a work item is
     
   fields = {"Title": "The Title","Effort": 2, "Description": "The description of the bug"}
   teamproject = "Scrum (TFVC)"

   wi = CreateWorkItem(teamproject ,"bug",fields)
         LogInfoMessage ("Work item '" + str(wi.Id) + "' has been created with the title '" + wi.Title +"'")
elif sys.argv[0] == "WorkItemEvent" :
         LogInfoMessage ("A wi event " + sys.argv[1])
elif sys.argv[0] == "CheckInEvent" :
         LogInfoMessage ("A checkin event " + sys.argv[1])
else:
         LogInfoMessage ("Was not expecting to get here")

Examples

The best documentation for the script usage can be found the unit test project. Some usable ‘production’ scripts are shown below

Note: The WIKI layout may alter the indenting in these samples due to line wrapping, so use some common sense

sendtemplatedemail.py

This sends a template based HTML format email that can include any work item fields

import sys
# Expect 2 args the event type and a WI id
if sys.argv[0] == "WorkItemEvent" :
    wi = int(sys.argv[1])
    dumpAllWorkItemFields = True
    dumpAllAlertFields = True
    showMissingFieldNames = True
    SendEmail(wi, CurrentScriptFolder() + "\EmailTemplate.htm", dumpAllWorkItemFields, dumpAllAlertFields, showMissingFieldNames)
    LogInfoMessage("Sending email for work item " + str(wi))
else:
    LogErrorMessage("Was not expecting to get here")
    LogErrorMessage(sys.argv)

setbuildretensionbyquality.py

This sends an email based on the quality of a build and also sets the retention  policy.

import sys
# Expect 2 args the event type and a value unique ID for the build
if sys.argv[0] == "BuildEvent" :
    uri = sys.argv[1]
    build = GetBuildDetails(uri)
    if build.Quality == "Test Failed" or build.Quality == "Rejected" :
        keepForever = False
    else:
        keepForever = True
    SetBuildRetension(uri, keepForever)
    msg = "'" + build.BuildNumber + "' retension set to '" + str(keepForever) + "' as quality was changed to '" + build.Quality +"'"
    SendEmail("richard@typhoontfs",build.BuildNumber + " quality changed", msg)
    LogInfoMessage(msg)
else:
    LogErrorMessage("Was not expecting to get here")
    LogErrorMessage(sys.argv)

 

changeparentworkitemstate.py

Performs a roll up state change on a parent work item, setting it to done when all the child work items are done

import sys
# Expect 2 args the event type and a value unique ID for the wi
if sys.argv[0] == "WorkItemEvent" :
    wi = GetWorkItem(int(sys.argv[1]))
    parentwi = GetParentWorkItem(wi)
    if parentwi == None:
        LogInfoMessage("Work item '" + str(wi.Id) + "' has no parent")
    else:
        LogInfoMessage("Work item '" + str(wi.Id) + "' has parent '" + str(parentwi.Id) + "'")
        results = [c for c in GetChildWorkItems(parentwi) if c.State != "Done"]
        if len(results) == 0:
            LogInfoMessage("All child work items are 'Done'")
            parentwi.State = "Done"
            UpdateWorkItem(parentwi)
            msg = "Work item '" + str(parentwi.Id) + "' has been set as 'Done' as all its child work items are done"
            SendEmail("richard@typhoontfs","Work item '" + str(parentwi.Id) + "' has been updated", msg)
            LogInfoMessage(msg)
        else:
            LogInfoMessage("Not all child work items are 'Done'")
else:
    LogErrorMessage("Was not expecting to get here")
    LogErrorMessage(sys.argv)

incrementbuildversion.py

Updates the minor version number when a built is set to the released state

import sys
# Expect 2 args the event type and a value unique ID for the build
if sys.argv[0] == "BuildEvent" :
    uri = sys.argv[1]
    build = GetBuildDetails(uri)
    if build.Quality == "Released" :
        IncrementBuildNumber(uri)
        msg = "'" + build.BuildDefinition.Name + "' version incremented to " + GetVersionNumber(uri) + " as last build quality set to '" + build.Quality +"'"
        SendEmail("richard@typhoontfs",build.BuildDefinition.Name + " version incremented", msg)
        LogInfoMessage(msg)
else:
    LogErrorMessage("Was not expecting to get here")
    LogErrorMessage(sys.argv)

Last edited May 12, 2014 at 7:43 PM by rfennell, version 10