scripts: add five-bugs-a-day script
[platform/upstream/gstreamer.git] / scripts / five-bugs-a-day.pl
1 #!/bin/perl -w
2 #
3 # ----------------------------------------------------------------------------
4 #
5 # five-bugs-a-day.pl
6 #
7 # Little script that outputs a list of N random open bugs from bugzilla
8 # for a certain bugzilla product
9 #
10 # ----------------------------------------------------------------------------
11 #
12 # Copyright (C) 2011-2012 Tim-Philipp Muller <tim centricular net>
13 #
14 # This script is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Library General Public
16 # License as published by the Free Software Foundation; either
17 # version 2 of the License, or (at your option) any later version.
18 #
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 # Library General Public License for more details.
23 #
24 # You should have received a copy of the GNU Library General Public
25 # License along with this library; if not, write to the
26 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 # Boston, MA 02111-1307, USA.
28 #
29 # ----------------------------------------------------------------------------
30 #
31 # You can use it to send yourself an e-mail with a few bugs to check up on
32 # every day, just put it into your crontab on a computer of your choice
33 # (with proper e-mail forwarding configured in some way):
34 #
35 # $ crontab -e
36 #
37 # add:
38 #
39 #   MAILTO=you@nowhere.org
40 #   # send ten random buglinks every day at 16.30
41 #   30 16### /usr/bin/perl  /path/to/five-bugs-a-day.pl
42 #
43 #
44 # Yes, it's PERL, sorry.
45 #
46 # Yes, I know the default is 10 bugs.
47 #
48 # ----------------------------------------------------------------------------
49 #
50 # Known bugs:
51 #   - there may be an issue with apostrophes or quotes in bug summaries,
52 #     descriptions sometimes get cut off
53 #
54 # ----------------------------------------------------------------------------
55
56 # ----------------------------------------------------------------------------
57 #    subroutines
58 # ----------------------------------------------------------------------------
59
60 sub shuffle
61 {
62     my $array = shift;
63     my $i = @$array;
64     while ( --$i )
65     {
66         my $j = int rand( $i+1 );
67         print "j = $j, i = $i \n";
68         @$array[$i,$j] = @$array[$j,$i];
69     }
70
71     return @$array;
72 }
73
74 # ----------------------------------------------------------------------------
75 #    main
76 # ----------------------------------------------------------------------------
77
78 my $NUM_BUGS = 10;
79
80 my $PRODUCT = "GStreamer";
81
82 # ----- command line options -----
83
84 if (@ARGV) {
85   $NUM_BUGS = shift @ARGV;
86 }
87
88 if (@ARGV) {
89   $PRODUCT = shift @ARGV;
90 }
91
92
93 my $QUERY_URL = "https://bugzilla.gnome.org/buglist.cgi?product=$PRODUCT&" .
94                 'bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&' .
95                 'bug_status=REOPENED&' .
96                 'query_format=advanced&ctype=csv';
97
98 my $COL_ID;
99 my $COL_DESC;
100
101 my %BUGS;
102
103 # for testing/debugging:
104 # unless (@lines = `cat bugs.csv`) {
105 unless (@lines = `wget --no-check-certificate --quiet -O - '$QUERY_URL'`) {
106   die 'Could not download bug list';
107 }
108
109 # ----- parse column headers -----
110
111 my $headers;
112
113 # get first line which contains the field names
114 $headers = shift @lines;
115
116 # get rid of newline at end
117 chop $headers;
118
119 my @fields = split (/,/, $headers);
120 my $num_fields = scalar(@fields);
121
122 for (my $c = 0; $c < $num_fields; $c++) {
123   #print "$c $fields[$c] \n";
124   if ($fields[$c] =~ m/bug_id/) {
125     $COL_ID = $c;
126   } elsif ($fields[$c] =~ m/short_desc/) {
127     $COL_DESC = $c;
128   }
129 }
130
131 die "Could not find bug_id column in CVS file" if not defined ($COL_ID);
132 die "Could not find short_desc column in CVS file" if not defined ($COL_DESC);
133
134 #print "bugid is column $COL_ID\n";
135 #print "desc  is column $COL_DESC\n";
136
137 foreach (@lines) {
138   if (m/,/) {
139     chop;
140
141     # We specify num_fields as limit here, because the short_desc field
142     # might contain commas as well, and we don't want it to get cut off.
143     # This is a hack for the fact that we don't handle quoted fields
144     # (12345,"UNCONFIRMED","foo, bar: errors out") properly. As long as the
145     # short_desc field is the last one, that should be ok. (FIXME)
146     my @vals = split (/,/, $_, $num_fields);
147     my $id = $vals[$COL_ID];
148     my $desc = $vals[$COL_DESC];
149
150     $desc =~ s/^"(.*)"$/$1/;
151     $BUGS{$id} = $desc;
152   }
153 }
154
155 my @all_bugs = keys %BUGS;
156 my @bugs = shuffle (\@all_bugs);
157
158 # only want first NUM_BUGS bugs
159 $#bugs = $NUM_BUGS;
160
161 print "\n";
162 print "$NUM_BUGS random bugs:\n";
163 print "\n";
164
165 for my $bug_id ( @bugs ) {
166   print "$BUGS{$bug_id}\n";
167   print "https://bugzilla.gnome.org/show_bug.cgi?id=$bug_id\n";
168   print "\n";
169 }
170
171 print "\n";
172 print "More bugs at:\n";
173 print "  - http://gstreamer.freedesktop.org/bugs/\n";
174 print "  - https://bugzilla.gnome.org/browse.cgi?product=GStreamer\n";
175 print "\n";