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 | account |
---|
36 | repository |
---|
37 | )); |
---|
38 | |
---|
39 | |
---|
40 | # both hash and hashref are acceptable |
---|
41 | sub new { |
---|
42 | my $class = shift; |
---|
43 | |
---|
44 | my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_}; |
---|
45 | if (! $args->{directory}) { |
---|
46 | $args->{directory} = "."; |
---|
47 | } |
---|
48 | |
---|
49 | return $class->SUPER::new($args); |
---|
50 | } |
---|
51 | |
---|
52 | sub repos { |
---|
53 | my $self = shift; |
---|
54 | return $self->{repos} if ($self->{repos}); |
---|
55 | |
---|
56 | my $account = $self->account or croak "account is not set"; |
---|
57 | if (my $repository = $self->repository) { |
---|
58 | $self->{repos} = [ |
---|
59 | GitHubBackup::Repository->new({ |
---|
60 | directory => sub {$self->directory}, |
---|
61 | full_name => "$account/$repository", |
---|
62 | }) |
---|
63 | ]; |
---|
64 | |
---|
65 | return $self->{repos}; |
---|
66 | } |
---|
67 | |
---|
68 | my $page = 1; |
---|
69 | my @repos; |
---|
70 | while (1) { |
---|
71 | my $result = utils::json_api("/users/$account/repos?per_page=100&page=$page"); |
---|
72 | if (ref($result) eq 'ARRAY' && scalar @$result > 0) { |
---|
73 | push @repos, @$result; |
---|
74 | $page++; |
---|
75 | |
---|
76 | next; |
---|
77 | } |
---|
78 | last; |
---|
79 | } |
---|
80 | |
---|
81 | foreach my $repos (@repos) { |
---|
82 | push @{$self->{repos}}, |
---|
83 | GitHubBackup::Repository->new({ |
---|
84 | directory => sub {$self->directory}, |
---|
85 | full_name => $repos->{full_name}, |
---|
86 | clone_url => $repos->{clone_url}, |
---|
87 | }) |
---|
88 | ; |
---|
89 | } |
---|
90 | |
---|
91 | return $self->{repos}; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | package GitHubBackup::Repository; |
---|
96 | use base qw(Class::Accessor::Fast); |
---|
97 | |
---|
98 | use strict; |
---|
99 | use warnings; |
---|
100 | use utf8; |
---|
101 | use Carp qw(croak); |
---|
102 | use Git::Repository; |
---|
103 | use File::chdir; |
---|
104 | |
---|
105 | |
---|
106 | sub new { |
---|
107 | my $class = shift; |
---|
108 | my $args = shift; |
---|
109 | |
---|
110 | if (! exists $args->{clone_url}) { |
---|
111 | my $result = utils::json_api('/repos/' . $args->{full_name}); |
---|
112 | $args->{clone_url} = $result->{clone_url}; |
---|
113 | } |
---|
114 | |
---|
115 | return $class->SUPER::new($args); |
---|
116 | } |
---|
117 | |
---|
118 | sub directory { |
---|
119 | my $self = shift; |
---|
120 | |
---|
121 | return $self->{directory}->(); |
---|
122 | } |
---|
123 | |
---|
124 | sub clone_git { |
---|
125 | my $self = shift; |
---|
126 | |
---|
127 | my $dir = $self->directory .'/'. $self->{full_name}; |
---|
128 | if (-d "$dir") { |
---|
129 | local $CWD = $dir; |
---|
130 | print "fetch ", $self->{full_name}, "\n"; |
---|
131 | Git::Repository->run(fetch => '--all'); |
---|
132 | return $self; |
---|
133 | } |
---|
134 | |
---|
135 | print "clone ", $self->{full_name}, "\n"; |
---|
136 | Git::Repository->run(clone => '--mirror' => $self->{clone_url} => $dir); |
---|
137 | return $self; |
---|
138 | } |
---|
139 | |
---|
140 | sub set_forks { |
---|
141 | my $self = shift; |
---|
142 | } |
---|
143 | |
---|
144 | sub clone_wiki { |
---|
145 | my $self = shift; |
---|
146 | } |
---|
147 | |
---|
148 | sub save_issues { |
---|
149 | my $self = shift; |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | 1; |
---|
155 | __END__ |
---|