GitHub/PullRequests.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« GitHub/EasyLinks.pm ↑ Summary GitHub.pm »
1 # A quick Bot::BasicBot::Pluggable module to fetch a count of open pull requests
2 # for a GitHub project.
3 #
4 # David Precious <davidp@preshweb.co.uk>
5  
6 package Bot::BasicBot::Pluggable::Module::GitHub::PullRequests;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Bot::BasicBot::Pluggable::Module::GitHub;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use base 'Bot::BasicBot::Pluggable::Module::GitHub';
10 use LWP::Simple ();
11 use JSON;
12  
13 sub help {
14 return <<HELPMSG;
15 Monitors outstanding pull requests on a GitHub project.
16  
17 Allows use of a !pr command to fetch the current count of open pull requests
18 including a tally by user.
19  
20 Usage: !pr user/project, or just !pr for the default project, configured by
21 setting the 'github_project' setting using the Vars module (or by directly
22 setting the user_github_project setting in the bot's store).
23 HELPMSG
24 }
25  
26  
27 sub said {
28 my ($self, $mess, $pri) = @_;
29
30 return unless $pri == 2;
31  
32 if ($mess->{body} =~ /^!pr (?: \s+ (\S+))?/xi) {
33 my $check_projects = $1;
34 $check_projects ||= $self->get('user_github_project');
35 if (!$check_projects) {
36 $self->reply(
37 $mess,
38 "No project(s) to check; either specify"
39 . " a project, e.g. '!pr username/project', or use the Vars"
40 . " module to configure the github_project setting for this"
41 . " module to set the default project to check."
42 );
43 return 1;
44 }
45 for my $project (split /,/, $check_projects) {
46 my $prs = $self->_get_pull_request_count($project);
47 $self->say(
48 channel => $mess->{channel},
49 body => "Open pull requests for $project : $prs",
50 );
51 }
52 return 1; # "swallow" this message
53 }
54 return 0; # This message didn't interest us
55 }
56  
57  
58 sub _get_pull_request_count {
59 my ($self, $project) = @_;
60 my $url = "http://github.com/api/v2/json/pulls/" . $project;
61 my $json = LWP::Simple::get($url)
62 or return "Unknown - error fetching $url";
63 my $pulls = JSON::from_json($json)
64 or return "Unknown - error parsing API response";
65  
66 my %pulls_by_author;
67 $pulls_by_author{$_}++
68 for map { $_->{issue_user}{login} } @{ $pulls->{pulls} };
69 my $msg = scalar @{ $pulls->{pulls} } . " pull requests open (";
70 $msg .= join(", ",
71 map { "$_:$pulls_by_author{$_}" }
72 sort { $pulls_by_author{$b} <=> $pulls_by_author{$a} }
73 keys %pulls_by_author
74 );
75 $msg .= ")";
76 return $msg;
77 }
78  
79 1;
80  
« GitHub/EasyLinks.pm ↑ Summary GitHub.pm »