This commit is contained in:
Andras Bacsai 2023-06-19 15:24:04 +02:00
parent f7dd110a49
commit 6d2b88c31f
11 changed files with 121 additions and 37 deletions

View File

@ -15,16 +15,12 @@ class DiscordSettings extends Component
protected $rules = [ protected $rules = [
'model.extra_attributes.discord_active' => 'nullable|boolean', 'model.extra_attributes.discord_active' => 'nullable|boolean',
'model.extra_attributes.discord_webhook' => 'required|url', 'model.extra_attributes.discord_webhook' => 'required|url',
'model.extra_attributes.notifications_test' => 'nullable|boolean', 'model.extra_attributes.notifications_discord_test' => 'nullable|boolean',
'model.extra_attributes.notifications_deployments' => 'nullable|boolean', 'model.extra_attributes.notifications_discord_deployments' => 'nullable|boolean',
]; ];
protected $validationAttributes = [ protected $validationAttributes = [
'model.extra_attributes.discord_webhook' => 'Discord Webhook', 'model.extra_attributes.discord_webhook' => 'Discord Webhook',
]; ];
public function instantSaveEvents()
{
$this->saveModel();
}
public function instantSave() public function instantSave()
{ {
try { try {
@ -34,7 +30,7 @@ public function instantSave()
$this->validate(); $this->validate();
} }
} }
private function saveModel() public function saveModel()
{ {
$this->model->save(); $this->model->save();
if (is_a($this->model, Team::class)) { if (is_a($this->model, Team::class)) {

View File

@ -24,8 +24,8 @@ class EmailSettings extends Component
'model.extra_attributes.smtp_password' => 'nullable', 'model.extra_attributes.smtp_password' => 'nullable',
'model.extra_attributes.smtp_timeout' => 'nullable', 'model.extra_attributes.smtp_timeout' => 'nullable',
'model.extra_attributes.smtp_test_recipients' => 'nullable', 'model.extra_attributes.smtp_test_recipients' => 'nullable',
'model.extra_attributes.notifications_deployments' => 'nullable|boolean', 'model.extra_attributes.notifications_email_test' => 'nullable|boolean',
'model.extra_attributes.notifications_test' => 'nullable|boolean', 'model.extra_attributes.notifications_email_deployments' => 'nullable|boolean',
]; ];
protected $validationAttributes = [ protected $validationAttributes = [
'model.extra_attributes.smtp_from_address' => 'From Address', 'model.extra_attributes.smtp_from_address' => 'From Address',
@ -62,7 +62,7 @@ public function submit()
$this->model->extra_attributes->smtp_test_recipients = str_replace(' ', '', $this->model->extra_attributes->smtp_test_recipients); $this->model->extra_attributes->smtp_test_recipients = str_replace(' ', '', $this->model->extra_attributes->smtp_test_recipients);
$this->saveModel(); $this->saveModel();
} }
private function saveModel() public function saveModel()
{ {
$this->model->save(); $this->model->save();
if (is_a($this->model, Team::class)) { if (is_a($this->model, Team::class)) {
@ -73,10 +73,7 @@ private function saveModel()
public function sendTestNotification() public function sendTestNotification()
{ {
Notification::send($this->model, new TestNotification); Notification::send($this->model, new TestNotification);
} $this->emit('success', 'Test notification sent.');
public function instantSaveEvents()
{
$this->saveModel();
} }
public function instantSave() public function instantSave()
{ {

View File

@ -9,7 +9,8 @@
use App\Models\Application; use App\Models\Application;
use App\Models\ApplicationDeploymentQueue; use App\Models\ApplicationDeploymentQueue;
use App\Models\ApplicationPreview; use App\Models\ApplicationPreview;
use App\Notifications\Notifications\ApplicationDeployedNotification; use App\Notifications\Notifications\Application\DeployedSuccessfullyNotification;
use App\Notifications\Notifications\Application\DeployedWithErrorNotification;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
@ -317,7 +318,12 @@ private function next(string $status)
dispatch(new InstanceProxyCheckJob()); dispatch(new InstanceProxyCheckJob());
} }
queue_next_deployment($this->application); queue_next_deployment($this->application);
Notification::send($this->application->environment->project->team, new ApplicationDeployedNotification($this->application, $this->deployment_uuid)); if ($status === ProcessStatus::ERROR->value) {
Notification::send($this->application->environment->project->team, new DeployedWithErrorNotification($this->application, $this->deployment_uuid));
}
if ($status === ProcessStatus::FINISHED->value) {
Notification::send($this->application->environment->project->team, new DeployedSuccessfullyNotification($this->application, $this->deployment_uuid));
}
} }
private function execute_in_builder(string $command) private function execute_in_builder(string $command)
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Notifications\Notifications; namespace App\Notifications\Notifications\Application;
use App\Models\Application; use App\Models\Application;
use App\Models\Team; use App\Models\Team;
@ -12,12 +12,13 @@
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class ApplicationDeployedNotification extends Notification implements ShouldQueue class DeployedSuccessfullyNotification extends Notification implements ShouldQueue
{ {
use Queueable; use Queueable;
public Application $application; public Application $application;
public string $application_name; public string $application_name;
public string $deployment_uuid; public string $deployment_uuid;
public string|null $deployment_url = null; public string|null $deployment_url = null;
public string $project_uuid; public string $project_uuid;
public string $environment_name; public string $environment_name;
@ -26,8 +27,8 @@ class ApplicationDeployedNotification extends Notification implements ShouldQueu
public function __construct(Application $application, string $deployment_uuid) public function __construct(Application $application, string $deployment_uuid)
{ {
$this->application = $application; $this->application = $application;
$this->application_name = data_get($application, 'name');
$this->deployment_uuid = $deployment_uuid; $this->deployment_uuid = $deployment_uuid;
$this->application_name = data_get($application, 'name');
$this->project_uuid = data_get($application, 'environment.project.uuid'); $this->project_uuid = data_get($application, 'environment.project.uuid');
$this->environment_name = data_get($application, 'environment.name'); $this->environment_name = data_get($application, 'environment.name');
$this->fqdn = data_get($application, 'fqdn'); $this->fqdn = data_get($application, 'fqdn');
@ -39,10 +40,10 @@ public function __construct(Application $application, string $deployment_uuid)
public function via(object $notifiable): array public function via(object $notifiable): array
{ {
$channels = []; $channels = [];
if ($notifiable->extra_attributes?->get('email_active') && $notifiable->extra_attributes?->get('notifications_deployments')) { if ($notifiable->extra_attributes?->get('smtp_active') && $notifiable->extra_attributes?->get('notifications_email_deployments')) {
$channels[] = EmailChannel::class; $channels[] = EmailChannel::class;
} }
if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_deployments')) { if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_discord_deployments')) {
$channels[] = DiscordChannel::class; $channels[] = DiscordChannel::class;
} }
return $channels; return $channels;
@ -50,8 +51,8 @@ public function via(object $notifiable): array
public function toMail(Team $team): MailMessage public function toMail(Team $team): MailMessage
{ {
$mail = new MailMessage(); $mail = new MailMessage();
$mail->subject("New version is deployed of {$this->application_name}"); $mail->subject("New version is deployed of {$this->application_name}");
$mail->view('emails.application-deployed', [ $mail->view('emails.application-deployed-successfully', [
'name' => $this->application_name, 'name' => $this->application_name,
'fqdn' => $this->fqdn, 'fqdn' => $this->fqdn,
'url' => $this->deployment_url, 'url' => $this->deployment_url,
@ -61,7 +62,8 @@ public function toMail(Team $team): MailMessage
public function toDiscord(): string public function toDiscord(): string
{ {
return '⚒️ A new version has been deployed of **' . $this->application_name . '**. return '✅ A new version has been deployed of **' . $this->application_name . '**.
[Application Link](' . $this->fqdn . ') | [Deployment logs](' . $this->deployment_url . ')'; [Application Link](' . $this->fqdn . ') | [Deployment logs](' . $this->deployment_url . ')';
} }
} }

View File

@ -0,0 +1,69 @@
<?php
namespace App\Notifications\Notifications\Application;
use App\Models\Application;
use App\Models\Team;
use App\Notifications\Channels\EmailChannel;
use App\Notifications\Channels\DiscordChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;
class DeployedWithErrorNotification extends Notification implements ShouldQueue
{
use Queueable;
public Application $application;
public string $application_name;
public string $deployment_uuid;
public string|null $deployment_url = null;
public string $project_uuid;
public string $environment_name;
public string $fqdn;
public function __construct(Application $application, string $deployment_uuid)
{
$this->application = $application;
$this->deployment_uuid = $deployment_uuid;
$this->application_name = data_get($application, 'name');
$this->project_uuid = data_get($application, 'environment.project.uuid');
$this->environment_name = data_get($application, 'environment.name');
$this->fqdn = data_get($application, 'fqdn');
if (Str::of($this->fqdn)->explode(',')->count() > 1) {
$this->fqdn = Str::of($this->fqdn)->explode(',')->first();
}
$this->deployment_url = base_url() . "/project/{$this->project_uuid}/{$this->environment_name}/application/{$this->application->uuid}/deployment/{$this->deployment_uuid}";
}
public function via(object $notifiable): array
{
$channels = [];
if ($notifiable->extra_attributes?->get('smtp_active') && $notifiable->extra_attributes?->get('notifications_email_deployments')) {
$channels[] = EmailChannel::class;
}
if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_discord_deployments')) {
$channels[] = DiscordChannel::class;
}
return $channels;
}
public function toMail(Team $team): MailMessage
{
$mail = new MailMessage();
$mail->subject("❌ Deployment failed of {$this->application_name}");
$mail->view('emails.application-deployed-with-error', [
'name' => $this->application_name,
'fqdn' => $this->fqdn,
'url' => $this->deployment_url,
]);
return $mail;
}
public function toDiscord(): string
{
return '❌ Deployment failed of **' . $this->application_name . '**.
[Deployment logs](' . $this->deployment_url . ')';
}
}

View File

@ -15,10 +15,10 @@ class TestNotification extends Notification implements ShouldQueue
public function via(object $notifiable): array public function via(object $notifiable): array
{ {
$channels = []; $channels = [];
if ($notifiable->extra_attributes?->get('email_active') && $notifiable->extra_attributes?->get('notifications_test')) { if ($notifiable->extra_attributes?->get('smtp_active') && $notifiable->extra_attributes?->get('notifications_email_test')) {
$channels[] = EmailChannel::class; $channels[] = EmailChannel::class;
} }
if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_test')) { if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_discord_test')) {
$channels[] = DiscordChannel::class; $channels[] = DiscordChannel::class;
} }
return $channels; return $channels;

View File

@ -1,9 +0,0 @@
<h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
<x-forms.checkbox instantSave="instantSaveEvents" id="model.extra_attributes.notifications_test"
label="Test Notifications" />
@endif
<x-forms.checkbox instantSave="instantSaveEvents" id="model.extra_attributes.notifications_deployments"
label="New Deployments" />
</div>

View File

@ -0,0 +1,7 @@
Hello,<br><br>
Deployment failed of "{{ $name }}" to <a target="_blank"
href="{{ $fqdn }}">{{ $fqdn }}</a><br><br>
Click the following link to view the deployment logs: <a target="_blank" href="{{ $url }}">View
Deployment</a><br><br>

View File

@ -19,5 +19,13 @@
helper="Generate a webhook in Discord.<br>Example: https://discord.com/api/webhooks/...." required helper="Generate a webhook in Discord.<br>Example: https://discord.com/api/webhooks/...." required
id="model.extra_attributes.discord_webhook" label="Webhook" /> id="model.extra_attributes.discord_webhook" label="Webhook" />
</form> </form>
<x-notification-subscription /> <h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_discord_test"
label="Test Notifications" />
@endif
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_discord_deployments"
label="New Deployments" />
</div>
</div> </div>

View File

@ -53,5 +53,13 @@
</div> </div>
</div> </div>
</form> </form>
<x-notification-subscription /> <h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_email_test"
label="Test Notifications" />
@endif
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_email_deployments"
label="New Deployments" />
</div>
</div> </div>