Debian Codegraph @

script/kgb-ci-report

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use DBD::Pg;
7 use DBI;
8 use Dpkg::Version;
9 use Getopt::Long;
10 use JSON::XS qw(decode_json);
11 use LWP::UserAgent;
12
13 my ( @opt_maintainer, $opt_kgb_config );
14 my $opt_verbose = 1;
15 my $opt_distribution = 'debian';
16 my $opt_release = 'sid';
17 my $opt_kgb_client = 'kgb-client';
18
19 GetOptions(
20 'm|maintainer=s' => \@opt_maintainer,
21 'kgb-config=s' => \$opt_kgb_config,
22 'verbose+' => \$opt_verbose,
23 'quiet' => sub { $opt_verbose = 0 },
24 'distribution=s' => \$opt_distribution,
25 'release=s' => \$opt_release,
26 'kgb-client=s' => \$opt_kgb_client,
27 ) or exit 1;
28
29 die "--maintainer is mandatory" unless @opt_maintainer;
30 $opt_verbose = 1 if not $opt_verbose and not $opt_kgb_client;
31
32 sub verbose {
33 return unless $opt_verbose;
34
35 warn @_, "\n";
36 }
37
38 my $db = DBI->connect( 'dbi:Pg:host=udd-mirror.debian.net;dbname=udd',
39 'udd-mirror', 'udd-mirror', { RaiseError => 1 } );
40 verbose('Connected to UDD');
41 my $st
42 = $db->prepare(
43 "with a as (select distinct source, source_version\n"
44 . 'from packages_summary where maintainer_email in ('
45 . join( ', ', ( ('?') x scalar(@opt_maintainer) ) )
46 . ") and distribution=? and release=?)\n"
47 . 'select source, max(source_version) from a group by 1'
48 );
49 $st->execute( @opt_maintainer, $opt_distribution, $opt_release );
50
51 my $rows = $st->fetchall_arrayref;
52 $db->disconnect;
53
54 verbose( scalar(@$rows) . ' packages found in UDD' );
55
56 my %wanted_packages =
57 map { ( $_->[0] => Dpkg::Version->new( $_->[1] ) ) } @$rows;
58 my $maintained = scalar @$rows;
59
60 undef $rows; # free the memory
61
62 my $debci_status_url = 'https://ci.debian.net/data/status/unstable/amd64/packages.json';
63
64 my $ua = LWP::UserAgent->new( agent => "$0" );
65 my $res = $ua->get($debci_status_url);
66 unless ($res->is_success) {
67 die "Error retrieving $debci_status_url: ".$res->status_line;
68 }
69
70 my $json = decode_json( ${$res->content_ref} );
71
72 verbose( scalar(@$json) . ' packages retrieved from debci' );
73
74 my $tested = 0;
75 my $tested_but_old = 0;
76 my $failing = 0;
77 my $regressions = 0;
78 my @regressions;
79
80 for my $pkg_info (@$json) {
81 next unless exists $wanted_packages{ $pkg_info->{package} };
82
83 $tested++;
84
85 my $ver = Dpkg::Version->new($pkg_info->{version});
86 my $status = $pkg_info->{status};
87 my $prev_status = $pkg_info->{previous_status};
88
89 my $test_is_old = $ver != $wanted_packages{ $pkg_info->{package} };
90 $tested_but_old++ if $test_is_old;
91
92 next if $test_is_old;
93
94 if ( $status eq 'fail' ) {
95 $failing++;
96
97 push @regressions, "$pkg_info->{package} $pkg_info->{version}"
98 if $prev_status and $prev_status eq 'pass';
99 }
100 }
101
102 my $msg;
103
104 if ( $failing ) {
105 $msg = 'CI coverage: ';
106 if ($tested_but_old) {
107 $msg .= sprintf( '%d+%d of %d',
108 $tested - $tested_but_old,
109 $tested_but_old, $maintained );
110 }
111 else {
112 $msg .= sprintf( '%d of %d', $tested, $maintained );
113 }
114
115 $msg .= sprintf( ' (%0.1f%%).',
116 $maintained ? 100 * $tested / $maintained : 0 );
117
118 $msg .= sprintf(
119 ' %d package%s fail CI tests (%0.1f%%).',
120 $failing,
121 ( $failing > 1 ) ? 's' : '',
122 $tested ? 100 * $failing / $tested : 0
123 );
124
125 if ( @regressions ) {
126 $msg .= ' Regressions: ';
127 my @verb = splice(@regressions, 0, 5);
128 if (@regressions) {
129 $msg .= join( ', ', @verb );
130 $msg .= sprintf ' and %d others', scalar(@regressions);
131 }
132 elsif (@verb > 1) {
133 $msg .= join( ', ', splice( @verb, 0, -1 ) );
134 $msg .= ' and ' . $verb[0];
135 }
136 else {
137 $msg .= $verb[0];
138 }
139 }
140 }
141 else {
142 $msg = "No packages fail their CI tests!";
143 }
144
145 verbose($msg);
146
147 if ($opt_kgb_config) {
148 system( $opt_kgb_client, '--conf', $opt_kgb_config, '--relay-msg', $msg )
149 == 0
150 or die $!;
151 }
152 else {
153 warn
154 "Please use --kgb-config if you want the notification to go to IRC.\n";
155 }
156
157 __END__
158
159 =head1 NAME
160
161 kgb-ci-report - put DebCI status of maintained packages on IRC
162
163 =head1 SYNOPSIS
164
165 B<kgb-ci-report> --maintainer I<email> --kgb-config I<path> [option...]
166
167 =head1 DESCRIPTION
168
169 B<kgb-ci-report> retrieves tha packages with failing CI tests and reports a
170 summary on IRC via L<kgb-client(1)>.
171
172 The packages are filtered by maintainer email. Intented purpose is invoking it
173 from cron, e.g. daily.
174
175 It uses UDD (L<https://udd.debian.org>) to get the information about the maintained
176 packages, and L<https://ci.debian.net> to fetch their status.
177
178 =head1 OPTIONS
179
180 =over
181
182 =item B<--maintainer> I<email>
183
184 =item B<-m> I<email>
185
186 Maintainer email. B<Mandatory>. If repeated multiple times all packages
187 maintained by any of the emails are considered in the report.
188
189 =item B<--kgb-config> I<path>
190
191 Path to the L<kgb-client(1)> configuration. B<Mandatory>.
192
193 =item B<--quiet>
194
195 Stop reporting progress and results.
196
197 =item B<--distribution> I<name>
198
199 Set the wanted distribution when retrieving the list of maintained packages.
200 Defaults to C<debian>.
201
202 =item B<--release> I<codename>
203
204 Sets the wanted release name when retrieving the list of maintained packages.
205 Defaults to C<sid>.
206
207 =item B<--kgb-client> I<path>
208
209 Path to the kgb-client executable. Defaults to C<kgb-client>.
210
211 =back
212
213 =head1 AUTHOR, COPYRIGHT AND LICENSE
214
215 kgb-ci-report was written during the pkg-perl sprint in Barcelona 2015.
216 L<https://wiki.debian.org/Sprints/2015/DebianPerlSprint>
217
218 Copyright (C) 2015 Damyan Ivanov L<[email protected]>
219
220 This program is free software; you can redistribute it and/or modify it under
221 the terms of the GNU General Public License as published by the Free Software
222 Foundation; either version 2, or (at your option) any later version.