Fixed not working variables on DiscordWebhooks and headers. (#1516)

Co-authored-by: notCharles <charles@pelican.dev>
This commit is contained in:
JoanFo 2025-07-31 23:47:46 +02:00 committed by GitHub
parent d307a2095b
commit bab8ec6e18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 23 deletions

View File

@ -19,6 +19,7 @@ use Filament\Forms\Components\Section;
use Filament\Forms\Components\Textarea; use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\ToggleButtons; use Filament\Forms\Components\ToggleButtons;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Resources\Pages\PageRegistration; use Filament\Resources\Pages\PageRegistration;
use Filament\Forms\Get; use Filament\Forms\Get;
@ -129,23 +130,12 @@ class WebhookResource extends Resource
->live() ->live()
->inline() ->inline()
->options(WebhookType::class) ->options(WebhookType::class)
->default(WebhookType::Regular->value) ->default(WebhookType::Regular->value),
->afterStateHydrated(function (string $state) {
if ($state === WebhookType::Discord->value) {
self::sendHelpBanner();
}
})
->afterStateUpdated(function (string $state) {
if ($state === WebhookType::Discord->value) {
self::sendHelpBanner();
}
}),
TextInput::make('description') TextInput::make('description')
->label(trans('admin/webhook.description')) ->label(trans('admin/webhook.description'))
->required(), ->required(),
TextInput::make('endpoint') TextInput::make('endpoint')
->label(trans('admin/webhook.endpoint')) ->label(trans('admin/webhook.endpoint'))
->activeUrl()
->required() ->required()
->columnSpanFull() ->columnSpanFull()
->afterStateUpdated(fn (string $state, Set $set) => $set('type', str($state)->contains('discord.com') ? WebhookType::Discord->value : WebhookType::Regular->value)), ->afterStateUpdated(fn (string $state, Set $set) => $set('type', str($state)->contains('discord.com') ? WebhookType::Discord->value : WebhookType::Regular->value)),
@ -153,6 +143,15 @@ class WebhookResource extends Resource
->hidden(fn (Get $get) => $get('type') === WebhookType::Discord->value) ->hidden(fn (Get $get) => $get('type') === WebhookType::Discord->value)
->dehydratedWhenHidden() ->dehydratedWhenHidden()
->schema(fn () => self::getRegularFields()) ->schema(fn () => self::getRegularFields())
->headerActions([
Action::make('reset_headers')
->label(trans('admin/webhook.reset_headers'))
->color('danger')
->icon('heroicon-o-trash')
->action(fn (Get $get, Set $set) => $set('headers', [
'X-Webhook-Event' => '{{event}}',
])),
])
->formBefore(), ->formBefore(),
Section::make(trans('admin/webhook.discord')) Section::make(trans('admin/webhook.discord'))
->hidden(fn (Get $get) => $get('type') === WebhookType::Regular->value) ->hidden(fn (Get $get) => $get('type') === WebhookType::Regular->value)
@ -163,8 +162,6 @@ class WebhookResource extends Resource
->aside() ->aside()
->formBefore(), ->formBefore(),
Section::make(trans('admin/webhook.events')) Section::make(trans('admin/webhook.events'))
->collapsible()
->collapsed(fn (Get $get) => count($get('events') ?? []))
->schema([ ->schema([
CheckboxList::make('events') CheckboxList::make('events')
->live() ->live()
@ -183,7 +180,10 @@ class WebhookResource extends Resource
{ {
return [ return [
KeyValue::make('headers') KeyValue::make('headers')
->label(trans('admin/webhook.headers')), ->label(trans('admin/webhook.headers'))
->default(fn () => [
'X-Webhook-Event' => '{{event}}',
]),
]; ];
} }

View File

@ -63,4 +63,15 @@ class CreateWebhookConfiguration extends CreateRecord
return $data; return $data;
} }
protected function getRedirectUrl(): string
{
return EditWebhookConfiguration::getUrl(['record' => $this->getRecord()]);
}
public function mount(): void
{
parent::mount();
WebhookResource::sendHelpBanner();
}
} }

View File

@ -123,4 +123,10 @@ class EditWebhookConfiguration extends EditRecord
{ {
$this->dispatch('refresh-widget'); $this->dispatch('refresh-widget');
} }
public function mount(int|string $record): void
{
parent::mount($record);
WebhookResource::sendHelpBanner();
}
} }

View File

@ -28,14 +28,14 @@ class ProcessWebhook implements ShouldQueue
public function handle(): void public function handle(): void
{ {
$data = $this->data[0]; $data = $this->data[0] ?? [];
if (count($data) === 1) {
$data = reset($data);
}
$data = is_array($data) ? $data : (json_decode($data, true) ?? []);
$data['event'] = $this->webhookConfiguration->transformClassName($this->eventName);
if ($this->webhookConfiguration->type === WebhookType::Discord) { if ($this->webhookConfiguration->type === WebhookType::Discord) {
$data = array_merge(
is_array($data) ? $data : json_decode($data, true),
['event' => $this->webhookConfiguration->transformClassName($this->eventName)]
);
$payload = json_encode($this->webhookConfiguration->payload); $payload = json_encode($this->webhookConfiguration->payload);
$tmp = $this->webhookConfiguration->replaceVars($data, $payload); $tmp = $this->webhookConfiguration->replaceVars($data, $payload);
$data = json_decode($tmp, true); $data = json_decode($tmp, true);
@ -53,9 +53,10 @@ class ProcessWebhook implements ShouldQueue
} }
try { try {
$customHeaders = $this->webhookConfiguration->headers;
$headers = []; $headers = [];
if ($this->webhookConfiguration->type === WebhookType::Regular && $customHeaders = $this->webhookConfiguration->headers) { foreach ($customHeaders as $key => $value) {
$headers = array_merge(['X-Webhook-Event', $this->eventName], $customHeaders); $headers[$key] = $this->webhookConfiguration->replaceVars($data, $value);
} }
Http::withHeaders($headers)->post($this->webhookConfiguration->endpoint, $data)->throw(); Http::withHeaders($headers)->post($this->webhookConfiguration->endpoint, $data)->throw();

View File

@ -19,6 +19,7 @@ return [
'headers' => 'Headers', 'headers' => 'Headers',
'events' => 'Events', 'events' => 'Events',
'regular' => 'Regular', 'regular' => 'Regular',
'reset_headers' => 'Reset Headers',
'discord' => 'Discord', 'discord' => 'Discord',
'discord_message' => [ 'discord_message' => [
'profile' => 'Profile', 'profile' => 'Profile',