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

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