magic search bar

This commit is contained in:
Andras Bacsai
2023-05-11 15:20:02 +02:00
parent 8e1c6d2bd2
commit 70d032ff23
17 changed files with 686 additions and 481 deletions

View File

@@ -6,6 +6,8 @@ use App\Models\Application;
use App\Models\GithubApp;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
@@ -16,6 +18,7 @@ class GithubPrivateRepository extends Component
public $github_apps;
public GithubApp $github_app;
public $parameters;
public $query;
public $type;
public int $selected_repository_id;
@@ -24,16 +27,10 @@ class GithubPrivateRepository extends Component
public string $selected_branch_name = 'main';
public int $selected_server_id;
public int $selected_destination_id;
public string $selected_destination_class;
public string $token;
protected int $page = 1;
public $servers;
public $destinations;
public $repositories;
public int $total_repositories_count = 0;
@@ -42,7 +39,6 @@ class GithubPrivateRepository extends Component
protected function loadRepositoryByPage()
{
Log::info('Loading page ' . $this->page);
$response = Http::withToken($this->token)->get("{$this->github_app->api_url}/installation/repositories?per_page=100&page={$this->page}");
$json = $response->json();
if ($response->status() !== 200) {
@@ -96,55 +92,37 @@ class GithubPrivateRepository extends Component
}
}
}
public function loadServers()
{
try {
$this->servers = Server::validated();
$this->selected_server_id = $this->servers[0]['id'];
} catch (\Exception $e) {
return generalErrorHandler($e);
}
}
public function loadDestinations()
{
try {
$server = $this->servers->where('id', $this->selected_server_id)->first();
if ($server->standaloneDockers->count() === 0 && $server->swarmDockers->count() === 0) {
$this->destinations = 0;
}
$this->destinations = $server->standaloneDockers->merge($server->swarmDockers);
$this->selected_destination_id = $this->destinations[0]['id'];
$this->selected_destination_class = $this->destinations[0]->getMorphClass();
} catch (\Exception $e) {
return generalErrorHandler($e);
}
}
public function submit()
{
try {
if ($this->type === 'project') {
$project = Project::create([
'name' => generateRandomName(),
'team_id' => session('currentTeam')->id
]);
$environment = $project->load(['environments'])->environments->first();
} else {
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
$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();
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
$application = Application::create([
'name' => generateRandomName() . "-{$this->selected_repository_owner}/{$this->selected_repository_repo}:{$this->selected_branch_name}",
'project_id' => $this->selected_repository_id,
'repository_project_id' => $this->selected_repository_id,
'git_repository' => "{$this->selected_repository_owner}/{$this->selected_repository_repo}",
'git_branch' => $this->selected_branch_name,
'build_pack' => 'nixpacks',
'ports_exposes' => '3000',
'environment_id' => $environment->id,
'destination_id' => $this->selected_destination_id,
'destination_type' => $this->selected_destination_class,
'destination_id' => $destination->id,
'destination_type' => $destination_class,
'source_id' => $this->github_app->id,
'source_type' => GithubApp::class,
]);
redirect()->route('project.application.configuration', [
'application_uuid' => $application->uuid,
'project_uuid' => $project->uuid,
@@ -157,7 +135,8 @@ class GithubPrivateRepository extends Component
public function mount()
{
$this->parameters = getParameters();
$this->repositories = $this->branches = $this->servers = $this->destinations = collect();
$this->query = request()->query();
$this->repositories = $this->branches = collect();
$this->github_apps = GithubApp::private();
}
}

View File

@@ -13,16 +13,11 @@ use Spatie\Url\Url;
class GithubPrivateRepositoryDeployKey extends Component
{
public $parameters;
public $query;
public $private_keys;
public int $private_key_id;
public string $repository_url;
public $servers;
public $standalone_docker;
public $swarm_docker;
public $chosenServer;
public $chosenDestination;
public int $port = 3000;
public string $type;
@@ -40,20 +35,8 @@ class GithubPrivateRepositoryDeployKey extends Component
$this->repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';
}
$this->parameters = getParameters();
$this->query = request()->query();
$this->private_keys = PrivateKey::where('team_id', session('currentTeam')->id)->get();
$this->servers = session('currentTeam')->load(['servers'])->servers;
}
public function chooseServer($server)
{
$this->chosenServer = $server;
$this->standalone_docker = StandaloneDocker::where('server_id', $server['id'])->get();
$this->swarm_docker = SwarmDocker::where('server_id', $server['id'])->get();
}
public function setDestination($destination_uuid, $destination_type)
{
$class = "App\Models\\{$destination_type}";
$instance = new $class;
$this->chosenDestination = $instance::where('uuid', $destination_uuid)->first();
}
public function instantSave()
{
@@ -71,43 +54,49 @@ class GithubPrivateRepositoryDeployKey extends Component
}
public function submit()
{
$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';
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';
if ($this->type === 'project') {
$project = Project::create([
'name' => generateRandomName(),
'team_id' => session('currentTeam')->id,
$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();
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
$application_init = [
'name' => generateRandomName() . "-{$git_repository}:{$git_branch}",
'git_repository' => $git_repository,
'git_branch' => $git_branch,
'git_full_url' => "git@$git_host:$git_repository.git",
'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,
];
$application = Application::create($application_init);
$application->settings->is_static = $this->is_static;
$application->settings->save();
return redirect()->route('project.application.configuration', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'application_uuid' => $application->uuid,
]);
$environment = $project->environments->first();
} else {
$project = Project::where('uuid', $this->parameters['project_uuid'])->firstOrFail();
$environment = $project->environments->where('name', $this->parameters['environment_name'])->firstOrFail();
} catch (\Exception $e) {
return generalErrorHandler($e, $this);
}
$application_init = [
'name' => generateRandomName() . "-{$git_repository}:{$git_branch}",
'git_repository' => $git_repository,
'git_branch' => $git_branch,
'git_full_url' => "git@$git_host:$git_repository.git",
'build_pack' => 'nixpacks',
'ports_exposes' => $this->port,
'publish_directory' => $this->publish_directory,
'environment_id' => $environment->id,
'destination_id' => $this->chosenDestination->id,
'destination_type' => $this->chosenDestination->getMorphClass(),
'private_key_id' => $this->private_key_id,
];
$application = Application::create($application_init);
$application->settings->is_static = $this->is_static;
$application->settings->save();
return redirect()->route('project.application.configuration', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'application_uuid' => $application->uuid,
]);
}
}

View File

@@ -8,7 +8,6 @@ use App\Models\GitlabApp;
use App\Models\Project;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Illuminate\Support\Facades\Route;
use Livewire\Component;
use Spatie\Url\Url;
@@ -18,12 +17,8 @@ class PublicGitRepository extends Component
public int $port = 3000;
public string $type;
public $parameters;
public $query;
public $servers;
public $standalone_docker;
public $swarm_docker;
public $chosenServer;
public $chosenDestination;
public $github_apps;
public $gitlab_apps;
@@ -43,19 +38,7 @@ class PublicGitRepository extends Component
$this->port = 3000;
}
$this->parameters = getParameters();
$this->servers = session('currentTeam')->load(['servers'])->servers;
}
public function chooseServer($server)
{
$this->chosenServer = $server;
$this->standalone_docker = StandaloneDocker::where('server_id', $server['id'])->get();
$this->swarm_docker = SwarmDocker::where('server_id', $server['id'])->get();
}
public function setDestination($destination_uuid, $destination_type)
{
$class = "App\Models\\{$destination_type}";
$instance = new $class;
$this->chosenDestination = $instance::where('uuid', $destination_uuid)->first();
$this->query = request()->query();
}
public function instantSave()
@@ -72,49 +55,58 @@ class PublicGitRepository extends Component
public function submit()
{
$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';
try {
$this->validate();
if ($this->type === 'project') {
$project = Project::create([
'name' => generateRandomName(),
'team_id' => session('currentTeam')->id,
$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) {
$destination = SwarmDocker::where('uuid', $destination_uuid)->first();
}
if (!$destination) {
throw new \Exception('Destination not found. What?!');
}
$destination_class = $destination->getMorphClass();
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
$application_init = [
'name' => generateRandomName() . "-{$git_repository}:{$git_branch}",
'git_repository' => $git_repository,
'git_branch' => $git_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,
];
if ($git_host == 'github.com') {
$application_init['source_id'] = GithubApp::where('name', 'Public GitHub')->first()->id;
$application_init['source_type'] = GithubApp::class;
} elseif ($git_host == 'gitlab.com') {
$application_init['source_id'] = GitlabApp::where('name', 'Public GitLab')->first()->id;
$application_init['source_type'] = GitlabApp::class;
} elseif ($git_host == 'bitbucket.org') {
}
$application = Application::create($application_init);
$application->settings->is_static = $this->is_static;
$application->settings->save();
return redirect()->route('project.application.configuration', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'application_uuid' => $application->uuid,
]);
$environment = $project->environments->first();
} else {
$project = Project::where('uuid', $this->parameters['project_uuid'])->firstOrFail();
$environment = $project->environments->where('name', $this->parameters['environment_name'])->firstOrFail();
} catch (\Exception $e) {
return generalErrorHandler($e);
}
$application_init = [
'name' => generateRandomName() . "-{$git_repository}:{$git_branch}",
'git_repository' => $git_repository,
'git_branch' => $git_branch,
'build_pack' => 'nixpacks',
'ports_exposes' => $this->port,
'publish_directory' => $this->publish_directory,
'environment_id' => $environment->id,
'destination_id' => $this->chosenDestination->id,
'destination_type' => $this->chosenDestination->getMorphClass(),
];
if ($git_host == 'github.com') {
$application_init['source_id'] = GithubApp::where('name', 'Public GitHub')->first()->id;
$application_init['source_type'] = GithubApp::class;
} elseif ($git_host == 'gitlab.com') {
$application_init['source_id'] = GitlabApp::where('name', 'Public GitLab')->first()->id;
$application_init['source_type'] = GitlabApp::class;
} elseif ($git_host == 'bitbucket.org') {
}
$application = Application::create($application_init);
$application->settings->is_static = $this->is_static;
$application->settings->save();
return redirect()->route('project.application.configuration', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'application_uuid' => $application->uuid,
]);
}
}