This commit is contained in:
Andras Bacsai
2023-06-12 14:38:32 +02:00
parent 7ef4d8dccb
commit d96cae2fff
7 changed files with 112 additions and 32 deletions

View File

@@ -39,7 +39,6 @@ class MagicController extends Controller
['name' => request()->query('name') ?? generate_random_name()], ['name' => request()->query('name') ?? generate_random_name()],
['team_id' => session('currentTeam')->id] ['team_id' => session('currentTeam')->id]
); );
ray($project);
return response()->json([ return response()->json([
'project_uuid' => $project->uuid 'project_uuid' => $project->uuid
]); ]);

View File

@@ -3,19 +3,36 @@
namespace App\Http\Livewire\Project\Application; namespace App\Http\Livewire\Project\Application;
use App\Models\Application; use App\Models\Application;
use App\Models\PrivateKey;
use Livewire\Component; use Livewire\Component;
class Source extends Component class Source extends Component
{ {
public $applicationId; public $applicationId;
public Application $application; public Application $application;
public $private_keys;
protected $rules = [ protected $rules = [
'application.git_repository' => 'required', 'application.git_repository' => 'required',
'application.git_branch' => 'required', 'application.git_branch' => 'required',
'application.git_commit_sha' => 'nullable', '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() public function submit()
{ {
$this->validate(); $this->validate();

View File

@@ -3,6 +3,8 @@
namespace App\Http\Livewire\Project\New; namespace App\Http\Livewire\Project\New;
use App\Models\Application; use App\Models\Application;
use App\Models\GithubApp;
use App\Models\GitlabApp;
use App\Models\PrivateKey; use App\Models\PrivateKey;
use App\Models\Project; use App\Models\Project;
use App\Models\StandaloneDocker; use App\Models\StandaloneDocker;
@@ -16,23 +18,40 @@ class GithubPrivateRepositoryDeployKey extends Component
public $query; public $query;
public $private_keys; public $private_keys;
public int $private_key_id; public int $private_key_id;
public string $repository_url;
public int $port = 3000; public int $port = 3000;
public string $type; public string $type;
public bool $is_static = false; public bool $is_static = false;
public null|string $publish_directory = null; 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 = [ protected $rules = [
'repository_url' => 'required|url', 'repository_url' => 'required|url',
'branch' => 'required|string',
'port' => 'required|numeric', 'port' => 'required|numeric',
'is_static' => 'required|boolean', 'is_static' => 'required|boolean',
'publish_directory' => 'nullable|string', 'publish_directory' => 'nullable|string',
]; ];
protected $validationAttributes = [
'repository_url' => 'Repository',
'branch' => 'Branch',
'port' => 'Port',
'is_static' => 'Is static',
'publish_directory' => 'Publish directory',
];
public function mount() public function mount()
{ {
if (config('app.env') === 'local') { 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->parameters = get_parameters();
$this->query = request()->query(); $this->query = request()->query();
@@ -52,15 +71,29 @@ class GithubPrivateRepositoryDeployKey extends Component
{ {
$this->private_key_id = $private_key_id; $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() public function submit()
{ {
$this->validate();
try { 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_uuid = $this->query['destination'];
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first(); $destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
if (!$destination) { if (!$destination) {
@@ -71,13 +104,15 @@ class GithubPrivateRepositoryDeployKey extends Component
} }
$destination_class = $destination->getMorphClass(); $destination_class = $destination->getMorphClass();
$this->get_git_source();
$project = Project::where('uuid', $this->parameters['project_uuid'])->first(); $project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first(); $environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
$application_init = [ $application_init = [
'name' => generate_random_name(), 'name' => generate_random_name(),
'git_repository' => $git_repository, 'git_repository' => $this->git_repository,
'git_branch' => $git_branch, 'git_branch' => $this->git_branch,
'git_full_url' => "git@$git_host:$git_repository.git", 'git_full_url' => "git@$this->git_host:$this->git_repository.git",
'build_pack' => 'nixpacks', 'build_pack' => 'nixpacks',
'ports_exposes' => $this->port, 'ports_exposes' => $this->port,
'publish_directory' => $this->publish_directory, 'publish_directory' => $this->publish_directory,
@@ -85,6 +120,8 @@ class GithubPrivateRepositoryDeployKey extends Component
'destination_id' => $destination->id, 'destination_id' => $destination->id,
'destination_type' => $destination_class, 'destination_type' => $destination_class,
'private_key_id' => $this->private_key_id, '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 = Application::create($application_init);
$application->settings->is_static = $this->is_static; $application->settings->is_static = $this->is_static;

View File

@@ -25,7 +25,7 @@
<li> <li>
<a target="_blank" class="text-xs text-white rounded-none hover:no-underline hover:bg-coollabs" <a target="_blank" class="text-xs text-white rounded-none hover:no-underline hover:bg-coollabs"
href="{{ $application->gitBranchLocation }}"> href="{{ $application->gitBranchLocation }}">
<x-git-icon git="{{ $application->source->getMorphClass() }}" /> <x-git-icon git="{{ $application->source?->getMorphClass() }}" />
Git Repository Git Repository
</a> </a>
</li> </li>

View File

@@ -15,4 +15,17 @@
<path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path d="M21 14l-9 7l-9 -7l3 -11l3 7h6l3 -7z" /> <path d="M21 14l-9 7l-9 -7l3 -11l3 7h6l3 -7z" />
</svg> </svg>
@else
<svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path d="M16 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M12 8m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M12 16m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M12 15v-6" />
<path d="M15 11l-2 -2" />
<path d="M11 7l-1.9 -1.9" />
<path
d="M13.446 2.6l7.955 7.954a2.045 2.045 0 0 1 0 2.892l-7.955 7.955a2.045 2.045 0 0 1 -2.892 0l-7.955 -7.955a2.045 2.045 0 0 1 0 -2.892l7.955 -7.955a2.045 2.045 0 0 1 2.892 0z" />
</svg>
@endif @endif

View File

@@ -1,11 +1,11 @@
<div> <div>
<form wire:submit.prevent='submit' class="flex flex-col w-96"> <form wire:submit.prevent='submit' class="flex flex-col">
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h2>Source</h2> <h2>Source</h2>
<x-forms.button type="submit">Save</x-forms.button> <x-forms.button type="submit">Save</x-forms.button>
<a target="_blank" class="hover:no-underline" href="{{ $application->gitBranchLocation }}"> <a target="_blank" class="hover:no-underline" href="{{ $application?->gitBranchLocation }}">
<x-forms.button> <x-forms.button>
<x-git-icon git="{{ $application->source->getMorphClass() }}" />Open Repository on Git <x-git-icon git="{{ $application->source?->getMorphClass() }}" />Open Repository on Git
<x-external-link /> <x-external-link />
</x-forms.button> </x-forms.button>
</a> </a>
@@ -15,7 +15,7 @@
<x-forms.input placeholder="main" id="application.git_branch" label="Branch" /> <x-forms.input placeholder="main" id="application.git_branch" label="Branch" />
<div class="flex items-end gap-2 w-96"> <div class="flex items-end gap-2 w-96">
<x-forms.input placeholder="HEAD" id="application.git_commit_sha" placeholder="HEAD" label="Commit SHA" /> <x-forms.input placeholder="HEAD" id="application.git_commit_sha" placeholder="HEAD" label="Commit SHA" />
<a target="_blank" class="flex hover:no-underline" href="{{ $application->gitCommits }}"> <a target="_blank" class="flex hover:no-underline" href="{{ $application?->gitCommits }}">
<x-forms.button><svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" viewBox="0 0 24 24" <x-forms.button><svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" viewBox="0 0 24 24"
stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round"> stroke-linejoin="round">
@@ -28,5 +28,17 @@
</x-forms.button> </x-forms.button>
</a> </a>
</div> </div>
@if ($application->private_key_id)
<h4 class="py-2 pt-4">Current Deploy Key: <span
class="text-warning">{{ $application->private_key->name }}</span></h4>
<div class="py-2 text-sm">Select another Deploy Key</div>
<div class="flex gap-2">
@foreach ($private_keys as $key)
<x-forms.button wire:click.defer="setPrivateKey('{{ $key->id }}')">{{ $key->name }}
</x-forms.button>
@endforeach
</div>
@endif
</form> </form>
</div> </div>

View File

@@ -1,7 +1,7 @@
<div> <div>
<h1>Create a new Application</h1> <h1>Create a new Application</h1>
<div class="pb-4 text-sm">Deploy any public or private git repositories through a Deploy Key.</div> <div class="pt-2 pb-10 text-sm">Deploy any public or private GIT repositories through a Deploy Key.</div>
<h3 class="py-2">Select a private key</h3> <h3 class="py-2">Select a Private Key</h3>
@foreach ($private_keys as $key) @foreach ($private_keys as $key)
@if ($private_key_id == $key->id) @if ($private_key_id == $key->id)
<x-forms.button class="bg-coollabs hover:bg-coollabs-100" <x-forms.button class="bg-coollabs hover:bg-coollabs-100"
@@ -12,19 +12,21 @@
</x-forms.button> </x-forms.button>
@endif @endif
@endforeach @endforeach
<a href="{{ route('private-key.new') }}">
<x-forms.button isHighlighted>+</x-forms.button>
</a>
@isset($private_key_id) @isset($private_key_id)
<form class="pt-6" wire:submit.prevent='submit'> <form class="flex flex-col gap-2" wire:submit.prevent='submit'>
<div class="flex items-end gap-2 pb-2"> <x-forms.input id="repository_url" label="Repository URL" helper="{!! __('repository.url') !!}" />
<x-forms.input class="w-96" id="repository_url" label="Repository URL" /> <x-forms.input id="branch" label="Branch" />
@if ($is_static) <x-forms.checkbox instantSave id="is_static" label="Static Site?" />
<x-forms.input id="publish_directory" label="Publish Directory" /> @if ($is_static)
@else <x-forms.input id="publish_directory" label="Publish Directory" />
<x-forms.input type="number" id="port" label="Port" :readonly="$is_static" /> @else
@endif <x-forms.input type="number" id="port" label="Port" :readonly="$is_static" />
<x-forms.checkbox instantSave id="is_static" label="Static Site?" /> @endif
</div>
<x-forms.button type="submit"> <x-forms.button type="submit">
Add New Application Save New Application
</x-forms.button> </x-forms.button>
</form> </form>
@endisset @endisset