lasthourcloud/app/Notifications/Application/DeploymentSuccess.php

136 lines
4.4 KiB
PHP
Raw Normal View History

2023-06-19 12:31:42 +00:00
<?php
2023-07-28 08:55:26 +00:00
namespace App\Notifications\Application;
2023-06-19 12:31:42 +00:00
use App\Models\Application;
2023-06-19 13:43:53 +00:00
use App\Models\ApplicationPreview;
2023-06-19 12:31:42 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
2023-07-28 08:55:26 +00:00
class DeploymentSuccess extends Notification implements ShouldQueue
2023-06-19 12:31:42 +00:00
{
use Queueable;
public $tries = 1;
2024-06-10 20:43:34 +00:00
2023-06-19 12:31:42 +00:00
public Application $application;
2024-06-10 20:43:34 +00:00
public ?ApplicationPreview $preview = null;
2023-06-19 13:24:04 +00:00
public string $deployment_uuid;
2024-06-10 20:43:34 +00:00
2023-06-19 13:34:39 +00:00
public string $application_name;
2024-06-10 20:43:34 +00:00
2023-06-19 12:31:42 +00:00
public string $project_uuid;
2024-06-10 20:43:34 +00:00
2023-06-19 12:31:42 +00:00
public string $environment_name;
public ?string $deployment_url = null;
2024-06-10 20:43:34 +00:00
public ?string $fqdn;
2023-06-19 12:31:42 +00:00
2024-06-10 20:43:34 +00:00
public function __construct(Application $application, string $deployment_uuid, ?ApplicationPreview $preview = null)
2023-06-19 12:31:42 +00:00
{
$this->application = $application;
$this->deployment_uuid = $deployment_uuid;
2023-06-19 13:43:53 +00:00
$this->preview = $preview;
2023-06-19 13:24:04 +00:00
$this->application_name = data_get($application, 'name');
2023-06-19 12:31:42 +00: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($this->fqdn)->explode(',')->count() > 1) {
$this->fqdn = str($this->fqdn)->explode(',')->first();
2023-06-19 12:31:42 +00:00
}
2024-06-10 20:43:34 +00:00
$this->deployment_url = base_url()."/project/{$this->project_uuid}/".urlencode($this->environment_name)."/application/{$this->application->uuid}/deployment/{$this->deployment_uuid}";
2023-06-19 12:31:42 +00:00
}
2023-06-19 12:31:42 +00:00
public function via(object $notifiable): array
{
2024-05-17 10:09:22 +00:00
$channels = setNotificationChannels($notifiable, 'deployments');
if (isCloud()) {
// TODO: Make batch notifications work with email
$channels = array_diff($channels, ['App\Notifications\Channels\EmailChannel']);
}
2024-06-10 20:43:34 +00:00
2024-05-17 10:09:22 +00:00
return $channels;
2023-06-19 12:31:42 +00:00
}
2024-06-10 20:43:34 +00:00
2023-06-21 08:48:43 +00:00
public function toMail(): MailMessage
2023-06-19 12:31:42 +00:00
{
$mail = new MailMessage();
2023-06-20 13:25:45 +00:00
$pull_request_id = data_get($this->preview, 'pull_request_id', 0);
2023-06-21 08:48:43 +00:00
$fqdn = $this->fqdn;
2023-06-20 13:25:45 +00:00
if ($pull_request_id === 0) {
2023-10-10 11:10:43 +00:00
$mail->subject("Coolify: New version is deployed of {$this->application_name}");
2023-06-20 13:25:45 +00:00
} else {
2023-06-21 08:48:43 +00:00
$fqdn = $this->preview->fqdn;
2023-10-10 11:10:43 +00:00
$mail->subject("Coolify: Pull request #{$pull_request_id} of {$this->application_name} deployed successfully");
2023-06-20 13:25:45 +00:00
}
2023-07-28 08:55:26 +00:00
$mail->view('emails.application-deployment-success', [
2023-06-19 12:31:42 +00:00
'name' => $this->application_name,
2023-06-21 08:48:43 +00:00
'fqdn' => $fqdn,
'deployment_url' => $this->deployment_url,
2023-06-20 13:25:45 +00:00
'pull_request_id' => $pull_request_id,
2023-06-19 12:31:42 +00:00
]);
2024-06-10 20:43:34 +00:00
2023-06-19 12:31:42 +00:00
return $mail;
}
public function toDiscord(): string
{
2023-06-19 13:43:53 +00:00
if ($this->preview) {
2024-06-10 20:43:34 +00:00
$message = 'Coolify: New PR'.$this->preview->pull_request_id.' version successfully deployed of '.$this->application_name.'
';
2023-06-21 10:06:35 +00:00
if ($this->preview->fqdn) {
2024-06-10 20:43:34 +00:00
$message .= '[Open Application]('.$this->preview->fqdn.') | ';
2023-06-21 10:06:35 +00:00
}
2024-06-10 20:43:34 +00:00
$message .= '[Deployment logs]('.$this->deployment_url.')';
2023-06-19 13:34:39 +00:00
} else {
2024-06-10 20:43:34 +00:00
$message = 'Coolify: New version successfully deployed of '.$this->application_name.'
';
2023-06-21 10:06:35 +00:00
if ($this->fqdn) {
2024-06-10 20:43:34 +00:00
$message .= '[Open Application]('.$this->fqdn.') | ';
2023-06-21 10:06:35 +00:00
}
2024-06-10 20:43:34 +00:00
$message .= '[Deployment logs]('.$this->deployment_url.')';
2023-06-19 13:34:39 +00:00
}
2024-06-10 20:43:34 +00:00
2023-06-19 13:34:39 +00:00
return $message;
2023-06-19 12:31:42 +00:00
}
2024-06-10 20:43:34 +00:00
2023-09-06 12:31:38 +00:00
public function toTelegram(): array
{
if ($this->preview) {
2024-06-10 20:43:34 +00:00
$message = 'Coolify: New PR'.$this->preview->pull_request_id.' version successfully deployed of '.$this->application_name.'';
2023-09-06 12:31:38 +00:00
if ($this->preview->fqdn) {
$buttons[] = [
2024-06-10 20:43:34 +00:00
'text' => 'Open Application',
'url' => $this->preview->fqdn,
2023-09-06 12:31:38 +00:00
];
}
} else {
2024-06-10 20:43:34 +00:00
$message = '✅ New version successfully deployed of '.$this->application_name.'';
2023-09-06 12:31:38 +00:00
if ($this->fqdn) {
$buttons[] = [
2024-06-10 20:43:34 +00:00
'text' => 'Open Application',
'url' => $this->fqdn,
2023-09-06 12:31:38 +00:00
];
}
}
$buttons[] = [
2024-06-10 20:43:34 +00:00
'text' => 'Deployment logs',
'url' => $this->deployment_url,
2023-09-06 12:31:38 +00:00
];
2024-06-10 20:43:34 +00:00
2023-09-06 12:31:38 +00:00
return [
2024-06-10 20:43:34 +00:00
'message' => $message,
'buttons' => [
...$buttons,
2023-09-06 12:31:38 +00:00
],
];
}
}