| 1 | #!/usr/bin/false | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Perl API for Bugzilla REST API | ||||||||||||||||||||||||||||
| 3 | # PODNAME: WebService::Bugzilla | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package WebService::Bugzilla 0.001; | ||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use URI::Escape qw( uri_escape_utf8 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | with 'WebService::Client'; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | has '+base_url' => (is => 'ro', default => sub { die 'base_url is required' }); | ||||||||||||||||||||||||||||
| 16 | has '+mode' => (is => 'ro', default => sub { 'v2' }); | ||||||||||||||||||||||||||||
| 17 | has api_key => (is => 'ro', predicate => 1); | ||||||||||||||||||||||||||||
| 18 | has allow_http => (is => 'ro', default => 0); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 21 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | my $url = $self->base_url; | |||||||||||||||||||||||||||
| 24 | 0 | $url =~ s{/+$}{}; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ($url =~ m{^http://(?!localhost\b|127\.|::1\b|\[::1\])} && !$self->allow_http) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$url =~ m(^http://(?!localhost\b|127\.|::1\b|\[::1\]))u and not $self->allow_http
MC/DC0%0/2
$url =~ m(^http://(?!localhost\b|127\.|::1\b|\[::1\]))u and not $self->allow_http
$url =~ m(^http://(?!localhost\b|127\.|::1\b|\[::1\]))u
not $self->allow_http
Truth table0%0/3
A and not(B)
A= $url =~ m(^http://(?!localhost\b|127\.|::1\b|\[::1\]))uB= not $self->allow_http
| |||||||||||||||||||||||||||||
| 27 | 0 | croak 'HTTPS required by default. ' | |||||||||||||||||||||||||||
| 28 | . 'Set allow_http => 1 to allow insecure HTTP.'; | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($url =~ m{^https?://[^/]+$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $self->{base_url} = "$url/bugzilla/rest/"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | my $ua = $self->ua; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ($self->has_api_key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | $ua->default_header('X-BUGZILLA-API-KEY' => $self->api_key); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | $ua->default_header( | ||||||||||||||||||||||||||||
| 40 | 0 | 'User-Agent' => sprintf( | |||||||||||||||||||||||||||
| 41 | 'WebService::Bugzilla %s (perl %s; %s)', | ||||||||||||||||||||||||||||
| 42 | $WebService::Bugzilla::VERSION, $^V, $^O | ||||||||||||||||||||||||||||
| 43 | ) | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | 0 | return; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Unwrap WebService::Client::Response objects, throw Exception on non-2xx, | ||||||||||||||||||||||||||||
| 49 | # and return undef for GET 404/410 (resource not found / gone). | ||||||||||||||||||||||||||||
| 50 | around req => sub { | ||||||||||||||||||||||||||||
| 51 | my ($orig, $self, $req, %args) = @_; | ||||||||||||||||||||||||||||
| 52 | my $res = $self->$orig($req, %args); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | if (!$res->ok && $req->method eq 'GET' && $res->code =~ m/^(?:404|410)$/) { | ||||||||||||||||||||||||||||
| 55 | return; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | if (!$res->ok) { | ||||||||||||||||||||||||||||
| 59 | require WebService::Bugzilla::Exception; | ||||||||||||||||||||||||||||
| 60 | my $data = eval { $res->data } // {}; | ||||||||||||||||||||||||||||
| 61 | WebService::Bugzilla::Exception->throw( | ||||||||||||||||||||||||||||
| 62 | message => ($data->{message} // $res->status_line), | ||||||||||||||||||||||||||||
| 63 | bz_code => $data->{code}, | ||||||||||||||||||||||||||||
| 64 | http_status => $res->code, | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Handle empty-body responses before calling $res->data | ||||||||||||||||||||||||||||
| 69 | return unless $res->content; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | my $data = eval { $res->data }; | ||||||||||||||||||||||||||||
| 72 | return $data if defined $data; | ||||||||||||||||||||||||||||
| 73 | return; | ||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # URL-encode all query parameter values before they reach | ||||||||||||||||||||||||||||
| 77 | # WebService::Client::get, which interpolates them raw into the URL. | ||||||||||||||||||||||||||||
| 78 | # Without this, values containing spaces, '&', '=', etc. produce | ||||||||||||||||||||||||||||
| 79 | # malformed query strings. See: | ||||||||||||||||||||||||||||
| 80 | # https://github.com/ironcamel/WebService-Client/issues/25 | ||||||||||||||||||||||||||||
| 81 | around get => sub { | ||||||||||||||||||||||||||||
| 82 | my ($orig, $self, $path, $params, @rest) = @_; | ||||||||||||||||||||||||||||
| 83 | if ($params && ref $params eq 'HASH') { | ||||||||||||||||||||||||||||
| 84 | my %encoded; | ||||||||||||||||||||||||||||
| 85 | for my $k (keys %{$params}) { | ||||||||||||||||||||||||||||
| 86 | my $v = $params->{$k}; | ||||||||||||||||||||||||||||
| 87 | if (ref $v eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 88 | $encoded{$k} = [ map { uri_escape_utf8($_) } @{$v} ]; | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | $encoded{$k} = uri_escape_utf8($v); | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | $params = \%encoded; | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | return $self->$orig($path, $params, @rest); | ||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | has 'attachment' => ( | ||||||||||||||||||||||||||||
| 100 | is => 'ro', | ||||||||||||||||||||||||||||
| 101 | lazy => 1, | ||||||||||||||||||||||||||||
| 102 | builder => sub { | ||||||||||||||||||||||||||||
| 103 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 104 | require WebService::Bugzilla::Attachment; | ||||||||||||||||||||||||||||
| 105 | return WebService::Bugzilla::Attachment->new(client => $self); | ||||||||||||||||||||||||||||
| 106 | }, | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | has 'bug' => ( | ||||||||||||||||||||||||||||
| 110 | is => 'ro', | ||||||||||||||||||||||||||||
| 111 | lazy => 1, | ||||||||||||||||||||||||||||
| 112 | builder => sub { | ||||||||||||||||||||||||||||
| 113 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 114 | require WebService::Bugzilla::Bug; | ||||||||||||||||||||||||||||
| 115 | return WebService::Bugzilla::Bug->new(client => $self); | ||||||||||||||||||||||||||||
| 116 | }, | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | has 'bug_user_last_visit' => ( | ||||||||||||||||||||||||||||
| 120 | is => 'ro', | ||||||||||||||||||||||||||||
| 121 | lazy => 1, | ||||||||||||||||||||||||||||
| 122 | builder => sub { | ||||||||||||||||||||||||||||
| 123 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 124 | require WebService::Bugzilla::BugUserLastVisit; | ||||||||||||||||||||||||||||
| 125 | return WebService::Bugzilla::BugUserLastVisit->new(client => $self); | ||||||||||||||||||||||||||||
| 126 | }, | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | has 'classification' => ( | ||||||||||||||||||||||||||||
| 130 | is => 'ro', | ||||||||||||||||||||||||||||
| 131 | lazy => 1, | ||||||||||||||||||||||||||||
| 132 | builder => sub { | ||||||||||||||||||||||||||||
| 133 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 134 | require WebService::Bugzilla::Classification; | ||||||||||||||||||||||||||||
| 135 | return WebService::Bugzilla::Classification->new(client => $self); | ||||||||||||||||||||||||||||
| 136 | }, | ||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | has 'comment' => ( | ||||||||||||||||||||||||||||
| 140 | is => 'ro', | ||||||||||||||||||||||||||||
| 141 | lazy => 1, | ||||||||||||||||||||||||||||
| 142 | builder => sub { | ||||||||||||||||||||||||||||
| 143 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 144 | require WebService::Bugzilla::Comment; | ||||||||||||||||||||||||||||
| 145 | return WebService::Bugzilla::Comment->new(client => $self); | ||||||||||||||||||||||||||||
| 146 | }, | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | has 'component' => ( | ||||||||||||||||||||||||||||
| 150 | is => 'ro', | ||||||||||||||||||||||||||||
| 151 | lazy => 1, | ||||||||||||||||||||||||||||
| 152 | builder => sub { | ||||||||||||||||||||||||||||
| 153 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 154 | require WebService::Bugzilla::Component; | ||||||||||||||||||||||||||||
| 155 | return WebService::Bugzilla::Component->new(client => $self); | ||||||||||||||||||||||||||||
| 156 | }, | ||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | has 'field' => ( | ||||||||||||||||||||||||||||
| 160 | is => 'ro', | ||||||||||||||||||||||||||||
| 161 | lazy => 1, | ||||||||||||||||||||||||||||
| 162 | builder => sub { | ||||||||||||||||||||||||||||
| 163 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 164 | require WebService::Bugzilla::Field; | ||||||||||||||||||||||||||||
| 165 | return WebService::Bugzilla::Field->new(client => $self); | ||||||||||||||||||||||||||||
| 166 | }, | ||||||||||||||||||||||||||||
| 167 | ); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | has 'flag_activity' => ( | ||||||||||||||||||||||||||||
| 170 | is => 'ro', | ||||||||||||||||||||||||||||
| 171 | lazy => 1, | ||||||||||||||||||||||||||||
| 172 | builder => sub { | ||||||||||||||||||||||||||||
| 173 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 174 | require WebService::Bugzilla::FlagActivity; | ||||||||||||||||||||||||||||
| 175 | return WebService::Bugzilla::FlagActivity->new(client => $self); | ||||||||||||||||||||||||||||
| 176 | }, | ||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | has 'github' => ( | ||||||||||||||||||||||||||||
| 180 | is => 'ro', | ||||||||||||||||||||||||||||
| 181 | lazy => 1, | ||||||||||||||||||||||||||||
| 182 | builder => sub { | ||||||||||||||||||||||||||||
| 183 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 184 | require WebService::Bugzilla::GitHub; | ||||||||||||||||||||||||||||
| 185 | return WebService::Bugzilla::GitHub->new(client => $self); | ||||||||||||||||||||||||||||
| 186 | }, | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | has 'group' => ( | ||||||||||||||||||||||||||||
| 190 | is => 'ro', | ||||||||||||||||||||||||||||
| 191 | lazy => 1, | ||||||||||||||||||||||||||||
| 192 | builder => sub { | ||||||||||||||||||||||||||||
| 193 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 194 | require WebService::Bugzilla::Group; | ||||||||||||||||||||||||||||
| 195 | return WebService::Bugzilla::Group->new(client => $self); | ||||||||||||||||||||||||||||
| 196 | }, | ||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | has 'information' => ( | ||||||||||||||||||||||||||||
| 200 | is => 'ro', | ||||||||||||||||||||||||||||
| 201 | lazy => 1, | ||||||||||||||||||||||||||||
| 202 | builder => sub { | ||||||||||||||||||||||||||||
| 203 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 204 | require WebService::Bugzilla::Information; | ||||||||||||||||||||||||||||
| 205 | return WebService::Bugzilla::Information->new(client => $self); | ||||||||||||||||||||||||||||
| 206 | }, | ||||||||||||||||||||||||||||
| 207 | ); | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | has 'product' => ( | ||||||||||||||||||||||||||||
| 210 | is => 'ro', | ||||||||||||||||||||||||||||
| 211 | lazy => 1, | ||||||||||||||||||||||||||||
| 212 | builder => sub { | ||||||||||||||||||||||||||||
| 213 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 214 | require WebService::Bugzilla::Product; | ||||||||||||||||||||||||||||
| 215 | return WebService::Bugzilla::Product->new(client => $self); | ||||||||||||||||||||||||||||
| 216 | }, | ||||||||||||||||||||||||||||
| 217 | ); | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | has 'reminder' => ( | ||||||||||||||||||||||||||||
| 220 | is => 'ro', | ||||||||||||||||||||||||||||
| 221 | lazy => 1, | ||||||||||||||||||||||||||||
| 222 | builder => sub { | ||||||||||||||||||||||||||||
| 223 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 224 | require WebService::Bugzilla::Reminder; | ||||||||||||||||||||||||||||
| 225 | return WebService::Bugzilla::Reminder->new(client => $self); | ||||||||||||||||||||||||||||
| 226 | }, | ||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | has 'user' => ( | ||||||||||||||||||||||||||||
| 230 | is => 'ro', | ||||||||||||||||||||||||||||
| 231 | lazy => 1, | ||||||||||||||||||||||||||||
| 232 | builder => sub { | ||||||||||||||||||||||||||||
| 233 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 234 | require WebService::Bugzilla::User; | ||||||||||||||||||||||||||||
| 235 | return WebService::Bugzilla::User->new(client => $self); | ||||||||||||||||||||||||||||
| 236 | }, | ||||||||||||||||||||||||||||
| 237 | ); | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 1; | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | __END__ | ||||||||||||||||||||||||||||