Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor(previews): move preview domain generation logic to ApplicationPreview model for better encapsulation and consistency across webhook handlers

Andras Bacsai5bdf2e84816f
diff --git a/app/Http/Controllers/Webhook/Bitbucket.php b/app/Http/Controllers/Webhook/Bitbucket.php
index 490b66e58b..078494f82b 100644
--- a/app/Http/Controllers/Webhook/Bitbucket.php
+++ b/app/Http/Controllers/Webhook/Bitbucket.php
@@ -143,12 +143,13 @@ class Bitbucket extends Controller
]);
$pr_app->generate_preview_fqdn_compose();
} else {
- ApplicationPreview::create([
+ $pr_app = ApplicationPreview::create([
'git_type' => 'bitbucket',
'application_id' => $application->id,
'pull_request_id' => $pull_request_id,
'pull_request_html_url' => $pull_request_html_url,
]);
+ $pr_app->generate_preview_fqdn();
}
}
$result = queue_application_deployment(
diff --git a/app/Http/Controllers/Webhook/Gitea.php b/app/Http/Controllers/Webhook/Gitea.php
index 3c3d6e0b6e..3e0c5a0b61 100644
--- a/app/Http/Controllers/Webhook/Gitea.php
+++ b/app/Http/Controllers/Webhook/Gitea.php
@@ -175,12 +175,13 @@ class Gitea extends Controller
]);
$pr_app->generate_preview_fqdn_compose();
} else {
- ApplicationPreview::create([
+ $pr_app = ApplicationPreview::create([
'git_type' => 'gitea',
'application_id' => $application->id,
'pull_request_id' => $pull_request_id,
'pull_request_html_url' => $pull_request_html_url,
]);
+ $pr_app->generate_preview_fqdn();
}
}
$result = queue_application_deployment(
diff --git a/app/Http/Controllers/Webhook/Github.php b/app/Http/Controllers/Webhook/Github.php
index 597ec023f5..8872754e56 100644
--- a/app/Http/Controllers/Webhook/Github.php
+++ b/app/Http/Controllers/Webhook/Github.php
@@ -183,12 +183,13 @@ class Github extends Controller
]);
$pr_app->generate_preview_fqdn_compose();
} else {
- ApplicationPreview::create([
+ $pr_app = ApplicationPreview::create([
'git_type' => 'github',
'application_id' => $application->id,
'pull_request_id' => $pull_request_id,
'pull_request_html_url' => $pull_request_html_url,
]);
+ $pr_app->generate_preview_fqdn();
}
}
diff --git a/app/Http/Controllers/Webhook/Gitlab.php b/app/Http/Controllers/Webhook/Gitlab.php
index d6d12a05fc..3187663d46 100644
--- a/app/Http/Controllers/Webhook/Gitlab.php
+++ b/app/Http/Controllers/Webhook/Gitlab.php
@@ -202,12 +202,13 @@ class Gitlab extends Controller
]);
$pr_app->generate_preview_fqdn_compose();
} else {
- ApplicationPreview::create([
+ $pr_app = ApplicationPreview::create([
'git_type' => 'gitlab',
'application_id' => $application->id,
'pull_request_id' => $pull_request_id,
'pull_request_html_url' => $pull_request_html_url,
]);
+ $pr_app->generate_preview_fqdn();
}
}
$result = queue_application_deployment(
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 708d2f15d7..67043555da 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -236,8 +236,11 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$this->preview->generate_preview_fqdn_compose();
}
} else {
- // For non-Docker Compose apps, use the application model's method
- $this->preview = $this->application->generate_preview_fqdn($this->pull_request_id);
+ // For non-Docker Compose apps, use the preview model's method
+ $this->preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pull_request_id);
+ if ($this->preview) {
+ $this->preview->generate_preview_fqdn();
+ }
}
if ($this->application->is_github_based()) {
ApplicationPullRequestUpdateJob::dispatch(application: $this->application, preview: $this->preview, deployment_uuid: $this->deployment_uuid, status: ProcessStatus::IN_PROGRESS);
diff --git a/app/Livewire/Project/Application/Previews.php b/app/Livewire/Project/Application/Previews.php
index c781c9d8a6..64e920cd73 100644
--- a/app/Livewire/Project/Application/Previews.php
+++ b/app/Livewire/Project/Application/Previews.php
@@ -86,7 +86,7 @@ class Previews extends Component
return;
}
- $this->application->generate_preview_fqdn($preview->pull_request_id);
+ $preview->generate_preview_fqdn();
$this->application->refresh();
$this->dispatch('update_links');
$this->dispatch('success', 'Domain generated.');
@@ -118,7 +118,7 @@ class Previews extends Component
'pull_request_html_url' => $pull_request_html_url,
]);
}
- $this->application->generate_preview_fqdn($pull_request_id);
+ $found->generate_preview_fqdn();
$this->application->refresh();
$this->dispatch('update_links');
$this->dispatch('success', 'Preview added.');
diff --git a/app/Models/Application.php b/app/Models/Application.php
index f304debad9..86eea1de8e 100644
--- a/app/Models/Application.php
+++ b/app/Models/Application.php
@@ -1583,34 +1583,6 @@ class Application extends BaseModel
}
}
- public function generate_preview_fqdn(int $pull_request_id)
- {
- $preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->id, $pull_request_id);
- if (is_null(data_get($preview, 'fqdn')) && $this->fqdn) {
- if (str($this->fqdn)->contains(',')) {
- $url = Url::fromString(str($this->fqdn)->explode(',')[0]);
- $preview_fqdn = getFqdnWithoutPort(str($this->fqdn)->explode(',')[0]);
- } else {
- $url = Url::fromString($this->fqdn);
- if (data_get($preview, 'fqdn')) {
- $preview_fqdn = getFqdnWithoutPort(data_get($preview, 'fqdn'));
- }
- }
- $template = $this->preview_url_template;
- $host = $url->getHost();
- $schema = $url->getScheme();
- $random = new Cuid2;
- $preview_fqdn = str_replace('{{random}}', $random, $template);
- $preview_fqdn = str_replace('{{domain}}', $host, $preview_fqdn);
- $preview_fqdn = str_replace('{{pr_id}}', $pull_request_id, $preview_fqdn);
- $preview_fqdn = "$schema://$preview_fqdn";
- $preview->fqdn = $preview_fqdn;
- $preview->save();
- }
-
- return $preview;
- }
-
public static function getDomainsByUuid(string $uuid): array
{
$application = self::where('uuid', $uuid)->first();
diff --git a/app/Models/ApplicationPreview.php b/app/Models/ApplicationPreview.php
index 7bf45de85e..a92eb320b5 100644
--- a/app/Models/ApplicationPreview.php
+++ b/app/Models/ApplicationPreview.php
@@ -50,6 +50,33 @@ class ApplicationPreview extends BaseModel
return $this->belongsTo(Application::class);
}
+ public function generate_preview_fqdn()
+ {
+ if (is_null($this->fqdn) && $this->application->fqdn) {
+ if (str($this->application->fqdn)->contains(',')) {
+ $url = Url::fromString(str($this->application->fqdn)->explode(',')[0]);
+ $preview_fqdn = getFqdnWithoutPort(str($this->application->fqdn)->explode(',')[0]);
+ } else {
+ $url = Url::fromString($this->application->fqdn);
+ if ($this->fqdn) {
+ $preview_fqdn = getFqdnWithoutPort($this->fqdn);
+ }
+ }
+ $template = $this->application->preview_url_template;
+ $host = $url->getHost();
+ $schema = $url->getScheme();
+ $random = new Cuid2;
+ $preview_fqdn = str_replace('{{random}}', $random, $template);
+ $preview_fqdn = str_replace('{{domain}}', $host, $preview_fqdn);
+ $preview_fqdn = str_replace('{{pr_id}}', $this->pull_request_id, $preview_fqdn);
+ $preview_fqdn = "$schema://$preview_fqdn";
+ $this->fqdn = $preview_fqdn;
+ $this->save();
+ }
+
+ return $this;
+ }
+
public function generate_preview_fqdn_compose()
{
$services = collect(json_decode($this->application->docker_compose_domains)) ?? collect();