public
Last active

Git task to create an email for bugzilla actions (fixing a ticket or merging a ticket)

  • Download Gist
git-bz
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#!/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 milestone since both of these are used
#============================================================================
my $action = shift || '';
my $target_milestone = shift || '---';
 
my $bz_email = Git::config('bz.email');
 
if (! $bz_email) {
croak 'No bugzilla email found';
}
 
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.
 
MAIL
 
} elsif ( $action eq 'merge' ) {
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
MAIL
 
} 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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.