Skip to content

Instantly share code, notes, and snippets.

@dagolden
Created March 27, 2013 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dagolden/5258683 to your computer and use it in GitHub Desktop.
Save dagolden/5258683 to your computer and use it in GitHub Desktop.
#!/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;
if ( $opts->get_repo ) {
@repos = $opts->get_repo;
}
elsif ( $opts->get_all ) {
@repos = map { $_->{name} } $gh->repos->list;
while ($gh->repos->has_next_page) {
push @repos, map { $_->{name} } $gh->repos->next_page;
}
}
else {
@repos = _detect_repo();
}
list_pulls($gh, $github_user, $_) for @repos;
@ecocode
Copy link

ecocode commented May 5, 2013

hi dagolden,

Nice script. I patched it so that it will now list repo pull requests for repos to which the user is a contributor too.
https://gist.github.com/ecocode/5520392/57f1cc1ba1ee8401bee3a987163f82fef9735d95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment