blib/lib/Github/Backup.pm

filter: Statementstmt 72.9% 70 / 96 Branchbran 36.9% 17 / 46 Conditioncond 83.3% 10 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 87.5% 14 / 16 Podpod 66.6% 2 / 3 totaltotal 65.0% 117 / 180 CC 30 SCAR 28.9
CC
30
Cov
63%
issues
56.0
repos
30.0
DESTROY
13.1
CRAP 17.9
↑ Summary
1 package Github::Backup;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Git::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Pithub;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 our $VERSION = '1.02';
19  
20 # external
21  
22 5 has api_user => (
23 is => 'rw',
24 );
25 5 has _clean => (
26 # used to clean up test backup directories
27 is => 'rw',
28 );
29 5 has dir => (
30 is => 'rw',
31 );
32 5 has forks => (
33 is => 'rw',
34 );
35 5 has token => (
36 is => 'rw',
37 );
38 5 has proxy => (
39 is => 'rw',
40 );
41 5 has user => (
42 is => 'rw',
43 );
44  
45 # internal
46  
47 5 has gh => (
48 # Pithub object
49 is => 'rw',
50 );
51 5 has stg => (
52 # staging dir
53 is => 'rw',
54 );
55  
56 sub BUILD {
57 6 ▶ my ($self) = @_;
Subroutine BUILD: called CC=10 SCAR=23.9
Pod undocumented
58  
59 6 ▶ if (! $self->token){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->token) 2 4 6
60 2 ▶ $self->token($ENV{GITHUB_TOKEN}) if $ENV{GITHUB_TOKEN};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GITHUB_TOKEN"} 0 2 2
61 }
62  
63 6 for ($self->api_user, $self->token, $self->dir){
64 13 ▶ if (! $_){
Branch: 1 branch
BranchTrueFalseTotal
unless ($_) 4 9 13
65 4 croak "When instantiating an object, the 'api_user', 'token' and " .
66 "'dir' parameters are mandatory...\n";
67 }
68 }
69  
70 2 my $ua = LWP::UserAgent->new;
71  
72 2 ▶ if ($self->proxy){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proxy) 1 1 2
73 1 $ENV{http_proxy} = $self->proxy;
74 1 $ENV{https_proxy} = $self->proxy;
75  
76 1 $ua->env_proxy;
77 }
78  
79 2 my $gh = Pithub->new(
80 ua => $ua,
81 user => $self->api_user,
82 token => $self->token
83 );
84  
85 2 $self->stg($self->dir . '.stg');
86 2 $self->gh($gh);
87  
88 2 ▶ $self->user($self->api_user) if ! defined $self->user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->user 2 0 2
89  
90 2 ▶ if (-d $self->stg){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->stg) 0 2 2
91 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
92 }
93  
94 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
95  
96 }
97 sub repos {
98 0 ▶ my ($self) = @_;
Subroutine repos: not called CC=5 SCAR=34.0
Pod documented
99  
100 0 my $repo_list = $self->gh->repos->list(user => $self->user);
101  
102 0 my @repos;
103  
104 0 ▶ while (my $repo = $repo_list->next){
Branch: 1 branch
BranchTrueFalseTotal
if (my $repo = $repo_list->next) 0 0 0
105 0 push @repos, $repo;
106 }
107  
108 0 for my $repo (@repos){
109  
110 0 my $stg = $self->stg . "/$repo->{name}";
111  
112 0 ▶ if (! $self->forks){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->forks) { } 0 0 0
113 0 ▶ if (! exists $repo->{parent}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $repo->{"parent"}) 0 0 0
114 Git::Repository->run(
115 0 clone => $repo->{clone_url} => $stg,
116 {quiet => 1}
117 );
118 }
119 }
120 else {
121 Git::Repository->run(
122 0 clone => $repo->{clone_url} => $stg,
123 { quiet => 1 }
124 );
125 }
126 }
127 }
128 sub issues {
129 0 ▶ my ($self) = @_;
Subroutine issues: not called CC=7 SCAR=40.3
Pod documented
130  
131 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
132  
133 0 my $repo_list = $self->gh->repos->list(user => $self->user);
134  
135 0 ▶ while (my $repo = $repo_list->next){
Branch: 1 branch
BranchTrueFalseTotal
if (my $repo = $repo_list->next) 0 0 0
136 my $issue_list = $self->gh->issues->list(
137 user => $self->user,
138 repo => $repo->{name}
139 0 );
140  
141 0 my $issue_dir = $self->stg . "/issues/$repo->{name}";
142  
143  
144 0 my $dir_created = 0;
145  
146 0 ▶ while (my $issue = $issue_list->next){
Branch: 1 branch
BranchTrueFalseTotal
if (my $issue = $issue_list->next) 0 0 0
147 0 ▶ if (! $dir_created) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir_created) 0 0 0
148 0 ▶ mkdir $issue_dir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $issue_dir or die $! 0 0 0
149 0 $dir_created = 1;
150 }
151 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
152 or die "can't create the issue file";
153  
154 0 print $fh encode_json $issue;
155 }
156 }
157 }
158 sub DESTROY {
159 6 ▶ my $self = shift;
Subroutine DESTROY: called CC=11 SCAR=25.7
160  
161 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
162 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
163 }
164  
165 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
166 2 ▶ move $self->stg,
Branch: 1 branch
BranchTrueFalseTotal
move($self->stg, $self->dir) or die "can't rename the staging directory: $!" 0 2 2
167 $self->dir or die "can't rename the staging directory: $!";
168 }
169  
170 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
171 # we're in testing mode, clean everything up
172 1 ▶ rmtree $self->dir
Branch: 1 branch
BranchTrueFalseTotal
rmtree($self->dir) or die "can't remove the test backup directory...$!" 0 1 1
173 or die "can't remove the test backup directory...$!";
174 }
175 }
176  
177 5 1;
178 __END__
↑ Summary