88use App \Rules \SafeExternalUrl ;
99use Illuminate \Foundation \Auth \Access \AuthorizesRequests ;
1010use Illuminate \Support \Facades \Cache ;
11- use Illuminate \Support \Facades \Http ;
1211use Illuminate \Support \Str ;
13- use Lcobucci \JWT \Configuration ;
14- use Lcobucci \JWT \Signer \Key \InMemory ;
15- use Lcobucci \JWT \Signer \Rsa \Sha256 ;
12+ use Illuminate \Validation \ValidationException ;
1613use Livewire \Component ;
1714
1815class Change extends Component
@@ -216,6 +213,8 @@ public function checkPermissions()
216213 return ;
217214 }
218215
216+ syncGithubAppName ($ this ->github_app );
217+
219218 GithubAppPermissionJob::dispatchSync ($ this ->github_app );
220219 $ this ->github_app ->refresh ()->makeVisible ('client_secret ' )->makeVisible ('webhook_secret ' );
221220 $ this ->syncData (false );
@@ -314,64 +313,34 @@ public function getGithubAppNameUpdatePath()
314313 return rtrim ($ this ->github_app ->html_url , '/ ' )."/settings/apps/ {$ name }" ;
315314 }
316315
317- private function generateGithubJwt ($ private_key , $ app_id ): string
318- {
319- $ configuration = Configuration::forAsymmetricSigner (
320- new Sha256 ,
321- InMemory::plainText ($ private_key ),
322- InMemory::plainText ($ private_key )
323- );
324-
325- $ now = time ();
326-
327- return $ configuration ->builder ()
328- ->issuedBy ((string ) $ app_id )
329- ->permittedFor ($ this ->github_app ->api_url )
330- ->identifiedBy ((string ) $ now )
331- ->issuedAt (new \DateTimeImmutable ("@ {$ now }" ))
332- ->expiresAt (new \DateTimeImmutable ('@ ' .($ now + 600 )))
333- ->getToken ($ configuration ->signer (), $ configuration ->signingKey ())
334- ->toString ();
335- }
336-
337316 public function updateGithubAppName ()
338317 {
339318 try {
340319 $ this ->authorize ('update ' , $ this ->github_app );
341320
342- $ privateKey = PrivateKey::ownedByCurrentTeam ()->find ($ this ->github_app ->private_key_id );
321+ $ this ->github_app ->app_id = $ this ->appId ;
322+ $ this ->github_app ->private_key_id = $ this ->privateKeyId ;
323+ $ this ->github_app ->unsetRelation ('privateKey ' );
324+
325+ if (! $ this ->appId ) {
326+ $ this ->dispatch ('error ' , 'App ID is required before synchronizing the GitHub App name. ' );
343327
344- if (! $ privateKey ) {
328+ return ;
329+ }
330+
331+ if (! PrivateKey::ownedByCurrentTeam ()->find ($ this ->privateKeyId )) {
345332 $ this ->dispatch ('error ' , 'No private key found for this GitHub App. ' );
346333
347334 return ;
348335 }
349336
350- $ jwt = $ this ->generateGithubJwt ($ privateKey ->private_key , $ this ->github_app ->app_id );
351-
352- $ response = Http::withHeaders ([
353- 'Accept ' => 'application/vnd.github+json ' ,
354- 'X-GitHub-Api-Version ' => '2022-11-28 ' ,
355- 'Authorization ' => "Bearer {$ jwt }" ,
356- ])->get ("{$ this ->github_app ->api_url }/app " );
357-
358- if ($ response ->successful ()) {
359- $ app_data = $ response ->json ();
360- $ app_slug = $ app_data ['slug ' ] ?? null ;
361-
362- if ($ app_slug ) {
363- $ this ->github_app ->name = $ app_slug ;
364- $ this ->name = str ($ app_slug )->kebab ();
365- $ privateKey ->name = "github-app- {$ app_slug }" ;
366- $ privateKey ->save ();
367- $ this ->github_app ->save ();
368- $ this ->dispatch ('success ' , 'GitHub App name and SSH key name synchronized successfully. ' );
369- } else {
370- $ this ->dispatch ('info ' , 'Could not find App Name (slug) in GitHub response. ' );
371- }
337+ $ appSlug = syncGithubAppName ($ this ->github_app , true );
338+
339+ if ($ appSlug ) {
340+ $ this ->name = str ($ appSlug )->kebab ();
341+ $ this ->dispatch ('success ' , 'GitHub App name and private key name synchronized successfully. ' );
372342 } else {
373- $ error_message = $ response ->json ()['message ' ] ?? 'Unknown error ' ;
374- $ this ->dispatch ('error ' , "Failed to fetch GitHub App information: {$ error_message }" );
343+ $ this ->dispatch ('info ' , 'Could not find App Name (slug) in GitHub response. ' );
375344 }
376345 } catch (\Throwable $ e ) {
377346 return handleError ($ e , $ this );
@@ -391,6 +360,8 @@ public function submit()
391360 $ this ->syncData (true );
392361 $ this ->github_app ->save ();
393362 $ this ->dispatch ('success ' , 'Github App updated. ' );
363+ } catch (ValidationException $ e ) {
364+ throw $ e ;
394365 } catch (\Throwable $ e ) {
395366 return handleError ($ e , $ this );
396367 }
0 commit comments