From d96cae2fffec126e14f4f4de6b58470472b0b506 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 12 Jun 2023 14:38:32 +0200 Subject: [PATCH] fix --- app/Http/Controllers/MagicController.php | 1 - .../Livewire/Project/Application/Source.php | 21 ++++++- .../New/GithubPrivateRepositoryDeployKey.php | 59 +++++++++++++++---- .../components/applications/navbar.blade.php | 2 +- resources/views/components/git-icon.blade.php | 13 ++++ .../project/application/source.blade.php | 20 +++++-- ...ub-private-repository-deploy-key.blade.php | 28 +++++---- 7 files changed, 112 insertions(+), 32 deletions(-) diff --git a/app/Http/Controllers/MagicController.php b/app/Http/Controllers/MagicController.php index b5a254c96..9a23b2fe1 100644 --- a/app/Http/Controllers/MagicController.php +++ b/app/Http/Controllers/MagicController.php @@ -39,7 +39,6 @@ class MagicController extends Controller ['name' => request()->query('name') ?? generate_random_name()], ['team_id' => session('currentTeam')->id] ); - ray($project); return response()->json([ 'project_uuid' => $project->uuid ]); diff --git a/app/Http/Livewire/Project/Application/Source.php b/app/Http/Livewire/Project/Application/Source.php index bfb102f3e..4d7274cdb 100644 --- a/app/Http/Livewire/Project/Application/Source.php +++ b/app/Http/Livewire/Project/Application/Source.php @@ -3,19 +3,36 @@ namespace App\Http\Livewire\Project\Application; use App\Models\Application; +use App\Models\PrivateKey; use Livewire\Component; class Source extends Component { public $applicationId; public Application $application; - + public $private_keys; protected $rules = [ 'application.git_repository' => 'required', 'application.git_branch' => 'required', 'application.git_commit_sha' => 'nullable', ]; - + private function get_private_keys() + { + $this->private_keys = PrivateKey::whereTeamId(session('currentTeam')->id)->get()->reject(function ($key) { + return $key->id == $this->application->private_key_id; + }); + } + public function mount() + { + $this->get_private_keys(); + } + public function setPrivateKey(int $private_key_id) + { + $this->application->private_key_id = $private_key_id; + $this->application->save(); + $this->application->refresh(); + $this->get_private_keys(); + } public function submit() { $this->validate(); diff --git a/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php b/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php index 52baa8790..94c2ca5c2 100644 --- a/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php +++ b/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php @@ -3,6 +3,8 @@ namespace App\Http\Livewire\Project\New; use App\Models\Application; +use App\Models\GithubApp; +use App\Models\GitlabApp; use App\Models\PrivateKey; use App\Models\Project; use App\Models\StandaloneDocker; @@ -16,23 +18,40 @@ class GithubPrivateRepositoryDeployKey extends Component public $query; public $private_keys; public int $private_key_id; - public string $repository_url; public int $port = 3000; public string $type; public bool $is_static = false; public null|string $publish_directory = null; + + public string $repository_url; + private object $repository_url_parsed; + public string $branch; + + private GithubApp|GitlabApp $git_source; + private string $git_host; + private string $git_repository; + private string $git_branch; + protected $rules = [ 'repository_url' => 'required|url', + 'branch' => 'required|string', 'port' => 'required|numeric', 'is_static' => 'required|boolean', 'publish_directory' => 'nullable|string', ]; + protected $validationAttributes = [ + 'repository_url' => 'Repository', + 'branch' => 'Branch', + 'port' => 'Port', + 'is_static' => 'Is static', + 'publish_directory' => 'Publish directory', + ]; public function mount() { if (config('app.env') === 'local') { - $this->repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify'; + $this->repository_url = 'https://github.com/coollabsio/coolify-examples'; } $this->parameters = get_parameters(); $this->query = request()->query(); @@ -52,15 +71,29 @@ class GithubPrivateRepositoryDeployKey extends Component { $this->private_key_id = $private_key_id; } + 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->branch) { + $this->git_branch = $this->branch; + } else { + $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 + } + } public function submit() { + $this->validate(); 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) { @@ -71,13 +104,15 @@ class GithubPrivateRepositoryDeployKey extends Component } $destination_class = $destination->getMorphClass(); + $this->get_git_source(); + $project = Project::where('uuid', $this->parameters['project_uuid'])->first(); $environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first(); $application_init = [ 'name' => generate_random_name(), - 'git_repository' => $git_repository, - 'git_branch' => $git_branch, - 'git_full_url' => "git@$git_host:$git_repository.git", + 'git_repository' => $this->git_repository, + 'git_branch' => $this->git_branch, + 'git_full_url' => "git@$this->git_host:$this->git_repository.git", 'build_pack' => 'nixpacks', 'ports_exposes' => $this->port, 'publish_directory' => $this->publish_directory, @@ -85,6 +120,8 @@ class GithubPrivateRepositoryDeployKey extends Component '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() ]; $application = Application::create($application_init); $application->settings->is_static = $this->is_static; diff --git a/resources/views/components/applications/navbar.blade.php b/resources/views/components/applications/navbar.blade.php index 6a5fa2665..0ef8728f6 100644 --- a/resources/views/components/applications/navbar.blade.php +++ b/resources/views/components/applications/navbar.blade.php @@ -25,7 +25,7 @@
  • - + Git Repository
  • diff --git a/resources/views/components/git-icon.blade.php b/resources/views/components/git-icon.blade.php index 89158de29..46fbb7e72 100644 --- a/resources/views/components/git-icon.blade.php +++ b/resources/views/components/git-icon.blade.php @@ -15,4 +15,17 @@ +@else + + + + + + + + + + @endif diff --git a/resources/views/livewire/project/application/source.blade.php b/resources/views/livewire/project/application/source.blade.php index 18edc0120..0b2dd384f 100644 --- a/resources/views/livewire/project/application/source.blade.php +++ b/resources/views/livewire/project/application/source.blade.php @@ -1,11 +1,11 @@
    -
    +

    Source

    Save - + - Open Repository on Git + Open Repository on Git @@ -15,7 +15,7 @@ + @if ($application->private_key_id) +

    Current Deploy Key: {{ $application->private_key->name }}

    + +
    Select another Deploy Key
    +
    + @foreach ($private_keys as $key) + {{ $key->name }} + + @endforeach +
    + @endif
    diff --git a/resources/views/livewire/project/new/github-private-repository-deploy-key.blade.php b/resources/views/livewire/project/new/github-private-repository-deploy-key.blade.php index 4300c77c6..3a267be8a 100644 --- a/resources/views/livewire/project/new/github-private-repository-deploy-key.blade.php +++ b/resources/views/livewire/project/new/github-private-repository-deploy-key.blade.php @@ -1,7 +1,7 @@

    Create a new Application

    -
    Deploy any public or private git repositories through a Deploy Key.
    -

    Select a private key

    +
    Deploy any public or private GIT repositories through a Deploy Key.
    +

    Select a Private Key

    @foreach ($private_keys as $key) @if ($private_key_id == $key->id) @endif @endforeach + + + + @isset($private_key_id) -
    -
    - - @if ($is_static) - - @else - - @endif - -
    + + + + + @if ($is_static) + + @else + + @endif - Add New Application + Save New Application @endisset