Created
April 26, 2011 11:59
-
-
Save bigpresh/942144 to your computer and use it in GitHub Desktop.
GitHubPullRequests - not fetching default project from bot store
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
# A quick Bot::BasicBot::Pluggable module to fetch a count of open pull requests | |
# for a GitHub project. | |
# | |
# David Precious <davidp@preshweb.co.uk> | |
package Bot::BasicBot::Pluggable::Module::GitHubPullRequests; | |
use strict; | |
use base 'Bot::BasicBot::Pluggable::Module'; | |
use LWP::Simple; | |
use JSON; | |
sub help { | |
return <<HELPMSG; | |
Monitors outstanding pull requests on a GitHub project. | |
Allows use of a !pr command to fetch the current count of open pull requests, | |
and can also periodically announce them on a timer. | |
HELPMSG | |
} | |
sub said { | |
my ($self, $mess, $pri) = @_; | |
#return unless $pri == 2; | |
if ($mess->{body} =~ /!pr (?: \s+ (\S+))?/xi) { | |
warn "About to check for PRs"; | |
my $check_projects = $1 || $self->get('monitor_projects'); | |
warn "Checking projects: $check_projects"; | |
$self->reply($mess, "OK, I'll check for PRs on $check_projects"); | |
for my $project (split /,/, $check_projects) { | |
warn "Checking PRs for $project"; | |
my $prs = _get_pull_request_count($project); | |
$self->say( | |
channel => $mess->{channel}, | |
body => "Open pull requests for $project : $prs", | |
); | |
} | |
return 1; # "swallow" this message | |
} | |
return; # This message didn't interest us | |
} | |
sub _get_pull_request_count { | |
my $project = shift; | |
my $url = "http://github.com/api/v2/json/pulls/" . $project; | |
my $json = LWP::Simple::get($url) | |
or return "Unknown - error fetching $url"; | |
my $pulls = JSON::from_json($json) | |
or return "Unknown - error parsing API response"; | |
my %pulls_by_author; | |
$pulls_by_author{$_}++ | |
for map { $_->{issue_user}{login} } @{ $pulls->{pulls} }; | |
my $msg = scalar @{ $pulls->{pulls} } . " pull requests open ("; | |
$msg .= join(", ", | |
map { "$_:$pulls_by_author{$_}" } | |
sort { $pulls_by_author{$b} <=> $pulls_by_author{$a} } | |
keys %pulls_by_author | |
); | |
$msg .= ")"; | |
return $msg; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment