blib/lib/Github/Backup.pm

filter: Statementstmt 57.3% 74 / 129 Branchbran 26.5% 17 / 64 Conditioncond 66.6% 10 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 75.0% 15 / 20 Podpod 80.0% 4 / 5 totaltotal 51.2% 124 / 242 CC 42 SCAR 37.2
CC
42
Cov
49%
issues
156.0
repos
42.0
finish
20.0
CRAP 41.3
↑ 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.04';
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 6 ▶sub BUILD {
Subroutine BUILD: called CC=10 SCAR=23.9
Pod undocumented
61 6 my ($self) = @_;
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" 2 0 2
99 }
100  
101 0 ▶sub list {
Subroutine list: not called CC=3 SCAR=24.8
Pod documented
102 0 my ($self) = @_;
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 0 ▶sub repos {
Subroutine repos: not called CC=6 SCAR=37.4
Pod documented
114 0 my ($self) = @_;
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 0 ▶sub issues {
Subroutine issues: not called CC=12 SCAR=50.5
Pod documented
150 0 my ($self) = @_;
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 $closed_issue_list = $self->gh->issues->list(
166 user => $self->user,
167 repo => $repo->{name},
168 0 params => {
169 state => 'closed'
170 }
171 );
172  
173 my $open_issue_list = $self->gh->issues->list(
174 user => $self->user,
175 repo => $repo->{name},
176 0 params => {
177 state => 'open'
178 }
179 );
180  
181 0 my $open_issues = $open_issue_list->content;
182 0 my $closed_issues = $closed_issue_list->content;
183  
184 0 my $issue_dir = $self->stg . "/issues/$repo->{name}";
185  
186 0 my $dir_created = 0;
187  
188 0 for my $issue (@$open_issues, @$closed_issues) {
189 0 ▶ if (! $dir_created) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir_created) 0 0 0
190 0 ▶ mkdir $issue_dir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $issue_dir or die $! 0 0 0
191 0 ▶ mkdir "$issue_dir/open" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "$issue_dir/open" or die $! 0 0 0
192 0 ▶ mkdir "$issue_dir/closed" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "$issue_dir/closed" or die $! 0 0 0
193 0 $dir_created = 1;
194 }
195  
196 0 ▶ my $issue_path = $issue->{state} eq 'open'
Branch: 1 branch
BranchTrueFalseTotal
$issue->{'state'} eq 'open' ? : 0 0 0
197 ? "$issue_dir/open/$issue->{id}"
198 : "$issue_dir/closed/$issue->{id}";
199  
200 0 ▶ open my $fh, '>', $issue_path or die "can't create the issue file";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $issue_path or die "can't create the issue file" 0 0 0
201  
202 0 print "Copied $repo->{name} issue #$issue->{number} to $issue_path\n";
203  
204 0 print $fh encode_json $issue;
205 }
206 }
207 }
208 0 ▶sub finish {
Subroutine finish: not called CC=4 SCAR=30.0
Pod documented
209 0 my ($self) = @_;
210 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
211 0 ▶ move $self->stg,
Branch: 1 branch
BranchTrueFalseTotal
move($self->stg, $self->dir) or die "can't rename the staging directory: $!" 0 0 0
212 $self->dir or die "can't rename the staging directory: $!";
213 }
214 }
215 0 ▶sub _trap {
Subroutine _trap: not called CC=2 SCAR=17.9
216 0 my ($self) = @_;
217 0 ▶ if (! $self->{trap}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"trap"}) 0 0 0
218 0 $self->{trap} = Hook::Output::Tiny->new;
219 }
220  
221 0 return $self->{trap};
222 }
223 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=11 SCAR=25.7
224 6 my $self = shift;
225  
226 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
227 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
228 }
229  
230 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
231 2 ▶ move $self->stg,
Branch: 1 branch
BranchTrueFalseTotal
move($self->stg, $self->dir) or die "can't rename the staging directory: $!" 2 0 2
232 $self->dir or die "can't rename the staging directory: $!";
233 }
234  
235 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
236 # we're in testing mode, clean everything up
237 1 ▶ rmtree $self->dir
Branch: 1 branch
BranchTrueFalseTotal
rmtree($self->dir) or die "can't remove the test backup directory...$!" 1 0 1
238 or die "can't remove the test backup directory...$!";
239 }
240 }
241  
242 6 1;
243 __END__
↑ Summary