| 1 | package utils; |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | use utf8; |
|---|
| 5 | use Carp qw(croak); |
|---|
| 6 | |
|---|
| 7 | use LWP::UserAgent; |
|---|
| 8 | use JSON; |
|---|
| 9 | my $ua = LWP::UserAgent->new; |
|---|
| 10 | my $json = JSON->new->utf8->indent; |
|---|
| 11 | |
|---|
| 12 | sub json_api { |
|---|
| 13 | my $url = shift; |
|---|
| 14 | |
|---|
| 15 | my $res = $ua->get( |
|---|
| 16 | "https://api.github.com$url" |
|---|
| 17 | ); |
|---|
| 18 | |
|---|
| 19 | $res->is_success or croak $res->status_line; |
|---|
| 20 | |
|---|
| 21 | return $json->decode($res->content); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | package GitHubBackup; |
|---|
| 26 | use base qw(Class::Accessor::Fast); |
|---|
| 27 | |
|---|
| 28 | use strict; |
|---|
| 29 | use warnings; |
|---|
| 30 | use utf8; |
|---|
| 31 | use Carp qw(croak); |
|---|
| 32 | |
|---|
| 33 | __PACKAGE__->mk_accessors( qw( |
|---|
| 34 | directory |
|---|
| 35 | )); |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | # both hash and hashref are acceptable |
|---|
| 39 | sub new { |
|---|
| 40 | my $class = shift; |
|---|
| 41 | |
|---|
| 42 | my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_}; |
|---|
| 43 | if (! $args->{directory}) { |
|---|
| 44 | $args->{directory} = "."; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | return $class->SUPER::new($args); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | sub account { |
|---|
| 51 | my $self = shift; |
|---|
| 52 | my $args = shift; |
|---|
| 53 | |
|---|
| 54 | if (defined $args) { |
|---|
| 55 | $self->{repos} = undef; |
|---|
| 56 | $self->{account} = $args; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | return $self->{account}; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | sub repository { |
|---|
| 63 | my $self = shift; |
|---|
| 64 | my $args = shift; |
|---|
| 65 | |
|---|
| 66 | if (defined $args) { |
|---|
| 67 | $self->{repos} = undef; |
|---|
| 68 | $self->{repository} = $args; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | return $self->{repository}; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | sub repos { |
|---|
| 75 | my $self = shift; |
|---|
| 76 | return $self->{repos} if ($self->{repos}); |
|---|
| 77 | |
|---|
| 78 | my $account = $self->account or croak "account is not set"; |
|---|
| 79 | if (my $repository = $self->repository) { |
|---|
| 80 | $self->{repos} = [ |
|---|
| 81 | GitHubBackup::Repository->new({ |
|---|
| 82 | directory => sub {$self->directory}, |
|---|
| 83 | full_name => "$account/$repository", |
|---|
| 84 | }) |
|---|
| 85 | ]; |
|---|
| 86 | |
|---|
| 87 | return $self->{repos}; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | my $page = 1; |
|---|
| 91 | my @repos; |
|---|
| 92 | while (1) { |
|---|
| 93 | my $result = utils::json_api("/users/$account/repos?per_page=100&page=$page"); |
|---|
| 94 | if (ref($result) eq 'ARRAY' && scalar @$result > 0) { |
|---|
| 95 | push @repos, @$result; |
|---|
| 96 | $page++; |
|---|
| 97 | |
|---|
| 98 | next; |
|---|
| 99 | } |
|---|
| 100 | last; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | foreach my $repos (@repos) { |
|---|
| 104 | push @{$self->{repos}}, |
|---|
| 105 | GitHubBackup::Repository->new({ |
|---|
| 106 | directory => sub {$self->directory}, |
|---|
| 107 | full_name => $repos->{full_name}, |
|---|
| 108 | clone_url => $repos->{clone_url}, |
|---|
| 109 | }) |
|---|
| 110 | ; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | return $self->{repos}; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | sub backup { |
|---|
| 117 | my $self = shift; |
|---|
| 118 | |
|---|
| 119 | foreach my $repos (@{$self->repos}) { |
|---|
| 120 | $repos->backup; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | return $self; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | package GitHubBackup::Repository; |
|---|
| 128 | use base qw(Class::Accessor::Fast); |
|---|
| 129 | |
|---|
| 130 | use strict; |
|---|
| 131 | use warnings; |
|---|
| 132 | use utf8; |
|---|
| 133 | use Carp qw(croak); |
|---|
| 134 | use Git::Repository; |
|---|
| 135 | use File::chdir; |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | sub new { |
|---|
| 139 | my $class = shift; |
|---|
| 140 | my $args = shift; |
|---|
| 141 | |
|---|
| 142 | if (! exists $args->{clone_url}) { |
|---|
| 143 | my $result = utils::json_api('/repos/' . $args->{full_name}); |
|---|
| 144 | $args->{clone_url} = $result->{clone_url}; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | return $class->SUPER::new($args); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | sub directory { |
|---|
| 151 | my $self = shift; |
|---|
| 152 | |
|---|
| 153 | return $self->{directory}->(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | sub clone_git { |
|---|
| 157 | my $self = shift; |
|---|
| 158 | |
|---|
| 159 | my $dir = $self->directory .'/'. $self->{full_name}; |
|---|
| 160 | if (-d "$dir") { |
|---|
| 161 | local $CWD = $dir; |
|---|
| 162 | print "fetch ", $dir, "\n"; |
|---|
| 163 | Git::Repository->run(fetch => '--all'); |
|---|
| 164 | return $self; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | print "clone ", $dir, "\n"; |
|---|
| 168 | Git::Repository->run(clone => '--mirror' => $self->{clone_url} => $dir); |
|---|
| 169 | return $self; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | sub set_forks { |
|---|
| 173 | my $self = shift; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | sub clone_wiki { |
|---|
| 177 | my $self = shift; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | sub save_issues { |
|---|
| 181 | my $self = shift; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | sub backup { |
|---|
| 185 | my $self = shift; |
|---|
| 186 | |
|---|
| 187 | $self->clone_git; |
|---|
| 188 | $self->set_forks; |
|---|
| 189 | $self->clone_wiki; |
|---|
| 190 | $self->save_issues; |
|---|
| 191 | |
|---|
| 192 | return $self; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | 1; |
|---|
| 197 | __END__ |
|---|