five-bugs-a-day: Make #! to perl more portable
[platform/upstream/gstreamer.git] / scripts / five-bugs-a-day.pl
1 #!/usr/bin/env perl
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 # ----------------------------------------------------------------------------
51 #    subroutines
52 # ----------------------------------------------------------------------------
53
54 sub shuffle
55 {
56     my $array = shift;
57     my $i = @$array;
58     while ( --$i )
59     {
60         my $j = int rand( $i+1 );
61         print "j = $j, i = $i \n";
62         @$array[$i,$j] = @$array[$j,$i];
63     }
64
65     return @$array;
66 }
67
68 # ----------------------------------------------------------------------------
69 #    main
70 # ----------------------------------------------------------------------------
71
72 my $NUM_BUGS = 10;
73
74 my $PRODUCT = "GStreamer";
75
76 # ----- command line options -----
77
78 if (@ARGV) {
79   $NUM_BUGS = shift @ARGV;
80 }
81
82 if (@ARGV) {
83   $PRODUCT = shift @ARGV;
84 }
85
86
87 my $QUERY_URL = "https://bugzilla.gnome.org/buglist.cgi?product=$PRODUCT&" .
88                 'bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&' .
89                 'bug_status=REOPENED&' .
90                 'query_format=advanced&ctype=csv';
91
92 my $COL_ID;
93 my $COL_DESC;
94
95 my %BUGS;
96
97 # for testing/debugging:
98 # unless (@lines = `cat bugs.csv`) {
99 unless (@lines = `wget --no-check-certificate --quiet -O - '$QUERY_URL'`) {
100   die 'Could not download bug list';
101 }
102
103 # ----- parse column headers -----
104
105 my $headers;
106
107 # get first line which contains the field names
108 $headers = shift @lines;
109
110 # get rid of newline at end
111 chop $headers;
112
113 my @fields = split (/,/, $headers);
114 my $num_fields = scalar(@fields);
115
116 for (my $c = 0; $c < $num_fields; $c++) {
117   #print "$c $fields[$c] \n";
118   if ($fields[$c] =~ m/bug_id/) {
119     $COL_ID = $c;
120   } elsif ($fields[$c] =~ m/short_desc/) {
121     $COL_DESC = $c;
122   }
123 }
124
125 die "Could not find bug_id column in CVS file" if not defined ($COL_ID);
126 die "Could not find short_desc column in CVS file" if not defined ($COL_DESC);
127
128 #print "bugid is column $COL_ID\n";
129 #print "desc  is column $COL_DESC\n";
130
131 foreach (@lines) {
132   if (m/,/) {
133     chop;
134
135     # We specify num_fields as limit here, because the short_desc field
136     # might contain commas as well, and we don't want it to get cut off.
137     # This is a hack for the fact that we don't handle quoted fields
138     # (12345,"UNCONFIRMED","foo, bar: errors out") properly. As long as the
139     # short_desc field is the last one, that should be ok. (FIXME)
140     my @vals = split (/,/, $_, $num_fields);
141     my $id = $vals[$COL_ID];
142     my $desc = $vals[$COL_DESC];
143
144     $desc =~ s/^"(.*)"$/$1/;
145     $BUGS{$id} = $desc;
146   }
147 }
148
149 my @all_bugs = keys %BUGS;
150 my @bugs = shuffle (\@all_bugs);
151
152 # only want first NUM_BUGS bugs
153 $#bugs = $NUM_BUGS;
154
155 print "\n";
156 print "$NUM_BUGS random bugs:\n";
157 print "\n";
158
159 for my $bug_id ( @bugs ) {
160   print "$BUGS{$bug_id}\n";
161   print "https://bugzilla.gnome.org/show_bug.cgi?id=$bug_id\n";
162   print "\n";
163 }
164
165 print "\n";
166 print "More bugs at:\n";
167 print "  - http://gstreamer.freedesktop.org/bugs/\n";
168 print "  - https://bugzilla.gnome.org/browse.cgi?product=GStreamer\n";
169 print "\n";