Skip to content

Commit 73ac8c4

Browse files
Add sendOnlyOnError for webhooks
Requested in issue #381
1 parent 94a4edb commit 73ac8c4

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

doc/config/log/webhook.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"type": "webhook",
33
"options": {
44
"uri": "https://example.com/webhook",
5+
"sendOnlyOnError": false,
6+
"sendOnSimulation": false,
57
"username": "misterX",
68
"password": "secret",
79
"method": "POST",

doc/config/log/webhook.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
<!-- mandatory -->
44
<option name="uri" value="https://some.host.com/webhook" />
55

6+
<!-- optional default=false -->
7+
<option name="sendOnlyOnError" value="true" />
8+
9+
<!-- optional default=true -->
10+
<option name="sendOnSimulation" value="false" />
11+
612
<!-- optional default=false -->
713
<option name="username" value="misterX" />
814

src/Log/Webhook.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use phpbu\App\Result;
99
use phpbu\App\Util\Arr;
1010
use phpbu\App\Util\Path;
11+
use phpbu\App\Util\Str;
1112
use Throwable;
1213

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

95+
/**
96+
* Call webhook only if errors occur
97+
*
98+
* @var bool
99+
*/
100+
private bool $sendOnlyOnError = false;
101+
102+
/**
103+
* Call the webhook while simulating
104+
*
105+
* @var bool
106+
*/
107+
private bool $sendSimulating = false;
108+
94109
/**
95110
* Constructor will only set the start time to be able to log duration
96111
*/
@@ -140,6 +155,8 @@ public function setup(array $options)
140155
throw new Exception('webhook URI is invalid');
141156
}
142157

158+
$this->sendOnlyOnError = Str::toBoolean(Arr::getValue($options, 'sendOnlyOnError'), false);
159+
$this->sendSimulating = Str::toBoolean(Arr::getValue($options, 'sendOnSimulation'), true);
143160
$this->uri = $options['uri'];
144161
$this->method = Arr::getValue($options, 'method', 'GET');
145162
$this->username = Arr::getValue($options, 'username', '');
@@ -156,7 +173,10 @@ public function setup(array $options)
156173
*/
157174
public function onPhpbuEnd(Event\App\End $event)
158175
{
159-
$result = $event->getResult();
176+
$result = $event->getResult();
177+
if ($this->shouldWebhookBeCalled($result) === false) {
178+
return;
179+
}
160180
$data = $this->getQueryStringData($result);
161181
$uri = $this->method === 'GET' ? $this->buildGetUri($data) : $this->uri;
162182
$formatter = $this->getBodyFormatter();
@@ -259,4 +279,15 @@ protected function fireRequest(string $uri, string $body = '')
259279
throw new Exception('could not reach webhook: ' . $this->uri);
260280
}
261281
}
282+
283+
/**
284+
* Should a webhook be called
285+
*
286+
* @param \phpbu\App\Result $result
287+
* @return bool
288+
*/
289+
protected function shouldWebhookBeCalled(Result $result) : bool
290+
{
291+
return (!$this->sendOnlyOnError || !$result->allOk()) && ($this->sendSimulating || !$this->isSimulation);
292+
}
262293
}

0 commit comments

Comments
 (0)