coolify/app/Http/Livewire/Project/New/PublicGitRepository.php

113 lines
3.9 KiB
PHP
Raw Normal View History

2023-04-25 14:43:35 +02:00
<?php
namespace App\Http\Livewire\Project\New;
use App\Models\Application;
use App\Models\GithubApp;
use App\Models\GitlabApp;
use App\Models\Project;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Livewire\Component;
use Spatie\Url\Url;
class PublicGitRepository extends Component
{
2023-05-10 13:05:32 +02:00
public string $repository_url;
public int $port = 3000;
2023-04-26 13:25:41 +02:00
public string $type;
public $parameters;
2023-05-11 15:20:02 +02:00
public $query;
2023-04-25 14:43:35 +02:00
public $github_apps;
public $gitlab_apps;
2023-04-26 13:01:09 +02:00
public bool $is_static = false;
2023-04-27 12:25:32 +02:00
public null|string $publish_directory = null;
2023-04-26 13:01:09 +02:00
2023-04-25 14:43:35 +02:00
protected $rules = [
2023-05-10 13:05:32 +02:00
'repository_url' => 'required|url',
'port' => 'required|numeric',
'is_static' => 'required|boolean',
2023-04-27 12:25:32 +02:00
'publish_directory' => 'nullable|string',
2023-04-25 14:43:35 +02:00
];
public function mount()
{
2023-05-03 08:55:03 +02:00
if (config('app.env') === 'local') {
2023-05-10 13:05:32 +02:00
$this->repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';
2023-04-25 14:43:35 +02:00
$this->port = 3000;
}
2023-05-08 13:36:49 +02:00
$this->parameters = getParameters();
2023-05-11 15:20:02 +02:00
$this->query = request()->query();
2023-04-25 14:43:35 +02:00
}
2023-04-26 13:01:09 +02:00
public function instantSave()
{
if ($this->is_static) {
$this->port = 80;
$this->publish_directory = '/dist';
} else {
$this->port = 3000;
$this->publish_directory = null;
}
2023-05-09 09:54:43 +02:00
$this->emit('saved', 'Application settings updated!');
2023-04-26 13:01:09 +02:00
}
2023-04-25 14:43:35 +02:00
public function submit()
{
2023-05-11 15:20:02 +02:00
try {
$this->validate();
$url = Url::fromString($this->repository_url);
$git_host = $url->getHost();
$git_repository = $url->getSegment(1) . '/' . $url->getSegment(2);
$git_branch = $url->getSegment(4) ?? 'main';
$destination_uuid = $this->query['destination'];
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
if (!$destination) {
$destination = SwarmDocker::where('uuid', $destination_uuid)->first();
}
if (!$destination) {
throw new \Exception('Destination not found. What?!');
}
$destination_class = $destination->getMorphClass();
2023-04-25 14:43:35 +02:00
2023-05-11 15:20:02 +02:00
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
$application_init = [
2023-05-17 13:32:18 +02:00
'name' => generateRandomName(),
2023-05-11 15:20:02 +02:00
'git_repository' => $git_repository,
'git_branch' => $git_branch,
'build_pack' => 'nixpacks',
'ports_exposes' => $this->port,
'publish_directory' => $this->publish_directory,
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => $destination_class,
];
if ($git_host == 'github.com') {
$application_init['source_id'] = GithubApp::where('name', 'Public GitHub')->first()->id;
$application_init['source_type'] = GithubApp::class;
} elseif ($git_host == 'gitlab.com') {
$application_init['source_id'] = GitlabApp::where('name', 'Public GitLab')->first()->id;
$application_init['source_type'] = GitlabApp::class;
} elseif ($git_host == 'bitbucket.org') {
}
$application = Application::create($application_init);
$application->settings->is_static = $this->is_static;
$application->settings->save();
return redirect()->route('project.application.configuration', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'application_uuid' => $application->uuid,
2023-04-26 13:25:41 +02:00
]);
2023-05-11 15:20:02 +02:00
} catch (\Exception $e) {
return generalErrorHandler($e);
2023-04-25 14:43:35 +02:00
}
}
}