App-GitHubPullRequest
view release on metacpan or search on metacpan
lib/App/GitHubPullRequest.pm view on Meta::CPAN
use Carp qw(croak);
use Encode qw(encode_utf8);
use File::Spec;
use constant DEBUG => $ENV{'GIT_PR_DEBUG'} || 0;
sub new {
my ($class) = @_;
return bless {}, $class;
}
sub run {
my ($self, @args) = @_;
my $cmd = scalar @args ? shift @args : 'list';
my $method = $self->can($cmd);
return $self->$method(@args) if $method;
return $self->help(@args);
}
sub help {
my ($self, @args) = @_;
print <<"EOM";
$0 [<command> <args> ...]
Where command is one of these:
help Show this page
list [<state>] Show all pull requests (state: open/closed)
show <number> Show details for the specified pull request
patch <number> Fetch a properly formatted patch for specified pull request
checkout <number> Create tracking branch for specified pull request
login [<user>] [<password>] [<two-factor-auth-token>]
Login to GitHub and receive an access token (deprecated)
authorize Create a GitHub OAuth personal access token
comment <number> [<text>] Create a comment on the specified pull request
close <number> Close the specified pull request
open <number> Reopen the specified pull request
EOM
return 1;
}
sub list {
my ($self, $state) = @_;
$state ||= 'open';
my $remote_repo = _find_github_remote();
my $prs = _api_read("/repos/$remote_repo/pulls?state=$state");
say ucfirst($state) . " pull requests for '$remote_repo':";
unless ( @$prs ) {
say "No pull requests found.";
return 0;
}
foreach my $pr ( @$prs ) {
my $number = $pr->{"number"};
my $title = encode_utf8( $pr->{"title"} );
my $date = $pr->{"updated_at"} || $pr->{'created_at'};
say join(" ", $number, $date, $title);
}
return 0;
}
sub show {
my ($self, $number, @args) = @_;
die("Please specify a pull request number.\n") unless $number;
my $pr = $self->_fetch_one($number);
die("Unable to fetch pull request $number.\n")
unless defined $pr;
{
my $user = $pr->{'user'}->{'login'};
my $title = encode_utf8( $pr->{"title"} );
my $body = encode_utf8( $pr->{"body"} );
my $date = $pr->{"updated_at"} || $pr->{'created_at'};
say "Date: $date";
say "From: $user";
say "Subject: $title";
say "Number: $number";
say "\n$body\n" if $body;
}
my $comments = _api_read( $pr->{'comments_url'} );
foreach my $comment (@$comments) {
my $user = $comment->{'user'}->{'login'};
my $date = $comment->{'updated_at'} || $comment->{'created_at'};
my $body = encode_utf8( $comment->{'body'} );
say "-" x 79;
say "Date: $date";
say "From: $user";
say "\n$body\n";
}
return 0;
}
sub patch {
my ($self, $number) = @_;
die("Please specify a pull request number.\n") unless $number;
my $patch = _get_url(
$self->_fetch_one($number)->{'patch_url'}
);
die("Unable to fetch patch for pull request $number.\n")
unless defined $patch;
print $patch;
return 0;
}
sub checkout {
my ($self, $number) = @_;
$number =~ s{[^\d]}{}g if defined $number;
die("Please specify a pull request number.\n") unless $number;
my $pr = $self->_fetch_one($number);
# Get required contributor branch info
my $head_repo = $pr->{'head'}->{'repo'}->{'clone_url'};
my $head_branch = $pr->{'head'}->{'ref'};
my $head_user = $pr->{'head'}->{'user'}->{'login'};
# Check if the remote already exists in our repo
my $head_remote;
foreach my $line ( _qx("git", "remote -v") ) {
my ($remote, $url, $type) = split /\s+/, $line;
next unless $type eq '(fetch)'; # only consider fetch remotes
if ( $url eq $head_repo ) {
$head_remote = $remote;
last;
}
}
if ( $head_remote ) {
# Remote already exists, try to update its state
unless ( _qx(qw(git branch --list -r), qq{"$head_remote/$head_branch"}) ) {
# Create a new tracking branch, because one doesn't already exist
my ($content, $rc) = _run_ext(
qw(git remote set-branches),
'--add', # don't remove any other existing tracking branches
$head_remote, # our remote's name/alias
$head_branch, # the ref we want to track
);
die("git failed with error $rc when trying to add tracking branch"
. " to existing remote.\n")
if $rc != 0;
}
# Fetch changes from just added remote
say "Fetching changes from '$head_remote/$head_branch'";
my ($content, $rc) = _run_ext(
qw(git fetch),
$head_remote,
);
die("git failed with error $rc when trying to update remote.\n")
if $rc != 0;
}
else {
# Create and fetch the branch info if it doesn't exist already
$head_remote = $head_user;
my ($content, $rc) = _run_ext(
qw(git remote add),
'-f', # only fetch specific refs
'-t', $head_branch, # add only a ref for this ref, not all
$head_remote, # what we'll name our remote
$head_repo, # URL to the head repo
);
die("git failed with error $rc when trying to add remote.\n")
if $rc != 0;
}
# Actually checkout the ref we just updated as pr/<number>
my ($content, $rc) = _run_ext(
qw(git checkout),
'-b', "pr/$number",
'--track', "$head_remote/$head_branch",
);
die("git failed with error $rc when trying to check out branch.\n")
if $rc != 0;
return 0;
}
sub close { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
my ($self, $number) = @_;
die("Please specify a pull request number.\n") unless $number;
my $pr = $self->_state($number, 'closed');
die("Unable to close pull request $number.\n")
unless defined $pr;
say "Pull request $number now in state: " . $pr->{'state'};
return 0;
}
sub open { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
my ($self, $number) = @_;
die("Please specify a pull request number.\n") unless $number;
my $pr = $self->_state($number, 'open');
die("Unable to open pull request $number.\n")
unless defined $pr;
say "Pull request $number now in state: " . $pr->{'state'};
return 0;
}
sub comment {
my ($self, $number, $text) = @_;
die("Please specify a pull request number.\n") unless $number;
my $remote_repo = _find_github_remote();
my $filename;
unless ( $text ) {
$filename = _tmpfile("$remote_repo-$number");
# Find and execute editor on temporary file
my $editor = $ENV{'EDITOR'};
unless ( $editor ) {
_require_binary('editor');
$editor = 'editor';
}
system($editor, $filename);
# Fetch text just edited (if any)
if ( -r $filename ) {
CORE::open my $fh, "<:encoding(UTF-8)", $filename or die "Can't open $filename: $!";
$text = join "", <$fh>;
CORE::close $fh;
}
# Abort if no text found
unless ( $text ) {
unlink $filename;
die("Your comment is empty. Command aborted.\n");
}
}
( run in 1.479 second using v1.01-cache-2.11-cpan-e1769b4cff6 )