code.gitea.io/gitea@v1.21.7/services/migrations/update.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package migrations
     5  
     6  import (
     7  	"context"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	user_model "code.gitea.io/gitea/models/user"
    11  	"code.gitea.io/gitea/modules/log"
    12  	"code.gitea.io/gitea/modules/structs"
    13  	"code.gitea.io/gitea/services/externalaccount"
    14  )
    15  
    16  // UpdateMigrationPosterID updates all migrated repositories' issues and comments posterID
    17  func UpdateMigrationPosterID(ctx context.Context) error {
    18  	for _, gitService := range structs.SupportedFullGitService {
    19  		select {
    20  		case <-ctx.Done():
    21  			log.Warn("UpdateMigrationPosterID aborted before %s", gitService.Name())
    22  			return db.ErrCancelledf("during UpdateMigrationPosterID before %s", gitService.Name())
    23  		default:
    24  		}
    25  		if err := updateMigrationPosterIDByGitService(ctx, gitService); err != nil {
    26  			log.Error("updateMigrationPosterIDByGitService failed: %v", err)
    27  		}
    28  	}
    29  	return nil
    30  }
    31  
    32  func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServiceType) error {
    33  	provider := tp.Name()
    34  	if len(provider) == 0 {
    35  		return nil
    36  	}
    37  
    38  	const batchSize = 100
    39  	var start int
    40  	for {
    41  		select {
    42  		case <-ctx.Done():
    43  			log.Warn("UpdateMigrationPosterIDByGitService(%s) cancelled", tp.Name())
    44  			return nil
    45  		default:
    46  		}
    47  
    48  		users, err := user_model.FindExternalUsersByProvider(user_model.FindExternalUserOptions{
    49  			Provider: provider,
    50  			Start:    start,
    51  			Limit:    batchSize,
    52  		})
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		for _, user := range users {
    58  			select {
    59  			case <-ctx.Done():
    60  				log.Warn("UpdateMigrationPosterIDByGitService(%s) cancelled", tp.Name())
    61  				return nil
    62  			default:
    63  			}
    64  			externalUserID := user.ExternalID
    65  			if err := externalaccount.UpdateMigrationsByType(ctx, tp, externalUserID, user.UserID); err != nil {
    66  				log.Error("UpdateMigrationsByType type %s external user id %v to local user id %v failed: %v", tp.Name(), user.ExternalID, user.UserID, err)
    67  			}
    68  		}
    69  
    70  		if len(users) < batchSize {
    71  			break
    72  		}
    73  		start += len(users)
    74  	}
    75  	return nil
    76  }