From d77d32853f7f57f855da0770e12dce55ead2f967 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 8 May 2023 13:36:49 +0200 Subject: [PATCH] save things --- app/Http/Livewire/PrivateKey/Change.php | 2 +- .../Livewire/Project/Application/Deploy.php | 2 +- .../Application/EnvironmentVariable/Add.php | 2 +- .../Application/EnvironmentVariable/Show.php | 2 +- .../Project/Application/Storages/Add.php | 2 +- .../Project/New/GithubPrivateRepository.php | 4 +- .../Project/New/PublicGitRepository.php | 2 +- app/Http/Livewire/Server/Form.php | 1 + app/Http/Livewire/Server/PrivateKey.php | 2 +- app/Http/Livewire/Source/Create.php | 48 +++++++++ app/Http/Livewire/Source/Github/Change.php | 99 +++++++++++++++++++ app/Models/EnvironmentVariable.php | 1 - app/Models/GithubApp.php | 1 + bootstrap/helpers.php | 8 +- ..._03_28_083723_create_github_apps_table.php | 2 +- resources/views/components/layout.blade.php | 2 +- resources/views/dashboard.blade.php | 8 ++ .../views/livewire/source/create.blade.php | 14 +++ .../livewire/source/github/change.blade.php | 30 ++++++ resources/views/source/github/show.blade.php | 4 + resources/views/source/new.blade.php | 4 + routes/web.php | 18 +++- 22 files changed, 240 insertions(+), 18 deletions(-) create mode 100644 app/Http/Livewire/Source/Create.php create mode 100644 app/Http/Livewire/Source/Github/Change.php create mode 100644 resources/views/livewire/source/create.blade.php create mode 100644 resources/views/livewire/source/github/change.blade.php create mode 100644 resources/views/source/github/show.blade.php create mode 100644 resources/views/source/new.blade.php diff --git a/app/Http/Livewire/PrivateKey/Change.php b/app/Http/Livewire/PrivateKey/Change.php index def6b40da..4f3382d51 100644 --- a/app/Http/Livewire/PrivateKey/Change.php +++ b/app/Http/Livewire/PrivateKey/Change.php @@ -35,7 +35,7 @@ class Change extends Component $this->private_key->save(); session('currentTeam')->privateKeys = PrivateKey::where('team_id', session('currentTeam')->id)->get(); } catch (\Exception $e) { - $this->addError('private_key_value', $e->getMessage()); + return generalErrorHandlerLivewire($e, $this); } } } diff --git a/app/Http/Livewire/Project/Application/Deploy.php b/app/Http/Livewire/Project/Application/Deploy.php index fc64dc3ce..66191f83c 100644 --- a/app/Http/Livewire/Project/Application/Deploy.php +++ b/app/Http/Livewire/Project/Application/Deploy.php @@ -23,7 +23,7 @@ class Deploy extends Component public function mount() { - $this->parameters = saveParameters(); + $this->parameters = getParameters(); $this->application = Application::where('id', $this->applicationId)->first(); $this->destination = $this->application->destination->getMorphClass()::where('id', $this->application->destination->id)->first(); } diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php b/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php index 1947da9dc..e169aa28a 100644 --- a/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php +++ b/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php @@ -23,7 +23,7 @@ class Add extends Component ]; public function mount() { - $this->parameters = saveParameters(); + $this->parameters = getParameters(); } public function submit() { diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php b/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php index e01caf139..67f2c1e77 100644 --- a/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php +++ b/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php @@ -20,7 +20,7 @@ class Show extends Component ]; public function mount() { - $this->parameters = saveParameters(); + $this->parameters = getParameters(); } public function submit() { diff --git a/app/Http/Livewire/Project/Application/Storages/Add.php b/app/Http/Livewire/Project/Application/Storages/Add.php index 4c03551b1..f0d66780c 100644 --- a/app/Http/Livewire/Project/Application/Storages/Add.php +++ b/app/Http/Livewire/Project/Application/Storages/Add.php @@ -23,7 +23,7 @@ class Add extends Component ]; public function mount() { - $this->parameters = saveParameters(); + $this->parameters = getParameters(); } public function submit() { diff --git a/app/Http/Livewire/Project/New/GithubPrivateRepository.php b/app/Http/Livewire/Project/New/GithubPrivateRepository.php index e7418e2c9..6b46119d3 100644 --- a/app/Http/Livewire/Project/New/GithubPrivateRepository.php +++ b/app/Http/Livewire/Project/New/GithubPrivateRepository.php @@ -130,12 +130,12 @@ class GithubPrivateRepository extends Component 'environment_name' => $environment->name ]); } catch (\Exception $e) { - $this->emit('error', $e->getMessage()); + return generalErrorHandlerLivewire($e, $this); } } public function mount() { - $this->parameters = saveParameters(); + $this->parameters = getParameters(); $this->repositories = $this->branches = $this->servers = $this->destinations = collect(); $this->github_apps = GithubApp::private(); } diff --git a/app/Http/Livewire/Project/New/PublicGitRepository.php b/app/Http/Livewire/Project/New/PublicGitRepository.php index da49c195d..6e2c1804d 100644 --- a/app/Http/Livewire/Project/New/PublicGitRepository.php +++ b/app/Http/Livewire/Project/New/PublicGitRepository.php @@ -42,7 +42,7 @@ class PublicGitRepository extends Component $this->public_repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify'; $this->port = 3000; } - $this->parameters = saveParameters(); + $this->parameters = getParameters(); $this->servers = session('currentTeam')->load(['servers'])->servers; } public function chooseServer($server) diff --git a/app/Http/Livewire/Server/Form.php b/app/Http/Livewire/Server/Form.php index 23094fd19..c0eba0503 100644 --- a/app/Http/Livewire/Server/Form.php +++ b/app/Http/Livewire/Server/Form.php @@ -54,6 +54,7 @@ class Form extends Component $this->dockerComposeVersion = 'Not installed.'; } } catch (\Exception $e) { + return generalErrorHandlerLivewire($e, $this); } } public function delete() diff --git a/app/Http/Livewire/Server/PrivateKey.php b/app/Http/Livewire/Server/PrivateKey.php index f70c3d502..dbc0474e8 100644 --- a/app/Http/Livewire/Server/PrivateKey.php +++ b/app/Http/Livewire/Server/PrivateKey.php @@ -20,7 +20,7 @@ class PrivateKey extends Component } public function mount() { - $this->parameters = saveParameters(); + $this->parameters = getParameters(); $this->private_keys = ModelsPrivateKey::where('team_id', session('currentTeam')->id)->get(); } } diff --git a/app/Http/Livewire/Source/Create.php b/app/Http/Livewire/Source/Create.php new file mode 100644 index 000000000..a36c63301 --- /dev/null +++ b/app/Http/Livewire/Source/Create.php @@ -0,0 +1,48 @@ +name = generateRandomName(); + } + public function createGitHubApp() + { + try { + $this->validate([ + "name" => 'required|string', + "organization" => 'nullable|string', + "api_url" => 'required|string', + "html_url" => 'required|string', + "custom_user" => 'required|string', + "custom_port" => 'required|int', + "is_system_wide" => 'required|bool', + ]); + GithubApp::create([ + 'name' => $this->name, + 'organization' => $this->organization, + 'api_url' => $this->api_url, + 'html_url' => $this->html_url, + 'custom_user' => $this->custom_user, + 'custom_port' => $this->custom_port, + 'is_system_wide' => $this->is_system_wide, + 'team_id' => session('currentTeam')->id, + ]); + } catch (\Exception $e) { + return generalErrorHandlerLivewire($e, $this); + } + } +} diff --git a/app/Http/Livewire/Source/Github/Change.php b/app/Http/Livewire/Source/Github/Change.php new file mode 100644 index 000000000..33b0585c2 --- /dev/null +++ b/app/Http/Livewire/Source/Github/Change.php @@ -0,0 +1,99 @@ + 'required|string', + 'github_app.organization' => 'nullable|string', + 'github_app.api_url' => 'required|string', + 'github_app.html_url' => 'required|string', + 'github_app.custom_user' => 'required|string', + 'github_app.custom_port' => 'required|int', + 'github_app.app_id' => 'required|int', + 'github_app.installation_id' => 'required|int', + 'github_app.client_id' => 'required|string', + 'github_app.client_secret' => 'required|string', + 'github_app.webhook_secret' => 'required|string', + 'github_app.is_system_wide' => 'required|bool', + ]; + public function submit() + { + try { + $this->validate(); + $this->github_app->save(); + } catch (\Exception $e) { + return generalErrorHandlerLivewire($e, $this); + } + } + public function instantSave() + { + try { + $this->github_app->is_system_wide = $this->is_system_wide; + $this->github_app->save(); + } catch (\Exception $e) { + return generalErrorHandlerLivewire($e, $this); + } + } + public function mount() + { + $this->parameters = getParameters(); + $this->github_app = GithubApp::where('uuid', $this->parameters['github_app_uuid'])->first(); + $this->is_system_wide = $this->github_app->is_system_wide; + } + public function createGithubApp() + { + $settings = InstanceSettings::first(); + $fqdn = $settings->fqdn; + if (!$fqdn) { + $fqdn = $this->host; + } + if ($this->github_app->organization) { + $url = 'organizations/' . $this->github_app->organization . '/settings/apps/new'; + } else { + $url = 'settings/apps/new'; + } + $name = Str::kebab('coolify' . $this->github_app->name); + $data = [ + "name" => $name, + "url" => $fqdn, + "hook_attributes" => [ + "url" => "$fqdn/webhooks/github/events" + ], + "redirect_url" => "$fqdn/webhooks/github", + "callback_url" => [ + "$fqdn/login/github/app", + ], + "public" => false, + "request_oauth_on_install" => false, + "setup_url" => "$fqdn/webhooks/github/install?source_id=" . $this->github_app->uuid, + "setup_on_update" => true, + "default_permissions" => [ + "contents" => 'read', + "metadata" => 'read', + "pull_requests" => 'read', + "emails" => 'read' + ], + "default_events" => ['pull_request', 'push'] + ]; + $response = Http::asForm()->post("{$this->github_app->html_url}/{$url}?state={$this->github_app->uuid}", [ + 'id' => 'manifest', + 'name' => 'manifest', + 'data' => json_encode($data), + ]); + dd($response); + } +} diff --git a/app/Models/EnvironmentVariable.php b/app/Models/EnvironmentVariable.php index 2bb4f211b..807cadb34 100644 --- a/app/Models/EnvironmentVariable.php +++ b/app/Models/EnvironmentVariable.php @@ -8,7 +8,6 @@ use Illuminate\Support\Str; class EnvironmentVariable extends Model { - protected $fillable = ['key', 'value', 'is_build_time', 'application_id']; protected $casts = [ "key" => 'string', diff --git a/app/Models/GithubApp.php b/app/Models/GithubApp.php index b7a624b5b..918beb53d 100644 --- a/app/Models/GithubApp.php +++ b/app/Models/GithubApp.php @@ -4,6 +4,7 @@ namespace App\Models; class GithubApp extends BaseModel { + protected $fillable = ['name', 'organization', 'api_url', 'html_url', 'custom_user', 'custom_port', 'team_id']; protected $casts = [ 'is_public' => 'boolean', ]; diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index 71505312a..0cc76a5c4 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -20,8 +20,10 @@ if (!function_exists('generalErrorHandlerLivewire')) { if ($e instanceof QueryException) { if ($e->errorInfo[0] === '23505') { $that->emit('error', 'Duplicate entry found.'); - } else { + } else if (count($e->errorInfo) === 4) { $that->emit('error', $e->errorInfo[3]); + } else { + $that->emit('error', $e->errorInfo[2]); } } else { $that->emit('error', $e); @@ -207,8 +209,8 @@ if (!function_exists('generate_github_token')) { return $token->json()['token']; } } -if (!function_exists('saveParameters')) { - function saveParameters() +if (!function_exists('getParameters')) { + function getParameters() { return Route::current()->parameters(); } diff --git a/database/migrations/2023_03_28_083723_create_github_apps_table.php b/database/migrations/2023_03_28_083723_create_github_apps_table.php index 5273f9306..27f193912 100644 --- a/database/migrations/2023_03_28_083723_create_github_apps_table.php +++ b/database/migrations/2023_03_28_083723_create_github_apps_table.php @@ -19,8 +19,8 @@ return new class extends Migration $table->string('organization')->nullable(); $table->string('api_url'); $table->string('html_url'); - $table->integer('custom_port')->default(22); $table->string('custom_user')->default('git'); + $table->integer('custom_port')->default(22); $table->integer('app_id')->nullable(); $table->integer('installation_id')->nullable(); diff --git a/resources/views/components/layout.blade.php b/resources/views/components/layout.blade.php index 9d6ba6fd8..8e6787fbe 100644 --- a/resources/views/components/layout.blade.php +++ b/resources/views/components/layout.blade.php @@ -76,7 +76,7 @@ window.location.reload(); }) Livewire.on('error', (message) => { - alert(message); + console.log(message); }) @endauth diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 7c3f11332..5bba94d54 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -31,4 +31,12 @@ @empty

