88use phpbu \App \Result ;
99use phpbu \App \Util \Arr ;
1010use phpbu \App \Util \Path ;
11+ use phpbu \App \Util \Str ;
1112use 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