@@ -19,6 +19,7 @@ import type { EditorAction, EditorDispatch } from '../../../components/editor/ac
19
19
import {
20
20
deleteFile ,
21
21
removeFileConflict ,
22
+ setGithubState ,
22
23
showToast ,
23
24
updateBranchContents ,
24
25
updateFile ,
@@ -38,7 +39,6 @@ import type {
38
39
import {
39
40
emptyGithubData ,
40
41
emptyGithubSettings ,
41
- FileChecksums ,
42
42
githubOperationPrettyName ,
43
43
projectGithubSettings ,
44
44
} from '../../../components/editor/store/editor-state'
@@ -184,10 +184,18 @@ export async function runGithubOperation(
184
184
dispatch ( [ updateGithubOperations ( operation , 'add' ) ] , 'everyone' )
185
185
result = await logic ( operation )
186
186
} catch ( error : any ) {
187
- dispatch (
188
- [ showToast ( notice ( `${ opName } failed. See the console for more information.` , 'ERROR' ) ) ] ,
189
- 'everyone' ,
190
- )
187
+ let actions : EditorAction [ ] = [ ]
188
+
189
+ const userDetails = await getUserDetailsFromServer ( )
190
+ if ( userDetails == null ) {
191
+ actions . push ( ...resetGithubStateAndDataActions ( ) )
192
+ } else {
193
+ actions . push (
194
+ showToast ( notice ( `${ opName } failed. See the console for more information.` , 'ERROR' ) ) ,
195
+ )
196
+ }
197
+
198
+ dispatch ( actions , 'everyone' )
191
199
console . error ( `[GitHub] operation "${ opName } " failed:` , error )
192
200
throw error
193
201
} finally {
@@ -287,7 +295,15 @@ export async function getBranchContentFromServer(
287
295
} )
288
296
}
289
297
290
- export async function getUserDetailsFromServer ( ) : Promise < Array < EditorAction > > {
298
+ function resetGithubStateAndDataActions ( ) : EditorAction [ ] {
299
+ return [ setGithubState ( { authenticated : false } ) , updateGithubData ( emptyGithubData ( ) ) ]
300
+ }
301
+
302
+ function updateUserDetailsFromServerActions ( userDetails : GithubUser ) : EditorAction [ ] {
303
+ return [ updateGithubData ( { githubUserDetails : userDetails } ) ]
304
+ }
305
+
306
+ export async function getUserDetailsFromServer ( ) : Promise < GithubUser | null > {
291
307
const url = GithubEndpoints . userDetails ( )
292
308
293
309
const response = await fetch ( url , {
@@ -301,19 +317,21 @@ export async function getUserDetailsFromServer(): Promise<Array<EditorAction>> {
301
317
const responseBody : GetGithubUserResponse = await response . json ( )
302
318
switch ( responseBody . type ) {
303
319
case 'FAILURE' :
304
- throw new Error (
320
+ console . error (
305
321
`Error when attempting to retrieve the user details: ${ responseBody . failureReason } ` ,
306
322
)
323
+ return null
307
324
case 'SUCCESS' :
308
- return [ updateGithubData ( { githubUserDetails : responseBody . user } ) ]
325
+ return responseBody . user
309
326
default :
310
327
const _exhaustiveCheck : never = responseBody
311
328
throw new Error ( `Unhandled response body ${ JSON . stringify ( responseBody ) } ` )
312
329
}
313
330
} else {
314
- throw new Error (
331
+ console . error (
315
332
`Github: Unexpected status returned from user details endpoint: ${ response . status } ` ,
316
333
)
334
+ return null
317
335
}
318
336
}
319
337
@@ -323,12 +341,20 @@ export async function updateUserDetailsWhenAuthenticated(
323
341
authenticationCheck : Promise < boolean > ,
324
342
) : Promise < boolean > {
325
343
const authenticationResult = await authenticationCheck
326
- if ( authenticationResult ) {
327
- await dispatchPromiseActions ( dispatch , getUserDetailsFromServer ( ) ) . catch ( ( error ) => {
328
- console . error ( `Error while attempting to retrieve Github user details: ${ error } ` )
329
- } )
344
+ if ( ! authenticationResult ) {
345
+ return false
346
+ }
347
+
348
+ const userDetails = await getUserDetailsFromServer ( )
349
+ if ( userDetails == null ) {
350
+ // if the user details are not available while the auth is successful, it means that
351
+ // there a mismatch between the cookies and the actual auth, so return false overall.
352
+ dispatch ( resetGithubStateAndDataActions ( ) )
353
+ return false
330
354
}
331
- return authenticationResult
355
+
356
+ dispatch ( updateUserDetailsFromServerActions ( userDetails ) )
357
+ return true
332
358
}
333
359
334
360
const githubFileChangesSelector = createSelector (
@@ -840,7 +866,7 @@ export const startGithubPolling =
840
866
githubPollingTimeoutId = window . setTimeout ( pollGithub , 0 )
841
867
}
842
868
843
- export async function refreshGithubData (
869
+ export function getRefreshGithubActions (
844
870
dispatch : EditorDispatch ,
845
871
githubAuthenticated : boolean ,
846
872
githubRepo : GithubRepo | null ,
@@ -850,14 +876,20 @@ export async function refreshGithubData(
850
876
previousCommitSha : string | null ,
851
877
originCommitSha : string | null ,
852
878
operationContext : GithubOperationContext ,
853
- ) : Promise < void > {
879
+ ) : Array < Promise < Array < EditorAction > > > {
854
880
// Collect actions which are the results of the various requests,
855
881
// but not those that show which Github operations are running.
856
- const promises : Array < Promise < Array < EditorAction > > > = [ ]
857
- if ( githubAuthenticated ) {
858
- if ( githubUserDetails === null ) {
859
- promises . push ( getUserDetailsFromServer ( ) )
860
- }
882
+ let promises : Array < Promise < Array < EditorAction > > > = [ ]
883
+ if ( ! githubAuthenticated ) {
884
+ promises . push ( Promise . resolve ( [ updateGithubData ( emptyGithubData ( ) ) ] ) )
885
+ } else if ( githubUserDetails === null ) {
886
+ const noUserDetailsActions = getUserDetailsFromServer ( ) . then ( ( userDetails ) => {
887
+ return userDetails == null
888
+ ? resetGithubStateAndDataActions ( )
889
+ : updateUserDetailsFromServerActions ( userDetails )
890
+ } )
891
+ promises . push ( noUserDetailsActions )
892
+ } else {
861
893
promises . push ( getUsersPublicGithubRepositories ( operationContext ) ( dispatch ) )
862
894
if ( githubRepo != null ) {
863
895
promises . push ( getBranchesForGithubRepository ( operationContext ) ( dispatch , githubRepo ) )
@@ -886,10 +918,33 @@ export async function refreshGithubData(
886
918
} else {
887
919
promises . push ( Promise . resolve ( [ updateGithubData ( { branches : null } ) ] ) )
888
920
}
889
- } else {
890
- promises . push ( Promise . resolve ( [ updateGithubData ( emptyGithubData ( ) ) ] ) )
891
921
}
892
922
923
+ return promises
924
+ }
925
+
926
+ export async function refreshGithubData (
927
+ dispatch : EditorDispatch ,
928
+ githubAuthenticated : boolean ,
929
+ githubRepo : GithubRepo | null ,
930
+ branchName : string | null ,
931
+ branchOriginChecksums : FileChecksumsWithFile | null ,
932
+ githubUserDetails : GithubUser | null ,
933
+ previousCommitSha : string | null ,
934
+ originCommitSha : string | null ,
935
+ operationContext : GithubOperationContext ,
936
+ ) : Promise < void > {
937
+ const promises = await getRefreshGithubActions (
938
+ dispatch ,
939
+ githubAuthenticated ,
940
+ githubRepo ,
941
+ branchName ,
942
+ branchOriginChecksums ,
943
+ githubUserDetails ,
944
+ previousCommitSha ,
945
+ originCommitSha ,
946
+ operationContext ,
947
+ )
893
948
// Resolve all the promises.
894
949
await Promise . allSettled ( promises ) . then ( ( results ) => {
895
950
let actions : Array < EditorAction > = [ ]
0 commit comments