Skip to content

Commit

Permalink
Add sendOnlyOnError for webhooks
Browse files Browse the repository at this point in the history
Requested in issue #381
  • Loading branch information
sebastianfeldmann committed Nov 15, 2024
1 parent 94a4edb commit 73ac8c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/config/log/webhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"type": "webhook",
"options": {
"uri": "https://example.com/webhook",
"sendOnlyOnError": false,
"sendOnSimulation": false,
"username": "misterX",
"password": "secret",
"method": "POST",
Expand Down
6 changes: 6 additions & 0 deletions doc/config/log/webhook.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<!-- mandatory -->
<option name="uri" value="https://some.host.com/webhook" />

<!-- optional default=false -->
<option name="sendOnlyOnError" value="true" />

<!-- optional default=true -->
<option name="sendOnSimulation" value="false" />

<!-- optional default=false -->
<option name="username" value="misterX" />

Expand Down
33 changes: 32 additions & 1 deletion src/Log/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpbu\App\Result;
use phpbu\App\Util\Arr;
use phpbu\App\Util\Path;
use phpbu\App\Util\Str;
use Throwable;

/**
Expand Down Expand Up @@ -91,6 +92,20 @@ class Webhook implements Listener, Logger
'application/xml' => '\\phpbu\\App\\Log\\ResultFormatter\\Xml'
];

/**
* Call webhook only if errors occur
*
* @var bool
*/
private bool $sendOnlyOnError = false;

/**
* Call the webhook while simulating
*
* @var bool
*/
private bool $sendSimulating = false;

/**
* Constructor will only set the start time to be able to log duration
*/
Expand Down Expand Up @@ -140,6 +155,8 @@ public function setup(array $options)
throw new Exception('webhook URI is invalid');
}

$this->sendOnlyOnError = Str::toBoolean(Arr::getValue($options, 'sendOnlyOnError'), false);
$this->sendSimulating = Str::toBoolean(Arr::getValue($options, 'sendOnSimulation'), true);
$this->uri = $options['uri'];
$this->method = Arr::getValue($options, 'method', 'GET');
$this->username = Arr::getValue($options, 'username', '');
Expand All @@ -156,7 +173,10 @@ public function setup(array $options)
*/
public function onPhpbuEnd(Event\App\End $event)
{
$result = $event->getResult();
$result = $event->getResult();
if ($this->shouldWebhookBeCalled($result) === false) {
return;
}
$data = $this->getQueryStringData($result);
$uri = $this->method === 'GET' ? $this->buildGetUri($data) : $this->uri;
$formatter = $this->getBodyFormatter();
Expand Down Expand Up @@ -259,4 +279,15 @@ protected function fireRequest(string $uri, string $body = '')
throw new Exception('could not reach webhook: ' . $this->uri);
}
}

/**
* Should a webhook be called
*
* @param \phpbu\App\Result $result
* @return bool
*/
protected function shouldWebhookBeCalled(Result $result) : bool
{
return (!$this->sendOnlyOnError || !$result->allOk()) && ($this->sendSimulating || !$this->isSimulation);
}
}

0 comments on commit 73ac8c4

Please sign in to comment.