Skip to content

Commit

Permalink
Starting new module's git repo.
Browse files Browse the repository at this point in the history
Getting it under version control; nowhere near ready to release, yet.
  • Loading branch information
bigpresh committed May 3, 2011
0 parents commit 0750b9b
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Changes
@@ -0,0 +1,13 @@
Revision history for Bot-BasicBot-Pluggable-Module-GitHub

0.01 2011-05-03
First version. These are modules I've been using in #dancer on
irc.perl.org, and I figured it would be helpful to package them up and
release them to CPAN.

I have further plans to implement, including a module to handle GitHub
hooks by spawning a super-basic web server on a configured port which
would receive JSON payloads from GitHub's hooks and do the appropriate
thing (announcing commits/changed issues/pull requests/etc)


11 changes: 11 additions & 0 deletions MANIFEST
@@ -0,0 +1,11 @@
Changes
MANIFEST
Makefile.PL
README
lib/Bot/BasicBot/Pluggable/Module/GitHub.pm
lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm
lib/Bot/BasicBot/Pluggable/Module/GitHub/PullRequests.pm
t/00-load.t
t/manifest.t
t/pod-coverage.t
t/pod.t
20 changes: 20 additions & 0 deletions Makefile.PL
@@ -0,0 +1,20 @@
use strict;
use warnings;
use ExtUtils::MakeMaker;

WriteMakefile(
NAME => 'Bot::BasicBot::Pluggable::Module::GitHub',
AUTHOR => q{David Precious <davidp@preshweb.co.uk>},
VERSION_FROM => 'lib/Bot/BasicBot/Pluggable/Module/GitHub.pm',
ABSTRACT_FROM => 'lib/Bot/BasicBot/Pluggable/Module/GitHub.pm',
($ExtUtils::MakeMaker::VERSION >= 6.3002
? ('LICENSE'=> 'perl')
: ()),
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'Bot::BasicBot::Pluggable::Module' => 0,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Bot-BasicBot-Pluggable-Module-GitHub-*' },
);
50 changes: 50 additions & 0 deletions README
@@ -0,0 +1,50 @@
Bot-BasicBot-Pluggable-Module-GitHub

A module for Bot::BasicBot::Pluggable IRC bots, providing GitHub related IRC
utilities.

See the individual modules within the distribution for details on what features
each one provides.


INSTALLATION

To install this module, run the following commands:

perl Makefile.PL
make
make test
make install

SUPPORT AND DOCUMENTATION

After installing, you can find documentation for this module with the
perldoc command.

perldoc Bot::BasicBot::Pluggable::Module::GitHub

You can also look for information at:

RT, CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bot-BasicBot-Pluggable-Module-GitHub

AnnoCPAN, Annotated CPAN documentation
http://annocpan.org/dist/Bot-BasicBot-Pluggable-Module-GitHub

CPAN Ratings
http://cpanratings.perl.org/d/Bot-BasicBot-Pluggable-Module-GitHub

Search CPAN
http://search.cpan.org/dist/Bot-BasicBot-Pluggable-Module-GitHub/


LICENSE AND COPYRIGHT

Copyright (C) 2011 David Precious

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

12 changes: 12 additions & 0 deletions ignore.txt
@@ -0,0 +1,12 @@
blib*
Makefile
Makefile.old
Build
Build.bat
_build*
pm_to_blib*
*.tar.gz
.lwpcookies
cover_db
pod2htm*.tmp
Bot-BasicBot-Pluggable-Module-GitHub-*
113 changes: 113 additions & 0 deletions lib/Bot/BasicBot/Pluggable/Module/GitHub.pm
@@ -0,0 +1,113 @@
package Bot::BasicBot::Pluggable::Module::GitHub;
use base 'Bot::BasicBot::Pluggable::Module::GitHub';

# This module is intended to be used as a base by the B::B::P::M::GitHub::*
# modules, and provides some shared functionality (reading the default project
# for a channel from the bot's store, etc).
#
# It should not be loaded directly by the bot; load the desired modules you
# want.

use warnings;
use strict;

our $VERSION = '0.01';


sub github_project {
my ($self, $channel) = @_;

my $projects = $self->get('user_github_project')
or return false;

# The user may have provided channel-specific projects definitions in the
# format:
# '#channel1:user/repo;#channel2:user/repo'
return $projects if ($projects !~ /:/);

# Or, they may have simply provided a project name which should apply to
# all channels the bot is in.
my %repo_for_channel = map {
split /[:]/
} split /[;,]/, $projects;
return %repo_for_channel{$channel};
}




=head1 NAME
Bot::BasicBot::Pluggable::Module::GitHub - GitHub-related modules for IRC bots running Bot::BasicBot::Pluggable
=head1 MODULES
The following modules are included - see the documentation for each for details
on how to use them.
=over 4
=item L<Bot::BasicBot::Pluggable::Module::GitHub::PullRequests>
Monitor pull requests for GitHub projects.
=item L<Bot::BasicBot::Pluggable::Module::GitHub::EasyLinks>
Provide quick URLs to view issues/pull requests etc.
=back
=head1 Loading modules
See the L<Bot::BasicBot::Pluggable> documentation for how to load these modules
into your bot.
Do not load this module directly; load the modules named above individually.
This module is intended only to provide a base for the other modules, including
shared functionality and common documentation.
=head1 Configuring the default project repo
The modules above need to know what GitHub project repository they should refer
to.
You can configure a project which applies to all channels the bot is in, or
configure a different project for each channel if the bot is in multiple
channels.
The easiest way to change the setting is using the
L<Bot::BasicBot::Pluggable::Module::Vars> module - with that module loaded,
authenticate with the bot (see L<Bot::BasicBot::Pluggable::Module::Auth>) then
send the bot a command like:
set github_project githubusername/githubprojectname
For example, for this project:
set gitpub_project bigpresh/Bot-BasicBot-Pluggable-Module-GitHub
Alternatively, you can change the C<user_github_project> setting directly in the
bot's store; see the L<Bot::BasicBot::Pluggable::Store> documentation.
=head1 AUTHOR
David Precious C<<davidp@preshweb.co.uk>>
=head1 LICENSE AND COPYRIGHT
Copyright 2011 David Precious.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut


1; # End of Bot::BasicBot::Pluggable::Module::GitHub
64 changes: 64 additions & 0 deletions lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm
@@ -0,0 +1,64 @@
# A quick Bot::BasicBot::Pluggable module to provide easy links when someone
# mentions an issue / pull request / commit.
#
# Use the vars module to configure the project in question. (So, this will only
# really be of use to a bot in a single-project-related channel.)
#
# David Precious <davidp@preshweb.co.uk>

package Bot::BasicBot::Pluggable::Module::GitHubLinks;
use strict;
use base 'Bot::BasicBot::Pluggable::Module';
use LWP::Simple ();
use JSON;

sub help {
return <<HELPMSG;
Provide convenient links to GitHub issues/pull requests/commits etc.
If someone says e.g. "Issue 42", the bot will helpfully provide an URL to view
that issue directly.
The project these relate to must be configured using the vars module to set the
'default project' setting (or directly set user_default_project in the bot's
store).
HELPMSG
}


sub said {
my ($self, $mess, $pri) = @_;

return unless $pri == 2;

if ($mess->{body} =~ /^!pr (?: \s+ (\S+))?/xi) {
my $check_project = $1;
$check_project ||= get_default_project(
$self->get('user_github_project'), $mess->{channel}
);
if (!$check_project) {
$self->reply(
$mess,
"No GitHub project defined"
);
return 1;
}
for my $project (split /,/, $check_project) {
my $prs = $self->_get_pull_request_count($project);
$self->say(
channel => $mess->{channel},
body => "Open pull requests for $project : $prs",
);
}
return 1; # "swallow" this message
}
return 0; # This message didn't interest us
}





1;

79 changes: 79 additions & 0 deletions lib/Bot/BasicBot/Pluggable/Module/GitHub/PullRequests.pm
@@ -0,0 +1,79 @@
# 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
including a tally by user.
Usage: !pr user/project, or just !pr for the default project, configured by
setting the 'github_project' setting using the Vars module (or by directly
setting the user_github_project setting in the bot's store).
HELPMSG
}


sub said {
my ($self, $mess, $pri) = @_;

return unless $pri == 2;

if ($mess->{body} =~ /^!pr (?: \s+ (\S+))?/xi) {
my $check_projects = $1;
$check_projects ||= $self->get('user_github_project');
if (!$check_projects) {
$self->reply(
$mess,
"No project(s) to check; either specify"
. " a project, e.g. '!pr username/project', or use the Vars"
. " module to configure the github_project setting for this"
. " module to set the default project to check."
);
return 1;
}
for my $project (split /,/, $check_projects) {
my $prs = $self->_get_pull_request_count($project);
$self->say(
channel => $mess->{channel},
body => "Open pull requests for $project : $prs",
);
}
return 1; # "swallow" this message
}
return 0; # This message didn't interest us
}


sub _get_pull_request_count {
my ($self, $project) = @_;
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;

14 changes: 14 additions & 0 deletions t/00-load.t
@@ -0,0 +1,14 @@
#!perl -T

# Just make sure they compile, at least.

use Test::More tests => 3;

BEGIN {
use_ok( 'Bot::BasicBot::Pluggable::Module::GitHub');;
use_ok( 'Bot::BasicBot::Pluggable::Module::GitHub::EasyLinks');
use_ok( 'Bot::BasicBot::Pluggable::Module::GitHub::PullRequests');

}

diag( "Testing Bot::BasicBot::Pluggable::Module::GitHub $Bot::BasicBot::Pluggable::Module::GitHub::VERSION, Perl $], $^X" );

0 comments on commit 0750b9b

Please sign in to comment.