line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::BitbucketServer; |
2
|
|
|
|
|
|
|
# ABSTRACT: Bindings for Bitbucket Server REST APIs |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
53052
|
use warnings; |
|
4
|
|
|
|
|
27
|
|
|
4
|
|
|
|
|
100
|
|
6
|
4
|
|
|
4
|
|
18
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
125
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.605'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
1503
|
use HTTP::AnyUA::Util qw(www_form_urlencode); |
|
4
|
|
|
|
|
9945
|
|
|
4
|
|
|
|
|
199
|
|
11
|
4
|
|
|
4
|
|
1468
|
use HTTP::AnyUA; |
|
4
|
|
|
|
|
61663
|
|
|
4
|
|
|
|
|
91
|
|
12
|
4
|
|
|
4
|
|
1348
|
use Module::Load qw(load); |
|
4
|
|
|
|
|
3250
|
|
|
4
|
|
|
|
|
20
|
|
13
|
4
|
|
|
4
|
|
179
|
use Scalar::Util qw(weaken); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
185
|
|
14
|
4
|
|
|
4
|
|
1568
|
use Types::Standard qw(Bool Object Str); |
|
4
|
|
|
|
|
223214
|
|
|
4
|
|
|
|
|
29
|
|
15
|
4
|
|
|
4
|
|
4388
|
use WebService::BitbucketServer::Response; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
122
|
|
16
|
4
|
|
|
4
|
|
1481
|
use WebService::BitbucketServer::Spec qw(api_info documentation_url); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
176
|
|
17
|
|
|
|
|
|
|
|
18
|
4
|
|
|
4
|
|
19
|
use Moo; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
18
|
|
19
|
4
|
|
|
4
|
|
947
|
use namespace::clean; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
12
|
|
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
0
|
|
0
|
sub _croak { require Carp; Carp::croak(@_) } |
|
0
|
|
|
|
|
0
|
|
22
|
0
|
|
|
0
|
|
0
|
sub _usage { _croak("Usage: @_\n") } |
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
|
0
|
|
0
|
sub _debug_log { print STDERR join(' ', @_), "\n" if $ENV{PERL_WEBSERVICE_BITBUCKETSERVER_DEBUG} } |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has base_url => ( |
28
|
|
|
|
|
|
|
is => 'ro', |
29
|
|
|
|
|
|
|
isa => Str, |
30
|
|
|
|
|
|
|
required => 1, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has path => ( |
35
|
|
|
|
|
|
|
is => 'lazy', |
36
|
|
|
|
|
|
|
isa => Str, |
37
|
|
|
|
|
|
|
default => 'rest', |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has [qw(username password)] => ( |
42
|
|
|
|
|
|
|
is => 'ro', |
43
|
|
|
|
|
|
|
isa => Str, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has ua => ( |
48
|
|
|
|
|
|
|
is => 'lazy', |
49
|
|
|
|
|
|
|
default => sub { |
50
|
|
|
|
|
|
|
load HTTP::Tiny; |
51
|
|
|
|
|
|
|
HTTP::Tiny->new( |
52
|
|
|
|
|
|
|
agent => "perl-webservice-bitbucketserver/$VERSION", |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
}, |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
has any_ua => ( |
59
|
|
|
|
|
|
|
is => 'lazy', |
60
|
|
|
|
|
|
|
isa => Object, |
61
|
|
|
|
|
|
|
default => sub { |
62
|
|
|
|
|
|
|
my $self = shift; |
63
|
|
|
|
|
|
|
HTTP::AnyUA->new(ua => $self->ua); |
64
|
|
|
|
|
|
|
}, |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
has json => ( |
69
|
|
|
|
|
|
|
is => 'lazy', |
70
|
|
|
|
|
|
|
isa => Object, |
71
|
|
|
|
|
|
|
default => sub { |
72
|
|
|
|
|
|
|
load JSON::MaybeXS; |
73
|
|
|
|
|
|
|
JSON::MaybeXS->new(utf8 => 1); |
74
|
|
|
|
|
|
|
}, |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
has no_security_warning => ( |
79
|
|
|
|
|
|
|
is => 'rwp', |
80
|
|
|
|
|
|
|
isa => Bool, |
81
|
|
|
|
|
|
|
lazy => 1, |
82
|
|
|
|
|
|
|
default => sub { $ENV{PERL_WEBSERVICE_BITBUCKETSERVER_NO_SECURITY_WARNING} || 0 }, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my %api_accessors; |
87
|
|
|
|
|
|
|
while (my ($namespace, $api) = each %WebService::BitbucketServer::Spec::API) { |
88
|
|
|
|
|
|
|
my $method = $api->{id}; |
89
|
|
|
|
|
|
|
my $package = __PACKAGE__ . '::' . $api->{package}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
next if $api_accessors{$method}; |
92
|
|
|
|
|
|
|
$api_accessors{$method} = 1; |
93
|
|
|
|
|
|
|
|
94
|
4
|
|
|
4
|
|
2106
|
no strict 'refs'; ## no critic ProhibitNoStrict |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
4707
|
|
95
|
|
|
|
|
|
|
*{__PACKAGE__."::${method}"} = sub { |
96
|
1
|
|
|
1
|
|
1573
|
my $self = shift; |
97
|
1
|
50
|
|
|
|
4
|
return $self->{$method} if defined $self->{$method}; |
98
|
1
|
|
|
|
|
6
|
load $package; |
99
|
1
|
|
|
|
|
11
|
my $api = $package->new(context => $self); |
100
|
1
|
|
|
|
|
11
|
$self->{$method} = $api; |
101
|
1
|
|
|
|
|
4
|
weaken($self->{$method}); |
102
|
1
|
|
|
|
|
4
|
return $api; |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub url { |
108
|
4
|
|
|
4
|
1
|
6
|
my $self = shift; |
109
|
4
|
|
|
|
|
15
|
my $base = $self->base_url; |
110
|
4
|
|
|
|
|
68
|
my $path = $self->path; |
111
|
4
|
|
|
|
|
111
|
$base =~ s!/+$!!; |
112
|
4
|
|
|
|
|
8
|
$path =~ s!^/+!!; |
113
|
4
|
|
|
|
|
13
|
return "$base/$path"; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub call { |
118
|
4
|
|
|
4
|
1
|
3279
|
my $self = shift; |
119
|
4
|
50
|
33
|
|
|
28
|
(@_ == 1 && ref($_[0]) eq 'HASH') || @_ % 2 == 0 |
|
|
|
33
|
|
|
|
|
120
|
|
|
|
|
|
|
or _usage(q{$api->call(method => $method, url => $url, %options)}); |
121
|
4
|
50
|
|
|
|
15
|
my $args = @_ == 1 ? shift : {@_}; |
122
|
|
|
|
|
|
|
|
123
|
4
|
50
|
|
|
|
13
|
$args->{url} or _croak("url is required\n"); |
124
|
|
|
|
|
|
|
|
125
|
4
|
|
50
|
|
|
13
|
my $method = $args->{method} || 'GET'; |
126
|
4
|
|
|
|
|
12
|
my $url = join('/', $self->url, $args->{url}); |
127
|
|
|
|
|
|
|
|
128
|
4
|
|
|
|
|
8
|
my %options; |
129
|
4
|
|
|
|
|
9
|
$options{headers}{Accept} = '*/*;q=0.2,application/json'; # prefer json response |
130
|
|
|
|
|
|
|
|
131
|
4
|
|
|
|
|
19
|
$self->_call_add_authorization($args, \%options); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# request body |
134
|
4
|
|
|
|
|
8
|
my $data = $args->{data}; |
135
|
4
|
|
50
|
|
|
16
|
my $data_type = $args->{data_type} || 'application/json'; |
136
|
4
|
50
|
|
|
|
9
|
if ($data) { |
137
|
0
|
0
|
0
|
|
|
0
|
if ($method eq 'GET' || $method eq 'HEAD') { |
138
|
0
|
0
|
|
|
|
0
|
my $params = ref($data) ? www_form_urlencode($data) : $data; |
139
|
0
|
0
|
|
|
|
0
|
my $sep = $url =~ /\?/ ? '&' : '?'; |
140
|
0
|
|
|
|
|
0
|
$url .= "${sep}${params}"; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
else { |
143
|
0
|
0
|
0
|
|
|
0
|
if ($data_type eq 'application/json' && ref($data)) { |
144
|
0
|
|
|
|
|
0
|
$data = $self->json->encode($data); |
145
|
|
|
|
|
|
|
} |
146
|
0
|
|
|
|
|
0
|
$options{content} = $data; |
147
|
0
|
|
|
|
|
0
|
$options{headers}{'content-type'} = $data_type; |
148
|
0
|
|
|
|
|
0
|
$options{headers}{'content-length'} = length $data; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
my $handle_response = sub { |
153
|
4
|
|
|
4
|
|
8
|
my $resp = shift; |
154
|
|
|
|
|
|
|
|
155
|
4
|
50
|
|
|
|
19
|
return $resp if $args->{raw}; |
156
|
|
|
|
|
|
|
|
157
|
4
|
|
|
|
|
58
|
return WebService::BitbucketServer::Response->new( |
158
|
|
|
|
|
|
|
context => $self, |
159
|
|
|
|
|
|
|
request_args => $args, |
160
|
|
|
|
|
|
|
raw => $resp, |
161
|
|
|
|
|
|
|
json => $self->json, |
162
|
|
|
|
|
|
|
); |
163
|
4
|
|
|
|
|
16
|
}; |
164
|
|
|
|
|
|
|
|
165
|
4
|
|
|
|
|
88
|
my $resp = $self->any_ua->request($method, $url, \%options); |
166
|
|
|
|
|
|
|
|
167
|
4
|
50
|
|
|
|
224
|
if ($self->any_ua->response_is_future) { |
168
|
0
|
|
|
|
|
0
|
return $resp->transform( |
169
|
|
|
|
|
|
|
done => $handle_response, |
170
|
|
|
|
|
|
|
fail => $handle_response, |
171
|
|
|
|
|
|
|
); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
else { |
174
|
4
|
|
|
|
|
50
|
return $handle_response->($resp); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# add the authorization header to request options |
179
|
|
|
|
|
|
|
sub _call_add_authorization { |
180
|
4
|
|
|
4
|
|
7
|
my $self = shift; |
181
|
4
|
|
|
|
|
4
|
my $args = shift; |
182
|
4
|
|
|
|
|
8
|
my $opts = shift; |
183
|
|
|
|
|
|
|
|
184
|
4
|
50
|
33
|
|
|
23
|
if ($self->username && $self->password) { |
185
|
4
|
|
|
|
|
9
|
my $url = $self->base_url; |
186
|
4
|
50
|
33
|
|
|
70
|
if (!$self->no_security_warning && $url !~ /^https/) { |
187
|
0
|
|
|
|
|
0
|
warn "Bitbucket Server authorization is being transferred unencrypted to $url !!!\n"; |
188
|
0
|
|
|
|
|
0
|
$self->_set_no_security_warning(0); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
4
|
|
|
|
|
115
|
my $payload = $self->username . ':' . $self->password; |
192
|
4
|
|
|
|
|
1047
|
require MIME::Base64; |
193
|
4
|
|
|
|
|
1431
|
my $auth_token = MIME::Base64::encode_base64($payload, ''); |
194
|
4
|
|
|
|
|
17
|
$opts->{headers}{'authorization'} = "Basic $auth_token"; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub write_api_packages { |
200
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
201
|
0
|
0
|
0
|
|
|
|
(@_ == 1 && ref($_[0]) eq 'HASH') || @_ % 2 == 0 |
|
|
|
0
|
|
|
|
|
202
|
|
|
|
|
|
|
or _usage(q{$api->write_api_packages(%args)}); |
203
|
0
|
0
|
|
|
|
|
my $args = @_ == 1 ? shift : {@_}; |
204
|
|
|
|
|
|
|
|
205
|
0
|
0
|
|
|
|
|
$self = __PACKAGE__->new(base_url => '') unless ref $self; |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
require WebService::BitbucketServer::WADL; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
my $handle_response = sub { |
210
|
0
|
|
|
0
|
|
|
my $resp = shift; |
211
|
|
|
|
|
|
|
|
212
|
0
|
0
|
|
|
|
|
if (!$resp->{success}) { |
213
|
0
|
|
|
|
|
|
warn "Failed to fetch $resp->{url} - $resp->{status} $resp->{reason}\n"; |
214
|
0
|
|
|
|
|
|
return; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
$self->_debug_log('Fetched WADL', $resp->{url}); |
218
|
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
|
my $wadl = WebService::BitbucketServer::WADL::parse_wadl($resp->{content}); |
220
|
|
|
|
|
|
|
|
221
|
0
|
|
|
|
|
|
my $api_info = api_info($wadl); |
222
|
0
|
0
|
|
|
|
|
if (!$api_info) { |
223
|
0
|
|
|
|
|
|
warn "Missing API info: $resp->{url}\n"; |
224
|
0
|
|
|
|
|
|
return; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
my ($package_code, $package) = WebService::BitbucketServer::WADL::generate_package($wadl, %$args, base => __PACKAGE__); |
228
|
|
|
|
|
|
|
|
229
|
0
|
|
|
|
|
|
require File::Path; |
230
|
0
|
|
|
|
|
|
require File::Spec; |
231
|
|
|
|
|
|
|
|
232
|
0
|
0
|
|
|
|
|
my @pm = ($args->{dir} ? $args->{dir} : (), _mod_to_pm($package)); |
233
|
0
|
|
|
|
|
|
my $pm = File::Spec->catfile(@pm); |
234
|
0
|
|
|
|
|
|
my $dir = File::Spec->catdir(@pm[0 .. (scalar @pm - 2)]); |
235
|
|
|
|
|
|
|
|
236
|
0
|
|
|
|
|
|
File::Path::make_path($dir); |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
# write the pm |
239
|
0
|
0
|
|
|
|
|
open(my $fh, '>', $pm) or die "open failed ($pm): $!"; |
240
|
0
|
|
|
|
|
|
print $fh $package_code; |
241
|
0
|
|
|
|
|
|
close($fh); |
242
|
|
|
|
|
|
|
|
243
|
0
|
|
|
|
|
|
my $submap = WebService::BitbucketServer::WADL::generate_submap($wadl, %$args); |
244
|
|
|
|
|
|
|
|
245
|
0
|
|
|
|
|
|
my $filename = "submap_$api_info->{id}.pl"; |
246
|
|
|
|
|
|
|
|
247
|
0
|
|
|
|
|
|
my $filepath = File::Spec->catfile(qw{shares spec}, $filename); |
248
|
0
|
|
|
|
|
|
$dir = File::Spec->catdir(qw{shares spec}); |
249
|
|
|
|
|
|
|
|
250
|
0
|
|
|
|
|
|
File::Path::make_path($dir); |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
# write the subroutine map |
253
|
0
|
0
|
|
|
|
|
open($fh, '>', $filepath) or die "open failed ($filepath): $!"; |
254
|
0
|
|
|
|
|
|
print $fh $submap; |
255
|
0
|
|
|
|
|
|
close($fh); |
256
|
0
|
|
|
|
|
|
}; |
257
|
|
|
|
|
|
|
|
258
|
0
|
|
|
|
|
|
my @responses; |
259
|
|
|
|
|
|
|
my %requested; |
260
|
|
|
|
|
|
|
|
261
|
0
|
|
|
|
|
|
for my $namespace (keys %WebService::BitbucketServer::Spec::API) { |
262
|
0
|
|
|
|
|
|
my $url = documentation_url($namespace, 'wadl', $args->{version}); |
263
|
|
|
|
|
|
|
|
264
|
0
|
0
|
|
|
|
|
next if $requested{$url}; |
265
|
0
|
|
|
|
|
|
$requested{$url} = 1; |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
|
|
|
|
my $resp = $self->any_ua->get($url); |
268
|
0
|
0
|
|
|
|
|
if ($self->any_ua->response_is_future) { |
269
|
0
|
|
|
|
|
|
push @responses, $resp->transform( |
270
|
|
|
|
|
|
|
done => $handle_response, |
271
|
|
|
|
|
|
|
fail => $handle_response, |
272
|
|
|
|
|
|
|
); |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
else { |
275
|
0
|
|
|
|
|
|
push @responses, $handle_response->($resp); |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
0
|
0
|
|
|
|
|
if ($self->any_ua->response_is_future) { |
280
|
0
|
|
|
|
|
|
return Future->wait_all(@responses); |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
else { |
283
|
0
|
|
|
|
|
|
return \@responses; |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
sub _mod_to_pm { |
288
|
0
|
|
|
0
|
|
|
my $mod = shift; |
289
|
0
|
|
|
|
|
|
my @parts = split(/::/, $mod); |
290
|
0
|
|
|
|
|
|
$parts[-1] = "$parts[-1].pm"; |
291
|
0
|
|
|
|
|
|
return @parts; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
1; |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
__END__ |