You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "context"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/structs"
  10. )
  11. // UpdateMigrationPosterID updates all migrated repositories' issues and comments posterID
  12. func UpdateMigrationPosterID(ctx context.Context) {
  13. for _, gitService := range structs.SupportedFullGitService {
  14. select {
  15. case <-ctx.Done():
  16. log.Warn("UpdateMigrationPosterID aborted due to shutdown before %s", gitService.Name())
  17. default:
  18. }
  19. if err := updateMigrationPosterIDByGitService(ctx, gitService); err != nil {
  20. log.Error("updateMigrationPosterIDByGitService failed: %v", err)
  21. }
  22. }
  23. }
  24. func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServiceType) error {
  25. provider := tp.Name()
  26. if len(provider) == 0 {
  27. return nil
  28. }
  29. const batchSize = 100
  30. var start int
  31. for {
  32. select {
  33. case <-ctx.Done():
  34. log.Warn("UpdateMigrationPosterIDByGitService(%s) aborted due to shutdown", tp.Name())
  35. return nil
  36. default:
  37. }
  38. users, err := models.FindExternalUsersByProvider(models.FindExternalUserOptions{
  39. Provider: provider,
  40. Start: start,
  41. Limit: batchSize,
  42. })
  43. if err != nil {
  44. return err
  45. }
  46. for _, user := range users {
  47. select {
  48. case <-ctx.Done():
  49. log.Warn("UpdateMigrationPosterIDByGitService(%s) aborted due to shutdown", tp.Name())
  50. return nil
  51. default:
  52. }
  53. externalUserID := user.ExternalID
  54. if err := models.UpdateMigrationsByType(tp, externalUserID, user.UserID); err != nil {
  55. log.Error("UpdateMigrationsByType type %s external user id %v to local user id %v failed: %v", tp.Name(), user.ExternalID, user.UserID, err)
  56. }
  57. }
  58. if len(users) < batchSize {
  59. break
  60. }
  61. start += len(users)
  62. }
  63. return nil
  64. }