Tweaking of close bug code
[git-perl-utils.git] / script / git-to-bugzilla.pl
1 #!/usr/bin/env perl
2 #
3 #
4 use strict;
5 use warnings;
6 use Carp;
7 use Config::Any;
8 use Data::Dump;
9 use File::Slurp;
10 use FindBin;
11 use Getopt::Long;
12 use Git::Repository;
13
14 use lib "$FindBin::Bin/../lib";
15 use WWW::Bugzilla;
16
17 my $verbose;
18 my $debug;
19
20 # ------------------------------------------------------------------------
21 sub update_bugzilla {
22     my $cfg  = shift;
23     my $info = shift;
24     my $set  = shift;
25
26     my $bz = WWW::Bugzilla->new(
27         server     => $cfg->{bugzilla}{server},
28         email      => $cfg->{bugzilla}{user},
29         password   => $cfg->{bugzilla}{pass},
30         bug_number => $set->{bug}
31     ) || croak "Cannot open bz - $!";
32
33     my $header = sprintf( "Git commit: %s/commitdiff/%s\n", $cfg->{gitweb}, $info->{rev} );
34     if ( scalar( @{ $info->{diff} } ) > 50 ) {
35
36         # big diff - we skip the diff
37         $bz->additional_comments(
38             join( "\n", $header, @{ $info->{info} }, '', @{ $info->{log} }, '----', @{ $info->{diffstat} } ) );
39     }
40     else {
41
42         # otherwise we do the whole thing
43         $bz->additional_comments( join( "\n", $header, @{ $info->{all} } ) );
44     }
45
46     $bz->change_status("fixed")  if ( $set->{action} =~ /fixes/ );
47     $bz->change_status("closed") if ( $set->{action} =~ /closes/ );
48
49     $bz->commit;
50
51     printf( "[%d] %s %s [%s]\n", $set->{bug}, $info->{rev}, $info->{log}[0], $set->{action} );
52 }
53
54 # ------------------------------------------------------------------------
55 sub find_bugzilla_references {
56     my $info = shift;
57     my $cfg  = shift;
58
59     my @results;
60     my $action = '';
61     my $bugid;
62     foreach my $line ( @{ $info->{log} } ) {
63         $line = lc($line);
64         if ( $line =~ /(closes|fixes|references):?\s*(?:bug(?:zilla)?)?\s*\#?(\d+)/ ) {
65             $action = $1;
66             $bugid  = $2;
67         }
68         elsif ( $line =~ /\b(?:bug(?:zilla)?)\s*\#?(\d+)/ ) {
69             $action = 'references';
70             $bugid  = $1;
71         }
72         else {
73             next;
74         }
75
76         # remap actions
77
78         push( @results, { bug => $bugid, action => $action } );
79         ##printf( "%s\n\taction = %s bugid = %s\n", $info->{rev}, $action, $bugid );
80     }
81     return @results;
82 }
83
84 # ------------------------------------------------------------------------
85
86 sub git_commit_info {
87     my $git = shift;
88     my $rev = shift;
89
90     my @lines = $git->run( 'show', '-M', '-C', '--patch-with-stat', '--pretty=fuller', $rev );
91
92     my $info = {
93         rev      => $rev,
94         info     => [],
95         log      => [],
96         diffstat => [],
97         diff     => [],
98         all      => [@lines],    # deliberate copy
99     };
100
101     while ( my $line = shift @lines ) {
102         last if ( $line =~ /^$/ );
103         push( @{ $info->{info} }, $line );
104     }
105
106     while ( my $line = shift @lines ) {
107         last if ( $line =~ /^---\s*$/ );
108         push( @{ $info->{log} }, $line );
109     }
110
111     while ( my $line = shift @lines ) {
112         last if ( $line =~ /^$/ );
113         push( @{ $info->{diffstat} }, $line );
114     }
115
116     # all the rest
117     $info->{diff} = \@lines;
118
119     return $info;
120 }
121
122 # ------------------------------------------------------------------------
123
124 sub walk_git_commits {
125     my $git = shift;
126     my $cfg = shift;
127
128     my $lastrev = $git->run( 'rev-parse', $cfg->{lastref} );
129     my $headrev = $git->run( 'rev-parse', $cfg->{branch_head} );
130
131     return if ( $lastrev eq $headrev );
132
133     my @revs = $git->run( 'rev-list', '--topo-order', '--no-merges', ( $lastrev . '..' . $headrev ) );
134
135     foreach my $rev ( reverse(@revs) ) {
136         my $info = git_commit_info( $git, $rev );
137
138         #ddx($info);
139         #dd( $info->{info}, $info->{log}[0] );
140         my @sets = find_bugzilla_references( $info, $cfg );
141         foreach my $set (@sets) {
142             update_bugzilla( $cfg, $info, $set );
143         }
144     }
145     return $headrev;
146 }
147
148 # ------------------------------------------------------------------------
149
150 # main
151 {
152     my $config;
153
154     GetOptions(
155         'config=s' => \$config,
156         'debug!'   => \$debug,
157         'verbose!' => \$verbose,
158     ) or die "Incorrect options";
159     die "No config file given\n" unless ( $config and -f $config );
160     my $cfg = ( values( %{ Config::Any->load_files( { files => [$config], use_ext => 1 } )->[0] } ) )[0];
161
162     die "No git_dir specified\n" unless ( $cfg->{git_dir} );
163     $cfg->{lasttag} ||= $cfg->{git_dir} . '/refs/tags/BugzillaDone';
164     $cfg->{branch_head} ||= 'HEAD';
165
166     $cfg->{lastref} = -f $cfg->{lasttag} ? read_file( $cfg->{lasttag} ) : 'HEAD';
167     chomp( $cfg->{lastref} );
168
169     my $git = Git::Repository->new( git_dir => $cfg->{git_dir} ) || die "No valid git repo\n";
170
171     my $newlast = walk_git_commits( $git, $cfg );
172     if ($newlast) {
173         write_file( $cfg->{lasttag}, $newlast );
174     }
175 }