EmptyRepository/Repository.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 117 CC 7 SCAR 20.8
CC
7
Cov
0%
_build_report
20.0
_get_commits
6.0
_get_branches
6.0
CRAP 8.0
1 package GitHub::EmptyRepository::Repository;
2  
3 use Moo;
4  
5 our $VERSION = '0.00002';
6  
7 use GitHub::EmptyRepository::Repository::Commit ();
8 use MooX::StrictConstructor;
9 use Types::Standard qw( HashRef ArrayRef Bool InstanceOf Str );
10 use URI ();
11  
12 has github_client => (
13 is => 'ro',
14 isa => InstanceOf ['Pithub'],
15 required => 1,
16 );
17  
18 has name => (
19 is => 'ro',
20 isa => Str,
21 init_arg => undef,
22 lazy => 1,
23 default => sub {
24 my $self = shift;
25 my ( undef, $name ) = $self->_parse_github_url( $self->url );
26 return $name;
27 },
28 );
29  
30 has report => (
31 is => 'ro',
32 isa => HashRef,
33 init_arg => undef,
34 lazy => 1,
35 builder => '_build_report',
36 );
37  
38 has url => (
39 is => 'ro',
40 isa => Str,
41 required => 1,
42 );
43  
44 has user => (
45 is => 'ro',
46 isa => Str,
47 init_arg => undef,
48 lazy => 1,
49 default => sub {
50 my $self = shift;
51 my ($user) = $self->_parse_github_url( $self->url );
52 return $user;
53 },
54 );
55  
56 sub _build_report {
57 my $self = shift;
58  
59 my $commits = $self->_get_commits;
60 my $branches = $self->_get_branches;
61 my $pullrequests = $self->_get_pullrequests;
62  
63 my $total_commits = $commits ? scalar @{$commits} : 0;
64 my $total_branches = $branches ? scalar @{$branches} : 0;
65 my $total_pullrequests = $pullrequests ? scalar @{$pullrequests} : 0;
66  
67 my %summary = (
68 nb_commits => $total_commits,
69 nb_branches => $total_branches,
70 nb_pullrequests => $total_pullrequests
71 );
72 $summary{files} = [];
73  
74 foreach my $commit ( @{$commits} ) {
75 foreach my $file ( @{ $commit->files } ) {
76 push @{ $summary{files} }, $file;
77 }
78 }
79  
80 return \%summary;
81 }
82  
83 ## no critic (ValuesAndExpressions::ProhibitAccessOfPrivateData)
84 sub _get_commits {
85 my $self = shift;
86  
87 my $result = $self->github_client->repos->commits->list(
88 user => $self->user,
89 repo => $self->name,
90 params => { per_page => 2, state => 'all' },
91 );
92  
93 $result->auto_pagination(0);
94  
95 my @commits;
96  
97 return \@commits unless $result->response->is_success;
98  
99 while ( my $row = $result->next ) {
100 my $commit = GitHub::EmptyRepository::Repository::Commit->new(
101 github_client => $self->github_client->repos->commits,
102 repo => $self->name,
103 user => $self->user,
104 sha => $row->{sha}
105 );
106  
107 push @commits, $commit;
108 }
109 return \@commits;
110 }
111  
112 sub _get_branches {
113 my $self = shift;
114  
115 my $result = $self->github_client->repos->branches(
116 user => $self->user,
117 repo => $self->name,
118 params => { per_page => 2, state => 'all' },
119 );
120  
121 $result->auto_pagination(0);
122  
123 my @branches;
124  
125 return \@branches unless $result->response->is_success;
126  
127 while ( my $row = $result->next ) {
128 my $branch = $row->{name};
129 push @branches, $branch;
130 }
131 return \@branches;
132 }
133  
134 sub _get_pullrequests {
135 my $self = shift;
136  
137 my $result = $self->github_client->pull_requests->list(
138 user => $self->user,
139 repo => $self->name,
140 params => { per_page => 2, state => 'all' },
141 );
142  
143 $result->auto_pagination(0);
144  
145 my @pullrequests;
146  
147 return \@pullrequests unless $result->response->is_success;
148  
149 while ( my $row = $result->next ) {
150 my $pullrequest = $row->{number};
151 push @pullrequests, $pullrequest;
152 }
153 return \@pullrequests;
154 }
155  
156 #
157 ## use critic
158  
159 sub _parse_github_url {
160 my $self = shift;
161 my $uri = URI->new(shift);
162  
163 my @parts = split m{/}, $uri->path;
164  
165 # paths may or may not have a leading slash (absolute vs relative)
166 my $user = shift @parts || shift @parts;
167 my $name = shift @parts;
168 $name =~ s{\.git}{};
169  
170 return ( $user, $name );
171 }
172  
173 1;
174  
175 =pod
176  
177 =encoding UTF-8
178  
179 =head1 NAME
180  
181 GitHub::EmptyRepository::Repository - Encapsulate repository data for a repository
182  
183 =head1 VERSION
184  
185 version 0.00002
186  
187 =head1 AUTHORS
188  
189 =over 4
190  
191 =item *
192  
193 Thibault Duponchelle <thibault.duponchelle@gmail.com>
194  
195 =item *
196  
197 Olaf Alders <olaf@wundercounter.com>
198  
199 =back
200  
201 =head1 COPYRIGHT AND LICENSE
202  
203 This software is copyright (c) 2022 by Thibault Duponchelle.
204  
205 This is free software; you can redistribute it and/or modify it under
206 the same terms as the Perl 5 programming language system itself.
207  
208 =cut
209  
210 __END__