Created
November 22, 2010 18:21
-
-
Save slackorama/710374 to your computer and use it in GitHub Desktop.
Git task to create an email for bugzilla actions (fixing a ticket or merging a ticket)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# git-bz -- given a version and target, print out a merge | |
# email for sending to bz | |
use warnings; | |
use strict; | |
use Carp; | |
use Git; | |
#============================================================================ | |
# map a shorthand version to the one bz expects for merges | |
#============================================================================ | |
my %versions = ( | |
'mne' => 'Merge (M&E/Patch)', | |
'rc' => 'Merge (RC/Major)', | |
'hotfix' => 'Merge (Hotfix)' | |
); | |
#============================================================================ | |
# get the action and config info | |
#============================================================================ | |
my $action = shift || ''; | |
my $bz_email = Git::config('bz.email'); | |
if (! $bz_email) { | |
croak 'No bugzilla email found'; | |
} | |
#============================================================================ | |
# start doing the work | |
#============================================================================ | |
if ( $action eq 'fix' ) { | |
my $user_email = get_user_email(); | |
my $bz_info = get_bz_info(); | |
print <<"MAIL"; | |
To: $bz_email | |
From: $user_email | |
Subject: Issue $bz_info->{bug} fixed | |
\@bug_id = $bz_info->{bug} | |
\@bug_status = RESOLVED | |
\@resolution = FIXED | |
Fixed in $bz_info->{branch} pending merge. | |
} elsif ( $action eq 'merge' ) { | |
my $target_milestone = shift || '---'; | |
my $merge_to = shift; | |
my $version = ( $merge_to && $versions{$merge_to} ) || | |
'unspecified'; | |
my $bz_info = get_bz_info(); | |
my $user_email = get_user_email(); | |
if ( !defined $bz_info->{bug} ) { | |
croak "No bug id found in branch name"; | |
} | |
my $bug_id = $bz_info->{bug}; | |
my $bz_branch = $bz_info->{branch}; | |
#============================================================================ | |
# get the remote branch, url and name | |
#============================================================================ | |
my @remote_info | |
= Git::command( 'config', '--get-regexp', '^branch\.' | |
. $bz_branch ); | |
my ( $remote, $remote_branch ); | |
for my $line (@remote_info) { | |
if ( $line =~ /\.remote\s+(.+)/x ) { | |
$remote = $1; | |
} elsif ( $line =~ /merge\s+((refs\/)?heads\/)?(.+)/x ) { | |
$remote_branch = $3; | |
} | |
} | |
my $remote_url = Git::command_oneline( 'config', '--get-regexp', | |
'remote.' . $remote . '.url' ); | |
if ( $remote_url =~ m/url\s+(.+)/x ) { | |
$remote_url = $1; | |
} | |
#============================================================================ | |
# get the commits to show | |
#============================================================================ | |
my $commits = Git::command( | |
'log', "$remote/$remote_branch..", | |
'--pretty=short', '--name-status' | |
); | |
#============================================================================ | |
# finally print out the email message | |
#============================================================================ | |
print <<"MAIL"; | |
To: $bz_email | |
From: $user_email | |
Subject: Please merge $bz_branch to $remote_branch | |
\@blocked = $bug_id | |
\@product = Application Defects | |
\@component = Merge Request | |
\@version = $version | |
\@target_milestone = $target_milestone | |
Please merge $bz_branch to the remote branch $remote_branch | |
Merge: $bz_branch | |
To: $remote_branch | |
Repository: $remote_url | |
$commits | |
} else { | |
croak <<"USAGE"; | |
error: Unknon subcommand: $action | |
usage: git bz fix <target> | |
or: git bz merge <merge_to> <target> | |
USAGE | |
} | |
#============================================================================ | |
# get current branch and bug ID | |
#============================================================================ | |
sub get_bz_info { | |
my $bz_branch = Git::command_oneline( 'symbolic-ref', 'HEAD' ); | |
$bz_branch =~ s{^refs\/heads/}{}x; | |
my ($bug_id) = $bz_branch =~ m/bz(\d+)/x; | |
return { branch => $bz_branch, bug => $bug_id }; | |
} | |
sub get_user_email { | |
my $email = Git::command_oneline( 'config', '--get', 'user.email' ); | |
return $email; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment