blib/lib/Youri/BTS/Bugzilla.pm

filter: Statementstmt 10.9% 15 / 137 Branchbran 0.0% 0 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 15.3% 4 / 26 Podpod 100% 13 / 13 totaltotal 13.9% 32 / 230 CC 29 SCAR 22.8
CC
29
Cov
8%
add_package
30.0
browse_packages
20.0
_get_single
12.0
CRAP 9.7
↑ Summary
1 # $Id: /mirror/youri/soft/BTS/trunk/lib/Youri/BTS/Bugzilla.pm 2347 2007-04-10T14:55:21.041566Z guillomovitch $
2 package Youri::BTS::Bugzilla;
3  
4 =head1 NAME
5  
6 Youri::BTS::Bugzilla - Youri Bugzilla interface
7  
8 =head1 SYNOPSIS
9  
10 use Youri::BTS::Bugzilla;
11  
12 my $bugzilla = Youri::BTS::Bugzilla->new();
13  
14 print $bugzilla->get_maintainer('foobar');
15  
16 =head1 DESCRIPTION
17  
18 This module implement a Bugzilla interface for managing packages.
19  
20 The legacy Bugzilla database model is mapped this way:
21  
22 =over
23  
24 =item *
25  
26 a maintainer is a user
27  
28 =item *
29  
30 a package is a product
31  
32 =item *
33  
34 each package has two pseudo components "program" and "package", owned by the
35 package maintainer
36  
37 =back
38  
39 =cut
40  
41 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use version; our $VERSION = qv('0.1.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 my %queries = (
47 get_package_id => 'SELECT id FROM products WHERE name = ?',
48 get_maintainer_id => 'SELECT userid FROM profiles WHERE login_name = ?',
49 get_versions => 'SELECT value FROM versions WHERE product_id = ?',
50 get_components => 'SELECT name FROM components WHERE product_id = ?',
51 add_package => 'INSERT INTO products (name, description, defaultmilestone) VALUES (?, ?, ?)',
52 add_component => 'INSERT INTO components (product_id, name, description,initialowner, initialqacontact) VALUES (?, ?, ?, ?, ?)',
53 add_version => 'INSERT INTO versions (product_id, value) VALUES (?, ?)',
54 add_milestone => 'INSERT INTO milestones (product_id, value) VALUES (?, ?)',
55 del_package => 'DELETE FROM products WHERE product = ?',
56 del_maintainer => 'DELETE FROM profiles WHERE login_name = ?',
57 del_components => 'DELETE FROM components WHERE program = ?',
58 del_versions => 'DELETE FROM versions WHERE program = ?',
59 browse_packages => <<EOF,
60 SELECT products.name, max(versions.value), login_name
61 FROM products, versions, profiles, components
62 WHERE versions.product_id = products.id
63 AND components.product_id = products.id
64 AND profiles.userid = components.initialowner
65 AND components.name = 'package'
66 GROUP BY name
67 EOF
68 get_maintainer => <<EOF
69 SELECT login_name
70 FROM profiles, components, products
71 WHERE profiles.userid = components.initialowner
72 AND components.name = 'package'
73 AND components.product_id = products.id
74 AND products.name = ?
75 EOF
76 );
77  
78 =head1 CLASS METHODS
79  
80 Except stated otherwise, maintainers are specified by their login, and packages
81 are specified by their name.
82  
83 =head2 new(%args)
84  
85 Creates a new Youri::Bugzilla object.
86  
87 Parameters:
88  
89 =over
90  
91 =item lib $lib
92  
93 Bugzilla library directory (default: /usr/share/bugzilla/lib).
94  
95 =item project $project
96  
97 Bugzilla project.
98  
99 =back
100  
101 =cut
102  
103 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
104 0 my $class = shift;
105 0 my %options = (
106 lib => '/usr/share/buzilla/lib',
107 project => undef,
108 @_
109 );
110  
111 # protect ENV against bugzilla cleanup
112 0 local %ENV = %ENV;
113  
114 0 ▶ $ENV{PROJECT} = $options{project} if $options{project};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"project"} 0 0 0
115 0 push(@INC, $options{lib});
116
117 # require main module for configuration
118 0 require Bugzilla;
119  
120 # require database module to shortcut caching in main module
121 0 require Bugzilla::DB;
122
123 0 my $self = bless {
124 _dbh => Bugzilla::DB::connect_main()
125 }, $class;
126  
127 0 return $self;
128 }
129  
130 =head1 INSTANCE METHODS
131  
132 =head2 has_package($package)
133  
134 Return true if bugzilla contains given package.
135  
136 =cut
137  
138 0 ▶sub has_package {
Subroutine has_package: not called CC=2 SCAR=17.9
Pod documented
139 0 my ($self, $package) = @_;
140 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
141 0 return $self->_get_package_id($package);
142 }
143  
144 =head2 has_maintainer($maintainer)
145  
146 Return true if bugzilla contains given maintainer.
147  
148 =cut
149  
150 0 ▶sub has_maintainer {
Subroutine has_maintainer: not called CC=2 SCAR=17.9
Pod documented
151 0 my ($self, $maintainer) = @_;
152 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
153 0 return $self->_get_maintainer_id($maintainer);
154 }
155  
156 =head2 get_maintainer($package)
157  
158 Return maintainer of given package.
159  
160 =cut
161  
162 0 ▶sub get_maintainer {
Subroutine get_maintainer: not called CC=2 SCAR=17.9
Pod documented
163 0 my ($self, $package) = @_;
164 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
165 0 return $self->_get_single('get_maintainer', $package);
166 }
167  
168 =head2 get_versions($package)
169  
170 Return versions from given package.
171  
172 =cut
173  
174 0 ▶sub get_versions {
Subroutine get_versions: not called CC=2 SCAR=17.9
Pod documented
175 0 my ($self, $package) = @_;
176 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
177 0 return $self->_get_multiple(
178 'get_versions',
179 $self->_get_package_id($package)
180 );
181 }
182  
183 =head2 get_components($package)
184  
185 Return components from given package.
186  
187 =cut
188  
189 0 ▶sub get_components {
Subroutine get_components: not called CC=2 SCAR=17.9
Pod documented
190 0 my ($self, $package) = @_;
191 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
192 0 return $self->_get_multiple(
193 'get_components',
194 $self->_get_package_id($package)
195 );
196 }
197  
198 =head2 get_packages()
199  
200 Return all packages from the database.
201  
202 =cut
203  
204 0 ▶sub get_packages {
Subroutine get_packages: not called CC=2 SCAR=17.9
Pod documented
205 0 my ($self) = @_;
206 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
207 0 return $self->_get_multiple('get_packages');
208 }
209  
210 0 ▶sub _get_package_id {
Subroutine _get_package_id: not called CC=1 SCAR=6.9
211 0 my ($self, $package) = @_;
212 0 return $self->_get_single('get_package_id', $package);
213 }
214  
215 0 ▶sub _get_maintainer_id {
Subroutine _get_maintainer_id: not called CC=1 SCAR=6.9
216 0 my ($self, $maintainer) = @_;
217 0 return $self->_get_single('get_maintainer_id', $maintainer);
218 }
219  
220 0 ▶sub _get_single {
Subroutine _get_single: not called CC=3 SCAR=24.8
221 0 my ($self, $type, $value) = @_;
222  
223 0 my $query = $self->{_queries}->{$type};
224 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
225 0 $query = $self->{_dbh}->prepare($queries{$type});
226 0 $self->{_queries}->{$type} = $query;
227 }
228  
229 0 $query->execute($value);
230  
231 0 my @row = $query->fetchrow_array();
232 0 ▶ return @row ? $row[0]: undef;
Branch: 1 branch
BranchTrueFalseTotal
@row ? : 0 0 0
233 }
234  
235 0 ▶sub _get_multiple {
Subroutine _get_multiple: not called CC=3 SCAR=24.8
236 0 my ($self, $type, $value) = @_;
237  
238 0 my $query = $self->{_queries}->{$type};
239 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
240 0 $query = $self->{_dbh}->prepare($queries{$type});
241 0 $self->{_queries}->{$type} = $query;
242 }
243  
244 0 $query->execute($value);
245  
246 0 my @results;
247 0 ▶ while (my @row = $query->fetchrow_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $query->fetchrow_array) 0 0 0
248 0 push @results, $row[0];
249 }
250 0 return @results;
251 }
252  
253 =head2 add_package($name, $summary, $version, $maintainer, $contact)
254  
255 Adds a new package in the database, with given name, summary, version,
256 maintainer and initial QA contact.
257  
258 =cut
259  
260 0 ▶sub add_package {
Subroutine add_package: not called CC=5 SCAR=34.0
Pod documented
261 0 my ($self, $name, $summary, $version, $maintainer, $contact) = @_;
262 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
263  
264 0 my $maintainer_id = $self->_get_maintainer_id($maintainer);
265 0 ▶ unless ($maintainer_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($maintainer_id) 0 0 0
266 0 carp "Unknown maintainer $maintainer, aborting";
267 0 return;
268 }
269  
270 0 my $contact_id = $self->_get_maintainer_id($contact);
271 0 ▶ unless ($contact_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($contact_id) 0 0 0
272 0 carp "Unknown QA contact $contact, aborting";
273 0 return;
274 }
275  
276 0 my $milestone = '---';
277  
278 0 my $query = $self->{_queries}->{add_package};
279 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
280 0 $query = $self->{_dbh}->prepare($queries{add_package});
281 0 $self->{_queries}->{add_package} = $query;
282 }
283  
284 0 $query->execute($name, $summary, $milestone);
285  
286 0 my $package_id = $self->_get_package_id($name);
287  
288 0 $self->_add_version($package_id, $version);
289 0 $self->_add_milestone($package_id, $milestone);
290 0 $self->_add_component(
291 $package_id,
292 'package',
293 'problem related to the package',
294 $maintainer_id,
295 $contact_id
296 );
297 0 $self->_add_component(
298 $package_id,
299 'program',
300 'problem related to the program',
301 $maintainer_id,
302 $contact_id
303 );
304 }
305  
306 =head2 add_version($package, $version)
307  
308 Adds a new version to given package.
309  
310 =cut
311  
312 0 ▶sub add_version {
Subroutine add_version: not called CC=2 SCAR=17.9
Pod documented
313 0 my ($self, $package, $version) = @_;
314 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
315  
316 0 my $package_id = $self->_get_package_id($package);
317 0 $self->_add_version($package_id, $version);
318 }
319  
320 0 ▶sub _add_version {
Subroutine _add_version: not called CC=2 SCAR=17.9
321 0 my ($self, $package_id, $version) = @_;
322  
323 0 my $query = $self->{_queries}->{add_version};
324 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
325 0 $query = $self->{_dbh}->prepare($queries{add_version});
326 0 $self->{_queries}->{add_version} = $query;
327 }
328  
329 0 $query->execute($package_id, $version);
330 }
331  
332 0 ▶sub _add_milestone {
Subroutine _add_milestone: not called CC=2 SCAR=17.9
333 0 my ($self, $package_id, $milestone) = @_;
334  
335 0 my $query = $self->{_queries}->{add_milestone};
336 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
337 0 $query = $self->{_dbh}->prepare($queries{add_milestone});
338 0 $self->{_queries}->{add_milestone} = $query;
339 }
340  
341 0 $query->execute($package_id, $milestone);
342 }
343  
344  
345 =head2 add_maintainer($name, $login)
346  
347 Adds a new maintainer in the database, with given name, login and password.
348  
349 =cut
350  
351 0 ▶sub add_maintainer {
Subroutine add_maintainer: not called CC=2 SCAR=17.9
Pod documented
352 0 my ($self, $name, $login) = @_;
353 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
354  
355 0 Bugzilla::User->create({
356 login_name => $login,
357 realname => $name,
358 cryptpassword => '*'
359 });
360 }
361  
362 0 ▶sub _add_component {
Subroutine _add_component: not called CC=2 SCAR=17.9
363 0 my ($self, $package_id, $name, $description, $maintainer_id, $contact_id) = @_;
364  
365 0 my $query = $self->{_queries}->{add_component};
366 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
367 0 $query = $self->{_dbh}->prepare($queries{add_component});
368 0 $self->{_queries}->{add_component} = $query;
369 }
370  
371 0 $query->execute($package_id, $name, $description, $maintainer_id, $contact_id);
372 }
373  
374 =head2 del_package($package)
375  
376 Delete given package from database.
377  
378 =cut
379  
380 0 ▶sub del_package {
Subroutine del_package: not called CC=2 SCAR=17.9
Pod documented
381 0 my ($self, $package) = @_;
382 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
383 0 $self->_delete('del_package', $package);
384 0 $self->_delete('del_versions', $package);
385 0 $self->_delete('del_components', $package);
386 }
387  
388 =head2 del_maintainer($maintainer)
389  
390 Delete given maintainer from database.
391  
392 =cut
393  
394 0 ▶sub del_maintainer {
Subroutine del_maintainer: not called CC=2 SCAR=17.9
Pod documented
395 0 my ($self, $maintainer) = @_;
396 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
397 0 $self->_delete('del_maintainer', $maintainer);
398 }
399  
400 0 ▶sub _delete {
Subroutine _delete: not called CC=2 SCAR=17.9
401 0 my ($self, $type, $value) = @_;
402  
403 0 my $query = $self->{_queries}->{$type};
404 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
405 0 $query = $self->{_dbh}->prepare($queries{$type});
406 0 $self->{_queries}->{$type} = $query;
407 }
408  
409 0 $query->execute($value);
410 }
411  
412 =head2 browse_packages($callback)
413  
414 Browse all packages from bugzilla, and execute given callback with name and
415 maintainer as argument for each of them.
416  
417 =cut
418  
419 0 ▶sub browse_packages {
Subroutine browse_packages: not called CC=4 SCAR=30.0
Pod documented
420 0 my ($self, $callback) = @_;
421 0 ▶ croak "Not a class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
422  
423 0 my $query = $self->{_queries}->{browse_packages};
424 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
425 0 $query = $self->{_dbh}->prepare($queries{browse_packages});
426 0 $self->{_queries}->{browse_packages} = $query;
427 }
428  
429 0 $query->execute();
430  
431 0 ▶ while (my @row = $query->fetchrow_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $query->fetchrow_array) 0 0 0
432 0 $callback->(@row);
433 }
434 }
435  
436 # close database connection
437 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
438 0 my ($self) = @_;
439  
440 0 foreach my $query (values %{$self->{_queries}}) {
441 0 ▶ $query->finish() if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 0 0 0
442 }
443 }
444  
445 =head1 COPYRIGHT AND LICENSE
446  
447 Copyright (C) 2002-2006, YOURI project
448  
449 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
450  
451 =cut
452  
453 1 1;
↑ Summary