coolify/app/Notifications/Application/DeploymentFailed.php

94 lines
3.7 KiB
PHP
Raw Normal View History

2023-06-19 14:31:42 +02:00
<?php
2023-07-28 10:55:26 +02:00
namespace App\Notifications\Application;
2023-06-19 14:31:42 +02:00
use App\Models\Application;
2023-06-19 15:43:53 +02:00
use App\Models\ApplicationPreview;
2023-06-19 14:31:42 +02:00
use App\Notifications\Channels\DiscordChannel;
use App\Notifications\Channels\EmailChannel;
2023-06-19 14:31:42 +02:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;
2023-07-28 10:55:26 +02:00
class DeploymentFailed extends Notification implements ShouldQueue
2023-06-19 14:31:42 +02:00
{
use Queueable;
2023-06-19 14:31:42 +02:00
public Application $application;
public string $deployment_uuid;
2023-06-19 15:43:53 +02:00
public ApplicationPreview|null $preview;
2023-06-19 15:24:04 +02:00
2023-06-19 15:34:39 +02:00
public string $application_name;
2023-06-19 14:31:42 +02:00
public string|null $deployment_url = null;
public string $project_uuid;
public string $environment_name;
2023-06-23 14:20:47 +02:00
public string|null $fqdn;
2023-06-19 14:31:42 +02:00
2023-06-19 15:43:53 +02:00
public function __construct(Application $application, string $deployment_uuid, ApplicationPreview|null $preview)
2023-06-19 14:31:42 +02:00
{
$this->application = $application;
$this->deployment_uuid = $deployment_uuid;
2023-06-19 15:43:53 +02:00
$this->preview = $preview;
2023-06-19 15:24:04 +02:00
$this->application_name = data_get($application, 'name');
2023-06-19 14:31:42 +02:00
$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}";
2023-06-19 14:31:42 +02:00
}
2023-06-19 14:31:42 +02:00
public function via(object $notifiable): array
{
$channels = [];
2023-08-31 15:00:59 +02:00
$isEmailEnabled = isEmailEnabled($notifiable);
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_deployments');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_deployments');
2023-06-20 19:08:43 +02:00
2023-07-26 15:20:04 +02:00
if ($isEmailEnabled && $isSubscribedToEmailEvent) {
2023-06-19 14:31:42 +02:00
$channels[] = EmailChannel::class;
}
2023-07-26 15:20:04 +02:00
if ($isDiscordEnabled && $isSubscribedToDiscordEvent) {
2023-06-19 14:31:42 +02:00
$channels[] = DiscordChannel::class;
}
return $channels;
}
2023-06-21 10:48:43 +02:00
public function toMail(): MailMessage
2023-06-19 14:31:42 +02:00
{
$mail = new MailMessage();
2023-06-21 10:48:43 +02:00
$pull_request_id = data_get($this->preview, 'pull_request_id', 0);
$fqdn = $this->fqdn;
if ($pull_request_id === 0) {
$mail->subject('❌ Deployment failed of ' . $this->application_name . '.');
} else {
$fqdn = $this->preview->fqdn;
$mail->subject('❌ Pull request #' . $this->preview->pull_request_id . ' of ' . $this->application_name . ' deployment failed.');
}
2023-07-28 10:55:26 +02:00
$mail->view('emails.application-deployment-failed', [
2023-06-19 14:31:42 +02:00
'name' => $this->application_name,
2023-06-21 10:48:43 +02:00
'fqdn' => $fqdn,
'deployment_url' => $this->deployment_url,
2023-06-20 15:25:45 +02:00
'pull_request_id' => data_get($this->preview, 'pull_request_id', 0),
2023-06-19 14:31:42 +02:00
]);
return $mail;
}
public function toDiscord(): string
{
2023-06-19 15:43:53 +02:00
if ($this->preview) {
2023-06-21 10:48:43 +02:00
$message = '❌ Pull request #' . $this->preview->pull_request_id . ' of **' . $this->application_name . '** (' . $this->preview->fqdn . ') deployment failed: ';
$message .= '[View Deployment Logs](' . $this->deployment_url . ')';
} else {
$message = '❌ Deployment failed of **' . $this->application_name . '** (' . $this->fqdn . '): ';
$message .= '[View Deployment Logs](' . $this->deployment_url . ')';
2023-06-19 15:34:39 +02:00
}
return $message;
2023-06-19 14:31:42 +02:00
}
}