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 2.0KB

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