| 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 | sub get { |
|---|
| 25 | my $url = shift; |
|---|
| 26 | |
|---|
| 27 | my $page = 1; |
|---|
| 28 | my $data; |
|---|
| 29 | while(1) { |
|---|
| 30 | my $result = json_api("$url?per_page=100&page=$page"); |
|---|
| 31 | if (ref($result) eq 'ARRAY' && scalar @$result > 0) { |
|---|
| 32 | push @$data, @$result; |
|---|
| 33 | $page++; |
|---|
| 34 | |
|---|
| 35 | next; |
|---|
| 36 | } |
|---|
| 37 | last; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | return $data; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | package GitHubBackup; |
|---|
| 44 | |
|---|
| 45 | use strict; |
|---|
| 46 | use warnings; |
|---|
| 47 | use utf8; |
|---|
| 48 | use Carp qw(croak); |
|---|
| 49 | use File::Spec; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | # both hash and hashref are acceptable |
|---|
| 53 | sub new { |
|---|
| 54 | my $class = shift; |
|---|
| 55 | my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_}; |
|---|
| 56 | |
|---|
| 57 | return bless $args, $class; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | sub account { |
|---|
| 61 | my $self = shift; |
|---|
| 62 | my $args = shift; |
|---|
| 63 | |
|---|
| 64 | if (defined $args) { |
|---|
| 65 | $self->{repos} = undef; |
|---|
| 66 | $self->{account} = $args; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | return $self->{account}; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | sub repository { |
|---|
| 73 | my $self = shift; |
|---|
| 74 | my $args = shift; |
|---|
| 75 | |
|---|
| 76 | if (defined $args) { |
|---|
| 77 | $self->{repos} = undef; |
|---|
| 78 | $self->{repository} = $args; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return $self->{repository}; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | sub directory { |
|---|
| 85 | my $self = shift; |
|---|
| 86 | my $args = shift; |
|---|
| 87 | |
|---|
| 88 | if (defined $args) { |
|---|
| 89 | $self->{directory} = File::Spec->rel2abs($args); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | return $self->{directory}; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | sub repos { |
|---|
| 96 | my $self = shift; |
|---|
| 97 | return $self->{repos} if ($self->{repos}); |
|---|
| 98 | |
|---|
| 99 | my $account = $self->account or croak "account is not set"; |
|---|
| 100 | if (my $repository = $self->repository) { |
|---|
| 101 | $self->{repos} = [ |
|---|
| 102 | GitHubBackup::Repository->new({ |
|---|
| 103 | directory => sub {$self->directory}, |
|---|
| 104 | full_name => "$account/$repository", |
|---|
| 105 | }) |
|---|
| 106 | ]; |
|---|
| 107 | |
|---|
| 108 | return $self->{repos}; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | my $result = utils::get("/users/$account/repos"); |
|---|
| 112 | foreach my $repos (@$result) { |
|---|
| 113 | push @{$self->{repos}}, |
|---|
| 114 | GitHubBackup::Repository->new({ |
|---|
| 115 | directory => sub {$self->directory}, |
|---|
| 116 | full_name => $repos->{full_name}, |
|---|
| 117 | clone_url => $repos->{clone_url}, |
|---|
| 118 | }) |
|---|
| 119 | ; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | return $self->{repos}; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | sub backup { |
|---|
| 126 | my $self = shift; |
|---|
| 127 | |
|---|
| 128 | foreach my $repos (@{$self->repos}) { |
|---|
| 129 | $repos->backup; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | return $self; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | package GitHubBackup::Repository; |
|---|
| 137 | |
|---|
| 138 | use strict; |
|---|
| 139 | use warnings; |
|---|
| 140 | use utf8; |
|---|
| 141 | use Carp qw(croak); |
|---|
| 142 | use Git::Repository; |
|---|
| 143 | use File::chdir; |
|---|
| 144 | use File::Spec; |
|---|
| 145 | use File::Path qw(mkpath); |
|---|
| 146 | |
|---|
| 147 | sub new { |
|---|
| 148 | my $class = shift; |
|---|
| 149 | my $args = shift; |
|---|
| 150 | |
|---|
| 151 | if (! exists $args->{clone_url}) { |
|---|
| 152 | my $result = utils::json_api('/repos/' . $args->{full_name}); |
|---|
| 153 | $args->{clone_url} = $result->{clone_url}; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | return bless $args, $class; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | sub directory { |
|---|
| 160 | my $self = shift; |
|---|
| 161 | |
|---|
| 162 | my $path = $self->{full_name}; |
|---|
| 163 | if (my $base = $self->{directory}->()) { |
|---|
| 164 | $path = File::Spec->catfile($base, $path); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | return $path; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | sub sync { |
|---|
| 171 | my $self = shift; |
|---|
| 172 | my $url = shift; |
|---|
| 173 | my $dir = shift; |
|---|
| 174 | |
|---|
| 175 | if (-d "$dir") { |
|---|
| 176 | local $CWD = $dir; |
|---|
| 177 | print "fetch ", $dir, "\n"; |
|---|
| 178 | Git::Repository->run(fetch => '--all'); |
|---|
| 179 | return $self; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | print "clone ", $dir, "\n"; |
|---|
| 183 | mkpath $dir; |
|---|
| 184 | Git::Repository->run(clone => '--mirror' => $url => $dir); |
|---|
| 185 | |
|---|
| 186 | return $self; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | sub clone_git { |
|---|
| 190 | my $self = shift; |
|---|
| 191 | |
|---|
| 192 | my $dir = $self->directory . '.git'; |
|---|
| 193 | my $url = $self->{clone_url}; |
|---|
| 194 | |
|---|
| 195 | $self->sync($url => $dir); |
|---|
| 196 | |
|---|
| 197 | return $self; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | sub get_forks { |
|---|
| 201 | my $self = shift; |
|---|
| 202 | return $self->{forks} if ($self->{forks}); |
|---|
| 203 | |
|---|
| 204 | $self->{forks} = utils::get("/repos/" . $self->{full_name} . "/forks"); |
|---|
| 205 | |
|---|
| 206 | return $self; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | sub set_forks { |
|---|
| 210 | my $self = shift; |
|---|
| 211 | |
|---|
| 212 | $self->get_forks; |
|---|
| 213 | |
|---|
| 214 | my $dir = $self->directory . '.git'; |
|---|
| 215 | local $CWD = $dir; |
|---|
| 216 | |
|---|
| 217 | my $remotes = Git::Repository->run(branch => '--remotes'); |
|---|
| 218 | my @fetch; |
|---|
| 219 | foreach my $fork (@{$self->{forks}}) { |
|---|
| 220 | if ($remotes =~ /$fork->{full_name}/) { |
|---|
| 221 | print "skip ", $fork->{full_name}, "\n"; |
|---|
| 222 | next; |
|---|
| 223 | } |
|---|
| 224 | print "add ", $fork->{full_name}, "\n"; |
|---|
| 225 | Git::Repository->run(remote => add => $fork->{full_name} => $fork->{clone_url}); |
|---|
| 226 | push @fetch, $fork->{full_name}; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | foreach my $fork (@fetch) { |
|---|
| 230 | print "fetch ", $fork, "\n"; |
|---|
| 231 | Git::Repository->run(fetch => $fork); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | return $self; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | sub clone_wiki { |
|---|
| 238 | my $self = shift; |
|---|
| 239 | |
|---|
| 240 | my $dir = $self->directory . '.wiki.git'; |
|---|
| 241 | my $url = 'https://github.com/' . $self->{full_name} . '.wiki.git'; |
|---|
| 242 | |
|---|
| 243 | $self->sync($url => $dir); |
|---|
| 244 | |
|---|
| 245 | return $self; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | sub save_issues { |
|---|
| 249 | my $self = shift; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | sub backup { |
|---|
| 253 | my $self = shift; |
|---|
| 254 | |
|---|
| 255 | $self->clone_git; |
|---|
| 256 | $self->set_forks; |
|---|
| 257 | $self->clone_wiki; |
|---|
| 258 | $self->save_issues; |
|---|
| 259 | |
|---|
| 260 | return $self; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | 1; |
|---|
| 265 | __END__ |
|---|