@@ -165,9 +165,9 @@ export const app = defineApp({
165165 return error ( request . requestId , 400 , "Missing parameters" ) ;
166166 }
167167
168- const { value : manifest } = await kv . app . get ( "manifest " ) ;
168+ const { value : storedState } = await kv . app . get ( "state " ) ;
169169
170- if ( ! manifest || manifest . state !== state ) {
170+ if ( ! storedState || storedState !== state ) {
171171 return error ( request . requestId , 400 , "Invalid request" ) ;
172172 }
173173
@@ -221,9 +221,9 @@ export const app = defineApp({
221221 }
222222
223223 if ( setup_action === "install" ) {
224- const { value } = await kv . app . get ( "manifest " ) ;
224+ const { value : storedState } = await kv . app . get ( "state " ) ;
225225
226- if ( ! value ?. state || value ?. state !== state ) {
226+ if ( ! storedState || storedState !== state ) {
227227 return error ( request . requestId , 400 , "Invalid request" ) ;
228228 }
229229
@@ -258,9 +258,9 @@ export const app = defineApp({
258258
259259 await lifecycle . prompt . delete ( createGitHubAppPromptKey ) ;
260260
261- await kv . app . delete ( [ "manifest " ] ) ;
261+ await kv . app . delete ( [ "state " ] ) ;
262262
263- await lifecycle . proceed ( ) ;
263+ await lifecycle . sync ( ) ;
264264
265265 return redirect ( request . requestId , app . installationUrl ) ;
266266 }
@@ -366,8 +366,11 @@ export const app = defineApp({
366366 } ,
367367 } ,
368368 onSync : async ( { app } ) => {
369- const [ { value : installationId } , { value : manifest } ] =
370- await kv . app . getMany ( [ "installationId" , "manifest" ] ) ;
369+ let [
370+ { value : installationId } ,
371+ { value : state } ,
372+ { value : storedOrganization } ,
373+ ] = await kv . app . getMany ( [ "installationId" , "state" , "organization" ] ) ;
371374
372375 if ( installationId ) {
373376 const { value : repositories } = await kv . app . get ( "repositories" ) ;
@@ -380,43 +383,59 @@ export const app = defineApp({
380383 } ;
381384 }
382385
383- if ( ! manifest ) {
384- const url = app . http . url ;
385-
386- const state = randomBytes ( 32 ) . toString ( "hex" ) ;
387-
388- const manifest = {
389- callback_urls : [ app . installationUrl ] ,
390- default_events : [
391- "issues" ,
392- "workflow_run" ,
393- "pull_request" ,
394- "push" ,
395- "issue_comment" ,
396- ] ,
397- default_permissions : {
398- issues : "write" ,
399- actions : "write" ,
400- contents : "write" ,
401- pull_requests : "write" ,
402- } ,
403- hook_attributes : {
404- url : `${ url } /webhook` ,
405- } ,
406- setup_url : `${ url } /setup` ,
407- name : `Spaceflows GH Integration` ,
408- public : false ,
409- redirect_url : `${ url } /redirect` ,
410- url : "https://spaceflows.io" ,
411- } ;
386+ const url = app . http . url ;
387+
388+ const manifest = {
389+ callback_urls : [ app . installationUrl ] ,
390+ default_events : [
391+ "issues" ,
392+ "workflow_run" ,
393+ "pull_request" ,
394+ "push" ,
395+ "issue_comment" ,
396+ ] ,
397+ default_permissions : {
398+ issues : "write" ,
399+ actions : "write" ,
400+ contents : "write" ,
401+ pull_requests : "write" ,
402+ } ,
403+ hook_attributes : {
404+ url : `${ url } /webhook` ,
405+ } ,
406+ setup_url : `${ url } /setup` ,
407+ name : `Spacelift Flows GH Integration` ,
408+ public : false ,
409+ redirect_url : `${ url } /redirect` ,
410+ url : "https://useflows.com" ,
411+ } ;
412+
413+ if ( ! state ) {
414+ state = randomBytes ( 32 ) . toString ( "hex" ) ;
415+
416+ await kv . app . set ( {
417+ key : "state" ,
418+ value : state ,
419+ } ) ;
420+ }
421+
422+ const currentOrganization = app . config . organization ;
423+ const hasOrganizationChanged = storedOrganization !== currentOrganization ;
424+ const hasPrompt = ! ! app . prompts [ createGitHubAppPromptKey ] ;
425+ const shouldRecreatePrompt = hasOrganizationChanged || ! hasPrompt ;
426+
427+ if ( shouldRecreatePrompt ) {
428+ if ( hasPrompt ) {
429+ await lifecycle . prompt . delete ( createGitHubAppPromptKey ) ;
430+ }
412431
413432 await lifecycle . prompt . create (
414433 createGitHubAppPromptKey ,
415434 "Create a new app GitHub App" ,
416435 {
417436 redirect : {
418- url : app . config . organization
419- ? `https://github.com/organizations/${ app . config . organization } /settings/apps/new`
437+ url : currentOrganization
438+ ? `https://github.com/organizations/${ currentOrganization } /settings/apps/new`
420439 : `https://github.com/settings/apps/new` ,
421440 method : "POST" ,
422441 formFields : {
@@ -428,10 +447,8 @@ export const app = defineApp({
428447 ) ;
429448
430449 await kv . app . set ( {
431- key : "manifest" ,
432- value : {
433- state,
434- } ,
450+ key : "organization" ,
451+ value : currentOrganization ,
435452 } ) ;
436453 }
437454
0 commit comments