Skip to content

Commit

Permalink
feat: added slack notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
yohannlog committed Jun 18, 2024
1 parent de7380f commit c3a4ae1
Show file tree
Hide file tree
Showing 27 changed files with 439 additions and 4 deletions.
47 changes: 47 additions & 0 deletions app/Jobs/SendMessageToSlackJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;

class SendMessageToSlackJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 5;

public $backoff = 10;

/**
* The maximum number of unhandled exceptions to allow before failing.
*/
public int $maxExceptions = 5;

public function __construct(
public string $text,
public string $webhookUrl
) {
}

/**
* Execute the job.
*/
public function handle(): void
{
$payload = [
'text' => $this->text,
];
Http::post($this->webhookUrl, $payload);
}
}
67 changes: 67 additions & 0 deletions app/Livewire/Notifications/Slack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Livewire\Notifications;

use App\Models\Team;
use App\Notifications\Test;
use Livewire\Component;

class Slack extends Component
{
public Team $team;

protected $rules = [
'team.slack_enabled' => 'nullable|boolean',
'team.slack_webhook_url' => 'required|url',
'team.slack_notifications_test' => 'nullable|boolean',
'team.slack_notifications_deployments' => 'nullable|boolean',
'team.slack_notifications_status_changes' => 'nullable|boolean',
'team.slack_notifications_database_backups' => 'nullable|boolean',
'team.slack_notifications_scheduled_tasks' => 'nullable|boolean',
];

protected $validationAttributes = [
'team.slack_webhook_url' => 'Slack Web API',
];

public function mount()
{
$this->team = auth()->user()->currentTeam();
}

public function instantSave()
{
try {
$this->submit();
} catch (\Throwable $e) {
ray($e->getMessage());
$this->team->slack_enabled = false;
$this->validate();
}
}

public function submit()
{
$this->resetErrorBag();
$this->validate();
$this->saveModel();
}

public function saveModel()
{
$this->team->save();
refreshSession();
$this->dispatch('success', 'Settings saved.');
}

public function sendTestNotification()
{
$this->team?->notify(new Test());
$this->dispatch('success', 'Test notification sent.');
}

public function render()
{
return view('livewire.notifications.slack');
}
}
10 changes: 8 additions & 2 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use App\Notifications\Channels\SendsDiscord;
use App\Notifications\Channels\SendsEmail;
use App\Notifications\Channels\SendsSlack;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Team extends Model implements SendsDiscord, SendsEmail
class Team extends Model implements SendsDiscord, SendsEmail, SendsSlack
{
use Notifiable;

Expand Down Expand Up @@ -62,6 +63,11 @@ public function routeNotificationForDiscord()
return data_get($this, 'discord_webhook_url', null);
}

public function routeNotificationForSlack()
{
return data_get($this, 'slack_webhook_url', null);
}

