Perl - Amon2::Plugin::Web::Github::Webhook を書いた

github webhook アプリをつくろうとしたとき

  • github からのアクセスのみに制限したい
  • x-github-envent ヘッダーに簡単にアクセスしたい
  • body parameter payload に簡単にアクセスしたい

という欲求がでてきたのでプラグインにしてみた。

このプラグインをロードすると $hash = $c->github メソッドが生え、 $hash から event, payload にアクセスできるようになる。 もしリクエストが github からのものでない場合は false が返るようになってる。

use Amon2::Lite;

__PACKAGE__->load_plugin('Web::Github::Webhook');

post '/hook' => sub {
    my $c = shift;
    return $c->res_403 unless my $github = $c->github;

    # $github = {
    #     evnet    => 'push',
    #     delivery => '19967914-9615-11e3-85b5-...',
    #     payload  => {
    #         after => '6fbadfc1ac7b6a7d32bca086ac95d049e0d8b03c',
    #         before => '4f9585dd9fbd86f20c21e3521e4de48a504f58b0',
    #         commits => [ {...} ],
    #         ...,
    #         pusher => ...,
    #         ref => 'refs/heads/master',
    #         repository => {...},
    #     },
    # };

    my $event = $github->{event};
    my $payload = $github->{payload};
    my $ref = $payload->{ref};
    ...
};

__PACKAGE__->to_app;

インストール方法

> cpanm git://github.com/shoichikaji/Amon2-Plugin-Web-Github-Webhook.git