Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notification for deployment completion #445

Merged
merged 9 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/Actions/Site/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use App\Enums\DeploymentStatus;
use App\Exceptions\DeploymentScriptIsEmptyException;
use App\Exceptions\SSHError;
use App\Facades\Notifier;
use App\Models\Deployment;
use App\Models\ServerLog;
use App\Models\Site;
use App\Notifications\DeploymentCompleted;

class Deploy
{
Expand Down Expand Up @@ -53,9 +55,11 @@ public function run(Site $site): Deployment
);
$deployment->status = DeploymentStatus::FINISHED;
$deployment->save();
})->catch(function () use ($deployment) {
Notifier::send($site, new DeploymentCompleted($deployment, $site));
})->catch(function () use ($deployment, $site) {
$deployment->status = DeploymentStatus::FAILED;
$deployment->save();
Notifier::send($site, new DeploymentCompleted($deployment, $site));
})->onConnection('ssh');

return $deployment;
Expand Down
50 changes: 50 additions & 0 deletions app/Notifications/DeploymentCompleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Notifications;

use App\Models\Deployment;
use App\Models\Site;
use Illuminate\Notifications\Messages\MailMessage;

class DeploymentCompleted extends AbstractNotification
{
protected Deployment $deployment;

protected Site $site;

public function __construct(Deployment $deployment, Site $site)
{
$this->deployment = $deployment;
$this->site = $site;
}

public function rawText(): string
{
return __('Deployment for site [:site] has completed with status: :status', [
'site' => $this->site->domain,
'status' => $this->deployment->status,
]);
}

public function toEmail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject(__('Deployment Completed'))
->line('Deployment for site ['.$this->site->domain.'] has completed with status: '.$this->deployment->status);
}

public function toSlack(object $notifiable): string
{
return $this->rawText();
}

public function toDiscord(object $notifiable): string
{
return $this->rawText();
}

public function toTelegram(object $notifiable): string
{
return $this->rawText();
}
}
5 changes: 5 additions & 0 deletions tests/Feature/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use App\Enums\DeploymentStatus;
use App\Facades\SSH;
use App\Models\GitHook;
use App\Notifications\DeploymentCompleted;
use App\Web\Pages\Servers\Sites\View;
use App\Web\Pages\Servers\Sites\Widgets\DeploymentsList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Notification;
use Livewire\Livewire;
use Tests\TestCase;

Expand Down Expand Up @@ -65,6 +67,7 @@ public function test_deploy(): void
],
]),
]);
Notification::fake();

$this->site->deploymentScript->update([
'content' => 'git pull',
Expand All @@ -88,6 +91,8 @@ public function test_deploy(): void
SSH::assertExecutedContains('cd /home/vito/'.$this->site->domain);
SSH::assertExecutedContains('git pull');

Notification::assertSentTo($this->notificationChannel, DeploymentCompleted::class);

$this->get(
View::getUrl([
'server' => $this->server,
Expand Down
8 changes: 5 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Tests;

use App\Enums\Database;
use App\Enums\NotificationChannel;
use App\Enums\ServiceStatus;
use App\Enums\UserRole;
use App\Enums\Webserver;
use App\Models\NotificationChannel;
use App\Models\Server;
use App\Models\Site;
use App\Models\SourceControl;
Expand All @@ -24,6 +24,8 @@ abstract class TestCase extends BaseTestCase

protected Site $site;

protected NotificationChannel $notificationChannel;

public const EXPECT_SUCCESS = true;

public const EXPECT_FAILURE = false;
Expand All @@ -40,8 +42,8 @@ protected function setUp(): void
]);
$this->user->createDefaultProject();

\App\Models\NotificationChannel::factory()->create([
'provider' => NotificationChannel::EMAIL,
$this->notificationChannel = NotificationChannel::factory()->create([
'provider' => \App\Enums\NotificationChannel::EMAIL,
'connected' => true,
'data' => [
'email' => '[email protected]',
Expand Down
Loading