2023-04-25 12:43:35 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\New;
|
2023-04-25 12:43:35 +00:00
|
|
|
|
|
|
|
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 09:11:51 +00:00
|
|
|
use Carbon\Carbon;
|
2023-04-25 12:43:35 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
use Spatie\Url\Url;
|
|
|
|
|
|
|
|
class PublicGitRepository extends Component
|
|
|
|
{
|
2023-05-10 11:05:32 +00:00
|
|
|
public string $repository_url;
|
|
|
|
public int $port = 3000;
|
2023-04-26 11:25:41 +00:00
|
|
|
public string $type;
|
|
|
|
public $parameters;
|
2023-05-11 13:20:02 +00:00
|
|
|
public $query;
|
2023-06-28 11:37:00 +00:00
|
|
|
public bool $branch_found = false;
|
2023-06-05 10:07:55 +00:00
|
|
|
public string $selected_branch = 'main';
|
2023-04-26 11:01:09 +00:00
|
|
|
public bool $is_static = false;
|
2023-06-05 10:07:55 +00:00
|
|
|
public string|null $publish_directory = null;
|
2023-07-11 09:11:51 +00:00
|
|
|
public string $git_branch = 'main';
|
2023-06-28 11:37:00 +00:00
|
|
|
public int $rate_limit_remaining = 0;
|
2023-07-11 09:11:51 +00:00
|
|
|
public $rate_limit_reset = 0;
|
2023-10-01 10:29:50 +00:00
|
|
|
private object $repository_url_parsed;
|
2023-10-06 11:46:42 +00:00
|
|
|
public GithubApp|GitlabApp|string $git_source = 'other';
|
2023-10-01 10:29:50 +00:00
|
|
|
public string $git_host;
|
|
|
|
public string $git_repository;
|
2024-01-24 11:07:58 +00:00
|
|
|
public $build_pack = 'nixpacks';
|
2024-01-17 14:41:32 +00:00
|
|
|
public bool $show_is_static = true;
|
2023-10-06 11:46:42 +00:00
|
|
|
|
2023-04-25 12:43:35 +00:00
|
|
|
protected $rules = [
|
2023-05-10 11:05:32 +00:00
|
|
|
'repository_url' => 'required|url',
|
2023-04-26 08:02:19 +00:00
|
|
|
'port' => 'required|numeric',
|
|
|
|
'is_static' => 'required|boolean',
|
2023-04-27 10:25:32 +00:00
|
|
|
'publish_directory' => 'nullable|string',
|
2024-01-17 14:41:32 +00:00
|
|
|
'build_pack' => 'required|string',
|
2023-04-25 12:43:35 +00:00
|
|
|
];
|
2023-06-16 10:35:40 +00:00
|
|
|
protected $validationAttributes = [
|
|
|
|
'repository_url' => 'repository',
|
|
|
|
'port' => 'port',
|
|
|
|
'is_static' => 'static',
|
|
|
|
'publish_directory' => 'publish directory',
|
2024-01-17 14:41:32 +00:00
|
|
|
'build_pack' => 'build pack',
|
2023-06-16 10:35:40 +00:00
|
|
|
];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-04-25 12:43:35 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-06-05 10:07:55 +00:00
|
|
|
$this->repository_url = 'https://github.com/coollabsio/coolify-examples';
|
2023-04-25 12:43:35 +00:00
|
|
|
$this->port = 3000;
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
$this->parameters = get_route_parameters();
|
2023-05-11 13:20:02 +00:00
|
|
|
$this->query = request()->query();
|
2023-04-25 12:43:35 +00:00
|
|
|
}
|
2024-01-17 14:41:32 +00:00
|
|
|
public function updatedBuildPack()
|
|
|
|
{
|
|
|
|
if ($this->build_pack === 'nixpacks') {
|
|
|
|
$this->show_is_static = true;
|
2024-01-25 07:59:11 +00:00
|
|
|
$this->port = 3000;
|
2024-01-17 14:41:32 +00:00
|
|
|
} else if ($this->build_pack === 'static') {
|
|
|
|
$this->show_is_static = false;
|
|
|
|
$this->is_static = false;
|
2024-01-25 07:59:11 +00:00
|
|
|
$this->port = 80;
|
2024-01-17 14:41:32 +00:00
|
|
|
} else {
|
|
|
|
$this->show_is_static = false;
|
|
|
|
$this->is_static = false;
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 11:01:09 +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-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'Application settings updated!');
|
2023-04-26 11:01:09 +00:00
|
|
|
}
|
2023-10-06 11:46:42 +00:00
|
|
|
public function load_any_git()
|
|
|
|
{
|
|
|
|
$this->branch_found = true;
|
|
|
|
}
|
2023-06-28 11:37:00 +00:00
|
|
|
public function load_branch()
|
2023-06-05 10:07:55 +00:00
|
|
|
{
|
2023-09-01 09:35:33 +00:00
|
|
|
try {
|
2024-04-16 08:09:59 +00:00
|
|
|
if (str($this->repository_url)->startsWith('git@')) {
|
|
|
|
$github_instance = str($this->repository_url)->after('git@')->before(':');
|
|
|
|
$repository = str($this->repository_url)->after(':')->before('.git');
|
|
|
|
$this->repository_url = 'https://' . str($github_instance) . '/' . $repository;
|
|
|
|
}
|
2024-04-29 11:43:45 +00:00
|
|
|
if (
|
|
|
|
(str($this->repository_url)->startsWith('https://') ||
|
|
|
|
str($this->repository_url)->startsWith('http://')) &&
|
2024-04-30 09:11:06 +00:00
|
|
|
!str($this->repository_url)->endsWith('.git') &&
|
2024-05-03 13:31:05 +00:00
|
|
|
(!str($this->repository_url)->contains('github.com') ||
|
|
|
|
!str($this->repository_url)->contains('git.sr.ht'))
|
2024-04-29 11:43:45 +00:00
|
|
|
) {
|
|
|
|
$this->repository_url = $this->repository_url . '.git';
|
|
|
|
}
|
2024-04-30 09:11:06 +00:00
|
|
|
if (str($this->repository_url)->contains('github.com')) {
|
|
|
|
$this->repository_url = str($this->repository_url)->before('.git')->value();
|
|
|
|
}
|
2023-10-11 10:56:57 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$this->branch_found = false;
|
2023-09-19 13:51:13 +00:00
|
|
|
$this->get_git_source();
|
|
|
|
$this->get_branch();
|
|
|
|
$this->selected_branch = $this->git_branch;
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-01 10:29:50 +00:00
|
|
|
ray($e->getMessage());
|
2023-09-30 13:51:01 +00:00
|
|
|
if (!$this->branch_found && $this->git_branch == 'main') {
|
|
|
|
try {
|
|
|
|
$this->git_branch = 'master';
|
|
|
|
$this->get_branch();
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-07-11 09:11:51 +00:00
|
|
|
}
|
2023-06-05 10:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-05 10:07:55 +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);
|
|
|
|
$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();
|
2023-10-06 11:52:15 +00:00
|
|
|
return;
|
2023-09-01 09:35:33 +00:00
|
|
|
}
|
2023-10-06 11:46:42 +00:00
|
|
|
$this->git_repository = $this->repository_url;
|
|
|
|
$this->git_source = 'other';
|
2023-06-05 10:07:55 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
private function get_branch()
|
|
|
|
{
|
2023-10-06 11:46:42 +00:00
|
|
|
if ($this->git_source === 'other') {
|
|
|
|
$this->branch_found = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($this->git_source->getMorphClass() === 'App\Models\GithubApp') {
|
|
|
|
['rate_limit_remaining' => $this->rate_limit_remaining, 'rate_limit_reset' => $this->rate_limit_reset] = githubApi(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-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 12:43:35 +00:00
|
|
|
public function submit()
|
|
|
|
{
|
2023-05-11 13:20:02 +00:00
|
|
|
try {
|
|
|
|
$this->validate();
|
2023-06-05 10:07:55 +00:00
|
|
|
$destination_uuid = $this->query['destination'];
|
|
|
|
$project_uuid = $this->parameters['project_uuid'];
|
|
|
|
$environment_name = $this->parameters['environment_name'];
|
2023-05-11 13:20:02 +00: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 12:43:35 +00:00
|
|
|
|
2023-06-05 10:07:55 +00:00
|
|
|
$project = Project::where('uuid', $project_uuid)->first();
|
|
|
|
$environment = $project->load(['environments'])->environments->where('name', $environment_name)->first();
|
2023-05-11 13:20:02 +00:00
|
|
|
|
2023-10-06 11:46:42 +00:00
|
|
|
if ($this->git_source === 'other') {
|
|
|
|
$application_init = [
|
2023-10-06 18:48:03 +00:00
|
|
|
'name' => generate_random_name(),
|
2023-10-06 11:46:42 +00:00
|
|
|
'git_repository' => $this->git_repository,
|
|
|
|
'git_branch' => $this->git_branch,
|
|
|
|
'ports_exposes' => $this->port,
|
|
|
|
'publish_directory' => $this->publish_directory,
|
|
|
|
'environment_id' => $environment->id,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_type' => $destination_class,
|
2024-01-17 14:41:32 +00:00
|
|
|
'build_pack' => $this->build_pack,
|
2023-10-06 11:46:42 +00:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$application_init = [
|
|
|
|
'name' => generate_application_name($this->git_repository, $this->git_branch),
|
|
|
|
'git_repository' => $this->git_repository,
|
|
|
|
'git_branch' => $this->git_branch,
|
|
|
|
'ports_exposes' => $this->port,
|
|
|
|
'publish_directory' => $this->publish_directory,
|
|
|
|
'environment_id' => $environment->id,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_type' => $destination_class,
|
|
|
|
'source_id' => $this->git_source->id,
|
2024-01-17 14:41:32 +00:00
|
|
|
'source_type' => $this->git_source->getMorphClass(),
|
|
|
|
'build_pack' => $this->build_pack,
|
2023-10-06 11:46:42 +00:00
|
|
|
];
|
|
|
|
}
|
2023-06-05 10:07:55 +00:00
|
|
|
|
2024-05-08 12:42:45 +00:00
|
|
|
if ($this->build_pack === 'dockerfile' || $this->build_pack === 'dockerimage') {
|
|
|
|
$application_init['health_check_enabled'] = false;
|
|
|
|
}
|
2023-05-11 13:20:02 +00:00
|
|
|
$application = Application::create($application_init);
|
2023-09-19 13:51:13 +00:00
|
|
|
|
2023-05-11 13:20:02 +00:00
|
|
|
$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->save();
|
|
|
|
|
2023-12-27 15:45:01 +00:00
|
|
|
return redirect()->route('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,
|
2023-12-27 15:45:01 +00:00
|
|
|
]);
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-04-25 12:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|