HOWTO: Send CVSTrac Ticket Updates

Introduction

As regular visitors will know, I use CVSTrac to organize most of my development efforts. A customized instance was integrated into this site, another holds a Web-based to-do list for my own consumption and yet another keeps track of internal developments my previous group did for over a year now.

I've been meaning to customize ticket notifications for a while now, and today I finally got around to hacking a quick Perl script using CPAN:MIME::Lite to send a nice (but simple) HTML mail with ticket change notifications to both the ticket owner and the requestor.

Prequisites

You need to know your way around UNIX systems, debug Perl (to change a few things in the script according to your install) and generally know what you're doing. An HTML-tolerant mailer and reasonable eyesight will also help in actually viewing the results.

Without further ado, here's the script (which could be prettier, but I'm thinking of expanding it a bit later on - hence the reliance on Getopt and my usual Hungarian Notation variable names):

#!/usr/bin/perl
use MIME::Lite;
use POSIX;
use Getopt::Long;

Getopt::Long::Configure('bundling');

if( @ARGV > 0 ) {
  GetOptions( 'a|assigned=s'    => \$gszAssignedMail,
              'n|name=s'        => \$gszAssignedName,
              'c|contact=s'     => \$gszContact,
              'd|description=s' => \$gszDescription,
              'i|ticket=s'      => \$gszTicketNumber,
              'p|project=s'     => \$gszProject,
              'r|remarks=s'     => \$gszRemarks,
              's|status=s'      => \$gszStatus,
              't|title=s'       => \$gszTitle,
              'u|changed=s'     => \$gszChanged,
              'o|originator=s'  => \$gszOriginator,
              'y|type=s'        => \$gszType );
$gszTime = POSIX::strftime("%a, %m %d %H:%M", localtime);

  $szBuffer = <<EOT;
<html>
<style>
BODY, P, TD, TH { background-color: white; font-size: 10px;
                  font-family: Arial, Helvetica, Sans-Serif; }
TH { text-align: left; font-size: 11px; background-color: #e0e0e0; }
PRE { font-family: Lucida Console, Monaco, Courier New, fixed; font-size: 10px; }
H1 { font-size: 18px; }
</style>
<body>
<h1>Ticket <a href="http://cvstrac/tktview?tn=$gszTicketNumber">#$gszTicketNumber</a> was updated by $gszChanged.</h1>
<table border=0 cellpadding=2 cellspacing=1 bgcolor="black" width="100%">
<tr><th>Title:</th><td width="100%">$gszTitle</td></tr>
<tr><th>Type:</th><td>$gszType</td></tr>
<tr>
  <th>Assigned to:</th>
  <td>
    <a href="mailto:$gszAssignedMail">$gszAssignedName</a>
  </td>
</tr>
<tr><th>Status:</th><td>$gszStatus</td></tr>
<tr><th colspan=2>Description:</th></tr>
<tr><td colspan=2><pre>$gszDescription</pre></td></tr>
<tr><th colspan=2>Remarks:</th></tr>
<tr><td colspan=2><pre>$gszRemarks</pre></td></tr>
</table>
Generated by notify.pl at $gszTime<br>
<a href="http://cvstrac/tktview?tn=$gszTicketNumber">View Ticket</a>
</body>
</html>
EOT

$msg = new MIME::Lite
  From     => '[email protected]',
  To       => $gszContact,
  Cc       => $gszAssignedMail,
  Subject  => "[$gszProject tracker] Ticket $gszTicketNumber updated by $gszChanged",
  Type     => "text/html",
  Data     => $szBuffer;
$msg->attr( 'content-type.charset' => "ISO-8859-1" );

$msg->send( 'smtp', 'smart.host.my.domain.com');
}

Invocation

To make it work, just plonk it somewhere (say /usr/local/bin) and configure the ticket change notification in CVSTrac to read (mind the \, it's there only to ensure readability here):

/usr/local/bin/notify.pl -n '%a' -a '%A' -c '%c' -d '%d' -i '%n' -p '%p' -r '%r' \
-s '%s' -t '%t' -u '%u' -y '%y' -o '%w'

And that's it.