diff --git a/doc/config/log/webhook.json b/doc/config/log/webhook.json
index f92d0510..e6903398 100644
--- a/doc/config/log/webhook.json
+++ b/doc/config/log/webhook.json
@@ -2,6 +2,8 @@
"type": "webhook",
"options": {
"uri": "https://example.com/webhook",
+ "sendOnlyOnError": false,
+ "sendOnSimulation": false,
"username": "misterX",
"password": "secret",
"method": "POST",
diff --git a/doc/config/log/webhook.xml b/doc/config/log/webhook.xml
index 5773f823..036b5968 100644
--- a/doc/config/log/webhook.xml
+++ b/doc/config/log/webhook.xml
@@ -3,6 +3,12 @@
+
+
+
+
+
+
diff --git a/src/Log/Webhook.php b/src/Log/Webhook.php
index 36e7c98e..22a37d81 100644
--- a/src/Log/Webhook.php
+++ b/src/Log/Webhook.php
@@ -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;
/**
@@ -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
*/
@@ -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', '');
@@ -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();
@@ -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);
+ }
}