From f7ebc8a88cddf748bb274bfb23383bface4ff7b0 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 20 Feb 2024 18:14:47 +0100 Subject: [PATCH] feat: save github app permission locally --- app/Console/Kernel.php | 1 - app/Jobs/GithubAppPermissionJob.php | 59 ++++++++++++++ app/Livewire/Source/Github/Change.php | 20 ++++- ...0_165045_add_permissions_to_github_app.php | 34 ++++++++ .../livewire/source/github/change.blade.php | 77 +++++++++++-------- 5 files changed, 157 insertions(+), 34 deletions(-) create mode 100644 app/Jobs/GithubAppPermissionJob.php create mode 100644 database/migrations/2024_02_20_165045_add_permissions_to_github_app.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index bba2da818..76fcbfc1f 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -4,7 +4,6 @@ use App\Jobs\CheckLogDrainContainerJob; use App\Jobs\CleanupInstanceStuffsJob; -use App\Jobs\ComplexContainerStatusJob; use App\Jobs\DatabaseBackupJob; use App\Jobs\ScheduledTaskJob; use App\Jobs\InstanceAutoUpdateJob; diff --git a/app/Jobs/GithubAppPermissionJob.php b/app/Jobs/GithubAppPermissionJob.php new file mode 100644 index 000000000..deb414a13 --- /dev/null +++ b/app/Jobs/GithubAppPermissionJob.php @@ -0,0 +1,59 @@ +github_app->uuid))]; + } + + public function uniqueId(): int + { + return $this->github_app->uuid; + } + + public function handle() + { + try { + $github_access_token = generate_github_jwt_token($this->github_app); + $response = Http::withHeaders([ + 'Authorization' => "Bearer $github_access_token", + 'Accept' => 'application/vnd.github+json' + ])->get("{$this->github_app->api_url}/app"); + $response = $response->json(); + $permissions = data_get($response, 'permissions'); + $this->github_app->contents = data_get($permissions, 'contents'); + $this->github_app->metadata = data_get($permissions, 'metadata'); + $this->github_app->pull_requests = data_get($permissions, 'pull_requests'); + $this->github_app->administration = data_get($permissions, 'administration'); + $this->github_app->save(); + $this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret'); + } catch (\Throwable $e) { + send_internal_notification('GithubAppPermissionJob failed with: ' . $e->getMessage()); + ray($e->getMessage()); + throw $e; + } + } +} diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index cdbec6486..e4baa36d0 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -2,6 +2,7 @@ namespace App\Livewire\Source\Github; +use App\Jobs\GithubAppPermissionJob; use App\Models\GithubApp; use App\Models\InstanceSettings; use Illuminate\Support\Facades\Http; @@ -35,8 +36,18 @@ class Change extends Component 'github_app.client_secret' => 'required|string', 'github_app.webhook_secret' => 'required|string', 'github_app.is_system_wide' => 'required|bool', + 'github_app.contents' => 'nullable|string', + 'github_app.metadata' => 'nullable|string', + 'github_app.pull_requests' => 'nullable|string', + 'github_app.administration' => 'nullable|string', ]; + public function checkPermissions() + { + GithubAppPermissionJob::dispatchSync($this->github_app); + $this->github_app->refresh()->makeVisible('client_secret')->makeVisible('webhook_secret'); + $this->dispatch('success', 'Success', 'Github App permissions updated.'); + } // public function check() // { @@ -138,7 +149,7 @@ public function submit() 'github_app.is_system_wide' => 'required|bool', ]); $this->github_app->save(); - $this->dispatch('success', 'Github App updated successfully.'); + $this->dispatch('success', 'Success', 'Github App updated.'); } catch (\Throwable $e) { return handleError($e, $this); } @@ -146,6 +157,13 @@ public function submit() public function instantSave() { + try { + $this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret'); + $this->github_app->save(); + $this->dispatch('success', 'Success', 'Github App updated.'); + } catch (\Throwable $e) { + return handleError($e, $this); + } } public function delete() diff --git a/database/migrations/2024_02_20_165045_add_permissions_to_github_app.php b/database/migrations/2024_02_20_165045_add_permissions_to_github_app.php new file mode 100644 index 000000000..fedb7395f --- /dev/null +++ b/database/migrations/2024_02_20_165045_add_permissions_to_github_app.php @@ -0,0 +1,34 @@ +string('contents')->nullable(); + $table->string('metadata')->nullable(); + $table->string('pull_requests')->nullable(); + $table->string('administration')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('github_apps', function (Blueprint $table) { + $table->dropColumn('contents'); + $table->dropColumn('metadata'); + $table->dropColumn('pull_requests'); + $table->dropColumn('administration'); + }); + } +}; diff --git a/resources/views/livewire/source/github/change.blade.php b/resources/views/livewire/source/github/change.blade.php index 769c9cab9..171111794 100644 --- a/resources/views/livewire/source/github/change.blade.php +++ b/resources/views/livewire/source/github/change.blade.php @@ -16,7 +16,6 @@ This source will be deleted. It is not reversible.
Please think again.
- {{-- Check --}}
Your Private GitHub App for private repositories.
@@ -33,39 +32,53 @@ Install Repositories on GitHub @else - @if (!isCloud()) -
- +
+
+ +
- @endif -
- - -
-
- - -
-
- @if ($github_app->html_url === 'https://github.com') - - - @else - - + @if (!isCloud()) +
+ +
@endif -
-
- - -
-
- - - +
+ + +
+
+ @if ($github_app->html_url === 'https://github.com') + + + @else + + + @endif +
+
+ + +
+
+ + + +
+
+

Permissions

+ Check Permissions +
+
+ + + + +
@endif