May 1, 2007

Labeling Defect Number On Check In

Surround SCM
A customer recently asked if there was a way to label a file when it is attached to a defect, and use the defect number as the label. While this cannot be accomplished out of the box, it can be accomplished through triggers. Using the environment variables, we can grab almost any information at run-time. These variables are listed in the Surround SCM User Guide and here. To accomplish this I created a trigger to be fired off on the attach to defect event. I set its action to be a batch file that contains the following line: Surround SCM 2008 and earlier sscm label %SSCM_FILE% -b%SSCM_BRANCH% -c- -l%SSCM_DEFECTNUMBER% -p%SSCM_Repository%
-yAdministrator:password -z127.0.0.1:4900
Surround SCM 2009 and later This example will still work with Surround SCM 2009. But as explained in this article, it will create a label that is only scoped to the branch where the check in took place. So what if you want a global label? This would allow you to add files from other branches to this label. After you promote a defect fix to another branch, you may want to indicate that the promoted files have the fix for this defect as well. One solution would be to attach those files to the same defect after promote, or you could also add them to this label. For this batch file, we need to edit the batch file and add a new line. The line will create the label. If the label already exists, Surround SCM will throw a message, but it will not stop the batch file from executing the next command, which adds the file to the label. Another change is that instead of simply using the defect number for the label name, a prefix of "ttp:" is used. Here is the batch file:
ECHO sscm addlabel ttp:%SSCM_DEFECTNUMBER% -p"%SSCM_MAINLINE%" -d"Label for TestTrack Defect %SSCM_DEFECTNUMBER%" -yAdministrator -z127.0.0.1:4900>>C:label.log

sscm addlabel ttp:%SSCM_DEFECTNUMBER% -p"%SSCM_MAINLINE%" -d"Label for TestTrack Defect %SSCM_DEFECTNUMBER%" -yAdministrator -z127.0.0.1:4900>>C:label.log

ECHO sscm label "%SSCM_FILE%" -b"%SSCM_BRANCH%" -cc"File added to label via trigger" -lttp:%SSCM_DEFECTNUMBER% -p"%SSCM_REPOSITORY%" -yAdministrator: -z127.0.0.1:4900>>C:label.log

sscm label "%SSCM_FILE%" -b"%SSCM_BRANCH%" -cc"File added to label via trigger" -lttp:%SSCM_DEFECTNUMBER% -p"%SSCM_REPOSITORY%" -yAdministrator -z127.0.0.1:4900>>C:label.log
Each command is first echoed to a text file. This is handy for testing, since it will expand the environment variables and show you how the command looks when it is launched by the trigger. Each command's ouput is also appended to the same text file. This is also for testing, since it will document any errors during processing.

Known issues

Make sure you handle the comment in the label command. If you do not, the CLI program (sscm) will be left running in the background waiting for you to enter a comment. This can cause subsequent triggers that use the CLI to not work. Either pass a comment, a comment file, or -c-. If your batch file does not appear to be working, it may be because of this. This solution may not work for you, however (it did not work for me). The only way I got this batch file to work with multiple check ins was to remove all logging, so my modified batch file looks like this:
sscm addlabel ttp:%SSCM_DEFECTNUMBER% -p"%SSCM_MAINLINE%" -d"Label for TestTrack Defect %SSCM_DEFECTNUMBER%" -yAdministrator -z127.0.0.1:4900

sscm label "%SSCM_FILE%" -b"%SSCM_BRANCH%" -cc"File added to label via trigger" -lttp:%SSCM_DEFECTNUMBER% -p"%SSCM_REPOSITORY%" -yAdministrator -z127.0.0.1:4900
This example could be taken a step further by creating a more meaningful label than just a defect number. You could concatenate the defect number with another string, like "Defect ". This way the label would be "Defect 23" instead of "23". Note: Seapine does not provide support for sample triggers and scripts.