From c74728162ede3aa137affd457bdc606d0d69be77 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:41:56 +0100 Subject: [PATCH 01/10] wip: test rename GitHub app --- app/Livewire/Source/Github/Change.php | 5 +++++ .../views/livewire/source/github/change.blade.php | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 07cef54f9..79cc02219 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -142,6 +142,11 @@ class Change extends Component } } + public function getUpdatePath() + { + return "{$this->github_app->html_url}/settings/apps/{$this->github_app->app_id}"; + } + public function submit() { try { diff --git a/resources/views/livewire/source/github/change.blade.php b/resources/views/livewire/source/github/change.blade.php index 5e576fa85..215c54411 100644 --- a/resources/views/livewire/source/github/change.blade.php +++ b/resources/views/livewire/source/github/change.blade.php @@ -58,7 +58,15 @@ @else
- +
From f38196c4213e1358d89c35c66f4c3b4f8ba6e258 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:54:20 +0100 Subject: [PATCH 02/10] fix: URL and sync new app name --- app/Livewire/Source/Github/Change.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 79cc02219..5d57ecf1e 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -4,6 +4,7 @@ namespace App\Livewire\Source\Github; use App\Jobs\GithubAppPermissionJob; use App\Models\GithubApp; +use Illuminate\Support\Facades\Http; use Livewire\Component; class Change extends Component @@ -144,7 +145,29 @@ class Change extends Component public function getUpdatePath() { - return "{$this->github_app->html_url}/settings/apps/{$this->github_app->app_id}"; + return "{$this->github_app->html_url}/settings/apps/{$this->github_app->name}"; + } + + public function syncGithubAppName() + { + try { + $github_access_token = generate_github_installation_token($this->github_app); + + $response = Http::withToken($github_access_token) + ->get("{$this->github_app->api_url}/app"); + + if ($response->successful()) { + $app_data = $response->json(); + if ($app_data['name'] !== $this->github_app->name) { + $this->github_app->name = $app_data['name']; + $this->github_app->save(); + $this->name = str($this->github_app->name)->kebab(); + $this->dispatch('success', 'Github App name synchronized.'); + } + } + } catch (\Throwable $e) { + return handleError($e, $this); + } } public function submit() From 737e81aa38f854b860047e3782912e33cab244a3 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:12:58 +0100 Subject: [PATCH 03/10] wip button to sync new app name --- app/Livewire/Source/Github/Change.php | 20 ++++++++++++++----- .../livewire/source/github/change.blade.php | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 5d57ecf1e..55ced1e3e 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -154,16 +154,26 @@ class Change extends Component $github_access_token = generate_github_installation_token($this->github_app); $response = Http::withToken($github_access_token) - ->get("{$this->github_app->api_url}/app"); + ->withHeaders([ + 'Accept' => 'application/vnd.github+json', + 'X-GitHub-Api-Version' => '2022-11-28', + ]) + ->get("{$this->github_app->api_url}/installation/repositories"); if ($response->successful()) { $app_data = $response->json(); - if ($app_data['name'] !== $this->github_app->name) { - $this->github_app->name = $app_data['name']; + $app_name = data_get($app_data, 'installation.app_slug'); + + if ($app_name && $app_name !== $this->github_app->name) { + $this->github_app->name = $app_name; + $this->name = str($app_name)->kebab(); $this->github_app->save(); - $this->name = str($this->github_app->name)->kebab(); - $this->dispatch('success', 'Github App name synchronized.'); + $this->dispatch('success', 'Github App name synchronized successfully.'); + } else { + $this->dispatch('info', 'Github App name is already up to date.'); } + } else { + $this->dispatch('error', 'Failed to fetch Github App information. Status: '.$response->status()); } } catch (\Throwable $e) { return handleError($e, $this); diff --git a/resources/views/livewire/source/github/change.blade.php b/resources/views/livewire/source/github/change.blade.php index 215c54411..2d873b465 100644 --- a/resources/views/livewire/source/github/change.blade.php +++ b/resources/views/livewire/source/github/change.blade.php @@ -60,6 +60,7 @@
+ Sync Name Update From a2860971a6dc8b17072b18962552ff604c28d552 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:36:30 +0100 Subject: [PATCH 04/10] try jwt --- app/Livewire/Source/Github/Change.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 55ced1e3e..4c41cb9e4 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -151,18 +151,18 @@ class Change extends Component public function syncGithubAppName() { try { - $github_access_token = generate_github_installation_token($this->github_app); + $jwt = $this->github_app->generateJWT(); - $response = Http::withToken($github_access_token) + $response = Http::withToken($jwt) ->withHeaders([ 'Accept' => 'application/vnd.github+json', 'X-GitHub-Api-Version' => '2022-11-28', ]) - ->get("{$this->github_app->api_url}/installation/repositories"); + ->get('https://api.github.com/app'); if ($response->successful()) { $app_data = $response->json(); - $app_name = data_get($app_data, 'installation.app_slug'); + $app_name = $app_data['name'] ?? null; if ($app_name && $app_name !== $this->github_app->name) { $this->github_app->name = $app_name; @@ -170,7 +170,7 @@ class Change extends Component $this->github_app->save(); $this->dispatch('success', 'Github App name synchronized successfully.'); } else { - $this->dispatch('info', 'Github App name is already up to date.'); + $this->dispatch('info', 'If you changed the name in GitHub, please wait a few moments and try syncing again.'); } } else { $this->dispatch('error', 'Failed to fetch Github App information. Status: '.$response->status()); From 6d43bbc6b95c552bf3398517a4b79c9b06a48642 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:43:11 +0100 Subject: [PATCH 05/10] fix naming --- app/Livewire/Source/Github/Change.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 4c41cb9e4..4f76d7299 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -151,14 +151,14 @@ class Change extends Component public function syncGithubAppName() { try { - $jwt = $this->github_app->generateJWT(); + $github_access_token = generate_github_installation_token($this->github_app); - $response = Http::withToken($jwt) + $response = Http::withToken($github_access_token) ->withHeaders([ 'Accept' => 'application/vnd.github+json', 'X-GitHub-Api-Version' => '2022-11-28', ]) - ->get('https://api.github.com/app'); + ->get("{$this->github_app->api_url}/app"); if ($response->successful()) { $app_data = $response->json(); From 56f6bdf7a776b951cdad2920c4e3b07a196ada00 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 15:11:35 +0100 Subject: [PATCH 06/10] use private key to make a jwt --- app/Livewire/Source/Github/Change.php | 58 +++++++++++++++++++++------ 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 4f76d7299..4968a549d 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -4,7 +4,11 @@ namespace App\Livewire\Source\Github; use App\Jobs\GithubAppPermissionJob; use App\Models\GithubApp; +use App\Models\PrivateKey; use Illuminate\Support\Facades\Http; +use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\Rsa\Sha256; use Livewire\Component; class Change extends Component @@ -148,32 +152,60 @@ class Change extends Component return "{$this->github_app->html_url}/settings/apps/{$this->github_app->name}"; } + private function generateGithubJwt($private_key, $app_id): string + { + $configuration = Configuration::forAsymmetricSigner( + new Sha256, + InMemory::plainText($private_key), + InMemory::plainText($private_key) + ); + + $now = time(); + + return $configuration->builder() + ->issuedBy((string) $app_id) + ->permittedFor('https://api.github.com') + ->identifiedBy((string) $now) + ->issuedAt(new \DateTimeImmutable("@{$now}")) + ->expiresAt(new \DateTimeImmutable('@'.($now + 600))) + ->getToken($configuration->signer(), $configuration->signingKey()) + ->toString(); + } + public function syncGithubAppName() { try { - $github_access_token = generate_github_installation_token($this->github_app); + $privateKey = PrivateKey::find($this->github_app->private_key_id); - $response = Http::withToken($github_access_token) - ->withHeaders([ - 'Accept' => 'application/vnd.github+json', - 'X-GitHub-Api-Version' => '2022-11-28', - ]) - ->get("{$this->github_app->api_url}/app"); + if (! $privateKey) { + $this->dispatch('error', 'Private key not found for this GitHub App.'); + + return; + } + + $jwt = $this->generateGithubJwt($privateKey->private_key, $this->github_app->app_id); + + $response = Http::withHeaders([ + 'Accept' => 'application/vnd.github+json', + 'X-GitHub-Api-Version' => '2022-11-28', + 'Authorization' => "Bearer {$jwt}", + ])->get("{$this->github_app->api_url}/app"); if ($response->successful()) { $app_data = $response->json(); - $app_name = $app_data['name'] ?? null; + $app_slug = $app_data['slug'] ?? null; - if ($app_name && $app_name !== $this->github_app->name) { - $this->github_app->name = $app_name; - $this->name = str($app_name)->kebab(); + if ($app_slug) { + $this->github_app->name = $app_slug; + $this->name = str($app_slug)->kebab(); $this->github_app->save(); $this->dispatch('success', 'Github App name synchronized successfully.'); } else { - $this->dispatch('info', 'If you changed the name in GitHub, please wait a few moments and try syncing again.'); + $this->dispatch('info', 'Could not find app slug in GitHub response.'); } } else { - $this->dispatch('error', 'Failed to fetch Github App information. Status: '.$response->status()); + $error_message = $response->json()['message'] ?? 'Unknown error'; + $this->dispatch('error', "Failed to fetch Github App information: {$error_message}"); } } catch (\Throwable $e) { return handleError($e, $this); From 5f985426abc7d11f8d16fb675f6c3430fc4e6ae1 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 15:27:20 +0100 Subject: [PATCH 07/10] feat: update private key nam with new slug as well --- app/Http/Controllers/Webhook/Github.php | 2 +- app/Livewire/Source/Github/Change.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Webhook/Github.php b/app/Http/Controllers/Webhook/Github.php index 3683adaa8..ac1d4ded2 100644 --- a/app/Http/Controllers/Webhook/Github.php +++ b/app/Http/Controllers/Webhook/Github.php @@ -463,7 +463,7 @@ class Github extends Controller $private_key = data_get($data, 'pem'); $webhook_secret = data_get($data, 'webhook_secret'); $private_key = PrivateKey::create([ - 'name' => $slug, + 'name' => "github-app-{$slug}", 'private_key' => $private_key, 'team_id' => $github_app->team_id, 'is_git_related' => true, diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 4968a549d..7f9200891 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -198,8 +198,10 @@ class Change extends Component if ($app_slug) { $this->github_app->name = $app_slug; $this->name = str($app_slug)->kebab(); + $privateKey->name = "github-app-{$app_slug}"; + $privateKey->save(); $this->github_app->save(); - $this->dispatch('success', 'Github App name synchronized successfully.'); + $this->dispatch('success', 'Github App name and SSH key name synchronized successfully.'); } else { $this->dispatch('info', 'Could not find app slug in GitHub response.'); } From fef8d0c62c103df93d840d3e86eab488940915fb Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 15:50:45 +0100 Subject: [PATCH 08/10] fix: typos and naming --- app/Livewire/Source/Github/Change.php | 14 ++++++++------ .../views/livewire/source/github/change.blade.php | 8 +++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 7f9200891..b1f0605bb 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -62,6 +62,7 @@ class Change extends Component $this->github_app->refresh()->makeVisible('client_secret')->makeVisible('webhook_secret'); $this->dispatch('success', 'Github App permissions updated.'); } + // public function check() // { @@ -95,6 +96,7 @@ class Change extends Component // ray($runners_by_repository); // } + public function mount() { try { @@ -147,7 +149,7 @@ class Change extends Component } } - public function getUpdatePath() + public function getGithubAppNameUpdatePath() { return "{$this->github_app->html_url}/settings/apps/{$this->github_app->name}"; } @@ -172,13 +174,13 @@ class Change extends Component ->toString(); } - public function syncGithubAppName() + public function updateGithubAppName() { try { $privateKey = PrivateKey::find($this->github_app->private_key_id); if (! $privateKey) { - $this->dispatch('error', 'Private key not found for this GitHub App.'); + $this->dispatch('error', 'No private key found for this GitHub App.'); return; } @@ -201,13 +203,13 @@ class Change extends Component $privateKey->name = "github-app-{$app_slug}"; $privateKey->save(); $this->github_app->save(); - $this->dispatch('success', 'Github App name and SSH key name synchronized successfully.'); + $this->dispatch('success', 'GitHub App name and SSH key name synchronized successfully.'); } else { - $this->dispatch('info', 'Could not find app slug in GitHub response.'); + $this->dispatch('info', 'Could not find App Name (slug) in GitHub response.'); } } else { $error_message = $response->json()['message'] ?? 'Unknown error'; - $this->dispatch('error', "Failed to fetch Github App information: {$error_message}"); + $this->dispatch('error', "Failed to fetch GitHub App information: {$error_message}"); } } catch (\Throwable $e) { return handleError($e, $this); diff --git a/resources/views/livewire/source/github/change.blade.php b/resources/views/livewire/source/github/change.blade.php index 2d873b465..3c11646c2 100644 --- a/resources/views/livewire/source/github/change.blade.php +++ b/resources/views/livewire/source/github/change.blade.php @@ -60,10 +60,12 @@
- Sync Name - + + Sync Name + + - Update + Rename From f51300a19213df0d29fbf0161fc91495bd296c52 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:17:35 +0100 Subject: [PATCH 09/10] fix: client and webhook secret disappear after sync --- app/Livewire/Source/Github/Change.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index b1f0605bb..51c5ae3e9 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -56,6 +56,13 @@ class Change extends Component 'github_app.administration' => 'nullable|string', ]; + public function boot() + { + if ($this->github_app) { + $this->github_app->makeVisible(['client_secret', 'webhook_secret']); + } + } + public function checkPermissions() { GithubAppPermissionJob::dispatchSync($this->github_app); @@ -102,10 +109,10 @@ class Change extends Component try { $github_app_uuid = request()->github_app_uuid; $this->github_app = GithubApp::ownedByCurrentTeam()->whereUuid($github_app_uuid)->firstOrFail(); + $this->github_app->makeVisible(['client_secret', 'webhook_secret']); $this->applications = $this->github_app->applications; $settings = instanceSettings(); - $this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret'); $this->name = str($this->github_app->name)->kebab(); $this->fqdn = $settings->fqdn; From c25be01af6881f13928a1ab64bc900eaa173464b Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 5 Dec 2024 11:00:57 +0100 Subject: [PATCH 10/10] fix --- app/Livewire/Source/Github/Change.php | 6 +++++- database/seeders/GithubAppSeeder.php | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.php index 51c5ae3e9..467927484 100644 --- a/app/Livewire/Source/Github/Change.php +++ b/app/Livewire/Source/Github/Change.php @@ -158,6 +158,10 @@ class Change extends Component public function getGithubAppNameUpdatePath() { + if (str($this->github_app->organization)->isNotEmpty()) { + return "{$this->github_app->html_url}/organizations/{$this->github_app->organization}/settings/apps/{$this->github_app->name}"; + } + return "{$this->github_app->html_url}/settings/apps/{$this->github_app->name}"; } @@ -184,7 +188,7 @@ class Change extends Component public function updateGithubAppName() { try { - $privateKey = PrivateKey::find($this->github_app->private_key_id); + $privateKey = PrivateKey::ownedByCurrentTeam()->find($this->github_app->private_key_id); if (! $privateKey) { $this->dispatch('error', 'No private key found for this GitHub App.'); diff --git a/database/seeders/GithubAppSeeder.php b/database/seeders/GithubAppSeeder.php index 2ece7a05b..3cfb82e64 100644 --- a/database/seeders/GithubAppSeeder.php +++ b/database/seeders/GithubAppSeeder.php @@ -23,6 +23,7 @@ class GithubAppSeeder extends Seeder GithubApp::create([ 'name' => 'coolify-laravel-development-public', 'uuid' => '69420', + 'organization' => 'coollabsio', 'api_url' => 'https://api.github.com', 'html_url' => 'https://github.com', 'is_public' => false,