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.

v180.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_15 //nolint
  4. import (
  5. "code.gitea.io/gitea/modules/json"
  6. "code.gitea.io/gitea/modules/util"
  7. "xorm.io/builder"
  8. "xorm.io/xorm"
  9. )
  10. func DeleteMigrationCredentials(x *xorm.Engine) (err error) {
  11. // Task represents a task
  12. type Task struct {
  13. ID int64
  14. DoerID int64 `xorm:"index"` // operator
  15. OwnerID int64 `xorm:"index"` // repo owner id, when creating, the repoID maybe zero
  16. RepoID int64 `xorm:"index"`
  17. Type int
  18. Status int `xorm:"index"`
  19. StartTime int64
  20. EndTime int64
  21. PayloadContent string `xorm:"TEXT"`
  22. Errors string `xorm:"TEXT"` // if task failed, saved the error reason
  23. Created int64 `xorm:"created"`
  24. }
  25. const TaskTypeMigrateRepo = 0
  26. const TaskStatusStopped = 2
  27. const batchSize = 100
  28. // only match migration tasks, that are not pending or running
  29. cond := builder.Eq{
  30. "type": TaskTypeMigrateRepo,
  31. }.And(builder.Gte{
  32. "status": TaskStatusStopped,
  33. })
  34. sess := x.NewSession()
  35. defer sess.Close()
  36. for start := 0; ; start += batchSize {
  37. tasks := make([]*Task, 0, batchSize)
  38. if err := sess.Limit(batchSize, start).Where(cond, 0).Find(&tasks); err != nil {
  39. return err
  40. }
  41. if len(tasks) == 0 {
  42. break
  43. }
  44. if err := sess.Begin(); err != nil {
  45. return err
  46. }
  47. for _, t := range tasks {
  48. if t.PayloadContent, err = removeCredentials(t.PayloadContent); err != nil {
  49. return err
  50. }
  51. if _, err := sess.ID(t.ID).Cols("payload_content").Update(t); err != nil {
  52. return err
  53. }
  54. }
  55. if err := sess.Commit(); err != nil {
  56. return err
  57. }
  58. }
  59. return err
  60. }
  61. func removeCredentials(payload string) (string, error) {
  62. // MigrateOptions defines the way a repository gets migrated
  63. // this is for internal usage by migrations module and func who interact with it
  64. type MigrateOptions struct {
  65. // required: true
  66. CloneAddr string `json:"clone_addr" binding:"Required"`
  67. CloneAddrEncrypted string `json:"clone_addr_encrypted,omitempty"`
  68. AuthUsername string `json:"auth_username"`
  69. AuthPassword string `json:"-"`
  70. AuthPasswordEncrypted string `json:"auth_password_encrypted,omitempty"`
  71. AuthToken string `json:"-"`
  72. AuthTokenEncrypted string `json:"auth_token_encrypted,omitempty"`
  73. // required: true
  74. UID int `json:"uid" binding:"Required"`
  75. // required: true
  76. RepoName string `json:"repo_name" binding:"Required"`
  77. Mirror bool `json:"mirror"`
  78. LFS bool `json:"lfs"`
  79. LFSEndpoint string `json:"lfs_endpoint"`
  80. Private bool `json:"private"`
  81. Description string `json:"description"`
  82. OriginalURL string
  83. GitServiceType int
  84. Wiki bool
  85. Issues bool
  86. Milestones bool
  87. Labels bool
  88. Releases bool
  89. Comments bool
  90. PullRequests bool
  91. ReleaseAssets bool
  92. MigrateToRepoID int64
  93. MirrorInterval string `json:"mirror_interval"`
  94. }
  95. var opts MigrateOptions
  96. err := json.Unmarshal([]byte(payload), &opts)
  97. if err != nil {
  98. return "", err
  99. }
  100. opts.AuthPassword = ""
  101. opts.AuthToken = ""
  102. opts.CloneAddr = util.SanitizeCredentialURLs(opts.CloneAddr)
  103. confBytes, err := json.Marshal(opts)
  104. if err != nil {
  105. return "", err
  106. }
  107. return string(confBytes), nil
  108. }