lasthourcloud/app/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php

165 lines
6.1 KiB
PHP
Raw Normal View History

2023-05-10 11:05:32 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\New;
2023-05-10 11:05:32 +00:00
use App\Models\Application;
2023-06-12 12:38:32 +00:00
use App\Models\GithubApp;
use App\Models\GitlabApp;
2023-05-10 11:05:32 +00:00
use App\Models\PrivateKey;
use App\Models\Project;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Livewire\Component;
use Spatie\Url\Url;
2023-10-06 12:39:30 +00:00
use Illuminate\Support\Str;
2023-05-10 11:05:32 +00:00
class GithubPrivateRepositoryDeployKey extends Component
{
public $current_step = 'private_keys';
2023-05-10 11:05:32 +00:00
public $parameters;
2023-05-11 13:20:02 +00:00
public $query;
2023-05-10 11:05:32 +00:00
public $private_keys;
public int $private_key_id;
public int $port = 3000;
public string $type;
public bool $is_static = false;
public null|string $publish_directory = null;
2023-06-12 12:38:32 +00:00
public string $repository_url;
public string $branch;
2023-05-10 11:05:32 +00:00
protected $rules = [
2023-10-06 12:39:30 +00:00
'repository_url' => 'required',
2023-06-12 12:38:32 +00:00
'branch' => 'required|string',
2023-05-10 11:05:32 +00:00
'port' => 'required|numeric',
'is_static' => 'required|boolean',
'publish_directory' => 'nullable|string',
];
2023-06-12 12:38:32 +00:00
protected $validationAttributes = [
'repository_url' => 'Repository',
'branch' => 'Branch',
'port' => 'Port',
'is_static' => 'Is static',
'publish_directory' => 'Publish directory',
];
private object $repository_url_parsed;
2023-10-06 12:39:30 +00:00
private GithubApp|GitlabApp|string $git_source = 'other';
private ?string $git_host = null;
private string $git_repository;
2023-05-10 11:05:32 +00:00
public function mount()
{
if (isDev()) {
2023-06-12 12:38:32 +00:00
$this->repository_url = 'https://github.com/coollabsio/coolify-examples';
2023-05-10 11:05:32 +00:00
}
$this->parameters = get_route_parameters();
2023-05-11 13:20:02 +00:00
$this->query = request()->query();
2023-08-22 15:44:49 +00:00
$this->private_keys = PrivateKey::where('team_id', currentTeam()->id)->where('id', '!=', 0)->get();
2023-05-10 11:05:32 +00:00
}
2023-05-10 11:05:32 +00: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-10 11:05:32 +00:00
public function setPrivateKey($private_key_id)
{
$this->private_key_id = $private_key_id;
$this->current_step = 'repository';
2023-05-10 11:05:32 +00:00
}
2023-06-12 12:38:32 +00:00
2023-05-10 11:05:32 +00:00
public function submit()
{
2023-06-12 12:38:32 +00:00
$this->validate();
2023-05-11 13:20:02 +00:00
try {
$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-06-12 12:38:32 +00:00
$this->get_git_source();
2023-05-11 13:20:02 +00:00
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
2023-10-06 12:39:30 +00:00
if ($this->git_source === 'other') {
$application_init = [
'name' => generate_random_name(),
'git_repository' => $this->git_repository,
'git_branch' => $this->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,
'private_key_id' => $this->private_key_id,
];
} else {
$application_init = [
'name' => generate_random_name(),
'git_repository' => $this->git_repository,
'git_branch' => $this->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,
'private_key_id' => $this->private_key_id,
'source_id' => $this->git_source->id,
'source_type' => $this->git_source->getMorphClass()
];
}
2023-05-11 13:20:02 +00:00
$application = Application::create($application_init);
$application->settings->is_static = $this->is_static;
$application->settings->save();
2023-09-30 13:39:40 +00:00
$fqdn = generateFqdn($destination->server, $application->uuid);
$application->fqdn = $fqdn;
2023-09-19 13:51:13 +00:00
$application->name = generate_random_name($application->uuid);
$application->save();
2023-12-07 21:56:55 +00:00
return $this->redirectRoute('project.application.configuration', [
2023-05-11 13:20:02 +00:00
'application_uuid' => $application->uuid,
2023-12-07 21:56:55 +00:00
'environment_name' => $environment->name,
'project_uuid' => $project->uuid,
], navigate: true);
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-10 11:05:32 +00:00
}
}
private function get_git_source()
{
2023-10-06 12:50:49 +00:00
$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);
if ($this->git_host == 'github.com') {
$this->git_source = GithubApp::where('name', 'Public GitHub')->first();
2023-10-06 12:39:30 +00:00
return;
}
2023-10-06 12:50:49 +00:00
if (Str::of($this->repository_url)->startsWith('http')) {
$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_repository = Str::finish("git@$this->git_host:$this->git_repository", '.git');
} else {
$this->git_repository = $this->repository_url;
2023-10-06 12:50:49 +00:00
}
2023-10-06 12:39:30 +00:00
$this->git_source = 'other';
}
2023-05-10 11:05:32 +00:00
}