Gitea/Role/HTTP.pm

filter: Statementstmt 75.3% 52 / 69 Branchbran 40.9% 9 / 22 Conditioncond 20.8% 5 / 24 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 76.9% 10 / 13 Podpod 100% 2 / 2 totaltotal 55.2% 79 / 143 CC 22 SCAR 31.8
CC
22
Cov
57%
request
49.8
__ANON__
6.0
_build_ua
6.0
CRAP 24.1
↑ Summary Gitea/API/PullRequests.pm »
1 package WWW::Gitea::Role::HTTP;
2  
3 # ABSTRACT: HTTP + token/basic auth role for the Gitea REST API
4  
5 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use JSON::MaybeXS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 4 requires 'api_url';
16 4 requires 'token';
17 4 requires 'username';
18 4 requires 'password';
19  
20 has ua => (
21 is => 'lazy',
22 builder => sub {
23 0 ▶ LWP::UserAgent->new(
Subroutine __ANON__: not called CC=2 SCAR=17.9
Subroutine _build_ua: not called CC=2 SCAR=17.9
Condition0%0/4
$WWW::Gitea::VERSION // "dev"
l!l
00
$WWW::Gitea::VERSION // "dev"
l!l
00
MC/DC0%0/1
$WWW::Gitea::VERSION // "dev"
$WWW::Gitea::VERSION
MC/DC0%0/1
$WWW::Gitea::VERSION // "dev"
$WWW::Gitea::VERSION
Truth table0%0/2
A
A= $WWW::Gitea::VERSION
Aresult
11
00
Truth table0%0/2
A
A= $WWW::Gitea::VERSION
Aresult
11
00
24 agent => 'WWW-Gitea/' . ($WWW::Gitea::VERSION // 'dev'),
25 timeout => 30,
26 );
27 },
28 4 );
29  
30  
31 sub _apply_auth {
32 6 ▶ my ($self, $req) = @_;
Subroutine _apply_auth: called CC=5 SCAR=17.0
33 6 ▶ if (defined $self->token && length $self->token) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->token and length $self->token) { } 5 1 6
elsif (defined $self->username and defined $self->password) { } 1 0 1
Condition66%2/3
defined $self->token and length $self->token
!ll&&!rl&&r
105
MC/DC50%1/2
defined $self->token and length $self->token
defined $self->token length $self->token
Truth table66%2/3
A and B
A= defined $self->tokenB= length $self->token
ABresult
0X0
100
111
34 5 $req->header(Authorization => 'token ' . $self->token);
35 }
36 ▶ elsif (defined $self->username && defined $self->password) {
Condition33%1/3
defined $self->username and defined $self->password
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->username and defined $self->password
defined $self->username defined $self->password
Truth table33%1/3
A and B
A= defined $self->usernameB= defined $self->password
ABresult
0X0
100
111
37 1 $req->header(Authorization => 'Basic '
38 . encode_base64($self->username . ':' . $self->password, ''));
39 }
40 6 return;
41 }
42  
43 sub request {
44 4 ▶ my ($self, $method, $path, %args) = @_;
Subroutine request: called CC=15 SCAR=39.1
Pod documented
45  
46 4 my $uri = URI->new($self->api_url . $path);
47 4 ▶ $uri->query_form($args{query}) if $args{query};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"query"} 4 0 4
48  
49 4 my $req = HTTP::Request->new($method => $uri);
50 4 $self->_apply_auth($req);
51 4 $req->header(Accept => 'application/json');
52 4 ▶ for my $k (keys %{ $args{headers} || {} }) {
Branch: 1 branch
BranchTrueFalseTotal
$args{"headers"} or {} 4 0 4
53 0 $req->header($k => $args{headers}{$k});
54 }
55  
56 4 ▶ if (defined $args{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"body"}) 0 4 4
57 0 ▶ my $ct = $args{content_type} || 'application/json';
Condition0%0/2
$args{'content_type'} || 'application/json'
l!l
00
MC/DC0%0/1
$args{'content_type'} || 'application/json'
$args{'content_type'}
Truth table0%0/2
A
A= $args{'content_type'}
Aresult
11
00
58 0 $req->header('Content-Type' => $ct);
59 0 ▶ $req->content(ref $args{body} ? encode_json($args{body}) : $args{body});
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'body'} ? : 0 0 0
60 }
61  
62 4 $log->debugf('Gitea %s %s', $method, $uri);
63 4 my $res = $self->ua->request($req);
64  
65 4 my $body = $res->decoded_content;
66 4 my $data;
67 4 ▶ if (defined $body && length $body && $body =~ /\A\s*[\{\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and length $body and $body =~ /\A\s*[\{\[]/) 4 0 4
Condition33%2/6
defined $body and length $body and $body =~ /\A\s*[\{\[]/
!ll&&!rl&&r
004
defined $body and length $body
!ll&&!rl&&r
004
MC/DC0%0/3
defined $body and length $body and $body =~ /\A\s*[\{\[]/
defined $body length $body $body =~ /\A\s*[\{\[]/
Truth table25%1/4
A and B and C
A= defined $bodyB= length $bodyC= $body =~ /\A\s*[\{\[]/
ABCresult
0XX0
10X0
1100
1111
68 4 $data = decode_json($body);
69 }
70  
71 4 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 4 4
72 my $msg = ref $data eq 'HASH'
73 0 ▶ ? ($data->{message} || $data->{error} || $res->status_line)
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 0 0 0
Condition0%0/6
$data->{"message"} or $data->{"error"} or $res->status_line
l!l&&r!l&&!r
000
$data->{"message"} or $data->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$data->{"message"} or $data->{"error"} or $res->status_line
$data->{"message"} $data->{"error"} $res->status_line
Truth table0%0/4
A or B or C
A= $data->{"message"}B= $data->{"error"}C= $res->status_line
ABCresult
1XX1
01X1
0011
0000
74 : $res->status_line;
75 0 $log->errorf('Gitea API error: %s', $msg);
76 0 croak "Gitea API error ($method $path): $msg";
77 }
78  
79 # 204 No Content (deletes etc.) — no body to decode
80 4 ▶ return defined $data ? $data : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 4 0 4
81 }
82  
83  
84 sub request_status {
85 0 ▶ my ($self, $method, $path, %args) = @_;
Subroutine request_status: not called CC=2 SCAR=17.9
Pod documented
86 0 my $uri = URI->new($self->api_url . $path);
87 0 ▶ $uri->query_form($args{query}) if $args{query};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"query"} 0 0 0
88 0 my $req = HTTP::Request->new($method => $uri);
89 0 $self->_apply_auth($req);
90 0 $req->header(Accept => 'application/json');
91 0 $log->debugf('Gitea %s %s (status only)', $method, $uri);
92 0 return $self->ua->request($req)->code;
93 }
94  
95  
96  
97 4 1;
98  
99 __END__
↑ Summary Gitea/API/PullRequests.pm »