Thursday, October 17, 2013

Integrating svn commit logs to bugzilla

Purpose: making mandate that provide bug id(should be exit in database of bugzilla) and commit log before doing any commit.

Note: Script was written long back so version depended issues may occur. So please take it as reference.

Go to the hooks directory and do required changes to pre-commit  file. Because it will be trigged before commit happens.

pre-commit
===============================
#!/usr/bin/perl

# config section
#minchars refers to min log length
$minchars = 20;
$svnlook = '/usr/bin/svnlook';

#--------------------------------------------
$repos = $ARGV[0];
$txn = $ARGV[1];

#open (FD, ">>/tmp/log_svn") or die "file doesn't exit";

sub check_bug_status
{
 use Mysql;
 my ($bug_id) = @_;
# print FD "bugid is $bug_id";
 # MYSQL CONFIG VARIABLES FOR BUGZILLA database
 $host = "localhost";
 $database = "bugs";
 $tablename = "bugs";
 $user = "bugs";
 $pw = "bugs";

 # PERL MYSQL CONNECT()
 $connect = Mysql->connect($host, $database, $user, $pw);

 # SELECT DB
 $connect->selectdb($database);

 # DEFINE A MySQL QUERY
 $myquery = "SELECT bug_status FROM $tablename where bug_id=$bug_id";

 # EXECUTE THE QUERY
 $execute = $connect->query($myquery);

 $rownumber = $execute->numrows();
 $fieldnumber = $execute->numfields();

 @array = $execute->fetchrow;
 my ($bug_status) = @array;
# print FD $bug_status;
 if ( $rownumber == 0 ) {
  print STDERR "Invalid Bugid ($bug_id)  in the comment\n";
  exit(1);
 }
 if ( $bug_status eq "RESOLVED" ) {
  print STDERR "Bugid ($bug_id) be in resolved state\n";
   exit(1);
 }
}


$comment = `$svnlook log -t "$txn" "$repos"`;
#print FD $comment;
chomp($comment);
$count = $comment =~ s/([a-z,0-9])/$1/gi;
#print FD $comment;
$tmp="Bugid ";

$txn =~ m/(^\d+)/;
my $rev = $1;
#print STDERR $txn;
#print STDERR $rev;

if ( $count  == 0 )
{
$comment = "Log is not entered by user\n";
  print STDERR "\nA comment is required!\n";
`/svn/scripts/error-email.pl "$repos" "$rev" --from commit-watcher\@mydomain.com -s "error: check in log size zero \n $comment $repos" admin\@mydomain.com `;
  exit(1);
}
elsif ( $count < $minchars )
{
 print STDERR "\nComment must be at least $minchars characters. Excluding spaces and new line characters\n";
`/svn/scripts/error-email.pl "$repos" "$rev" --from commit-watcher\@mydomain.com -s "error: check in log less than 40 characters \n $comment $repos" admin\@mydomain.com `;
 exit(1);
}
elsif ($comment =~ m/^\s*$tmp\s*(\d+)/ig)
{
 #   print FD "bugid from comment:$1\n";
 check_bug_status ($1) ;
 exit(0);
}
print STDERR "Invalid format for comment. Proper format: \"bugid checkin comments \" \n";
`/svn/scripts/error-email.pl "$repos" "$rev" --from commit-watcher\@mydomain.com -s "error:bugid format is wrongly entered \n $comment $repos" admin\@mydomain.com `;
exit(1);

No comments: