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.

update.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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) error {
  13. for _, gitService := range structs.SupportedFullGitService {
  14. select {
  15. case <-ctx.Done():
  16. log.Warn("UpdateMigrationPosterID aborted before %s", gitService.Name())
  17. return models.ErrCancelledf("during UpdateMigrationPosterID before %s", gitService.Name())
  18. default:
  19. }
  20. if err := updateMigrationPosterIDByGitService(ctx, gitService); err != nil {
  21. log.Error("updateMigrationPosterIDByGitService failed: %v", err)
  22. }
  23. }
  24. return nil
  25. }
  26. func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServiceType) error {
  27. provider := tp.Name()
  28. if len(provider) == 0 {
  29. return nil
  30. }
  31. const batchSize = 100
  32. var start int
  33. for {
  34. select {
  35. case <-ctx.Done():
  36. log.Warn("UpdateMigrationPosterIDByGitService(%s) cancelled", tp.Name())
  37. return nil
  38. default:
  39. }
  40. users, err := models.FindExternalUsersByProvider(models.FindExternalUserOptions{
  41. Provider: provider,
  42. Start: start,
  43. Limit: batchSize,
  44. })
  45. if err != nil {
  46. return err
  47. }
  48. for _, user := range users {
  49. select {
  50. case <-ctx.Done():
  51. log.Warn("UpdateMigrationPosterIDByGitService(%s) cancelled", tp.Name())
  52. return nil
  53. default:
  54. }
  55. externalUserID := user.ExternalID
  56. if err := models.UpdateMigrationsByType(tp, externalUserID, user.UserID); err != nil {
  57. log.Error("UpdateMigrationsByType type %s external user id %v to local user id %v failed: %v", tp.Name(), user.ExternalID, user.UserID, err)
  58. }
  59. }
  60. if len(users) < batchSize {
  61. break
  62. }
  63. start += len(users)
  64. }
  65. return nil
  66. }