7
|
1 package Benzin::Bugzilla::XmlRpcClient;
|
|
2 use strict;
|
|
3
|
|
4 use LWP::UserAgent;
|
|
5 use XMLRPC::Lite;
|
|
6 use YAML::XS qw(Dump);
|
|
7
|
9
|
8 use IMPL::declare {
|
|
9 require => {
|
|
10 Bug => 'Benzin::Bugzilla::Bug',
|
|
11 BugComment => 'Benzin::Bugzilla::BugComment'
|
|
12 },
|
|
13 base => { 'IMPL::Object::Fields' => undef }
|
7
|
14 };
|
|
15
|
|
16 use fields qw(url apikey);
|
|
17
|
|
18 sub new {
|
|
19 my $class = shift;
|
|
20 $class = ref $class || $class;
|
|
21
|
|
22 my $inst = fields::new($class);
|
|
23 $inst->CTOR(@_);
|
|
24
|
|
25 return $inst;
|
|
26 }
|
|
27
|
|
28 sub CTOR {
|
|
29 my SELF $this = shift;
|
|
30 my %params = @_;
|
|
31
|
|
32 $this->{url} = $params{url} or die "An url is required";
|
|
33 $this->{apikey} = $params{apikey} if $params{apikey};
|
|
34 }
|
|
35
|
|
36 sub GetBugs {
|
|
37 my SELF $this = shift;
|
|
38
|
9
|
39 return [
|
|
40 map Bug->new($_),
|
|
41 @{ $this->_CallService( 'Bug.get', shift )->{bugs} || [] }
|
|
42 ];
|
7
|
43 }
|
|
44
|
9
|
45 sub PopulateBugsWithComments {
|
7
|
46 my SELF $this = shift;
|
|
47 my $bugs = shift || [];
|
|
48
|
|
49 if ( my @ids = map $_->{id}, @$bugs ) {
|
|
50
|
9
|
51 my $resp = $this->_CallService( 'Bug.comments', { ids => \@ids } );
|
|
52
|
7
|
53 for my Bug $bug (@$bugs) {
|
9
|
54 $bug->{comments} = [
|
|
55 map BugComment->new($_),
|
|
56 @{ $resp->{bugs}{ $bug->{id} }->{comments} || [] }
|
|
57 ];
|
7
|
58 }
|
|
59 }
|
9
|
60 return;
|
|
61 }
|
|
62
|
|
63 sub PopulateBugsHistory {
|
|
64 my SELF $this = shift;
|
|
65
|
|
66 my %bugs = map { $_->{id}, $_ } @{ shift || [] };
|
|
67
|
|
68 if ( keys %bugs ) {
|
|
69
|
|
70 my $resp =
|
|
71 $this->_CallService( 'Bug.history', { ids => [ keys %bugs ] } )->{bugs};
|
|
72
|
|
73 for my $data (@$resp) {
|
|
74 my Bug $bug = $bugs{$data->{id}};
|
|
75
|
|
76 $bug->{history} = $data->{history};
|
|
77 }
|
|
78 }
|
|
79 return;
|
7
|
80 }
|
|
81
|
|
82 sub _CallService {
|
|
83 my SELF $this = shift;
|
|
84 my ( $method, $params ) = @_;
|
|
85
|
|
86 die "Method must be specified" unless $method;
|
|
87 $params ||= {};
|
|
88
|
|
89 $params->{api_key} = $this->{apikey};
|
|
90 my $url = URI->new_abs( 'xmlrpc.cgi', $this->{url} );
|
|
91
|
|
92 my $result = XMLRPC::Lite->proxy($url)->call( $method, $params );
|
|
93
|
|
94 die $result->fault if $result->fault;
|
|
95 return $result->result;
|
|
96 }
|
|
97
|
|
98 1;
|