| 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 | |
|---|
| 10 | sub json_api { |
|---|
| 11 | my $url = shift; |
|---|
| 12 | |
|---|
| 13 | my $ua = LWP::UserAgent->new; |
|---|
| 14 | my $json = JSON->new->utf8->indent; |
|---|
| 15 | |
|---|
| 16 | my $res = $ua->get( |
|---|
| 17 | "https://api.github.com$url" |
|---|
| 18 | ); |
|---|
| 19 | |
|---|
| 20 | $res->is_success or croak $res->status_line; |
|---|
| 21 | |
|---|
| 22 | return $json->decode($res->content); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | sub get { |
|---|
| 26 | my $url = shift; |
|---|
| 27 | my %parameters = @_; |
|---|
| 28 | |
|---|
| 29 | my $parameters = ''; |
|---|
| 30 | while (my($key, $value) = each %parameters) { |
|---|
| 31 | $parameters .= "&$key=$value"; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | my $page = 1; |
|---|
| 35 | my $data = []; |
|---|
| 36 | while(1) { |
|---|
| 37 | my $result = json_api("$url?per_page=100&page=$page$parameters"); |
|---|
| 38 | if (ref($result) eq 'ARRAY' && scalar @$result > 0) { |
|---|
| 39 | push @$data, @$result; |
|---|
| 40 | $page++; |
|---|
| 41 | |
|---|
| 42 | next; |
|---|
| 43 | } |
|---|
| 44 | last; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | return $data; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | package GitHubBackup; |
|---|
| 51 | |
|---|
| 52 | use strict; |
|---|
| 53 | use warnings; |
|---|
| 54 | use utf8; |
|---|
| 55 | use Carp qw(croak); |
|---|
| 56 | use File::Spec; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | # both hash and hashref are acceptable |
|---|
| 60 | sub new { |
|---|
| 61 | my $class = shift; |
|---|
| 62 | my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_}; |
|---|
| 63 | |
|---|
| 64 | return bless $args, $class; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | sub account { |
|---|
| 68 | my $self = shift; |
|---|
| 69 | my $args = shift; |
|---|
| 70 | |
|---|
| 71 | if (defined $args) { |
|---|
| 72 | $self->{repos} = undef; |
|---|
| 73 | $self->{account} = $args; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | return $self->{account}; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | sub repository { |
|---|
| 80 | my $self = shift; |
|---|
| 81 | my $args = shift; |
|---|
| 82 | |
|---|
| 83 | if (defined $args) { |
|---|
| 84 | $self->{repos} = undef; |
|---|
| 85 | $self->{repository} = $args; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | return $self->{repository}; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | sub directory { |
|---|
| 92 | my $self = shift; |
|---|
| 93 | my $args = shift; |
|---|
| 94 | |
|---|
| 95 | if (defined $args) { |
|---|
| 96 | $self->{directory} = File::Spec->rel2abs($args); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | return $self->{directory}; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | sub repos { |
|---|
| 103 | my $self = shift; |
|---|
| 104 | return $self->{repos} if ($self->{repos}); |
|---|
| 105 | |
|---|
| 106 | $self->{repos} = []; |
|---|
| 107 | |
|---|
| 108 | my $account = $self->account or croak "account is not set"; |
|---|
| 109 | my $result; |
|---|
| 110 | if (my $repository = $self->repository) { |
|---|
| 111 | $result = [ utils::json_api("/repos/$account/$repository") ]; |
|---|
| 112 | } |
|---|
| 113 | else { |
|---|
| 114 | $result = utils::get("/users/$account/repos"); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | foreach my $repos (@$result) { |
|---|
| 118 | push @{$self->{repos}}, |
|---|
| 119 | GitHubBackup::Repository->new({ |
|---|
| 120 | directory => sub {$self->directory}, |
|---|
| 121 | repos => $repos, |
|---|
| 122 | }) |
|---|
| 123 | ; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | return $self->{repos}; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | sub backup { |
|---|
| 130 | my $self = shift; |
|---|
| 131 | |
|---|
| 132 | foreach my $repos (@{$self->repos}) { |
|---|
| 133 | $repos->backup; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | return $self; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | package GitHubBackup::Repository; |
|---|
| 141 | |
|---|
| 142 | use strict; |
|---|
| 143 | use warnings; |
|---|
| 144 | use utf8; |
|---|
| 145 | use Carp qw(croak); |
|---|
| 146 | use Git::Repository; |
|---|
| 147 | use File::chdir; |
|---|
| 148 | use File::Spec; |
|---|
| 149 | use File::Path qw(mkpath); |
|---|
| 150 | use LWP::UserAgent; |
|---|
| 151 | use JSON; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | sub new { |
|---|
| 155 | my $class = shift; |
|---|
| 156 | my $args = shift; |
|---|
| 157 | |
|---|
| 158 | return bless $args, $class; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | sub clone_url { |
|---|
| 162 | return (shift)->{repos}{clone_url}; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | sub full_name { |
|---|
| 166 | return (shift)->{repos}{full_name}; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | sub has_downloads { |
|---|
| 170 | return (shift)->{repos}{has_downloads}; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | sub forks_count { |
|---|
| 174 | return (shift)->{repos}{forks_count}; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | sub has_wiki { |
|---|
| 178 | return (shift)->{repos}{has_wiki}; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | sub has_issues { |
|---|
| 182 | return (shift)->{repos}{has_issues}; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | sub directory { |
|---|
| 186 | my $self = shift; |
|---|
| 187 | |
|---|
| 188 | my $path = $self->full_name; |
|---|
| 189 | if (my $base = $self->{directory}->()) { |
|---|
| 190 | $path = File::Spec->catfile($base, $path); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | return $path; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | sub message { |
|---|
| 197 | my $self = shift; |
|---|
| 198 | my $message = shift; |
|---|
| 199 | |
|---|
| 200 | print $self->full_name, " $message\n"; |
|---|
| 201 | |
|---|
| 202 | return $self; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | sub sync { |
|---|
| 206 | my $self = shift; |
|---|
| 207 | my $url = shift; |
|---|
| 208 | my $dir = shift; |
|---|
| 209 | |
|---|
| 210 | if (-d "$dir") { |
|---|
| 211 | local $CWD = $dir; |
|---|
| 212 | $self->message("++> $dir"); |
|---|
| 213 | Git::Repository->run(fetch => '--all'); |
|---|
| 214 | return $self; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | $self->message("==> $dir"); |
|---|
| 218 | mkpath $dir; |
|---|
| 219 | Git::Repository->run(clone => '--mirror' => $url => $dir); |
|---|
| 220 | |
|---|
| 221 | return $self; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | sub clone_git { |
|---|
| 225 | my $self = shift; |
|---|
| 226 | |
|---|
| 227 | my $dir = $self->directory . '.git'; |
|---|
| 228 | my $url = $self->clone_url; |
|---|
| 229 | |
|---|
| 230 | $self->sync($url => $dir); |
|---|
| 231 | |
|---|
| 232 | return $self; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | sub forks { |
|---|
| 236 | my $self = shift; |
|---|
| 237 | return $self->{forks} if ($self->{forks}); |
|---|
| 238 | |
|---|
| 239 | $self->{forks} = utils::get("/repos/" . $self->full_name . "/forks"); |
|---|
| 240 | |
|---|
| 241 | return $self->{forks}; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | sub set_forks { |
|---|
| 245 | my $self = shift; |
|---|
| 246 | |
|---|
| 247 | my $dir = $self->directory . '.git'; |
|---|
| 248 | local $CWD = $dir; |
|---|
| 249 | |
|---|
| 250 | my $remotes = Git::Repository->run(branch => '--remotes'); |
|---|
| 251 | my @fetch; |
|---|
| 252 | foreach my $fork (@{$self->forks}) { |
|---|
| 253 | if ($remotes =~ /$fork->{full_name}/) { |
|---|
| 254 | $self->message("--- ". $fork->{full_name}); |
|---|
| 255 | next; |
|---|
| 256 | } |
|---|
| 257 | $self->message("+++ ". $fork->{full_name}); |
|---|
| 258 | Git::Repository->run(remote => add => $fork->{full_name} => $fork->{clone_url}); |
|---|
| 259 | push @fetch, $fork->{full_name}; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | foreach my $fork (@fetch) { |
|---|
| 263 | $self->message("--> $fork"); |
|---|
| 264 | Git::Repository->run(fetch => $fork); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | return $self; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | sub clone_wiki { |
|---|
| 271 | my $self = shift; |
|---|
| 272 | |
|---|
| 273 | my $dir = $self->directory . '.wiki.git'; |
|---|
| 274 | my $url = 'https://github.com/' . $self->full_name . '.wiki.git'; |
|---|
| 275 | |
|---|
| 276 | my $ua = LWP::UserAgent->new(max_redirect => 0); |
|---|
| 277 | my $res = $ua->head( |
|---|
| 278 | 'https://github.com/' . $self->full_name . '/wiki' |
|---|
| 279 | ); |
|---|
| 280 | if ($res->code != 200) { |
|---|
| 281 | $self->message("wiki does not exist"); |
|---|
| 282 | return $self; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | $self->sync($url => $dir); |
|---|
| 286 | |
|---|
| 287 | return $self; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | sub issues { |
|---|
| 291 | my $self = shift; |
|---|
| 292 | return $self->{issues} if ($self->{issues}); |
|---|
| 293 | |
|---|
| 294 | my $open = utils::get("/repos/" . $self->full_name . "/issues"); |
|---|
| 295 | my $closed = utils::get("/repos/" . $self->full_name . "/issues", state => 'closed'); |
|---|
| 296 | |
|---|
| 297 | if ($open) { push @{$self->{issues}}, @$open } |
|---|
| 298 | if ($closed) { push @{$self->{issues}}, @$closed } |
|---|
| 299 | |
|---|
| 300 | return $self->{issues}; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | sub save_issues { |
|---|
| 304 | my $self = shift; |
|---|
| 305 | |
|---|
| 306 | my $ua = LWP::UserAgent->new; |
|---|
| 307 | my $json = JSON->new->utf8->indent; |
|---|
| 308 | |
|---|
| 309 | my $dir = $self->directory . '.issues'; |
|---|
| 310 | mkpath $dir unless (-d $dir); |
|---|
| 311 | local $CWD = $dir; |
|---|
| 312 | foreach my $issue (@{$self->issues}) { |
|---|
| 313 | my $number = $issue->{number}; |
|---|
| 314 | $self->message("+++ issue/$number"); |
|---|
| 315 | |
|---|
| 316 | open my $fh, ">$number.json"; |
|---|
| 317 | print $fh $json->encode($issue); |
|---|
| 318 | close $fh; |
|---|
| 319 | |
|---|
| 320 | if (exists $issue->{pull_request}{patch_url}) { |
|---|
| 321 | $ua->mirror($issue->{pull_request}{patch_url} => "$number.patch"); |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | return $self; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | sub backup { |
|---|
| 329 | my $self = shift; |
|---|
| 330 | |
|---|
| 331 | $self->clone_git if ($self->has_downloads eq 'true'); |
|---|
| 332 | $self->set_forks if ($self->forks_count > 0); |
|---|
| 333 | $self->clone_wiki if ($self->has_wiki eq 'true'); |
|---|
| 334 | $self->save_issues if ($self->has_issues eq 'true'); |
|---|
| 335 | |
|---|
| 336 | return $self; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | 1; |
|---|
| 341 | __END__ |
|---|