@@ -15,20 +15,33 @@ import { sanitizeRedirectPath } from "~/utils";
1515const QuerySchema = z . discriminatedUnion ( "setup_action" , [
1616 z . object ( {
1717 setup_action : z . literal ( "install" ) ,
18- installation_id : z . coerce . number ( ) ,
19- state : z . string ( ) ,
18+ installation_id : z . coerce . number ( ) . int ( ) . positive ( ) . safe ( ) ,
19+ state : z . string ( ) . min ( 1 ) ,
20+ code : z . string ( ) . min ( 1 ) ,
2021 } ) ,
2122 z . object ( {
2223 setup_action : z . literal ( "update" ) ,
23- installation_id : z . coerce . number ( ) ,
24- state : z . string ( ) ,
24+ installation_id : z . coerce . number ( ) . int ( ) . positive ( ) . safe ( ) ,
25+ state : z . string ( ) . min ( 1 ) ,
2526 } ) ,
2627 z . object ( {
2728 setup_action : z . literal ( "request" ) ,
28- state : z . string ( ) ,
29+ state : z . string ( ) . min ( 1 ) ,
2930 } ) ,
3031] ) ;
3132
33+ function loggableError ( error : unknown ) {
34+ if ( ! ( error instanceof Error ) ) {
35+ return { name : "Unknown error" } ;
36+ }
37+
38+ return {
39+ name : error . name ,
40+ message : error . message ,
41+ status : "status" in error ? error . status : undefined ,
42+ } ;
43+ }
44+
3245export async function loader ( { request } : LoaderFunctionArgs ) {
3346 const url = new URL ( request . url ) ;
3447 const queryParams = Object . fromEntries ( url . searchParams ) ;
@@ -38,7 +51,8 @@ export async function loader({ request }: LoaderFunctionArgs) {
3851
3952 if ( ! result . success ) {
4053 logger . warn ( "GitHub App callback with invalid params" , {
41- queryParams,
54+ setupAction : url . searchParams . get ( "setup_action" ) ,
55+ parameterNames : [ ...url . searchParams . keys ( ) ] ,
4256 } ) ;
4357 return redirectWithErrorMessage ( "/" , request , "Failed to install GitHub app" ) ;
4458 }
@@ -49,7 +63,9 @@ export async function loader({ request }: LoaderFunctionArgs) {
4963
5064 if ( ! sessionResult . valid ) {
5165 logger . error ( "GitHub App callback with invalid session" , {
52- callbackData,
66+ setupAction : callbackData . setup_action ,
67+ installationId :
68+ callbackData . setup_action === "request" ? undefined : callbackData . installation_id ,
5369 error : sessionResult . error ,
5470 } ) ;
5571
@@ -90,12 +106,19 @@ export async function loader({ request }: LoaderFunctionArgs) {
90106 switch ( callbackData . setup_action ) {
91107 case "install" : {
92108 const [ error ] = await tryCatch (
93- linkGitHubAppInstallation ( callbackData . installation_id , organizationId )
109+ linkGitHubAppInstallation ( {
110+ installationId : callbackData . installation_id ,
111+ organizationId,
112+ installedByUserId : user . id ,
113+ oauthCode : callbackData . code ,
114+ } )
94115 ) ;
95116
96117 if ( error ) {
97118 logger . error ( "Failed to link GitHub App installation" , {
98- error,
119+ installationId : callbackData . installation_id ,
120+ organizationId,
121+ error : loggableError ( error ) ,
99122 } ) ;
100123 return consumingSession (
101124 await redirectWithErrorMessage ( redirectTo , request , "Failed to install GitHub app" )
@@ -114,7 +137,9 @@ export async function loader({ request }: LoaderFunctionArgs) {
114137
115138 if ( error ) {
116139 logger . error ( "Failed to update GitHub App installation" , {
117- error,
140+ installationId : callbackData . installation_id ,
141+ organizationId,
142+ error : loggableError ( error ) ,
118143 } ) ;
119144 return consumingSession (
120145 await redirectWithErrorMessage ( redirectTo , request , "Failed to update GitHub App" )
@@ -129,9 +154,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
129154 case "request" : {
130155 // This happens when a non-admin user requests installation
131156 // The installation_id won't be available until an admin approves
132- logger . info ( "GitHub App installation requested, awaiting approval" , {
133- callbackData,
134- } ) ;
157+ logger . info ( "GitHub App installation requested, awaiting approval" ) ;
135158
136159 return redirectWithSuccessMessage ( redirectTo , request , "GitHub App installation requested" ) ;
137160 }
0 commit comments