下面是codeStriker与svn、bugzilla集成使用的脚本(放在codestriker安装目录的bin下):
#!/usr/bin/perl -w
#########################################################################
# Create codestriker topic based on subversion commit.
# Install as a post-commit subversion hook.
# dmp 03/23/07
#########################################################################
# Absolute path of file mapping the author of a commit to a reviewer for it.
# Each line of the file should contain an author email address,
# followed by whitespace, followed by a reviewer email address.
# Lines starting with '#' are comments.
$REVIEWERS_FILE = "D:/hooks/reviewers.txt";
# Reviewer email to use if no mapping found in REVIEWERS_FILE
$DEFAULT_REVIEWER = "username\@hotmail.com";
# Suffix to add to username to get email address.
# TODO: Would be better to get mappings from a file.
$EMAIL_SUFFIX = "\@hotmail.com";
# Codestriker specific parameters for topic creation.
$CODESTRIKER_URL = 'http://localhost/codestriker/codestriker.pl?action=create';
$CODESTRIKER_PROJECT = '2';
$CODESTRIKER_REPOSITORY = 'svn:file:///D:/svnroo';
$CODESTRIKER_CC = '';
# Used for development - write verbosely to log file
$DEBUG = 1;
$LOGFILE = "D:/codestriker-1.9.8/bin/post-commit-codestriker.log";
use lib 'D:/codestriker-1.9.8/bin';
########################################################################
# Shouldn't need to change anything under here.
# Codestriker-specific imports.
use CodestrikerClient;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
# Print message to stdout if DEBUG is set.
sub debug {
local($msg) = @_;
if ($DEBUG) {
open(LOG, ">>$LOGFILE") || die ("Cannot open log file $LOGFILE: $!\n");
print(LOG $msg, "\n");
close(LOG);
}
}
# Get the email address for the given username. Currently this just adds
# $EMAIL_SUFFIX to the username, but it would be better to use a mapping file.
# param username
# return email address
sub get_email {
local($username) = @_;
return $username . $EMAIL_SUFFIX;
}
# Determine reviewer for review, based on mappings in
# REVIEWERS file.
# param: author email address
# returns: the reviewer email address from the file, or DEFAULT_REVIEWER
sub determine_reviewer {
local($author_email) = @_;
my $reviewer = '';
debug("author email is: " . $author_email);
debug("attempting to open reviewers file: $REVIEWERS_FILE");
open (RECLIST, "< $REVIEWERS_FILE") ||
return $DEFAULT_REVIEWER;
while (<RECLIST>) {
# skip comment lines
if (/^#/) {
next;
}
@fields = split;
if ($fields[0] eq $author_email) {
$reviewer = $fields[1];
debug("The reviewer for " . $author_email . " is: " . $reviewer);
last;
}
}
close RECLIST;
if (! $reviewer) {
$reviewer = $DEFAULT_REVIEWER;
debug("Assigned default reviewer: " . $reviewer);
}
return $reviewer;
}
########################################################################
# Main program
my $repo=$ARGV[0];
my $rev=$ARGV[1];
debug("repos=$repo; rev=$rev");
my @svninfo_out=split("\n", `svnlook info -r $rev $repo`);
my $user=$svninfo_out[0];
my $date=$svninfo_out[1];
my $loglength=$svninfo_out[2];
my $topic_title = "SVN commit: " . $svninfo_out[3];
my $mesg=join("\n", @svninfo_out[3..$#svninfo_out]);
debug("user=$user");
debug("date=$date");
debug("loglength=$loglength");
debug("mesg=$mesg");
my $createTopic = 0;
if ($mesg =~ /REVIEW/) {
debug("Found REVIEW - creating topic.");
$createTopic = 1;
}
# Look for bug numbers in the log message, which need to be in the form:
# " Bug: 4250 " (without quotes, case insensitive, colon and space optional).
my @bugs = ();
my $bug_ids = $mesg;
while ($bug_ids =~ /\b[Bb][Uu][Gg]:?\s*(\d+)\b/g) {
push @bugs, $1;
debug("Found bug number $1; creating topic.");
$createTopic = 1;
}
if ($createTopic) {
# Truncate the title if necessary.
if (length($topic_title) > 57) {
$topic_title = substr($topic_title, 0, 57) . "...";
}
my $reviewer = determine_reviewer(get_email($user));
debug("Creating topic...");
my $ua = new LWP::UserAgent;
my $content = [ action => 'submit_new_topic',
topic_title => $topic_title,
topic_description => $mesg,
projectid => $CODESTRIKER_PROJECT,
repository => $CODESTRIKER_REPOSITORY,
bug_ids => join(", ", @bugs),
email => get_email($user),
reviewers => $reviewer,
cc => $CODESTRIKER_CC,
topic_state => 'Open',
module => "/",
start_tag => $rev - 1,
end_tag => $rev];
my $response =
$ua->request(HTTP::Request::Common::POST($CODESTRIKER_URL,
Content_Type => 'form-data',
Content => $content));
# Indicate if the operation was successful.
my $response_content = $response->content;
my $rc = $response_content =~ /Topic URL: \<A HREF=\"(.*)\"/i;
print STDERR "Failed to create topic, response: " .
$response_content . "\n" if $rc == 0;
} else {
debug("Not creating topic.");
}