@@ -10,22 +10,29 @@ type Query = {
1010 state : string ;
1111 installation_id : string ;
1212 setup_action : string ;
13- userId : string ;
1413} ;
1514
1615export default async function handler (
1716 req : NextApiRequest ,
1817 res : NextApiResponse ,
1918) {
20- const { code, state, installation_id, userId } : Query = req . query as Query ;
19+ const { code, state, installation_id } : Query = req . query as Query ;
2120
2221 if ( ! code ) {
2322 return res . status ( 400 ) . json ( { error : "Missing code parameter" } ) ;
2423 }
25- const [ action , value ] = state ?. split ( ":" ) ;
26- // Value could be the organizationId or the githubProviderId
24+ const [ action , ...rest ] = state ?. split ( ":" ) ;
25+ // For gh_init: rest[0] = organizationId, rest[1] = userId
26+ // For gh_setup: rest[0] = githubProviderId
2727
2828 if ( action === "gh_init" ) {
29+ const organizationId = rest [ 0 ] ;
30+ const userId = rest [ 1 ] || ( req . query . userId as string ) ;
31+
32+ if ( ! userId ) {
33+ return res . status ( 400 ) . json ( { error : "Missing userId parameter" } ) ;
34+ }
35+
2936 const octokit = new Octokit ( { } ) ;
3037 const { data } = await octokit . request (
3138 "POST /app-manifests/{code}/conversions" ,
@@ -44,7 +51,7 @@ export default async function handler(
4451 githubWebhookSecret : data . webhook_secret ,
4552 githubPrivateKey : data . pem ,
4653 } ,
47- value as string ,
54+ organizationId as string ,
4855 userId ,
4956 ) ;
5057 } else if ( action === "gh_setup" ) {
@@ -53,7 +60,7 @@ export default async function handler(
5360 . set ( {
5461 githubInstallationId : installation_id ,
5562 } )
56- . where ( eq ( github . githubId , value as string ) )
63+ . where ( eq ( github . githubId , rest [ 0 ] as string ) )
5764 . returning ( ) ;
5865 }
5966
0 commit comments