No servers found.

@endforelse +

GitHub Apps + New +

+ @forelse ($github_apps as $github_app) + {{ data_get($github_app, 'name') }} + @empty +

No servers found.

+ @endforelse diff --git a/resources/views/livewire/source/create.blade.php b/resources/views/livewire/source/create.blade.php new file mode 100644 index 000000000..b704cc995 --- /dev/null +++ b/resources/views/livewire/source/create.blade.php @@ -0,0 +1,14 @@ +
+
+ + + + + + + + + Submit + + +
diff --git a/resources/views/livewire/source/github/change.blade.php b/resources/views/livewire/source/github/change.blade.php new file mode 100644 index 000000000..8ef701c36 --- /dev/null +++ b/resources/views/livewire/source/github/change.blade.php @@ -0,0 +1,30 @@ +
+

Change Github App

+
+ + + @if ($github_app->app_id) + + @else + + @endif + + + + + @if ($github_app->app_id) + + + + + + Save + @else +
+ Save + Create GitHub Application +
+ @endif + +
diff --git a/resources/views/source/github/show.blade.php b/resources/views/source/github/show.blade.php new file mode 100644 index 000000000..0af07010f --- /dev/null +++ b/resources/views/source/github/show.blade.php @@ -0,0 +1,4 @@ + +

