trypost/app/Actions/Automation/Node/RunWebhookNode.php
Paulo Castellano 31b6544c57 Cover node-run error paths and align webhook failure handling
Wrap the webhook HTTP send in a try/catch so a connection error returns
a clean failed result (reason: request_failed) instead of bubbling up as
a job failure — matching the HTTP request node.

Add tests for the gaps in node-run coverage:
- HTTP request: basic auth, PUT/PATCH/DELETE, non-2xx responses,
  connection exceptions, and an items_path that doesn't resolve to a list.
- Webhook: every HTTP method, header expression resolution, and the new
  connection-failure path.
- Fetch RSS: non-2xx feed responses, malformed XML, items without a
  publish date (skipped), and the link fallback when an item has no guid.
- Delay: unknown unit throws.
- Publish: dry runs don't publish or queue.
2026-06-13 09:56:18 -03:00

93 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions\Automation\Node;
use App\DataTransferObjects\Automation\NodeRunResult;
use App\Models\AutomationRun;
use App\Services\Automation\ExpressionResolver;
use App\Services\Brand\SafeHttpFetcher;
use Illuminate\Support\Facades\Http;
use RuntimeException;
use Throwable;
class RunWebhookNode
{
public function __construct(
private ExpressionResolver $resolver,
private SafeHttpFetcher $safeHttp,
) {}
public function __invoke(AutomationRun $run, array $config): NodeRunResult
{
$context = $run->resolverContext();
$url = $this->resolver->resolve($config['url'] ?? '', $context);
$method = strtoupper($config['method'] ?? 'POST');
try {
$this->safeHttp->guardAgainstSsrf($url);
} catch (RuntimeException) {
return NodeRunResult::failed(__('automations.errors.url_not_allowed'), [
'reason' => 'url_not_allowed',
'url' => $url,
]);
}
$headers = [];
foreach ($config['headers'] ?? [] as $k => $v) {
$headers[$k] = $this->resolver->resolve((string) $v, $context);
}
// Parse the template as JSON FIRST, then resolve placeholders in its
// string leaves — so a value containing `"`/`&`/newlines can't corrupt
// the JSON (the final json_encode escapes it).
$template = $config['payload_template'] ?? '{}';
$trimmedTemplate = trim($template);
if ($trimmedTemplate === '' || $trimmedTemplate === 'null') {
$payload = [];
} else {
$decodedTemplate = json_decode($template, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return NodeRunResult::failed(__('automations.errors.webhook_invalid_payload_json'), [
'reason' => 'invalid_payload_json',
]);
}
$payload = $this->resolver->resolveStructured($decodedTemplate ?? [], $context);
}
if ($run->is_dry_run) {
return NodeRunResult::completed(output: [
'webhook' => ['method' => $method, 'url' => $url, 'dry_run' => true],
]);
}
try {
$response = Http::withHeaders($headers)
->withUserAgent(config('trypost.user_agent'))
->send($method, $url, ['json' => $payload]);
} catch (Throwable $e) {
return NodeRunResult::failed(__('automations.errors.webhook_request_failed'), [
'reason' => 'request_failed',
'message' => $e->getMessage(),
]);
}
if ($response->serverError()) {
return NodeRunResult::failed(__('automations.errors.webhook_server_error'), [
'status' => $response->status(),
'body' => substr($response->body(), 0, 500),
]);
}
return NodeRunResult::completed(output: [
'webhook' => [
'status' => $response->status(),
'body' => substr($response->body(), 0, 500),
],
]);
}
}