summaryrefslogtreecommitdiff
path: root/scripts/five-bugs-a-day.pl
blob: 7a4b2844e3c86538d6467faf4a16f94b7b0a0cb1 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env perl
#
# ----------------------------------------------------------------------------
#
# five-bugs-a-day.pl
#
# Little script that outputs a list of N random open bugs from bugzilla
# for a certain bugzilla product
#
# ----------------------------------------------------------------------------
#
# Copyright (C) 2011-2012 Tim-Philipp Muller <tim centricular net>
#
# This script is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
# ----------------------------------------------------------------------------
#
# You can use it to send yourself an e-mail with a few bugs to check up on
# every day, just put it into your crontab on a computer of your choice
# (with proper e-mail forwarding configured in some way):
#
# $ crontab -e
#
# add:
#
#   MAILTO=you@nowhere.org
#   # send ten random buglinks every day at 16.30
#   30 16 * * *  /usr/bin/perl  /path/to/five-bugs-a-day.pl
#
#
# Yes, it's PERL, sorry.
#
# Yes, I know the default is 10 bugs.
#
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
#    subroutines
# ----------------------------------------------------------------------------

sub shuffle
{
    my $array = shift;
    my $i = @$array;
    while ( --$i )
    {
        my $j = int rand( $i+1 );
        @$array[$i,$j] = @$array[$j,$i];
    }

    return @$array;
}

# ----------------------------------------------------------------------------
#    main
# ----------------------------------------------------------------------------

my $NUM_BUGS = 10;

my $PRODUCT = "GStreamer";

# ----- command line options -----

if (@ARGV) {
  $NUM_BUGS = shift @ARGV;
}

if (@ARGV) {
  $PRODUCT = shift @ARGV;
}


my $QUERY_URL = "https://bugzilla.gnome.org/buglist.cgi?product=$PRODUCT&" .
                'bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&' .
                'bug_status=REOPENED&' .
                'query_format=advanced&ctype=csv';

my $COL_ID;
my $COL_DESC;

my %BUGS;

# for testing/debugging:
# unless (@lines = `cat bugs.csv`) {
unless (@lines = `wget --no-check-certificate --quiet -O - '$QUERY_URL'`) {
  die 'Could not download bug list';
}

# ----- parse column headers -----

my $headers;

# get first line which contains the field names
$headers = shift @lines;

# get rid of newline at end
chop $headers;

my @fields = split (/,/, $headers);
my $num_fields = scalar(@fields);

for (my $c = 0; $c < $num_fields; $c++) {
  #print "$c $fields[$c] \n";
  if ($fields[$c] =~ m/bug_id/) {
    $COL_ID = $c;
  } elsif ($fields[$c] =~ m/short_desc/) {
    $COL_DESC = $c;
  }
}

die "Could not find bug_id column in CVS file" if not defined ($COL_ID);
die "Could not find short_desc column in CVS file" if not defined ($COL_DESC);

#print "bugid is column $COL_ID\n";
#print "desc  is column $COL_DESC\n";

foreach (@lines) {
  if (m/,/) {
    chop;

    # We specify num_fields as limit here, because the short_desc field
    # might contain commas as well, and we don't want it to get cut off.
    # This is a hack for the fact that we don't handle quoted fields
    # (12345,"UNCONFIRMED","foo, bar: errors out") properly. As long as the
    # short_desc field is the last one, that should be ok. (FIXME)
    my @vals = split (/,/, $_, $num_fields);
    my $id = $vals[$COL_ID];
    my $desc = $vals[$COL_DESC];

    $desc =~ s/^"(.*)"$/$1/;
    $BUGS{$id} = $desc;
  }
}

my @all_bugs = keys %BUGS;
my @bugs = shuffle (\@all_bugs);

# only want first NUM_BUGS bugs
@bugs = splice (@bugs, 0, $NUM_BUGS);

print "\n";
print "$NUM_BUGS random bugs:\n";
print "\n";

for my $bug_id ( @bugs ) {
  print "$BUGS{$bug_id}\n";
  print "https://bugzilla.gnome.org/show_bug.cgi?id=$bug_id\n";
  print "\n";
}

print "\n";
print "More bugs at:\n";
print "  - http://gstreamer.freedesktop.org/bugs/\n";
print "  - https://bugzilla.gnome.org/browse.cgi?product=GStreamer\n";
print "\n";