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;
|
2024-11-26 09:19:05 +00:00
|
|
|
use App\Notifications\CustomEmailNotification;
|
2024-10-01 20:07:24 +00:00
|
|
|
use App\Notifications\Dto\DiscordMessage;
|
2024-12-11 14:54:11 +00:00
|
|
|
use App\Notifications\Dto\PushoverMessage;
|
2024-11-12 21:37:55 +00:00
|
|
|
use App\Notifications\Dto\SlackMessage;
|
2023-06-19 12:31:42 +00:00
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
|
|
|
|
2024-11-26 09:19:05 +00:00
|
|
|
class DeploymentFailed extends CustomEmailNotification
|
2023-06-19 12:31:42 +00:00
|
|
|
{
|
|
|
|
|
public Application $application;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-01 13:52:18 +00:00
|
|
|
public ?ApplicationPreview $preview = null;
|
2023-06-19 13:24:04 +00:00
|
|
|
|
2023-09-15 13:34:25 +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;
|
2023-09-15 13:34:25 +00:00
|
|
|
|
|
|
|
|
public ?string $deployment_url = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-01 13:52:18 +00:00
|
|
|
public ?string $fqdn = null;
|
2023-06-19 12:31:42 +00:00
|
|
|
|
2023-09-01 13:52:18 +00:00
|
|
|
public function __construct(Application $application, string $deployment_uuid, ?ApplicationPreview $preview = null)
|
2023-06-19 12:31:42 +00:00
|
|
|
{
|
2024-11-22 10:16:01 +00:00
|
|
|
$this->onQueue('high');
|
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');
|
2024-06-25 08:37:10 +00:00
|
|
|
if (str($this->fqdn)->explode(',')->count() > 1) {
|
|
|
|
|
$this->fqdn = str($this->fqdn)->explode(',')->first();
|
2023-06-19 12:31:42 +00:00
|
|
|
}
|
2024-12-09 15:57:15 +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-08-08 09:51:36 +00:00
|
|
|
|
2023-06-19 12:31:42 +00:00
|
|
|
public function via(object $notifiable): array
|
|
|
|
|
{
|
2024-12-09 15:57:15 +00:00
|
|
|
return $notifiable->getEnabledChannels('deployment_failure');
|
2023-06-19 12:31:42 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-21 08:48:43 +00:00
|
|
|
public function toMail(): MailMessage
|
2023-06-19 12:31:42 +00:00
|
|
|
{
|
2024-07-24 12:27:21 +00:00
|
|
|
$mail = new MailMessage;
|
2023-06-21 08:48:43 +00:00
|
|
|
$pull_request_id = data_get($this->preview, 'pull_request_id', 0);
|
|
|
|
|
$fqdn = $this->fqdn;
|
|
|
|
|
if ($pull_request_id === 0) {
|
2024-12-09 15:57:15 +00:00
|
|
|
$mail->subject('Coolify: Deployment failed of '.$this->application_name.'.');
|
2023-06-21 08:48:43 +00:00
|
|
|
} else {
|
|
|
|
|
$fqdn = $this->preview->fqdn;
|
2024-12-09 15:57:15 +00:00
|
|
|
$mail->subject('Coolify: Deployment failed of pull request #'.$this->preview->pull_request_id.' of '.$this->application_name.'.');
|
2023-06-21 08:48:43 +00:00
|
|
|
}
|
2023-07-28 08:55:26 +00:00
|
|
|
$mail->view('emails.application-deployment-failed', [
|
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' => data_get($this->preview, 'pull_request_id', 0),
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 20:07:24 +00:00
|
|
|
public function toDiscord(): DiscordMessage
|
2023-06-19 12:31:42 +00:00
|
|
|
{
|
2023-06-19 13:43:53 +00:00
|
|
|
if ($this->preview) {
|
2024-10-01 20:07:24 +00:00
|
|
|
$message = new DiscordMessage(
|
2024-10-21 20:40:43 +00:00
|
|
|
title: ':cross_mark: Deployment failed',
|
2024-12-09 15:57:15 +00:00
|
|
|
description: 'Pull request: '.$this->preview->pull_request_id,
|
2024-10-01 20:07:24 +00:00
|
|
|
color: DiscordMessage::errorColor(),
|
|
|
|
|
isCritical: true,
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-21 20:40:43 +00:00
|
|
|
$message->addField('Project', data_get($this->application, 'environment.project.name'), true);
|
|
|
|
|
$message->addField('Environment', $this->environment_name, true);
|
|
|
|
|
$message->addField('Name', $this->application_name, true);
|
|
|
|
|
|
2024-12-09 15:57:15 +00:00
|
|
|
$message->addField('Deployment Logs', '[Link]('.$this->deployment_url.')');
|
2024-10-21 20:40:43 +00:00
|
|
|
if ($this->fqdn) {
|
|
|
|
|
$message->addField('Domain', $this->fqdn, true);
|
|
|
|
|
}
|
2023-06-21 08:48:43 +00:00
|
|
|
} else {
|
2024-10-21 20:40:43 +00:00
|
|
|
if ($this->fqdn) {
|
2024-12-09 15:57:15 +00:00
|
|
|
$description = '[Open application]('.$this->fqdn.')';
|
2024-10-21 20:40:43 +00:00
|
|
|
} else {
|
|
|
|
|
$description = '';
|
|
|
|
|
}
|
2024-10-01 20:07:24 +00:00
|
|
|
$message = new DiscordMessage(
|
2024-10-21 20:40:43 +00:00
|
|
|
title: ':cross_mark: Deployment failed',
|
|
|
|
|
description: $description,
|
2024-10-01 20:07:24 +00:00
|
|
|
color: DiscordMessage::errorColor(),
|
|
|
|
|
isCritical: true,
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-21 20:40:43 +00:00
|
|
|
$message->addField('Project', data_get($this->application, 'environment.project.name'), true);
|
|
|
|
|
$message->addField('Environment', $this->environment_name, true);
|
|
|
|
|
$message->addField('Name', $this->application_name, true);
|
|
|
|
|
|
2024-12-09 15:57:15 +00:00
|
|
|
$message->addField('Deployment Logs', '[Link]('.$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-12-09 15:57:15 +00:00
|
|
|
$message = 'Coolify: Pull request #'.$this->preview->pull_request_id.' of '.$this->application_name.' ('.$this->preview->fqdn.') deployment failed: ';
|
2023-09-06 12:31:38 +00:00
|
|
|
} else {
|
2024-12-09 15:57:15 +00:00
|
|
|
$message = 'Coolify: Deployment failed of '.$this->application_name.' ('.$this->fqdn.'): ';
|
2023-09-06 12:31:38 +00:00
|
|
|
}
|
2023-11-08 13:37:01 +00:00
|
|
|
$buttons[] = [
|
2024-06-10 20:43:34 +00:00
|
|
|
'text' => 'Deployment logs',
|
|
|
|
|
'url' => $this->deployment_url,
|
2023-11-08 13:37:01 +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
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
2024-11-12 21:37:55 +00:00
|
|
|
|
2024-12-11 14:54:11 +00:00
|
|
|
public function toPushover(): PushoverMessage
|
|
|
|
|
{
|
|
|
|
|
if ($this->preview) {
|
|
|
|
|
$title = "Pull request #{$this->preview->pull_request_id} deployment failed";
|
|
|
|
|
$message = "Pull request deployment failed for {$this->application_name}";
|
|
|
|
|
} else {
|
|
|
|
|
$title = 'Deployment failed';
|
|
|
|
|
$message = "Deployment failed for {$this->application_name}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$buttons[] = [
|
|
|
|
|
'text' => 'Deployment logs',
|
|
|
|
|
'url' => $this->deployment_url,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return new PushoverMessage(
|
|
|
|
|
title: $title,
|
|
|
|
|
level: 'error',
|
|
|
|
|
message: $message,
|
|
|
|
|
buttons: [
|
|
|
|
|
...$buttons,
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 21:37:55 +00:00
|
|
|
public function toSlack(): SlackMessage
|
|
|
|
|
{
|
|
|
|
|
if ($this->preview) {
|
|
|
|
|
$title = "Pull request #{$this->preview->pull_request_id} deployment failed";
|
|
|
|
|
$description = "Pull request deployment failed for {$this->application_name}";
|
|
|
|
|
if ($this->preview->fqdn) {
|
|
|
|
|
$description .= "\nPreview URL: {$this->preview->fqdn}";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-12-09 15:57:15 +00:00
|
|
|
$title = 'Deployment failed';
|
2024-11-12 21:37:55 +00:00
|
|
|
$description = "Deployment failed for {$this->application_name}";
|
|
|
|
|
if ($this->fqdn) {
|
|
|
|
|
$description .= "\nApplication URL: {$this->fqdn}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 15:57:15 +00:00
|
|
|
$description .= "\n\n**Project:** ".data_get($this->application, 'environment.project.name');
|
2024-11-12 21:37:55 +00:00
|
|
|
$description .= "\n**Environment:** {$this->environment_name}";
|
|
|
|
|
$description .= "\n**Deployment Logs:** {$this->deployment_url}";
|
|
|
|
|
|
|
|
|
|
return new SlackMessage(
|
|
|
|
|
title: $title,
|
|
|
|
|
description: $description,
|
|
|
|
|
color: SlackMessage::errorColor()
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|