#!/usr/bin/perl
#
# Trigger that adds job numbers to submit description.
#
# This script will do the following:
# 1. Add any Jobs attached to the changelist to the changelist description
#
# Unix usage:
# perl /whatever/add_jobs_to_description.pl %changelist% %serverport%
# NT usage:
# c:/perl/bin/perl c:/whatever/add_jobs_to_description.pl %changelist% %serverport%
# (Note that the name of this script might need to be an "8.3" filename,
# depending on the version of Perl you're running.)
#
# Example 'triggers' section:
# Triggers:
# addJobs //... change-commit "c:/perl/bin/perl c:/whatever/add_jobs_to_description.pl %changelist% %serverport%"
#
# Tested on Platforms: Linux, NT (as program, not service).
#
# You might need to...
# 1. You might need to run change the "$p4 = ......." line, below,
# to add a username and password ('p4 -u hardcodedusername -P
# hardcodeduserpasswd -p $ServerPort') if the default user it's
# connecting as isn't appropriate.
############################ code start: add_jobs_to_description.pl
$ChangeNum = $ARGV[0];
$ServerPort = $ARGV[1];
$p4 = "p4 -p $ServerPort";
# get fixes/jobs associated with our changelist
@JobInfo = `$p4 fixes -c $ChangeNum`;
# append job names into a string
foreach (@JobInfo) {
($JobName, undef) = split (/ /, $_); # parse out job name
$JobsList = $JobsList . "\t$JobName\n";
} # foreach
# get pending changelist info
$ChangeInfo = `$p4 change -o $ChangeNum`;
# change linebreaks to something else
$ChangeInfo =~ s/\n/\<LINEBREAK\>/g;
# use some clever substitution to append jobs list to description
$ChangeInfo =~ s/(\<LINEBREAK\>Description.*\<LINEBREAK\>)Jobs/\1$JobsList\nJobs/g;
# change something else to linebreaks
$ChangeInfo =~ s/\<LINEBREAK\>/\n/g;
# create a temporary file for input to the p4 change command
open P4DTEMP, "> ./changelist.$ChangeNum";
print P4DTEMP $ChangeInfo;
close P4DTEMP;
# update the pending change description
@EditDescriptionResults = `$p4 change -i < ./changelist.$ChangeNum`;
#print @EditDescriptionResults;
# delete the temp file
system("del changelist.$ChangeNum");
exit(0); # always exit with success
############################ code end