GitHub App

+ +
diff --git a/resources/views/source/new.blade.php b/resources/views/source/new.blade.php new file mode 100644 index 000000000..b75470a16 --- /dev/null +++ b/resources/views/source/new.blade.php @@ -0,0 +1,4 @@ + +

New Git App

+ +
diff --git a/routes/web.php b/routes/web.php index 474786aab..6909cd74d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -8,7 +8,10 @@ use App\Models\PrivateKey; use App\Models\StandaloneDocker; use App\Models\SwarmDocker; use App\Http\Controllers\ServerController; +use App\Models\GithubApp; +use App\Models\Project; use App\Models\Server; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; /* @@ -26,18 +29,21 @@ use Illuminate\Support\Facades\Route; Route::middleware(['auth'])->group(function () { Route::get('/', function () { - $projects = session('currentTeam')->load(['projects'])->projects; - $servers = session('currentTeam')->load(['servers'])->servers; + $id = session('currentTeam')->id; + $projects = Project::where('team_id', $id)->get(); + $servers = Server::where('team_id', $id)->get(); $destinations = $servers->map(function ($server) { return $server->standaloneDockers->merge($server->swarmDockers); })->flatten(); - $private_keys = session('currentTeam')->load(['privateKeys'])->privateKeys; + $private_keys = PrivateKey::where('team_id', $id)->get(); + $github_apps = GithubApp::private(); return view('dashboard', [ 'servers' => $servers->sortBy('name'), 'projects' => $projects->sortBy('name'), 'destinations' => $destinations->sortBy('name'), 'private_keys' => $private_keys->sortBy('name'), + 'github_apps' => $github_apps->sortBy('name'), ]); })->name('dashboard'); @@ -75,6 +81,12 @@ Route::middleware(['auth'])->group(function () { ]); })->name('private-key.show'); }); +Route::middleware(['auth'])->group(function () { + Route::get('/source/new', fn () => view('source.new'))->name('source.new'); + Route::get('/source/github/{github_app_uuid}', function (Request $request) { + return view('source.github.show', ['host' => $request->schemeAndHttpHost()]); + })->name('source.github.show'); +}); Route::middleware(['auth'])->group(function () { Route::get('/server/new', fn () => view('server.new'))->name('server.new'); Route::get('/server/{server_uuid}', function () {