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

167 lines
5.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;
2023-07-11 11:11:51 +02:00
use Carbon\Carbon;
2023-04-25 14:43:35 +02:00
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;
public bool $branch_found = false;
2023-06-05 12:07:55 +02:00
public string $selected_branch = 'main';
2023-04-26 13:01:09 +02:00
public bool $is_static = false;
2023-06-05 12:07:55 +02:00
public string|null $publish_directory = null;
2023-07-11 11:11:51 +02:00
public string $git_branch = 'main';
public int $rate_limit_remaining = 0;
2023-07-11 11:11:51 +02:00
public $rate_limit_reset = 0;
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
];
2023-06-16 12:35:40 +02:00
protected $validationAttributes = [
'repository_url' => 'repository',
'port' => 'port',
'is_static' => 'static',
'publish_directory' => 'publish directory',
];
private object $repository_url_parsed;
private GithubApp|GitlabApp $git_source;
private string $git_host;
private string $git_repository;
2023-04-25 14:43:35 +02:00
public function mount()
{
2023-06-16 11:01:27 +02:00
if (isDev()) {
2023-06-05 12:07:55 +02:00
$this->repository_url = 'https://github.com/coollabsio/coolify-examples';
2023-04-25 14:43:35 +02:00
$this->port = 3000;
}
2023-06-15 09:15:41 +02:00
$this->parameters = getRouteParameters();
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-06-15 10:48:13 +02:00
$this->emit('success', 'Application settings updated!');
2023-04-26 13:01:09 +02:00
}
public function load_branch()
2023-06-05 12:07:55 +02:00
{
$this->branch_found = false;
2023-06-06 12:08:14 +02:00
$this->validate([
'repository_url' => 'required|url'
]);
2023-06-05 12:07:55 +02:00
$this->get_git_source();
try {
2023-07-11 11:11:51 +02:00
$this->get_branch();
$this->selected_branch = $this->git_branch;
2023-07-11 11:11:51 +02:00
} catch (\Exception $e) {
return general_error_handler(err: $e, that: $this);
2023-07-11 11:11:51 +02:00
}
if (!$this->branch_found && $this->git_branch == 'main') {
try {
$this->git_branch = 'master';
$this->get_branch();
} catch (\Exception $e) {
return general_error_handler(err: $e, that: $this);
}
2023-06-05 12:07:55 +02:00
}
}
2023-06-05 12:07:55 +02:00
private function get_git_source()
{
$this->repository_url_parsed = Url::fromString($this->repository_url);
$this->git_host = $this->repository_url_parsed->getHost();
$this->git_repository = $this->repository_url_parsed->getSegment(1) . '/' . $this->repository_url_parsed->getSegment(2);
$this->git_branch = $this->repository_url_parsed->getSegment(4) ?? 'main';
if ($this->git_host == 'github.com') {
$this->git_source = GithubApp::where('name', 'Public GitHub')->first();
} elseif ($this->git_host == 'gitlab.com') {
$this->git_source = GitlabApp::where('name', 'Public GitLab')->first();
} elseif ($this->git_host == 'bitbucket.org') {
// Not supported yet
}
}
private function get_branch()
{
['rate_limit_remaining' => $this->rate_limit_remaining, 'rate_limit_reset' => $this->rate_limit_reset] = git_api(source: $this->git_source, endpoint: "/repos/{$this->git_repository}/branches/{$this->git_branch}");
$this->rate_limit_reset = Carbon::parse((int)$this->rate_limit_reset)->format('Y-M-d H:i:s');
$this->branch_found = true;
}
2023-04-25 14:43:35 +02:00
public function submit()
{
2023-05-11 15:20:02 +02:00
try {
$this->validate();
2023-06-05 12:07:55 +02:00
$destination_uuid = $this->query['destination'];
$project_uuid = $this->parameters['project_uuid'];
$environment_name = $this->parameters['environment_name'];
2023-05-11 15:20:02 +02:00
2023-06-05 12:07:55 +02:00
$this->get_git_source();
$this->git_branch = $this->selected_branch ?? $this->git_branch;
2023-05-11 15:20:02 +02:00
$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-06-05 12:07:55 +02:00
$project = Project::where('uuid', $project_uuid)->first();
$environment = $project->load(['environments'])->environments->where('name', $environment_name)->first();
2023-05-11 15:20:02 +02:00
$application_init = [
2023-06-05 12:07:55 +02:00
'name' => generate_application_name($this->git_repository, $this->git_branch),
'git_repository' => $this->git_repository,
'git_branch' => $this->git_branch,
2023-05-11 15:20:02 +02:00
'build_pack' => 'nixpacks',
'ports_exposes' => $this->port,
'publish_directory' => $this->publish_directory,
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => $destination_class,
2023-06-05 12:07:55 +02:00
'source_id' => $this->git_source->id,
'source_type' => $this->git_source->getMorphClass()
2023-05-11 15:20:02 +02:00
];
2023-06-05 12:07:55 +02:00
2023-05-11 15:20:02 +02:00
$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) {
2023-06-09 15:55:21 +02:00
return general_error_handler(err: $e, that: $this);
2023-04-25 14:43:35 +02:00
}
}
}