| 1 | package Zapp::Trigger::Webhook; | ||||||||||
| 2 | # ABSTRACT: Trigger a plan from a web request | ||||||||||
| 3 | |||||||||||
| 4 | #pod =head1 SYNOPSIS | ||||||||||
| 5 | #pod | ||||||||||
| 6 | #pod =head1 DESCRIPTION | ||||||||||
| 7 | #pod | ||||||||||
| 8 | #pod =head1 CONFIG | ||||||||||
| 9 | #pod | ||||||||||
| 10 | #pod =head1 OUTPUT | ||||||||||
| 11 | #pod | ||||||||||
| 12 | #pod =head1 SEE ALSO | ||||||||||
| 13 | #pod | ||||||||||
| 14 | #pod =cut | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use Mojo::Base 'Zapp::Trigger', -signatures; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Mojo::JSON qw( decode_json ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use Scalar::Util qw( blessed ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Yancy::Util qw( currym ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 0 | ▶ | sub install( $self, $app, $config={} ) { | ||||||||
|
Subroutine
install: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 22 | 0 | $self->SUPER::install( $app, $config ); | |||||||||
| 23 | # Set up base webhook handler | ||||||||||
| 24 | 0 | $app->routes->any( '/webhook/:slug' )->to( cb => currym( $self, '_handle_webhook' ) ); | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | ▶ | sub _handle_webhook( $self, $c ) { | |||||||||
|
Subroutine
_handle_webhook: not called CC=4 SCAR=30.0
| |||||||||||
| 28 | # Find webhook from slug | ||||||||||
| 29 | 0 | my $slug = $c->param( 'slug' ); | |||||||||
| 30 | 0 | my $hook; | |||||||||
| 31 | # XXX: Yancy has no JSON query capability... | ||||||||||
| 32 | 0 | for my $h ( $c->yancy->list( zapp_triggers => { type => $self->moniker } ) ) { | |||||||||
| 33 | # XXX: Auto-decode JSON fields in Yancy | ||||||||||
| 34 | 0 | my $config = $h->{config} = decode_json( $h->{config} ); | |||||||||
| 35 | 0 | ▶ | if ( $config->{slug} eq $slug ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | $hook = $h; | |||||||||
| 37 | 0 | last; | |||||||||
| 38 | } | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | if ( !$hook ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | $c->log->warn( sprintf 'No webhook for slug %s', $slug ); | |||||||||
| 43 | 0 | return $c->reply->not_found; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | # Map params to plan input | ||||||||||
| 47 | 0 | my $context = { | |||||||||
| 48 | params => $c->req->params->to_hash, | ||||||||||
| 49 | # XXX: Configure parameter names and types and run them through | ||||||||||
| 50 | # Type class's process_input | ||||||||||
| 51 | }; | ||||||||||
| 52 | |||||||||||
| 53 | # Enqueue job | ||||||||||
| 54 | # XXX: Config for synchronous runs: Run the entire plan before | ||||||||||
| 55 | # sending the response. Include data from plan in response. | ||||||||||
| 56 | 0 | my $run = $self->enqueue( $hook->{trigger_id}, $context ); | |||||||||
| 57 | |||||||||||
| 58 | # XXX: Configure response | ||||||||||
| 59 | 0 | $c->rendered( 204 ); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 1 | 1; | |||||||||
| 63 | |||||||||||
| 64 | =pod | ||||||||||
| 65 | |||||||||||
| 66 | =head1 NAME | ||||||||||
| 67 | |||||||||||
| 68 | Zapp::Trigger::Webhook - Trigger a plan from a web request | ||||||||||
| 69 | |||||||||||
| 70 | =head1 VERSION | ||||||||||
| 71 | |||||||||||
| 72 | version 0.001 | ||||||||||
| 73 | |||||||||||
| 74 | =head1 SYNOPSIS | ||||||||||
| 75 | |||||||||||
| 76 | =head1 DESCRIPTION | ||||||||||
| 77 | |||||||||||
| 78 | =head1 CONFIG | ||||||||||
| 79 | |||||||||||
| 80 | =head1 OUTPUT | ||||||||||
| 81 | |||||||||||
| 82 | =head1 SEE ALSO | ||||||||||
| 83 | |||||||||||
| 84 | =head1 AUTHOR | ||||||||||
| 85 | |||||||||||
| 86 | Doug Bell <preaction@cpan.org> | ||||||||||
| 87 | |||||||||||
| 88 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 89 | |||||||||||
| 90 | This software is copyright (c) 2020 by Doug Bell. | ||||||||||
| 91 | |||||||||||
| 92 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 93 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | __DATA__ | ||||||||||