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\TextInput;
use Filament\Forms\Components\ToggleButtons;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Form;
use Filament\Resources\Pages\PageRegistration;
use Filament\Forms\Get;
@ -129,23 +130,12 @@ class WebhookResource extends Resource
->live()
->inline()
->options(WebhookType::class)
->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();
}
}),
->default(WebhookType::Regular->value),
TextInput::make('description')
->label(trans('admin/webhook.description'))
->required(),
TextInput::make('endpoint')
->label(trans('admin/webhook.endpoint'))
->activeUrl()
->required()
->columnSpanFull()
->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)
->dehydratedWhenHidden()
->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(),
Section::make(trans('admin/webhook.discord'))
->hidden(fn (Get $get) => $get('type') === WebhookType::Regular->value)
@ -163,8 +162,6 @@ class WebhookResource extends Resource
->aside()
->formBefore(),
Section::make(trans('admin/webhook.events'))
->collapsible()
->collapsed(fn (Get $get) => count($get('events') ?? []))
->schema([
CheckboxList::make('events')
->live()
@ -183,7 +180,10 @@ class WebhookResource extends Resource
{
return [
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;
}
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');
}
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
{
$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) {
$data = array_merge(
is_array($data) ? $data : json_decode($data, true),
['event' => $this->webhookConfiguration->transformClassName($this->eventName)]
);
$payload = json_encode($this->webhookConfiguration->payload);
$tmp = $this->webhookConfiguration->replaceVars($data, $payload);
$data = json_decode($tmp, true);
@ -53,9 +53,10 @@ class ProcessWebhook implements ShouldQueue
}
try {
$customHeaders = $this->webhookConfiguration->headers;
$headers = [];
if ($this->webhookConfiguration->type === WebhookType::Regular && $customHeaders = $this->webhookConfiguration->headers) {
$headers = array_merge(['X-Webhook-Event', $this->eventName], $customHeaders);
foreach ($customHeaders as $key => $value) {
$headers[$key] = $this->webhookConfiguration->replaceVars($data, $value);
}
Http::withHeaders($headers)->post($this->webhookConfiguration->endpoint, $data)->throw();

View File

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