I was recently asked to include build by build change history in our build notification messages by a Senior Developer and the QA Supervisor. Some things to note ahead of time is that the company I work for is on the small side and we do not have a Configuration Manager so everyone shares the responsibility in Development and Quality Assurance. My company boasts around 30 separate applications written in a variety of languages. The source control tree looks somewhat like a tumbleweed due to dependencies between application common components.
Checklist of steps used
- Query for and store the current Date and Time in Visual Build Pro.
- Split the current Date and Time into string format that is usable in TFS.
- I accomplished this using VBP’s VBScripting.
- Query history on TFS using the last known build date and time and the current time.
- Once a build succeeds, update the last known build date and save the VBP project.
Step 1: Define and Split Current Date and Time
To start with, we need to obtain the current Date and Time. This will be used later if the build succeeds to update the last known build time. It is important to get it before pulling down all of the source code so that we do not miss any history. It would be better for this report to have more information than needed rather than missing information.
- Inside of Visual Build, add a “Set Macro” object and name it something appropriate on the General tab (ie: Define DATETIMENOW).
- Define it as a Temporary Macro from the Macro tab.
- In the Value field, enter %DATETIME%.
- This is an internal macro that will return the current date and time.
- Name your temporary macro (ie: DATETIMENOW).
- Click OK.
- Next, add a “Run Script” object and name it something appropriate on the General tab (ie: Format DATETIMENOW for Team Foundation).
- Input the script below on the Script tab (Make sure to select VBScript from the dropdown).
- Read below for a breakdown on what this script is doing.
- Click OK.
- You should end up with the following 2 items.
DATETIMENOW = Application.ExpandMacros("%DATETIMENOW%")
sYEAR = vbld_FormatDateEx(DATETIMENOW, "yyyy")
sMONTH = vbld_FormatDateEx(DATETIMENOW, "mm")
sDAY = vbld_FormatDateEx(DATETIMENOW, "dd")
sHOUR = vbld_FormatDateEx(DATETIMENOW, "hh")
sMINUTE = vbld_FormatDateEx(DATETIMENOW, "MM")
HISTORYSTRING = "D" & sYEAR & "-" & sMONTH & "-" & sDAY & "T" & sHOUR & ":" & sMINUTE & "~T"
Set objMacro = vbld_TempMacros.Add("HISTORYSTRING", HISTORYSTRING)
The VBScript above does the following:
- DATETIMENOW = Application.ExpandMacros(“%DATETIMENOW%”)
- Application.ExpandMacros takes the value you assigned in DATETIMENOW and applies it to a variable inside of the script. In this case, I used the same variable name. Note that macro names need to be enclosed in % signs and VBScript variables names do not when in the VBScript editor.
- sYEAR = vbld_FormatDateEx(DATETIMENOW, “yyyy”)
- vbld_FormatDateEx returns a formatted date string based on a date and time string in the first parameter along with a format string in the second parameter.
- sMONTH, sDAY, SHOUR, SMINUTE all do the same as sYEAR except for their respective date/time elements.
- HISTORYSTRING = “D” & sYEAR & “-” & sMONTH & “-” & sDAY & “T” & sHOUR & “:” & sMINUTE & “~T”
- HISTORYSTRING is the formatted date/time string required for TFS to retrieve the desired history.
- When supplying a Date element, you preceed it with a D.
- When supplying a Time element, you preceed it with a T.
- If you only specify a T (as I did at the end), it wil default to the current date and time.
- HISTOYSTRING contains {the last known build date and time in TFS format ~ current date and time}.
- Set objMacro = vbld_TempMacros.Add(“HISTORYSTRING”, HISTORYSTRING)
- This line creates a temporary macro with the name of HISTORYSTRING that contains the contents of the VBScript variable HISTORYSTRING.
- By creating a temporary macro, you make it available to other steps in the build script.
Step 2: Retrieve History for your Projects
Now that you have the date and time saved in a TFS ready format, it is time to go retrieve the history for all of the projects referenced in your solution. For example, one of our projects contains 5 separate root source locations (Common libraries, Extension libraries, etc). In order to get a complete change history, we need to get the history of all 5 root source locations. I will explain how to do 1, you can repeat the process as many times as you need.
- Inside of Visual Build, add a “Set Macro” object and name it something appropriate on the General tab (ie: Define CommonLibraryRevisionText).
- Define it as a Temporary Macro from the Macro tab.
- Click OK.
- Add a “Team Foundation” object and name it something appropriate on the General tab (ie: Get History for Common Libraries).
- Select “History” from the dropdown on the Command tab.
- Enter the source path to the root source location you wish to retrieve history from in the “Item spec” field.
- Select “detailed” or leave the option as default depending on the need of history detail returned.
- Note that I found detailed to contain the complete history text of each check in whereas the default or brief could be cut off after a certain length. I opted for the full text by using detailed.
- Enter the following command in the “Additional command-line options”.
- /version:%LAST_BLD_DATETIME%
- We will define LAST_BLD_DATETIME in the final step.
- Click OK.
- Repeat these steps for each “root source location” that you need to report history for.
Step 3: Last Build Date and Time Project Macro
You now have the meat of gathering the change history for each project. The last thing you will need to do is create a Project Macro that can contain and persist the last time a successful build was finished. This would go towards the end of your build script just before the save project step. This way, when it is updated from a successful run, the next run will be able to apply that new date and time to the run and retrieve only the changes that occurred between that date and time and now.
- Inside of Visual Build, add a “Set Macro” object and name it Set LAST_BLD_DATETIME.
- Define it as a Project Macro from the Macro tab.
- On the Macro tab, provide the name LAST_BLD_DATETIME.
- In the Description box, supply the temporary macro from the VBScript named %HISTORYSTRING%.
- Now LAST_BLD_DATETIME has the date and time string from the beginning of the build script in a TFS recognized format for querying history.
- Click OK.
- Add a “Run Script” object and name it something appropriate on the General tab (ie: Save Project when changed).
- On the script tab, insert the following script. Make sure to select VBScript from the dropdown.
- Input the following script: If Project.IsModified Then Project.Save
- Click OK.
Step 4: Create LAST_BLD_DATETIME Macro and Update the value
The last thing you will need to do is add the Project Macro of “LAST_BLD_DATETIME” to the Macro section, populate it with the current date and time and save the file. Then the next build you create will be ready.
- From the Macros tab, right click on Project Macros and click Insert.
- Supply the name of LAST_BLD_DATETIME in the name field.
- Add a description if you like.
- Make sure that Project Macro is selected.
- Click OK.
- Go back to the Project Steps Tab and build the “Define DATETIMENOW” and “Format DATETIMENOW for Team Foundation” steps.
- Build the “Set LAST_BLD_DATETIME” step.
- Save your project.
- Now the next time you build your project it will be ready to pull a limited change history that you can report to others any way you wish. I include it in an email but your organization may do things differently.
Acknowledgements
Team Foundation Server (TFS) is owned by Microsoft. To find out more about TFS, visit them here http://msdn.microsoft.com/en-us/vstudio/ff637362.aspx.
Visual Build Professional is owned by Kinook. To find out more about VBP, visit them here http://www.kinook.com/VisBuildPro/
You can also download a PDF version of this guide here: How to Generate Build to Build Change History using Visual Build Professional and Team Foundation System which includes screenshots for more clear steps.
Related posts: