Skip to content

Commit

Permalink
My initial work towards handling hooks.
Browse files Browse the repository at this point in the history
This was intended to use the new hook-configuring support in GH's v3 API, but
they deprecated the auth tokens we use for logging in, so I put this on hold
ages back as it will mean overhauling the module to want username and password
instead of username and API token.

I don't much like that, TBH, but trying to get oAuth working for this  is about
as attractive as being buggered by a herde of randy wildebeest with sand for
lube whilst listening to Justin Bieber, so it's the lesser of two evils.
  • Loading branch information
bigpresh committed Sep 21, 2012
1 parent 21c3da6 commit d129a67
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lib/Bot/BasicBot/Pluggable/Module/GitHub/HookAnnounce.pm
@@ -0,0 +1,81 @@
use v5.12;
package Bot::BasicBot::Pluggable::Module::GitHub::HookAnnounce;
use base qw(Bot::BasicBot::Pluggable::Module::GitHub);

# Use GitHub's hooks API to add web hooks so we can be told about pushes,
# issues, pull requests etc and announce them.

# Upon startup, add a hook matching all the event types to hit us, unless
# there's already one there (if there is, maybe delete & recreate it, just in
# case?)

# I guess arranging all the hooks makes us a hooker?

# See http://developer.github.com/v3/repos/hooks/

use POE qw/Component::Server::HTTP/;
use CGI::Simple;
use JSON qw(from_json);

sub help {
my ($self, $msg) = @_;
return "GitHub Module for interpreting github service hook postbacks";
}

sub init {
my $self = shift;
$self->config({ user_port => 3333, user_url => '/github' });


# For every repo we're configured to work with, get the list of hooks
# configured, and add any that are missing via Net::GitHub::V3

# Configure each hook to hit this server with the event type appended, e.g.:
# http://hostname:port/bbmp-github-hook?event=issues

POE::Component::Server::HTTP->new(
Port => $self->get('user_port'),
ContentHandler => {
$self->get('user_url') => sub {
my ($req, $res) = @_;
my $cgi = CGI::Simple->new($req->content);
my $event = $cgi->param('event')
or return "Missing event type param";
my $payload_json = $cgi->param('payload')
or return "Missing payload";

my $payload = JSON::from_json($payload_json)
or return "Invalid JSON?";

$self->handle_hook_payload($event, $payload);
},
},
);
}

# Handle a hook being hit. Receives a payload, the format of which varies
# depending on the type of event.
sub handle_hook_payload {
my ($self, $event_type, $payload) = @_;
my $payload = CGI::Simple->new($req->content)->param('payload');
...
}

sub commit {
my ($self, $payload) = @_;
my @channels = $self->bot->channels;

for my $commit (@{ $payload->{commits} || [] }) {
my $hash_id = substr($commit->{id}, 0, 7);
my $branch = (split(/\//, $payload->{ref}))[2];
for (@channels){
$self->bot->notice(
channel => $_,
body => "[$payload->{repository}{name}/$branch $hash_id]: ".
"($commit->{author}{name}) $commit->{message}",
);
}
}
}

1;

0 comments on commit d129a67

Please sign in to comment.