From 51d716253f196e89c983fa25db1439818d79ea61 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 28 Mar 2024 15:05:12 +0100 Subject: [PATCH] feat: watch paths --- app/Http/Controllers/Webhook/Github.php | 44 +++++++++++++------ app/Livewire/Project/Application/General.php | 2 + app/Models/Application.php | 21 +++++++++ bootstrap/helpers/applications.php | 1 + ...4_03_28_114620_add_watch_paths_to_apps.php | 28 ++++++++++++ resources/css/app.css | 2 +- .../project/application/general.blade.php | 8 +++- 7 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 database/migrations/2024_03_28_114620_add_watch_paths_to_apps.php diff --git a/app/Http/Controllers/Webhook/Github.php b/app/Http/Controllers/Webhook/Github.php index 02ba017ca..433d29c58 100644 --- a/app/Http/Controllers/Webhook/Github.php +++ b/app/Http/Controllers/Webhook/Github.php @@ -204,6 +204,7 @@ public function manual(Request $request) } public function normal(Request $request) { + ray('asdf'); try { $return_payloads = collect([]); $id = null; @@ -266,6 +267,10 @@ public function normal(Request $request) if (Str::isMatch('/refs\/heads\/*/', $branch)) { $branch = Str::after($branch, 'refs/heads/'); } + $added_files = data_get($payload, 'commits.*.added'); + $removed_files = data_get($payload, 'commits.*.removed'); + $modified_files = data_get($payload, 'commits.*.modified'); + $changed_files = collect($added_files)->concat($removed_files)->concat($modified_files)->unique()->flatten(); ray('Webhook GitHub Push Event: ' . $id . ' with branch: ' . $branch); } if ($x_github_event === 'pull_request') { @@ -306,19 +311,32 @@ public function normal(Request $request) } if ($x_github_event === 'push') { if ($application->isDeployable()) { - ray('Deploying ' . $application->name . ' with branch ' . $branch); - $deployment_uuid = new Cuid2(7); - queue_application_deployment( - application: $application, - deployment_uuid: $deployment_uuid, - force_rebuild: false, - is_webhook: true - ); - $return_payloads->push([ - 'application' => $application->name, - 'status' => 'success', - 'message' => 'Deployment queued.', - ]); + $watch_files_trigger = $application->watchPathCheck($changed_files); + if (!$watch_files_trigger) { + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Watch paths does not have the changed files. Deployment ignored.', + 'details' => [ + 'changed_files' => $changed_files, + 'watch_paths' => $application->watch_paths, + ], + ]); + } else { + ray('Deploying ' . $application->name . ' with branch ' . $branch); + $deployment_uuid = new Cuid2(7); + queue_application_deployment( + application: $application, + deployment_uuid: $deployment_uuid, + force_rebuild: false, + is_webhook: true, + ); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'success', + 'message' => 'Deployment queued.', + ]); + } } else { $return_payloads->push([ 'application' => $application->name, diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 9485cdd03..c6b8f2e51 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -73,6 +73,7 @@ class General extends Component 'application.settings.is_static' => 'boolean|required', 'application.settings.is_raw_compose_deployment_enabled' => 'boolean|required', 'application.settings.is_build_server_enabled' => 'boolean|required', + 'application.watch_paths' => 'nullable', ]; protected $validationAttributes = [ 'application.name' => 'name', @@ -108,6 +109,7 @@ class General extends Component 'application.settings.is_static' => 'Is static', 'application.settings.is_raw_compose_deployment_enabled' => 'Is raw compose deployment enabled', 'application.settings.is_build_server_enabled' => 'Is build server enabled', + 'application.watch_paths' => 'Watch paths', ]; public function mount() { diff --git a/app/Models/Application.php b/app/Models/Application.php index bc3b27bb0..fc03f8b1e 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Collection; use Spatie\Activitylog\Models\Activity; use Illuminate\Support\Str; use RuntimeException; @@ -903,4 +904,24 @@ public function fqdns(): Attribute : explode(',', $this->fqdn), ); } + public function watchPaths(): Attribute + { + return Attribute::make( + set: function ($value) { + if ($value) { + return trim($value); + } + } + ); + } + public function watchPathCheck(Collection $modified_files): bool + { + $watch_paths = collect(explode("\n", $this->watch_paths)); + $matches = $modified_files->filter(function ($file) use ($watch_paths) { + return $watch_paths->contains(function ($glob) use ($file) { + return fnmatch($glob, $file); + }); + }); + return $matches->count() > 0; + } } diff --git a/bootstrap/helpers/applications.php b/bootstrap/helpers/applications.php index bf3f15514..8d810da0f 100644 --- a/bootstrap/helpers/applications.php +++ b/bootstrap/helpers/applications.php @@ -6,6 +6,7 @@ use App\Models\ApplicationDeploymentQueue; use App\Models\Server; use App\Models\StandaloneDocker; +use Illuminate\Support\Collection; use Spatie\Url\Url; function queue_application_deployment(Application $application, string $deployment_uuid, int | null $pull_request_id = 0, string $commit = 'HEAD', bool $force_rebuild = false, bool $is_webhook = false, bool $restart_only = false, ?string $git_type = null, bool $no_questions_asked = false, Server $server = null, StandaloneDocker $destination = null, bool $only_this_server = false) diff --git a/database/migrations/2024_03_28_114620_add_watch_paths_to_apps.php b/database/migrations/2024_03_28_114620_add_watch_paths_to_apps.php new file mode 100644 index 000000000..d58adcd62 --- /dev/null +++ b/database/migrations/2024_03_28_114620_add_watch_paths_to_apps.php @@ -0,0 +1,28 @@ +longText('watch_paths')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('applications', function (Blueprint $table) { + $table->dropColumn('watch_paths'); + }); + } +}; diff --git a/resources/css/app.css b/resources/css/app.css index c2ca9f612..5976e2285 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -13,7 +13,7 @@ body { .input, .select { - @apply text-black dark:bg-coolgray-100 dark:text-white ring-neutral-300 dark:ring-coolgray-300; + @apply text-black dark:bg-coolgray-100 dark:text-white ring-neutral-200 dark:ring-coolgray-300; } /* Readonly */ diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.php index 44578c23c..e89431365 100644 --- a/resources/views/livewire/project/application/general.blade.php +++ b/resources/views/livewire/project/application/general.blade.php @@ -148,7 +148,8 @@ class="underline" href="https://coolify.io/docs/knowledge-base/docker/registry" id="application.start_command" label="Start Command" />
Nixpacks will detect the required configuration automatically. - Framework Specific Docs + Framework + Specific Docs
@endif @endif @@ -201,7 +202,12 @@ class="underline" href="https://coolify.io/docs/knowledge-base/docker/registry" label="Publish Directory" /> @endif @endif + + @if ($this->application->is_github_based()) + + @endif
The following options are for advanced use cases. Only modify them if you know what are you doing.