@@ -598,43 +598,50 @@ const filterUsers = async (req, res) => {
598
598
// one time script function to perform the migration - adding github_user_id field to the document
599
599
const migrate = async ( req , res ) => {
600
600
const usersNotFound = [ ] ;
601
+ let countUserFound = 0 ;
601
602
let countUserNotFound = 0 ;
602
603
try {
603
604
// Fetch user data from GitHub API for each document in the users collection
604
605
// divided by 500 because firestore api guarantee that we can process in batch of 500.
605
606
const usersSnapshot = await firestore . collection ( "users" ) . get ( ) ;
606
- const batchCount = Math . ceil ( usersSnapshot . docs . length / 500 ) ;
607
+ const totalUsers = usersSnapshot . docs . length ;
608
+ const batchCount = Math . ceil ( totalUsers / 500 ) ;
607
609
// Create batch write operations for each batch of documents
608
610
for ( let i = 0 ; i < batchCount ; i ++ ) {
609
611
const batchDocs = usersSnapshot . docs . slice ( i * 500 , ( i + 1 ) * 500 ) ;
610
612
const batchWrite = firestore . batch ( ) ;
611
613
const batchWrites = [ ] ;
612
614
for ( const userDoc of batchDocs ) {
613
615
const githubUsername = userDoc . data ( ) . github_id ;
614
- const userName = userDoc . data ( ) . github_display_name ;
616
+ const username = userDoc . data ( ) . username ;
617
+ const userId = userDoc . id ;
615
618
batchWrite . update ( userDoc . ref , { github_user_id : null } ) ;
616
619
batchWrites . push (
617
620
axios
618
621
. get ( `https://api.github.com/users/${ githubUsername } ` , {
619
622
headers : {
620
623
"Content-Type" : "application/json" ,
621
- auth : `Bearer <Auth Token>` ,
624
+ } ,
625
+ auth : {
626
+ username : config . get ( "githubOauth.clientId" ) ,
627
+ password : config . get ( "githubOauth.clientSecret" ) ,
622
628
} ,
623
629
} )
624
630
. then ( ( response ) => {
625
631
const githubUserId = response . data . id ;
626
632
batchWrite . update ( userDoc . ref , { github_user_id : `${ githubUserId } ` } ) ;
633
+ countUserFound ++ ;
627
634
} )
628
635
. catch ( ( error ) => {
629
636
if ( error . response && error . response . status === 404 ) {
630
637
countUserNotFound ++ ;
631
638
const invalidUsers = {
632
- name : `${ userName } ` ,
633
- username : `${ githubUsername } ` ,
639
+ userId : `${ userId } ` ,
640
+ username : `${ username } ` ,
641
+ githubUsername : `${ githubUsername } ` ,
634
642
} ;
635
643
usersNotFound . push ( invalidUsers ) ;
636
644
} else {
637
- // Other error occurred
638
645
logger . error ( "An error occurred at axios.get:" , error ) ;
639
646
}
640
647
} )
@@ -645,10 +652,12 @@ const migrate = async (req, res) => {
645
652
}
646
653
647
654
return res . status ( 200 ) . json ( {
648
- message : "All Users github_user_id added successfully " ,
655
+ message : "Result of migration " ,
649
656
data : {
650
- invalidUsers : usersNotFound ,
651
- totalCount : countUserNotFound ,
657
+ totalUsers : totalUsers ,
658
+ usersUpdated : countUserFound ,
659
+ usersNotUpdated : countUserNotFound ,
660
+ invalidUsersDetails : usersNotFound ,
652
661
} ,
653
662
} ) ;
654
663
} catch ( error ) {
0 commit comments