-
Notifications
You must be signed in to change notification settings - Fork 1
/
Github.pm
186 lines (133 loc) · 4.22 KB
/
Github.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package Group::Git::Github;
# Created on: 2013-05-04 20:18:31
# Create by: Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$
use Moo;
use strict;
use warnings;
use version;
use Carp;
use English qw/ -no_match_vars /;
use Net::GitHub;
use Path::Class;
our $VERSION = version->new('0.5.0');
extends 'Group::Git';
has github => (
is => 'rw',
#isa => 'Net::GitHub',
builder => '_github',
lazy => 1,
);
sub _repos {
my ($self) = @_;
my ($conf) = $self->conf;
my %repos = %{ $self->SUPER::_repos() };
my $repo = $self->github->repos;
my @list = $repo->list;
my $page = 0;
do {
$page++;
@list = $repo->query( $repo->next_url ) if !@list;
for my $repo (@list) {
my $url = $repo->{git_url};
# convert urls of the form:
# git://github.com/ivanwills/meteor.git
# to
# git@github.com:ivanwills/meteor.git
# as git doesn't like the form that github uses
$url =~ s{git://github.com/([^/]+)}{git\@github.com:$1};
$repos{ $repo->{name} } = Group::Git::Repo->new(
name => dir($repo->{name}),
git => $url,
);
# tag fork repos and original repos
if ( $repo->{fork} ) {
push @{ $conf->{tags}{forks} }, $repo->{name};
}
else {
push @{ $conf->{tags}{originals} }, $repo->{name};
}
}
@list = ();
} while ( $repo->has_next_page );
return \%repos;
}
sub _github {
my ($self) = @_;
my $conf = $self->conf;
return Net::GitHub->new(
$conf->{access_token}
? ( access_token => $conf->{access_token} )
: (
login => $conf->{username} ? $conf->{username} : prompt( -prompt => 'github.com username : ' ),
pass => $conf->{password} ? $conf->{password} : prompt( -prompt => 'github.com password : ', -echo => '*' ),
)
);
}
1;
__END__
=head1 NAME
Group::Git::Github - Adds reading all repositories you have access to on github.com
=head1 VERSION
This documentation refers to Group::Git::Github version 0.5.0.
=head1 SYNOPSIS
use Group::Git::Github;
# pull (or clone missing) all repositories that joeblogs has created/forked
my $ggg = Group::Git::Github->new(
conf => {
username => 'joeblogs@gmail.com',
password => 'myverysecurepassword',
},
);
# Alternitavely using personal access tokens
# You can setup at https://github.com/settings/applications
my $ggg = Group::Git::Github->new(
conf => {
access_token => '...',
},
);
# list all repositories
my $repositories = $ggg->repo();
# do something to each repository
for my $repo (keys %{$repositories}) {
# eg do a pull
$ggg->pull($repo);
}
=head1 DESCRIPTION
Reads all repositories for the configured user (if none set user will be
prompted to enter one as well as a password)
=head2 Configuration
There are three configuration parameters that are currently used
=over 4
=item access_token
A github OAuth personal access token. If supplied then username and password
are ignored.
=item username
Specify the user to login as, if not specified the user will be prompted to
enter a username.
=item password
Specify the password to login with, if not specified the user will be prompted
to enter a password.
=back
=head1 SUBROUTINES/METHODS
=head1 DIAGNOSTICS
=head1 CONFIGURATION AND ENVIRONMENT
=head1 DEPENDENCIES
=head1 INCOMPATIBILITIES
=head1 BUGS AND LIMITATIONS
There are no known bugs in this module.
Please report problems to Ivan Wills (ivan.wills@gmail.com).
Patches are welcome.
=head1 AUTHOR
Ivan Wills - (ivan.wills@gmail.com)
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2013 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
All rights reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. See L<perlartistic>. This program is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
=cut