Skip to content

Instantly share code, notes, and snippets.

@throughnothing
Created October 18, 2011 20:28
Show Gist options
  • Save throughnothing/1296623 to your computer and use it in GitHub Desktop.
Save throughnothing/1296623 to your computer and use it in GitHub Desktop.
Github Postback receiver for announcing events in IRC using Bot::BasicBot::Pluggable::Module
use v5.12;
package Bot::BasicBot::Pluggable::Module::GitHub::PostBack;
use base qw(Bot::BasicBot::Pluggable::Module);
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' });
POE::Component::Server::HTTP->new(
Port => $self->get('user_port'),
ContentHandler => {
$self->get('user_url') => sub {
my ($req, $res) = @_;
my $payload = CGI::Simple->new($req->content)->param('payload');
$self->commit(from_json($payload)) if $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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment