-
-
Save ecocode/5520392 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use v5.10; | |
use strict; | |
use warnings; | |
use autodie; | |
use Carp; | |
use Net::GitHub; | |
use Getopt::Lucid ':all'; | |
use Text::Wrap qw/fill/; | |
my $opts = Getopt::Lucid->getopt( | |
[ | |
Param('repo|r'), | |
Switch('all'), | |
] | |
); | |
$opts->validate; | |
sub _detect_repo { | |
my ($origin) = grep { /origin/ } `git remote -v`; | |
die "Couldn't determine origin\n" unless $origin; | |
chomp $origin; | |
$origin =~ s/^origin\s+//; | |
$origin =~ s/\s+\(.*$//; | |
my $repo; | |
if ( $origin =~ m{^.+?://github.com/[^/]+/(.+)\.git$} ) { | |
$repo = $1; | |
} | |
elsif ( $origin =~ m{^git\@github\.com:[^/]+/(.+)\.git$} ) { | |
$repo = $1; | |
} | |
else { | |
die "Can't determine repo name from '$origin'. Try manually with -r REPO\n" | |
} | |
return $repo; | |
} | |
sub _git_config { | |
my $key = shift; | |
chomp( my $value = `git config --get $key` ); | |
croak "Unknown $key" unless $value; | |
return $value; | |
} | |
sub list_pulls { | |
my ($gh, $user, $repo) = @_; | |
my $pull_request = $gh->pull_request; | |
my @pulls = $pull_request->pulls($user, $repo); | |
return unless @pulls; | |
say "$user/$repo:\n"; | |
for my $p ( @pulls ) { | |
my $title = " $p->{created_at} $p->{title}\n"; | |
my $text = ''; | |
$text .= fill('','',$p->{body}) . "\n\n" if $p->{body}; | |
$text .= "$p->{html_url}\n\n"; | |
$text .= "git-fetch-branch $p->{head}{repo}{git_url} $p->{head}{ref}\n"; | |
$text =~ s/^/ /msg; | |
say "$title\n$text\n"; | |
} | |
} | |
my $github_user = _git_config("github.user"); | |
my $github_token = _git_config("github.token"); | |
my $gh = Net::GitHub->new( access_token => $github_token ); | |
my @repos; # array of repo hashes containing owner and (repo)name | |
if ( $opts->get_repo ) { | |
@repos = [{ owner => $github_user, | |
name => $opts->get_repo }]; | |
} | |
elsif ( $opts->get_all ) { | |
@repos = map { { owner => $_->{owner}->{login}, | |
name => $_->{name} } } $gh->repos->list; | |
while ($gh->repos->has_next_page) { | |
push @repos, map { { owner => $_->{owner}->{login}, | |
name => $_->{name} } } $gh->repos->next_page; | |
} | |
} | |
else { | |
@repos = [ { owner=>$github_user, name =>_detect_repo() } ]; | |
} | |
list_pulls($gh, $_->{owner}, $_->{name}) for @repos; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment