Gitea/Role/HTTP.pm

filter: Statementstmt 78.2% 61 / 78 Branchbran 50.0% 12 / 24 Conditioncond 26.6% 8 / 30 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 78.5% 11 / 14 Podpod 100% 2 / 2 totaltotal 57.9% 95 / 164 CC 26 SCAR 33.2
CC
26
Cov
61%
request
53.2
__ANON__
6.0
_build_ua
6.0
CRAP 27.8
↑ Summary Gitea/API/Issues.pm »
1 package WWW::Gitea::Role::HTTP;
2  
3 # ABSTRACT: HTTP + token/basic auth role for the Gitea REST API
4  
5 5 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use JSON::MaybeXS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 5 requires 'api_url';
17 5 requires 'token';
18 5 requires 'username';
19 5 requires 'password';
20  
21 has ua => (
22 is => 'lazy',
23 0 ▶ builder => sub {
Subroutine _build_ua: not called CC=2 SCAR=17.9
Subroutine __ANON__: not called CC=2 SCAR=17.9
24 0 ▶ LWP::UserAgent->new(
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
25 agent => 'WWW-Gitea/' . ($WWW::Gitea::VERSION // 'dev'),
26 timeout => 30,
27 );
28 },
29 5 );
30  
31  
32 8 ▶sub _apply_auth {
Subroutine _apply_auth: called CC=5 SCAR=17.0
33 8 my ($self, $req) = @_;
34 8 ▶ if (defined $self->token && length $self->token) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->token and length $self->token) { } 7 1 8
elsif (defined $self->username and defined $self->password) { } 1 0 1
Condition66%2/3
defined $self->token and length $self->token
!ll&&!rl&&r
107
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
35 7 $req->header(Authorization => 'token ' . $self->token);
36 }
37 ▶ 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
38 1 $req->header(Authorization => 'Basic '
39 . encode_base64($self->username . ':' . $self->password, ''));
40 }
41 8 return;
42 }
43  
44 6 ▶sub request {
Subroutine request: called CC=19 SCAR=39.7
Pod documented
45 6 my ($self, $method, $path, %args) = @_;
46  
47 6 my $uri = URI->new($self->api_url . $path);
48 6 ▶ $uri->query_form($args{query}) if $args{query};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"query"} 6 0 6
49  
50 6 my $req;
51 6 ▶ if (my $up = $args{upload}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $up = $args{'upload'}) { } 2 4 6
52 2 ▶ my $field = $up->{field} || 'attachment';
Condition50%1/2
$up->{'field'} || 'attachment'
l!l
20
MC/DC0%0/1
$up->{'field'} || 'attachment'
$up->{'field'}
Truth table50%1/2
A
A= $up->{'field'}
Aresult
11
00
53 my $value = defined $up->{file}
54 ? [ $up->{file}, $up->{filename} ]
55 2 ▶ : [ undef, $up->{filename}, Content => ($up->{content} // '') ];
Branch: 1 branch
BranchTrueFalseTotal
defined $up->{'file'} ? : 1 1 2
Condition50%1/2
$up->{"content"} // ""
l!l
10
MC/DC0%0/1
$up->{"content"} // ""
$up->{"content"}
Truth table50%1/2
A
A= $up->{"content"}
Aresult
11
00
56 2 $req = HTTP::Request::Common::POST(
57 "$uri",
58 Content_Type => 'form-data',
59 Content => [ $field => $value ],
60 );
61 2 $req->method($method);
62 }
63 else {
64 4 $req = HTTP::Request->new($method => $uri);
65 }
66 6 $self->_apply_auth($req);
67 6 $req->header(Accept => 'application/json');
68 6 ▶ for my $k (keys %{ $args{headers} || {} }) {
Condition50%1/2
$args{'headers'} || {}
l!l
06
MC/DC0%0/1
$args{'headers'} || {}
$args{'headers'}
Truth table50%1/2
A
A= $args{'headers'}
Aresult
11
00
69 0 $req->header($k => $args{headers}{$k});
70 }
71  
72 6 ▶ if (defined $args{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"body"}) 0 6 6
73 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
74 0 $req->header('Content-Type' => $ct);
75 0 ▶ $req->content(ref $args{body} ? encode_json($args{body}) : $args{body});
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'body'} ? : 0 0 0
76 }
77  
78 6 $log->debugf('Gitea %s %s', $method, $uri);
79 6 my $res = $self->ua->request($req);
80  
81 6 my $body = $res->decoded_content;
82 6 my $data;
83 6 ▶ if (defined $body && length $body && $body =~ /\A\s*[\{\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and length $body and $body =~ /\A\s*[\{\[]/) 6 0 6
Condition33%2/6
defined $body and length $body and $body =~ /\A\s*[\{\[]/
!ll&&!rl&&r
006
defined $body and length $body
!ll&&!rl&&r
006
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
84 6 $data = decode_json($body);
85 }
86  
87 6 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 6 6
88 my $msg = ref $data eq 'HASH'
89 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
90 : $res->status_line;
91 0 $log->errorf('Gitea API error: %s', $msg);
92 0 croak "Gitea API error ($method $path): $msg";
93 }
94  
95 # 204 No Content (deletes etc.) — no body to decode
96 6 ▶ return defined $data ? $data : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 6 0 6
97 }
98  
99  
100 0 ▶sub request_status {
Subroutine request_status: not called CC=2 SCAR=17.9
Pod documented
101 0 my ($self, $method, $path, %args) = @_;
102 0 my $uri = URI->new($self->api_url . $path);
103 0 ▶ $uri->query_form($args{query}) if $args{query};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"query"} 0 0 0
104 0 my $req = HTTP::Request->new($method => $uri);
105 0 $self->_apply_auth($req);
106 0 $req->header(Accept => 'application/json');
107 0 $log->debugf('Gitea %s %s (status only)', $method, $uri);
108 0 return $self->ua->request($req)->code;
109 }
110  
111  
112  
113 5 1;
114  
115 __END__
↑ Summary Gitea/API/Issues.pm »