Permalink
Browse files

collaborator API

  • Loading branch information...
1 parent 767038d commit 1f20c105996579c516ccbf3c49c2587a803774d1 @fayland committed Sep 23, 2011
Showing with 119 additions and 5 deletions.
  1. +1 −1 Makefile.PL
  2. +118 −4 lib/Net/GitHub/V3/Repos.pm
View
2 Makefile.PL
@@ -5,7 +5,7 @@ all_from 'lib/Net/GitHub.pm';
author 'Fayland Lam <fayland@gmail.com>';
license 'perl';
-repository 'http://github.com/fayland/perl-net-github/tree/master';
+repository 'https://github.com/fayland/perl-net-github';
requires 'MIME::Base64';
requires 'URI';
View
122 lib/Net/GitHub/V3/Repos.pm
@@ -9,6 +9,9 @@ use URI::Escape;
with 'Net::GitHub::V3::Query';
+has 'user' => (is => 'rw', isa => 'Str');
+has 'repos' => (is => 'rw', isa => 'Str');
+
sub list {
my ( $self, $type ) = @_;
$type ||= 'all';
@@ -19,6 +22,7 @@ sub list {
sub list_user {
my ($self, $user, $type) = @_;
+ $user ||= $self->user;
$type ||= 'all';
my $u = "/users/" . uri_escape($user) . "/repos";
$u .= '?type=' . $type if $type ne 'all';
@@ -47,37 +51,110 @@ sub create {
sub get {
my ($self, $user, $repos) = @_;
+ $user ||= $self->user; $repos ||= $self->repos;
return $self->query("/repos/" . uri_escape($user) . "/" . uri_escape($repos));
}
sub update {
- my ($self, $user, $repos, $new_repos) = @_;
+ my $self = shift;
+
+ if (@_ == 1) {
+ unshift @_, $self->repos;
+ unshift @_, $self->user;
+ }
+ my ($user, $repos, $new_repos) = @_;
my $u = "/repos/" . uri_escape($user) . "/" . uri_escape($repos);
return $self->query('PATCH', $u, $new_repos);
}
sub contributors {
my ($self, $user, $repos) = @_;
+ $user ||= $self->user; $repos ||= $self->repos;
return $self->query("/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/contributors');
}
sub languages {
my ($self, $user, $repos) = @_;
+ $user ||= $self->user; $repos ||= $self->repos;
return $self->query("/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/languages');
}
sub teams {
my ($self, $user, $repos) = @_;
+ $user ||= $self->user; $repos ||= $self->repos;
return $self->query("/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/teams');
}
sub tags {
my ($self, $user, $repos) = @_;
+ $user ||= $self->user; $repos ||= $self->repos;
return $self->query("/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/tags');
}
sub branches {
my ($self, $user, $repos) = @_;
+ $user ||= $self->user; $repos ||= $self->repos;
return $self->query("/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/branches');
}
+## http://developer.github.com/v3/repos/collaborators/
+
+sub collaborators {
+ my ($self, $user, $repos) = @_;
+ $user ||= $self->user; $repos ||= $self->repos;
+ return $self->query("/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/collaborators');
+}
+
+sub is_collaborator {
+ my $self = shift;
+
+ if (@_ == 1) {
+ unshift @_, $self->repos;
+ unshift @_, $self->user;
+ }
+ my ($user, $repos, $collaborator) = @_;
+
+ my $u = "/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/collaborators/' . uri_escape($collaborator);
+
+ my $old_raw_response = $self->raw_response;
+ $self->raw_response(1); # need check header
+ my $res = $self->query($u);
+ $self->raw_response($old_raw_response);
+ return $res->header('Status') =~ /204/ ? 1 : 0;
+}
+
+sub add_collaborator {
+ my $self = shift;
+
+ if (@_ == 1) {
+ unshift @_, $self->repos;
+ unshift @_, $self->user;
+ }
+ my ($user, $repos, $collaborator) = @_;
+
+ my $u = "/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/collaborators/' . uri_escape($collaborator);
+
+ my $old_raw_response = $self->raw_response;
+ $self->raw_response(1); # need check header
+ my $res = $self->query('PUT', $u);
+ $self->raw_response($old_raw_response);
+ return $res->header('Status') =~ /204/ ? 1 : 0;
+}
+sub delete_collaborator {
+ my $self = shift;
+
+ if (@_ == 1) {
+ unshift @_, $self->repos;
+ unshift @_, $self->user;
+ }
+ my ($user, $repos, $collaborator) = @_;
+
+ my $u = "/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/collaborators/' . uri_escape($collaborator);
+
+ my $old_raw_response = $self->raw_response;
+ $self->raw_response(1); # need check header
+ my $res = $self->query('DELETE', $u);
+ $self->raw_response($old_raw_response);
+ return $res->header('Status') =~ /204/ ? 1 : 0;
+}
+
no Any::Moose;
__PACKAGE__->meta->make_immutable;
@@ -94,6 +171,12 @@ Net::GitHub::V3::Repos - GitHub Repos API
my $gh = Net::GitHub::V3->new; # read L<Net::GitHub::V3> to set right authentication info
my $repos = $gh->repos;
+
+ # set user/repos for simple calls
+ $repos->user('fayland');
+ $repos->repos('perl-net-github');
+ my @contributors = $repos->contributors; # don't need pass user and repos
+
=head1 DESCRIPTION
@@ -111,7 +194,7 @@ L<http://developer.github.com/v3/repos/>
=item list_org
- my $rp = $repos->list;
+ my $rp = $repos->list; # or my @rp = $repos->list;
my $rp = $repos->list('private');
my $rp = $repos->list_user('c9s');
my $rp = $repos->list_user('c9s', 'member');
@@ -136,10 +219,20 @@ L<http://developer.github.com/v3/repos/>
=item get
+ my $rp = $repos->get('fayland', 'perl-net-github');
+
+=back
+
+<B>SET user/repos before call methods below</B>
+
+ $repos->user('fayland');
+ $repos->repos('perl-net-github');
+
+=over 4
+
=item update
- my $rp = $repos->get('fayland', 'perl-net-github');
- $repos->update('fayland', 'perl-net-github', { homepage => 'https://metacpan.org/module/Net::GitHub' });
+ $repos->update({ homepage => 'https://metacpan.org/module/Net::GitHub' });
=item contributors
@@ -159,6 +252,27 @@ L<http://developer.github.com/v3/repos/>
=back
+=head3 Repo Collaborators API
+
+L<http://developer.github.com/v3/repos/collaborators/>
+
+=over 4
+
+=item collaborators
+
+=item is_collaborator
+
+=item add_collaborator
+
+=item delete_collaborator
+
+ my @collaborators = $repos->collaborators;
+ my $is = $repos->is_collaborator('fayland');
+ $repos->add_collaborator('fayland');
+ $repos->delete_collaborator('fayland');
+
+=back
+
=head1 AUTHOR
Fayland Lam, C<< <fayland at gmail.com> >>

0 comments on commit 1f20c10

Please sign in to comment.