Show web-hook test result immediately after saving it

Closes #12
This commit is contained in:
Daniel Friesel 2019-05-14 20:05:45 +02:00
parent 5c3a0a562a
commit b85db3a10d
3 changed files with 67 additions and 22 deletions

View file

@ -1059,12 +1059,15 @@ sub startup {
$self->helper( $self->helper(
'run_hook' => sub { 'run_hook' => sub {
my ( $self, $uid, $reason ) = @_; my ( $self, $uid, $reason, $callback ) = @_;
my $hook = $self->get_webhook($uid); my $hook = $self->get_webhook($uid);
if ( not $hook->{enabled} or not $hook->{url} =~ m{^ https?:// }x ) if ( not $hook->{enabled} or not $hook->{url} =~ m{^ https?:// }x )
{ {
if ($callback) {
&$callback();
}
return; return;
} }
@ -1080,7 +1083,12 @@ sub startup {
} }
my $ua = $self->ua; my $ua = $self->ua;
$ua->request_timeout(10); if ($callback) {
$ua->request_timeout(4);
}
else {
$ua->request_timeout(10);
}
$ua->post_p( $hook->{url} => $header => json => $hook_body )->then( $ua->post_p( $hook->{url} => $header => json => $hook_body )->then(
sub { sub {
@ -1093,11 +1101,17 @@ sub startup {
$self->mark_hook_status( $uid, $hook->{url}, 1, $self->mark_hook_status( $uid, $hook->{url}, 1,
$tx->result->body ); $tx->result->body );
} }
if ($callback) {
&$callback();
}
} }
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$self->mark_hook_status( $uid, $hook->{url}, 0, $err ); $self->mark_hook_status( $uid, $hook->{url}, 0, $err );
if ($callback) {
&$callback();
}
} }
)->wait; )->wait;
} }

View file

@ -246,9 +246,18 @@ sub webhook {
token => $hook->{token}, token => $hook->{token},
enabled => $hook->{enabled} enabled => $hook->{enabled}
); );
$self->flash( success => 'webhook' ); $self->run_hook(
$self->redirect_to('account'); $self->current_user->{id},
$self->run_hook( $self->current_user->{id}, 'ping' ); 'ping',
sub {
$self->render(
'webhooks',
hook => $self->get_webhook,
new_hook => 1
);
}
);
return;
} }
else { else {
$self->param( url => $hook->{url} ); $self->param( url => $hook->{url} );

View file

@ -4,7 +4,29 @@
<h1>Web Hooks</h1> <h1>Web Hooks</h1>
<!-- -H "Authorization: Bearer ${TOKEN}" --> % if (stash('new_hook') and $hook->{enabled}) {
<div class="row">
<div class="col s12">
% if ($hook->{errored}) {
<div class="card red darken-4">
<div class="card-content white-text">
<span class="card-title">Web-Hook fehlerhaft</span>
<p><%= $hook->{output} %></p>
</div>
</div>
% }
% else {
<div class="card green darken-4">
<div class="card-content white-text">
<span class="card-title">Web-Hook erfolgreich getestet</span>
<p><%= $hook->{output} %></p>
</div>
</div>
% }
</div>
</div>
% }
<div class="row"> <div class="row">
<div class="col s12"> <div class="col s12">
<p> <p>
@ -31,22 +53,6 @@
%= text_field 'token', id => 'token', class => 'validate', maxlength => 250 %= text_field 'token', id => 'token', class => 'validate', maxlength => 250
<label for="token">Token</label> <label for="token">Token</label>
</div> </div>
<div class="col s12">
% if ($hook->{latest_run}->epoch) {
Zuletzt ausgeführt <%= $hook->{latest_run}->strftime('am %d.%m.%Y um %H:%M:%S') %><br/>
% if ($hook->{errored}) {
<i class="material-icons left">error</i>
Status: <%= $hook->{output} %>
% }
% else {
<i class="material-icons left">check</i>
Server-Antwort: <%= $hook->{output} %>
% }
% }
% else {
Noch nicht ausgeführt.
% }
</div>
<div class="col s12 center-align"> <div class="col s12 center-align">
<button class="btn waves-effect waves-light" type="submit" name="action" value="save"> <button class="btn waves-effect waves-light" type="submit" name="action" value="save">
Speichern Speichern
@ -54,6 +60,22 @@
</button> </button>
</div> </div>
%= end %= end
<div class="col s12" style="margin-top: 1em; margin-bottom: 2em;">
% if ($hook->{latest_run}->epoch) {
Zuletzt ausgeführt <%= $hook->{latest_run}->strftime('am %d.%m.%Y um %H:%M:%S') %><br/>
% if ($hook->{errored}) {
<i class="material-icons left">error</i>
Status: <%= $hook->{output} %>
% }
% else {
<i class="material-icons left">check</i>
Server-Antwort: <%= $hook->{output} %>
% }
% }
% else {
Noch nicht ausgeführt.
% }
</div>
<div class="col s12"> <div class="col s12">
<p>Events werden als JSON POST mit folgender Payload übertragen.</p> <p>Events werden als JSON POST mit folgender Payload übertragen.</p>
<p style="font-family: Monospace;"> <p style="font-family: Monospace;">