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

144 lines
5.1 KiB
PHP
Raw Normal View History

2023-05-10 11:05:32 +00:00
<?php
namespace App\Http\Livewire\Project\New;
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;
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 = [
'repository_url' => 'required|url',
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-08-24 19:09:58 +00:00
private GithubApp|GitlabApp|null $git_source = null;
private string $git_host;
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();
$application_init = [
2023-05-24 12:26:50 +00:00
'name' => generate_random_name(),
2023-06-12 12:38:32 +00:00
'git_repository' => $this->git_repository,
2023-09-30 13:59:39 +00:00
'git_branch' => $this->branch,
2023-06-12 12:38:32 +00:00
'git_full_url' => "git@$this->git_host:$this->git_repository.git",
2023-05-11 13:20:02 +00: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,
'private_key_id' => $this->private_key_id,
2023-06-12 12:38:32 +00:00
'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-05-11 13:20:02 +00:00
return redirect()->route('project.application.configuration', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'application_uuid' => $application->uuid,
2023-05-10 11:05:32 +00:00
]);
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()
{
$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();
} elseif ($this->git_host == 'gitlab.com') {
$this->git_source = GitlabApp::where('name', 'Public GitLab')->first();
} elseif ($this->git_host == 'bitbucket.org') {
// Not supported yet
}
}
2023-05-10 11:05:32 +00:00
}