annotate lib/Benzin/Bugzilla/XmlRpcClient.pm @ 9:cc7244ab1b9f

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