blib/lib/Github/Backup.pm

filter: Statementstmt 60.1% 74 / 123 Branchbran 28.3% 17 / 60 Conditioncond 66.6% 10 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 75.0% 15 / 20 Podpod 80.0% 4 / 5 totaltotal 53.4% 124 / 232 SCAR 53.8
CC
39
Cov
51%
issues
90.0
repos
42.0
finish
20.0
CRAP 217.8
↑ Summary
1 package Github::Backup;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Git::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Hook::Output::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Pithub;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 our $VERSION = '1.03';
20  
21 # external
22  
23 6 has api_user => (
24 is => 'rw',
25 );
26 6 has _clean => (
27 # used to clean up test backup directories
28 is => 'rw',
29 );
30 6 has dir => (
31 is => 'rw',
32 );
33 6 has forks => (
34 is => 'rw',
35 );
36 6 has token => (
37 is => 'rw',
38 );
39 6 has proxy => (
40 is => 'rw',
41 );
42 6 has user => (
43 is => 'rw',
44 );
45 6 has limit => (
46 is => 'rw',
47 );
48  
49 # internal
50  
51 6 has gh => (
52 # Pithub object
53 is => 'rw',
54 );
55 6 has stg => (
56 # staging dir
57 is => 'rw',
58 );
59  
60 sub BUILD {
61 6 ▶ my ($self) = @_;
Subroutine BUILD: called CC=10 SCAR=23.9
Pod undocumented
62  
63 6 ▶ if (! $self->token){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->token) 2 4 6
64 2 ▶ $self->token($ENV{GITHUB_TOKEN}) if $ENV{GITHUB_TOKEN};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GITHUB_TOKEN"} 0 2 2
65 }
66  
67 6 for my $key (qw/api_user token dir/){
68 13 ▶ if (! $self->{$key}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key}) 4 9 13
69 4 croak "ERROR: Missing mandatory parameter [$key].\n";
70 }
71 }
72  
73 2 my $ua = LWP::UserAgent->new;
74  
75 2 ▶ if ($self->proxy){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proxy) 1 1 2
76 1 $ENV{http_proxy} = $self->proxy;
77 1 $ENV{https_proxy} = $self->proxy;
78  
79 1 $ua->env_proxy;
80 }
81  
82 2 my $gh = Pithub->new(
83 ua => $ua,
84 user => $self->api_user,
85 token => $self->token,
86 auto_pagination => 1,
87 );
88  
89 2 $self->stg($self->dir . '.stg');
90 2 $self->gh($gh);
91  
92 2 ▶ $self->user($self->api_user) if ! defined $self->user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->user 2 0 2
93  
94 2 ▶ if (-d $self->stg){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->stg) 0 2 2
95 0 ▶ rmtree $self->stg or die "can't remove the old staging directory...$!";
Branch: 1 branch
BranchTrueFalseTotal
rmtree($self->stg) or die "can't remove the old staging directory...$!" 0 0 0
96 }
97  
98 2 ▶ mkdir $self->stg or die "can't create the backup staging directory...$!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->stg or die "can't create the backup staging directory...$!\n" 0 2 2
99 }
100  
101 sub list {
102 0 ▶ my ($self) = @_;
Subroutine list: not called CC=3 SCAR=24.8
Pod documented
103  
104 0 ▶ if (! $self->{repo_list}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"repo_list"}) 0 0 0
105 0 my $repo_list = $self->gh->repos->list(user => $self->user);
106 0 ▶ while (my $repo = $repo_list->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $repo = $repo_list->next) 0 0 0
107 0 push @{ $self->{repo_list} }, $repo;
108 }
109 }
110  
111 0 return $self->{repo_list};
112 }
113 sub repos {
114 0 ▶ my ($self) = @_;
Subroutine repos: not called CC=6 SCAR=37.4
Pod documented
115  
116 0 my $repos = $self->list;
117  
118 0 my $repo_count = 0;
119 0 for my $repo (@$repos){
120 0 $repo_count++;
121  
122 0 ▶ if ($self->limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->limit) 0 0 0
123 0 ▶ last if $repo_count >= $self->limit;
Branch: 1 branch
BranchTrueFalseTotal
if $repo_count >= $self->limit 0 0 0
124 }
125  
126 0 $self->_trap->hook('stderr');
127  
128 0 print "Cloning $repo->{name}\n";
129  
130 0 my $stg = $self->stg . "/$repo->{name}";
131  
132 0 ▶ if (! $self->forks){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->forks) { } 0 0 0
133 0 ▶ if (! exists $repo->{parent}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $repo->{"parent"}) 0 0 0
134 Git::Repository->run(
135 0 clone => $repo->{clone_url} => $stg,
136 { quiet => 0 }
137 );
138 }
139 }
140 else {
141 Git::Repository->run(
142 0 clone => $repo->{clone_url} => $stg,
143 { quiet => 0 }
144 );
145 }
146 0 $self->_trap->unhook('stderr');
147 }
148 }
149 sub issues {
150 0 ▶ my ($self) = @_;
Subroutine issues: not called CC=9 SCAR=45.0
Pod documented
151  
152 0 ▶ mkdir $self->stg . "/issues" or die "can't create the 'issues' dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->stg . '/issues' or die "can't create the 'issues' dir: $!" 0 0 0
153  
154 0 my $repos = $self->list;
155  
156 0 my $repo_count = 0;
157  
158 0 for my $repo (@$repos) {
159 0 $repo_count++;
160  
161 0 ▶ if ($self->limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->limit) 0 0 0
162 0 ▶ last if $repo_count >= $self->limit;
Branch: 1 branch
BranchTrueFalseTotal
if $repo_count >= $self->limit 0 0 0
163 }
164  
165 my $issue_list = $self->gh->issues->list(
166 user => $self->user,
167 repo => $repo->{name}
168 0 );
169  
170 0 my $issue_dir = $self->stg . "/issues/$repo->{name}";
171  
172 0 my $dir_created = 0;
173  
174 0 ▶ while (my $issue = $issue_list->next){
Branch: 1 branch
BranchTrueFalseTotal
if (my $issue = $issue_list->next) 0 0 0
175 0 ▶ if (! $dir_created) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir_created) 0 0 0
176 0 ▶ mkdir $issue_dir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $issue_dir or die $! 0 0 0
177 0 $dir_created = 1;
178 }
179 0 ▶ open my $fh, '>', "$issue_dir/$issue->{id}"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", qq[$issue_dir/$issue->{"id"}] or die "can't create the issue file" 0 0 0
180 or die "can't create the issue file";
181  
182 0 print "Copied $repo->{name} issue #$issue->{number}\n";
183  
184 0 print $fh encode_json $issue;
185 }
186 }
187 }
188 sub finish {
189 0 ▶ my ($self) = @_;
Subroutine finish: not called CC=4 SCAR=30.0
Pod documented
190 0 ▶ if ($self->stg && -d $self->stg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stg and -d $self->stg) 0 0 0
Condition0%0/3
$self->stg and -d $self->stg
!ll&&!rl&&r
000
MC/DC0%0/2
$self->stg and -d $self->stg
$self->stg -d $self->stg
Truth table0%0/3
A and B
A= $self->stgB= -d $self->stg
ABresult
0X0
100
111
191 0 ▶ move $self->stg,
Branch: 1 branch
BranchTrueFalseTotal
move($self->stg, $self->dir) or die "can't rename the staging directory: $!" 0 0 0
192 $self->dir or die "can't rename the staging directory: $!";
193 }
194 }
195 sub _trap {
196 0 ▶ my ($self) = @_;
Subroutine _trap: not called CC=2 SCAR=17.9
197 0 ▶ if (! $self->{trap}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"trap"}) 0 0 0
198 0 $self->{trap} = Hook::Output::Tiny->new;
199 }
200  
201 0 return $self->{trap};
202 }
203 sub DESTROY {
204 6 ▶ my $self = shift;
Subroutine DESTROY: called CC=11 SCAR=25.7
205  
206 6 ▶ if ($self->dir && -d $self->dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dir and -d $self->dir) 0 6 6
Condition66%2/3
$self->dir and -d $self->dir
!ll&&!rl&&r
240
MC/DC0%0/2
$self->dir and -d $self->dir
$self->dir -d $self->dir
Truth table66%2/3
A and B
A= $self->dirB= -d $self->dir
ABresult
0X0
100
111
207 0 ▶ rmtree $self->dir or die "can't remove the old backup directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
rmtree($self->dir) or die "can't remove the old backup directory: $!" 0 0 0
208 }
209  
210 6 ▶ if ($self->stg && -d $self->stg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stg and -d $self->stg) 2 4 6
Condition66%2/3
$self->stg and -d $self->stg
!ll&&!rl&&r
402
MC/DC50%1/2
$self->stg and -d $self->stg
$self->stg -d $self->stg
Truth table66%2/3
A and B
A= $self->stgB= -d $self->stg
ABresult
0X0
100
111
211 2 ▶ move $self->stg,
Branch: 1 branch
BranchTrueFalseTotal
move($self->stg, $self->dir) or die "can't rename the staging directory: $!" 0 2 2
212 $self->dir or die "can't rename the staging directory: $!";
213 }
214  
215 6 ▶ if ($self->dir && -d $self->dir && $self->_clean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dir and -d $self->dir and $self->_clean) 1 5 6
Condition100%6/6
$self->dir and -d $self->dir and $self->_clean
!ll&&!rl&&r
411
$self->dir and -d $self->dir
!ll&&!rl&&r
222
MC/DC100%3/3
$self->dir and -d $self->dir and $self->_clean
$self->dir -d $self->dir $self->_clean
Truth table100%4/4
A and B and C
A= $self->dirB= -d $self->dirC= $self->_clean
ABCresult
0XX0
10X0
1100
1111
216 # we're in testing mode, clean everything up
217 1 ▶ rmtree $self->dir
Branch: 1 branch
BranchTrueFalseTotal
rmtree($self->dir) or die "can't remove the test backup directory...$!" 0 1 1
218 or die "can't remove the test backup directory...$!";
219 }
220 }
221  
222 6 1;
223 __END__
↑ Summary