| 1 | package Net::Async::TravisCI; | ||||||||||||||||||||
| 2 | # ABSTRACT: API support for travis-ci.com and travis-ci.org | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.002'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | Net::Async::TravisCI - interact with the Travis CI API | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | version 0.002 | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | Does things to Travis. Terrible, nasty things, most of which are sadly not yet documented. | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 1 | ▶ | no indirect; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 1 | ▶ | use Future; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 1 | ▶ | use Dir::Self; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 1 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 1 | ▶ | use URI::Template; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 1 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 1 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 1 | ▶ | use File::ShareDir (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 1 | ▶ | use Log::Any qw($log); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 1 | ▶ | use Path::Tiny (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 1 | ▶ | use Net::Async::Pusher; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 1 | ▶ | use Net::Async::TravisCI::Account; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | 1 | ▶ | use Net::Async::TravisCI::Annotation; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | 1 | ▶ | use Net::Async::TravisCI::Branch; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | 1 | ▶ | use Net::Async::TravisCI::Commit; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | 1 | ▶ | use Net::Async::TravisCI::Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | 1 | ▶ | use Net::Async::TravisCI::Job; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | 1 | ▶ | use Net::Async::TravisCI::Build; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 1 | my $json = JSON::MaybeXS->new; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head2 configure | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Applies configuration, which at the moment would involve zero or more of the following | ||||||||||||||||||||
| 53 | named parameters: | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =over 4 | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =item * token - a L<TravisCI token|https://blog.travis-ci.com/2013-01-28-token-token-token> | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =back | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | sub configure { | ||||||||||||||||||
|
Subroutine
configure: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 64 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 65 | 0 | for my $k (grep exists $args{$_}, qw(token)) { | |||||||||||||||||||
| 66 | 0 | $self->{$k} = delete $args{$k}; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | 0 | $self->SUPER::configure(%args); | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =head2 endpoints | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | Returns the hashref of API endpoints, loading them on first call from the C<share/endpoints.json> file. | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 0 | ▶ | sub endpoints { | ||||||||||||||||||
|
Subroutine
endpoints: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 78 | 0 | my ($self) = @_; | |||||||||||||||||||
| 79 | 0 | ▶ | $self->{endpoints} ||= do { | ||||||||||||||||||
|
Condition0%0/3
$self->{"endpoints"} ||= do {
my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json");
$path = Path::Tiny::path(File::ShareDir::dist_file("Net-Async-TravisCI", "endpoints.json")) unless $path->exists;
$json->decode($path->slurp_utf8)
}
MC/DC0%0/2
$self->{"endpoints"} ||= do {
my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json");
$path = Path::Tiny::path(File::ShareDir::dist_file("Net-Async-TravisCI", "endpoints.json")) unless $path->exists;
$json->decode($path->slurp_utf8)
}
$self->{"endpoints"}
do {
my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json");
$path = Path::Tiny::path(File::ShareDir::dist_file("Net-Async-TravisCI", "endpoints.json")) unless $path->exists;
$json->decode($path->slurp_utf8)
}
Truth table0%0/3
A ||= B
A= $self->{"endpoints"}B= do {
my $path = Path::Tiny::path(&__DIR__())->parent(3)->child("share/endpoints.json");
$path = Path::Tiny::path(File::ShareDir::dist_file("Net-Async-TravisCI", "endpoints.json")) unless $path->exists;
$json->decode($path->slurp_utf8)
}
| |||||||||||||||||||||
| 80 | 0 | my $path = Path::Tiny::path(__DIR__)->parent(3)->child('share/endpoints.json'); | |||||||||||||||||||
| 81 | 0 | ▶ | $path = Path::Tiny::path( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | File::ShareDir::dist_file( | ||||||||||||||||||||
| 83 | 'Net-Async-TravisCI', | ||||||||||||||||||||
| 84 | 'endpoints.json' | ||||||||||||||||||||
| 85 | ) | ||||||||||||||||||||
| 86 | ) unless $path->exists; | ||||||||||||||||||||
| 87 | 0 | $json->decode($path->slurp_utf8) | |||||||||||||||||||
| 88 | }; | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =head2 endpoint | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | Processes the given endpoint as a template, using the named parameters | ||||||||||||||||||||
| 94 | passed to the method. | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | ▶ | sub endpoint { | ||||||||||||||||||
|
Subroutine
endpoint: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 99 | 0 | my ($self, $endpoint, %args) = @_; | |||||||||||||||||||
| 100 | 0 | URI::Template->new($self->endpoints->{$endpoint . '_url'})->process(%args); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =head2 http | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | Returns the HTTP instance used for communicating with Travis. | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | Currently autocreates a L<Net::Async::HTTP> instance. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | ▶ | sub http { | ||||||||||||||||||
|
Subroutine
http: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 112 | 0 | my ($self) = @_; | |||||||||||||||||||
| 113 | 0 | ▶ | $self->{http} ||= do { | ||||||||||||||||||
|
Condition0%0/3
$self->{"http"} ||= do {
require Net::Async::HTTP;
$self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "max_connections_per_host", 2, "pipeline", 1, "max_in_flight", 8, "decode_content", 1, "timeout", 30, "user_agent", "Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::TravisCI; TEAM\@cpan.org)"));
$ua
}
MC/DC0%0/2
$self->{"http"} ||= do {
require Net::Async::HTTP;
$self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "max_connections_per_host", 2, "pipeline", 1, "max_in_flight", 8, "decode_content", 1, "timeout", 30, "user_agent", "Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::TravisCI; TEAM\@cpan.org)"));
$ua
}
$self->{"http"}
do {
require Net::Async::HTTP;
$self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "max_connections_per_host", 2, "pipeline", 1, "max_in_flight", 8, "decode_content", 1, "timeout", 30, "user_agent", "Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::TravisCI; TEAM\@cpan.org)"));
$ua
}
Truth table0%0/3
A ||= B
A= $self->{"http"}B= do {
require Net::Async::HTTP;
$self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "max_connections_per_host", 2, "pipeline", 1, "max_in_flight", 8, "decode_content", 1, "timeout", 30, "user_agent", "Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::TravisCI; TEAM\@cpan.org)"));
$ua
}
| |||||||||||||||||||||
| 114 | 0 | require Net::Async::HTTP; | |||||||||||||||||||
| 115 | 0 | $self->add_child( | |||||||||||||||||||
| 116 | my $ua = Net::Async::HTTP->new( | ||||||||||||||||||||
| 117 | fail_on_error => 1, | ||||||||||||||||||||
| 118 | max_connections_per_host => 2, | ||||||||||||||||||||
| 119 | pipeline => 1, | ||||||||||||||||||||
| 120 | max_in_flight => 8, | ||||||||||||||||||||
| 121 | decode_content => 1, | ||||||||||||||||||||
| 122 | timeout => 30, | ||||||||||||||||||||
| 123 | user_agent => 'Mozilla/4.0 (perl; https://metacpan.org/pod/Net::Async::TravisCI; TEAM@cpan.org)', | ||||||||||||||||||||
| 124 | ) | ||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||
| 126 | 0 | $ua | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head2 auth_info | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | Returns authentication info as parameters suitable for the L</http> methods. | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | ▶ | sub auth_info { | ||||||||||||||||||
|
Subroutine
auth_info: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 137 | 0 | my ($self) = @_; | |||||||||||||||||||
| 138 | 0 | ▶ | if(my $key = $self->api_key) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 139 | return ( | ||||||||||||||||||||
| 140 | 0 | user => $self->api_key, | |||||||||||||||||||
| 141 | pass => '', | ||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||
| 143 | } elsif(my $token = $self->token) { | ||||||||||||||||||||
| 144 | return ( | ||||||||||||||||||||
| 145 | 0 | headers => { | |||||||||||||||||||
| 146 | Authorization => 'token "' . $token . '"' | ||||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | ) | ||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | 0 | return; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =head2 api_key | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | Github API key. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | sub api_key { shift->{api_key} } | ||||||||||||||||||
|
Subroutine
api_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head2 token | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | Travis token. | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 0 | ▶ | sub token { shift->{token} } | ||||||||||||||||||
|
Subroutine
token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =head2 mime_type | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | MIME type to use for requests. Hardcoded default to C<travis-ci.2+json>. | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 0 | ▶ | sub mime_type { shift->{mime_type} //= 'application/vnd.travis-ci.2+json' } | ||||||||||||||||||
|
Subroutine
mime_type: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
+(shift())->{"mime_type"} //= "application/vnd.travis-ci.2+json"
MC/DC0%0/1
+(shift())->{"mime_type"} //= "application/vnd.travis-ci.2+json"
+(shift())->{"mime_type"}
Truth table0%0/2
A
A= +(shift())->{"mime_type"}
| |||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =head2 base_uri | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | Base URI for Travis requests. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | Hardcoded to the B<private> Travis CI server, L<https://api.travis-ci.com>. | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 0 | ▶ | sub base_uri { shift->{base_uri} //= URI->new('https://api.travis-ci.com') } | ||||||||||||||||||
|
Subroutine
base_uri: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/3
+(shift())->{"base_uri"} //= "URI"->new("https://api.travis-ci.com")
MC/DC0%0/2
+(shift())->{"base_uri"} //= "URI"->new("https://api.travis-ci.com")
+(shift())->{"base_uri"}
"URI"->new("https://api.travis-ci.com")
Truth table0%0/3
A //= B
A= +(shift())->{"base_uri"}B= "URI"->new("https://api.travis-ci.com")
| |||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =head2 http_get | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | Issues an HTTP GET request. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 0 | ▶ | sub http_get { | ||||||||||||||||||
|
Subroutine
http_get: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 194 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 195 | 0 | my %auth = $self->auth_info; | |||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 0 | ▶ | if(my $hdr = delete $auth{headers}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 198 | 0 | ▶ | $args{headers}{$_} //= $hdr->{$_} for keys %$hdr | ||||||||||||||||||
|
Condition0%0/3
$args{"headers"}{$_} //= $hdr->{$_}
MC/DC0%0/2
$args{"headers"}{$_} //= $hdr->{$_}
$args{"headers"}{$_}
$hdr->{$_}
Truth table0%0/3
A //= B
A= $args{"headers"}{$_}B= $hdr->{$_}
| |||||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | 0 | ▶ | $args{headers}{Accept} //= $self->mime_type; | ||||||||||||||||||
|
Condition0%0/3
$args{"headers"}{"Accept"} //= $self->mime_type
MC/DC0%0/2
$args{"headers"}{"Accept"} //= $self->mime_type
$args{"headers"}{"Accept"}
$self->mime_type
Truth table0%0/3
A //= B
A= $args{"headers"}{"Accept"}B= $self->mime_type
| |||||||||||||||||||||
| 201 | 0 | ▶ | $args{$_} //= $auth{$_} for keys %auth; | ||||||||||||||||||
|
Condition0%0/3
$args{$_} //= $auth{$_}
MC/DC0%0/2
$args{$_} //= $auth{$_}
$args{$_}
$auth{$_}
Truth table0%0/3
A //= B
A= $args{$_}B= $auth{$_}
| |||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 0 | my $uri = delete $args{uri}; | |||||||||||||||||||
| 204 | 0 | $log->tracef("GET %s { %s }", "$uri", \%args); | |||||||||||||||||||
| 205 | $self->http->GET( | ||||||||||||||||||||
| 206 | $uri, | ||||||||||||||||||||
| 207 | %args | ||||||||||||||||||||
| 208 | 0 | ▶ | )->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 209 | 0 | my ($resp) = @_; | |||||||||||||||||||
| 210 | 0 | ▶ | return { } if $resp->code == 204; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 211 | 0 | ▶ | return { } if 3 == ($resp->code / 100); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 212 | try { | ||||||||||||||||||||
| 213 | 0 | $log->tracef('HTTP response for %s was %s', "$uri", $resp->as_string("\n")); | |||||||||||||||||||
| 214 | 0 | return Future->done($json->decode($resp->decoded_content)) | |||||||||||||||||||
| 215 | 0 | } catch { | |||||||||||||||||||
| 216 | 0 | $log->errorf("JSON decoding error %s from HTTP response %s", $@, $resp->as_string("\n")); | |||||||||||||||||||
| 217 | 0 | return Future->fail($@ => json => $resp); | |||||||||||||||||||
| 218 | } | ||||||||||||||||||||
| 219 | 0 | ▶ | })->else(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 220 | 0 | my ($err, $src, $resp, $req) = @_; | |||||||||||||||||||
| 221 | 0 | ▶ | $src //= ''; | ||||||||||||||||||
|
Condition0%0/2
$src //= ""
MC/DC0%0/1
$src //= ""
$src
Truth table0%0/2
A
A= $src
| |||||||||||||||||||||
| 222 | 0 | ▶ | if($src eq 'http') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | 0 | $log->errorf("HTTP error %s, request was %s with response %s", $err, $req->as_string("\n"), $resp->as_string("\n")); | |||||||||||||||||||
| 224 | } else { | ||||||||||||||||||||
| 225 | 0 | ▶ | $log->errorf("Other failure (%s): %s", $src // 'unknown', $err); | ||||||||||||||||||
|
Condition0%0/2
$src // "unknown"
MC/DC0%0/1
$src // "unknown"
$src
Truth table0%0/2
A
A= $src
| |||||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | 0 | Future->fail(@_); | |||||||||||||||||||
| 228 | }) | ||||||||||||||||||||
| 229 | 0 | } | |||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =head2 http_post | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | Performs an HTTP POST request. | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =cut | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 0 | ▶ | sub http_post { | ||||||||||||||||||
|
Subroutine
http_post: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 238 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 239 | 0 | my %auth = $self->auth_info; | |||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 0 | ▶ | if(my $hdr = delete $auth{headers}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 242 | 0 | ▶ | $args{headers}{$_} //= $hdr->{$_} for keys %$hdr | ||||||||||||||||||
|
Condition0%0/3
$args{"headers"}{$_} //= $hdr->{$_}
MC/DC0%0/2
$args{"headers"}{$_} //= $hdr->{$_}
$args{"headers"}{$_}
$hdr->{$_}
Truth table0%0/3
A //= B
A= $args{"headers"}{$_}B= $hdr->{$_}
| |||||||||||||||||||||
| 243 | } | ||||||||||||||||||||
| 244 | 0 | ▶ | $args{headers}{Accept} //= $self->mime_type; | ||||||||||||||||||
|
Condition0%0/3
$args{"headers"}{"Accept"} //= $self->mime_type
MC/DC0%0/2
$args{"headers"}{"Accept"} //= $self->mime_type
$args{"headers"}{"Accept"}
$self->mime_type
Truth table0%0/3
A //= B
A= $args{"headers"}{"Accept"}B= $self->mime_type
| |||||||||||||||||||||
| 245 | 0 | ▶ | $args{$_} //= $auth{$_} for keys %auth; | ||||||||||||||||||
|
Condition0%0/3
$args{$_} //= $auth{$_}
MC/DC0%0/2
$args{$_} //= $auth{$_}
$args{$_}
$auth{$_}
Truth table0%0/3
A //= B
A= $args{$_}B= $auth{$_}
| |||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | 0 | my $content = delete $args{content}; | |||||||||||||||||||
| 248 | 0 | ▶ | $content = $json->encode($content) if ref $content; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 0 | $log->tracef("POST %s { %s }", ''. $args{uri}, $content, \%args); | |||||||||||||||||||
| 251 | $self->http->POST( | ||||||||||||||||||||
| 252 | (delete $args{uri}), | ||||||||||||||||||||
| 253 | $content, | ||||||||||||||||||||
| 254 | content_type => 'application/json', | ||||||||||||||||||||
| 255 | %args | ||||||||||||||||||||
| 256 | 0 | ▶ | )->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 257 | 0 | my ($resp) = @_; | |||||||||||||||||||
| 258 | 0 | ▶ | return Future->done({ }) if $resp->code == 204; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 259 | 0 | ▶ | return Future->done({ }) if 3 == ($resp->code / 100); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 260 | try { | ||||||||||||||||||||
| 261 | 0 | warn "have " . $resp->as_string("\n"); | |||||||||||||||||||
| 262 | 0 | return Future->done($json->decode($resp->decoded_content)) | |||||||||||||||||||
| 263 | 0 | } catch { | |||||||||||||||||||
| 264 | 0 | $log->errorf("JSON decoding error %s from HTTP response %s", $@, $resp->as_string("\n")); | |||||||||||||||||||
| 265 | 0 | return Future->fail($@ => json => $resp); | |||||||||||||||||||
| 266 | } | ||||||||||||||||||||
| 267 | 0 | ▶ | })->else(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 268 | 0 | my ($err, $src, $resp, $req) = @_; | |||||||||||||||||||
| 269 | 0 | ▶ | $src //= ''; | ||||||||||||||||||
|
Condition0%0/2
$src //= ""
MC/DC0%0/1
$src //= ""
$src
Truth table0%0/2
A
A= $src
| |||||||||||||||||||||
| 270 | 0 | ▶ | if($src eq 'http') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 271 | 0 | $log->errorf("HTTP error %s, request was %s with response %s", $err, $req->as_string("\n"), $resp->as_string("\n")); | |||||||||||||||||||
| 272 | } else { | ||||||||||||||||||||
| 273 | 0 | ▶ | $log->errorf("Other failure (%s): %s", $src // 'unknown', $err); | ||||||||||||||||||
|
Condition0%0/2
$src // "unknown"
MC/DC0%0/1
$src // "unknown"
$src
Truth table0%0/2
A
A= $src
| |||||||||||||||||||||
| 274 | } | ||||||||||||||||||||
| 275 | 0 | Future->fail(@_); | |||||||||||||||||||
| 276 | }) | ||||||||||||||||||||
| 277 | 0 | } | |||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | =head2 github_token | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | Sets the github token. | ||||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | =cut | ||||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | 0 | ▶ | sub github_token { | ||||||||||||||||||
|
Subroutine
github_token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 286 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 287 | $self->http_post( | ||||||||||||||||||||
| 288 | uri => URI->new($self->base_uri . '/auth/github'), | ||||||||||||||||||||
| 289 | content => { | ||||||||||||||||||||
| 290 | github_token => delete $args{token} | ||||||||||||||||||||
| 291 | } | ||||||||||||||||||||
| 292 | )->transform( | ||||||||||||||||||||
| 293 | 0 | ▶ | done => sub { shift->{access_token} }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 294 | ) | ||||||||||||||||||||
| 295 | 0 | } | |||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | =head2 accounts | ||||||||||||||||||||
| 298 | |||||||||||||||||||||
| 299 | Retrieves accounts. | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | =cut | ||||||||||||||||||||
| 302 | |||||||||||||||||||||
| 303 | 0 | ▶ | sub accounts { | ||||||||||||||||||
|
Subroutine
accounts: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 304 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 305 | $self->http_get( | ||||||||||||||||||||
| 306 | uri => URI->new($self->base_uri . '/accounts'), | ||||||||||||||||||||
| 307 | )->transform( | ||||||||||||||||||||
| 308 | 0 | ▶ | done => sub { map Net::Async::TravisCI::Account->new(%$_), @{ shift->{accounts} } }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 309 | ) | ||||||||||||||||||||
| 310 | 0 | } | |||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | =head2 users | ||||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | Retrieves users. | ||||||||||||||||||||
| 315 | |||||||||||||||||||||
| 316 | =cut | ||||||||||||||||||||
| 317 | |||||||||||||||||||||
| 318 | 0 | ▶ | sub users { | ||||||||||||||||||
|
Subroutine
users: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 319 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 320 | 0 | $self->http_get( | |||||||||||||||||||
| 321 | uri => URI->new($self->base_uri . '/users'), | ||||||||||||||||||||
| 322 | # )->transform( | ||||||||||||||||||||
| 323 | # done => sub { map Net::Async::TravisCI::Account->new(%$_), @{ shift->{accounts} } }, | ||||||||||||||||||||
| 324 | ) | ||||||||||||||||||||
| 325 | } | ||||||||||||||||||||
| 326 | |||||||||||||||||||||
| 327 | =head2 jobs | ||||||||||||||||||||
| 328 | |||||||||||||||||||||
| 329 | Retrieves jobs. | ||||||||||||||||||||
| 330 | |||||||||||||||||||||
| 331 | =cut | ||||||||||||||||||||
| 332 | |||||||||||||||||||||
| 333 | 0 | ▶ | sub jobs { | ||||||||||||||||||
|
Subroutine
jobs: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 334 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 335 | $self->http_get( | ||||||||||||||||||||
| 336 | uri => URI->new($self->base_uri . '/jobs'), | ||||||||||||||||||||
| 337 | )->transform( | ||||||||||||||||||||
| 338 | 0 | ▶ | done => sub { map Net::Async::TravisCI::Job->new(%$_), @{ shift->{jobs} } }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 339 | ) | ||||||||||||||||||||
| 340 | 0 | } | |||||||||||||||||||
| 341 | |||||||||||||||||||||
| 342 | =head2 cancel_job | ||||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | Cancels a specific job by ID. | ||||||||||||||||||||
| 345 | |||||||||||||||||||||
| 346 | =cut | ||||||||||||||||||||
| 347 | |||||||||||||||||||||
| 348 | 0 | ▶ | sub cancel_job { | ||||||||||||||||||
|
Subroutine
cancel_job: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 349 | 0 | my ($self, $job, %args) = @_; | |||||||||||||||||||
| 350 | $self->http_post( | ||||||||||||||||||||
| 351 | uri => URI->new($self->base_uri . '/jobs/' . $job->id . '/cancel'), | ||||||||||||||||||||
| 352 | content => { }, | ||||||||||||||||||||
| 353 | )->transform( | ||||||||||||||||||||
| 354 | 0 | ▶ | done => sub { }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 355 | ) | ||||||||||||||||||||
| 356 | 0 | } | |||||||||||||||||||
| 357 | |||||||||||||||||||||
| 358 | =head2 pusher_auth | ||||||||||||||||||||
| 359 | |||||||||||||||||||||
| 360 | Deals with pusher auth, used for tailing logs. | ||||||||||||||||||||
| 361 | |||||||||||||||||||||
| 362 | =cut | ||||||||||||||||||||
| 363 | |||||||||||||||||||||
| 364 | 0 | ▶ | sub pusher_auth { | ||||||||||||||||||
|
Subroutine
pusher_auth: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 365 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 366 | 0 | ▶ | $self->pusher->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 367 | 0 | my ($conn) = @_; | |||||||||||||||||||
| 368 | 0 | ▶ | $conn->connected->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 369 | 0 | $log->tracef("Pusher socket ID is %s", $conn->socket_id); | |||||||||||||||||||
| 370 | 0 | Future->done($conn->socket_id) | |||||||||||||||||||
| 371 | }) | ||||||||||||||||||||
| 372 | 0 | ▶ | })->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 373 | 0 | ▶ | $args{socket_id} = shift or die "need a socket ID"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 374 | 0 | $self->http_post( | |||||||||||||||||||
| 375 | uri => URI->new($self->base_uri . '/pusher/auth'), | ||||||||||||||||||||
| 376 | content => \%args | ||||||||||||||||||||
| 377 | ) | ||||||||||||||||||||
| 378 | 0 | ▶ | })->transform(done => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 379 | shift->{channels} | ||||||||||||||||||||
| 380 | 0 | }) | |||||||||||||||||||
| 381 | 0 | } | |||||||||||||||||||
| 382 | |||||||||||||||||||||
| 383 | =head2 pusher | ||||||||||||||||||||
| 384 | |||||||||||||||||||||
| 385 | Handles the pusher instance. | ||||||||||||||||||||
| 386 | |||||||||||||||||||||
| 387 | =cut | ||||||||||||||||||||
| 388 | |||||||||||||||||||||
| 389 | 0 | ▶ | sub pusher { | ||||||||||||||||||
|
Subroutine
pusher: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 390 | 0 | my ($self) = @_; | |||||||||||||||||||
| 391 | 0 | ▶ | $self->{pusher} //= $self->config->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 392 | 0 | my $key = shift->pusher->{key}; | |||||||||||||||||||
| 393 | 0 | $self->add_child( | |||||||||||||||||||
| 394 | my $pusher = Net::Async::Pusher->new | ||||||||||||||||||||
| 395 | ); | ||||||||||||||||||||
| 396 | 0 | $pusher->connect( | |||||||||||||||||||
| 397 | key => $key, | ||||||||||||||||||||
| 398 | ) | ||||||||||||||||||||
| 399 | 0 | ▶ | }); | ||||||||||||||||||
|
Condition0%0/3
$self->{"pusher"} //= $self->config->then(sub {
my $key = +(shift())->pusher->{"key"};
$self->add_child(my $pusher = "Net::Async::Pusher"->new);
$pusher->connect("key", $key);
}
)
MC/DC0%0/2
$self->{"pusher"} //= $self->config->then(sub {
my $key = +(shift())->pusher->{"key"};
$self->add_child(my $pusher = "Net::Async::Pusher"->new);
$pusher->connect("key", $key);
}
)
$self->{"pusher"}
$self->config->then(sub {
my $key = +(shift())->pusher->{"key"};
$self->add_child(my $pusher = "Net::Async::Pusher"->new);
$pusher->connect("key", $key);
}
)
Truth table0%0/3
A //= B
A= $self->{"pusher"}B= $self->config->then(sub {
my $key = +(shift())->pusher->{"key"};
$self->add_child(my $pusher = "Net::Async::Pusher"->new);
$pusher->connect("key", $key);
}
)
| |||||||||||||||||||||
| 400 | } | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | =head2 config | ||||||||||||||||||||
| 403 | |||||||||||||||||||||
| 404 | Applies Travis config. | ||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | =cut | ||||||||||||||||||||
| 407 | |||||||||||||||||||||
| 408 | 0 | ▶ | sub config { | ||||||||||||||||||
|
Subroutine
config: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 409 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 410 | $self->{config} //= $self->http_get( | ||||||||||||||||||||
| 411 | uri => URI->new($self->base_uri . '/config'), | ||||||||||||||||||||
| 412 | )->transform( | ||||||||||||||||||||
| 413 | 0 | ▶ | done => sub { map Net::Async::TravisCI::Config->new(%$_), shift->{config} }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 414 | ) | ||||||||||||||||||||
| 415 | 0 | ▶ | } | ||||||||||||||||||
|
Condition0%0/3
$self->{"config"} //= $self->http_get("uri", "URI"->new($self->base_uri . '/config'))->transform("done", sub {
map "Net::Async::TravisCI::Config"->new(%$_), +(shift())->{"config"};
}
)
MC/DC0%0/2
$self->{"config"} //= $self->http_get("uri", "URI"->new($self->base_uri . '/config'))->transform("done", sub {
map "Net::Async::TravisCI::Config"->new(%$_), +(shift())->{"config"};
}
)
$self->{"config"}
$self->http_get("uri", "URI"->new($self->base_uri . '/config'))->transform("done", sub {
map "Net::Async::TravisCI::Config"->new(%$_), +(shift())->{"config"};
}
)
Truth table0%0/3
A //= B
A= $self->{"config"}B= $self->http_get("uri", "URI"->new($self->base_uri . '/config'))->transform("done", sub {
map "Net::Async::TravisCI::Config"->new(%$_), +(shift())->{"config"};
}
)
| |||||||||||||||||||||
| 416 | |||||||||||||||||||||
| 417 | 1 | 1; | |||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | =head1 AUTHOR | ||||||||||||||||||||
| 420 | |||||||||||||||||||||
| 421 | Tom Molesworth <TEAM@cpan.org> | ||||||||||||||||||||
| 422 | |||||||||||||||||||||
| 423 | =head1 LICENSE | ||||||||||||||||||||
| 424 | |||||||||||||||||||||
| 425 | Copyright Tom Molesworth 2015-2017. Licensed under the same terms as Perl itself. | ||||||||||||||||||||