public function routeNotificationForTelegram()
{
return [
Expand Down Expand Up @@ -225,7 +231,7 @@ public function isAnyNotificationEnabled()
if (isCloud()) {
return true;
}
if ($this->smtp_enabled || $this->resend_enabled || $this->discord_enabled || $this->telegram_enabled || $this->use_instance_email_settings) {
if ($this->smtp_enabled || $this->resend_enabled || $this->discord_enabled || $this->telegram_enabled || $this->slack_enabled || $this->use_instance_email_settings) {
return true;
}

Expand Down
12 changes: 12 additions & 0 deletions app/Notifications/Application/DeploymentFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public function toDiscord(): string
return $message;
}

public function toSlack(): string
{
if ($this->preview) {
$message = 'Coolify: Pull request #'.$this->preview->pull_request_id.' of '.$this->application_name.' ('.$this->preview->fqdn.') deployment failed: ';
} else {
$message = 'Coolify: Deployment failed of '.$this->application_name.' ('.$this->fqdn.'): ';
}
$message .= '<'.$this->deployment_url.'|Deployment logs>';

return $message;
}

public function toTelegram(): array
{
if ($this->preview) {
Expand Down
18 changes: 18 additions & 0 deletions app/Notifications/Application/DeploymentSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ public function toDiscord(): string
return $message;
}

public function toSlack(): string
{
if ($this->preview) {
$message = 'New PR'.$this->preview->pull_request_id.' version successfully deployed of '.$this->application_name.'';
if ($this->preview->fqdn) {
$message .= ' | <'.$this->preview->fqdn.'|Open Application>';
}
} else {
$message = 'New version successfully deployed of '.$this->application_name.'';
if ($this->fqdn) {
$message .= ' | <'.$this->fqdn.'|Open Application>';
}
}
$message .= ' | <'.$this->deployment_url.'|Deployment logs>';

return $message;
}

public function toTelegram(): array
{
if ($this->preview) {
Expand Down
15 changes: 15 additions & 0 deletions app/Notifications/Application/StatusChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ public function toDiscord(): string
return $message;
}

public function toSlack(): array
{
$message = 'Coolify: '.$this->resource_name.' has been stopped.';

return [
'text' => $message,
'attachments' => [
[
'text' => $message,
'color' => '#FF0000',
],
],
];
}

public function toTelegram(): array
{
$message = 'Coolify: '.$this->resource_name.' has been stopped.';
Expand Down
8 changes: 8 additions & 0 deletions app/Notifications/Channels/SendsSlack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Notifications\Channels;

interface SendsSlack
{
public function routeNotificationForSlack();
}
22 changes: 22 additions & 0 deletions app/Notifications/Channels/SlackChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Notifications\Channels;

use App\Jobs\SendMessageToSlackJob;
use Illuminate\Notifications\Notification;

class SlackChannel
{
/**
* Send the given notification.
*/
public function send(SendsSlack $notifiable, Notification $notification): void
{
$message = $notification->toSlack($notifiable);
$webhookUrl = $notifiable->routeNotificationForSlack();
if (! $webhookUrl) {
return;
}
dispatch(new SendMessageToSlackJob($message, $webhookUrl));
}
}
7 changes: 7 additions & 0 deletions app/Notifications/Container/ContainerRestarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public function toDiscord(): string
return $message;
}

public function toSlack(): string
{
$message = "Coolify: A resource ({$this->name}) has been restarted automatically on {$this->server->name}";

return $message;
}

public function toTelegram(): array
{
$message = "Coolify: A resource ({$this->name}) has been restarted automatically on {$this->server->name}";
Expand Down
7 changes: 7 additions & 0 deletions app/Notifications/Container/ContainerStopped.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public function toDiscord(): string
return $message;
}

public function toSlack(): string
{
$message = "Coolify: A resource ($this->name) has been stopped unexpectedly on {$this->server->name}";

return $message;
}

public function toTelegram(): array
{
$message = "Coolify: A resource ($this->name) has been stopped unexpectedly on {$this->server->name}";
Expand Down
5 changes: 5 additions & 0 deletions app/Notifications/Database/BackupFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function toDiscord(): string
return "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was FAILED.\n\nReason:\n{$this->output}";
}

public function toSlack(): string
{
return "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was FAILED.\n\nReason:\n{$this->output}";
}

public function toTelegram(): array
{
$message = "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was FAILED.\n\nReason:\n{$this->output}";
Expand Down
5 changes: 5 additions & 0 deletions app/Notifications/Database/BackupSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function toDiscord(): string
return "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was successful.";
}

public function toSlack(): string
{
return "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was successful.";
}

public function toTelegram(): array
{
$message = "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was successful.";
Expand Down
8 changes: 7 additions & 1 deletion app/Notifications/Database/DailyBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Notifications\Database;

use App\Notifications\Channels\DiscordChannel;
use App\Notifications\Channels\SlackChannel;
use App\Notifications\Channels\TelegramChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -22,7 +23,7 @@ public function __construct(public $databases)

public function via(object $notifiable): array
{
return [DiscordChannel::class, TelegramChannel::class, MailChannel::class];
return [DiscordChannel::class, TelegramChannel::class, MailChannel::class, SlackChannel::class];
}

public function toMail(): MailMessage
Expand All @@ -41,6 +42,11 @@ public function toDiscord(): string
return 'Coolify: Daily backup statuses';
}

public function toSlack(): string
{
return 'Coolify: Daily backup statuses';
}

public function toTelegram(): array
{
$message = 'Coolify: Daily backup statuses';
Expand Down
11 changes: 10 additions & 1 deletion app/Notifications/Internal/GeneralNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Notifications\Internal;

use App\Notifications\Channels\DiscordChannel;
use App\Notifications\Channels\SlackChannel;
use App\Notifications\Channels\TelegramChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -23,14 +24,17 @@ public function via(object $notifiable): array
$channels = [];
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isTelegramEnabled = data_get($notifiable, 'telegram_enabled');
$isSlackEnabled = data_get($notifiable, 'slack_enabled');

if ($isDiscordEnabled) {
$channels[] = DiscordChannel::class;
}
if ($isTelegramEnabled) {
$channels[] = TelegramChannel::class;
}

if ($isSlackEnabled) {
$channels[] = SlackChannel::class;
}
return $channels;
}

Expand All @@ -39,6 +43,11 @@ public function toDiscord(): string
return $this->message;
}

public function toSlack(): string
{
return $this->message;
}

public function toTelegram(): array
{
return [
Expand Down
5 changes: 5 additions & 0 deletions app/Notifications/ScheduledTask/TaskFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function toDiscord(): string
return "Coolify: Scheduled task ({$this->task->name}, [link]({$this->url})) failed with output: {$this->output}";
}

public function toSlack(): string
{
return "Coolify: Scheduled task ({$this->task->name}) failed with output: {$this->output}";
}

public function toTelegram(): array
{
$message = "Coolify: Scheduled task ({$this->task->name}) failed with output: {$this->output}";
Expand Down
Loading

0 comments on commit c3a4ae1

Please sign in to comment.