Skip to content

Instantly share code, notes, and snippets.

@dch
Created April 30, 2012 20:42
Show Gist options
  • Save dch/2562515 to your computer and use it in GitHub Desktop.
Save dch/2562515 to your computer and use it in GitHub Desktop.
pullreq.pl
#!/usr/bin/env perl
use Modern::Perl;
use Mojo::UserAgent;
my $api = "https://api.github.com";
my $repo = "$api/repos/apache/couchdb";
################################################################################
# TODO
# modify AUTHORS directly
# checkout and patch to new branch
my %pull_req = ();
my ($github, $response);
$pull_req{id} = shift or die "Usage: {$0} <pull_request_id>";
$pull_req{url} = "$repo/pulls/$pull_req{id}";
$github = Mojo::UserAgent->new();
# from github Pull Request API JSON object
$response = $github->get($pull_req{url})->res;
$pull_req{clone_url} = $response->json('/head/repo/clone_url');
$pull_req{commit_sha} = $response->json('/head/sha');
$pull_req{patch_url} = $response->json('/patch_url');
$pull_req{title} = $response->json('/title');
$pull_req{owner} = $response->json('/user/login');
$pull_req{patch} = $github->get($pull_req{patch_url})->res->body;
# extract the name & email from the patch in case /user/:id is empty
{
# From: [optional name] <[email protected]>
my @tmp = grep {s/^From:\s*//gx} (split /\n/, $pull_req{patch});
$pull_req{contributor} = shift @tmp;
}
# may return empty if user has not set field, check in patch if needed
$response = $github->get("$api/users/$pull_req{owner}")->res;
$pull_req{contributor} =~ m/(.+) \s* < (.+) >/gx;
$pull_req{name} =
$response->json('/name')
? $response->json('/name')
: $1;
$pull_req{email} =
$response->json('/email')
? $response->json('/email')
: $2;
say
qq(git merge --verbose --edit --stat FETCH_HEAD -m 'Merged pull request \#$pull_req{id} from \@$pull_req{owner}, with thanks');
say qq(Contributed by: $pull_req{name} <$pull_req{email}>);
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment