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

migrate.go 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Copyright 2018 Jonas Franz. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package migrations
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/migrations/base"
  11. )
  12. // MigrateOptions is equal to base.MigrateOptions
  13. type MigrateOptions = base.MigrateOptions
  14. var (
  15. factories []base.DownloaderFactory
  16. )
  17. // RegisterDownloaderFactory registers a downloader factory
  18. func RegisterDownloaderFactory(factory base.DownloaderFactory) {
  19. factories = append(factories, factory)
  20. }
  21. // MigrateRepository migrate repository according MigrateOptions
  22. func MigrateRepository(doer *models.User, ownerName string, opts base.MigrateOptions) (*models.Repository, error) {
  23. var (
  24. downloader base.Downloader
  25. uploader = NewGiteaLocalUploader(doer, ownerName, opts.Name)
  26. )
  27. for _, factory := range factories {
  28. if match, err := factory.Match(opts); err != nil {
  29. return nil, err
  30. } else if match {
  31. downloader, err = factory.New(opts)
  32. if err != nil {
  33. return nil, err
  34. }
  35. break
  36. }
  37. }
  38. if downloader == nil {
  39. opts.Wiki = true
  40. opts.Milestones = false
  41. opts.Labels = false
  42. opts.Releases = false
  43. opts.Comments = false
  44. opts.Issues = false
  45. opts.PullRequests = false
  46. downloader = NewPlainGitDownloader(ownerName, opts.Name, opts.RemoteURL)
  47. log.Trace("Will migrate from git: %s", opts.RemoteURL)
  48. }
  49. if err := migrateRepository(downloader, uploader, opts); err != nil {
  50. if err1 := uploader.Rollback(); err1 != nil {
  51. log.Error("rollback failed: %v", err1)
  52. }
  53. return nil, err
  54. }
  55. return uploader.repo, nil
  56. }
  57. // migrateRepository will download informations and upload to Uploader, this is a simple
  58. // process for small repository. For a big repository, save all the data to disk
  59. // before upload is better
  60. func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts base.MigrateOptions) error {
  61. repo, err := downloader.GetRepoInfo()
  62. if err != nil {
  63. return err
  64. }
  65. repo.IsPrivate = opts.Private
  66. repo.IsMirror = opts.Mirror
  67. if opts.Description != "" {
  68. repo.Description = opts.Description
  69. }
  70. log.Trace("migrating git data")
  71. if err := uploader.CreateRepo(repo, opts); err != nil {
  72. return err
  73. }
  74. if opts.Milestones {
  75. log.Trace("migrating milestones")
  76. milestones, err := downloader.GetMilestones()
  77. if err != nil {
  78. return err
  79. }
  80. msBatchSize := uploader.MaxBatchInsertSize("milestone")
  81. for len(milestones) > 0 {
  82. if len(milestones) < msBatchSize {
  83. msBatchSize = len(milestones)
  84. }
  85. if err := uploader.CreateMilestones(milestones...); err != nil {
  86. return err
  87. }
  88. milestones = milestones[msBatchSize:]
  89. }
  90. }
  91. if opts.Labels {
  92. log.Trace("migrating labels")
  93. labels, err := downloader.GetLabels()
  94. if err != nil {
  95. return err
  96. }
  97. lbBatchSize := uploader.MaxBatchInsertSize("label")
  98. for len(labels) > 0 {
  99. if len(labels) < lbBatchSize {
  100. lbBatchSize = len(labels)
  101. }
  102. if err := uploader.CreateLabels(labels...); err != nil {
  103. return err
  104. }
  105. labels = labels[lbBatchSize:]
  106. }
  107. }
  108. if opts.Releases {
  109. log.Trace("migrating releases")
  110. releases, err := downloader.GetReleases()
  111. if err != nil {
  112. return err
  113. }
  114. relBatchSize := uploader.MaxBatchInsertSize("release")
  115. for len(releases) > 0 {
  116. if len(releases) < relBatchSize {
  117. relBatchSize = len(releases)
  118. }
  119. if err := uploader.CreateReleases(releases[:relBatchSize]...); err != nil {
  120. return err
  121. }
  122. releases = releases[relBatchSize:]
  123. }
  124. }
  125. var commentBatchSize = uploader.MaxBatchInsertSize("comment")
  126. if opts.Issues {
  127. log.Trace("migrating issues and comments")
  128. var issueBatchSize = uploader.MaxBatchInsertSize("issue")
  129. for i := 1; ; i++ {
  130. issues, isEnd, err := downloader.GetIssues(i, issueBatchSize)
  131. if err != nil {
  132. return err
  133. }
  134. for _, issue := range issues {
  135. if !opts.IgnoreIssueAuthor {
  136. issue.Content = fmt.Sprintf("Author: @%s \n\n%s", issue.PosterName, issue.Content)
  137. }
  138. }
  139. if err := uploader.CreateIssues(issues...); err != nil {
  140. return err
  141. }
  142. if !opts.Comments {
  143. continue
  144. }
  145. var allComments = make([]*base.Comment, 0, commentBatchSize)
  146. for _, issue := range issues {
  147. comments, err := downloader.GetComments(issue.Number)
  148. if err != nil {
  149. return err
  150. }
  151. for _, comment := range comments {
  152. if !opts.IgnoreIssueAuthor {
  153. comment.Content = fmt.Sprintf("Author: @%s \n\n%s", comment.PosterName, comment.Content)
  154. }
  155. }
  156. allComments = append(allComments, comments...)
  157. if len(allComments) >= commentBatchSize {
  158. if err := uploader.CreateComments(allComments[:commentBatchSize]...); err != nil {
  159. return err
  160. }
  161. allComments = allComments[commentBatchSize:]
  162. }
  163. }
  164. if len(allComments) > 0 {
  165. if err := uploader.CreateComments(allComments...); err != nil {
  166. return err
  167. }
  168. }
  169. if isEnd {
  170. break
  171. }
  172. }
  173. }
  174. if opts.PullRequests {
  175. log.Trace("migrating pull requests and comments")
  176. var prBatchSize = models.MaxBatchInsertSize("pullrequest")
  177. for i := 1; ; i++ {
  178. prs, err := downloader.GetPullRequests(i, prBatchSize)
  179. if err != nil {
  180. return err
  181. }
  182. for _, pr := range prs {
  183. if !opts.IgnoreIssueAuthor {
  184. pr.Content = fmt.Sprintf("Author: @%s \n\n%s", pr.PosterName, pr.Content)
  185. }
  186. }
  187. if err := uploader.CreatePullRequests(prs...); err != nil {
  188. return err
  189. }
  190. if !opts.Comments {
  191. continue
  192. }
  193. var allComments = make([]*base.Comment, 0, commentBatchSize)
  194. for _, pr := range prs {
  195. comments, err := downloader.GetComments(pr.Number)
  196. if err != nil {
  197. return err
  198. }
  199. for _, comment := range comments {
  200. if !opts.IgnoreIssueAuthor {
  201. comment.Content = fmt.Sprintf("Author: @%s \n\n%s", comment.PosterName, comment.Content)
  202. }
  203. }
  204. allComments = append(allComments, comments...)
  205. if len(allComments) >= commentBatchSize {
  206. if err := uploader.CreateComments(allComments[:commentBatchSize]...); err != nil {
  207. return err
  208. }
  209. allComments = allComments[commentBatchSize:]
  210. }
  211. }
  212. if len(allComments) > 0 {
  213. if err := uploader.CreateComments(allComments...); err != nil {
  214. return err
  215. }
  216. }
  217. if len(prs) < prBatchSize {
  218. break
  219. }
  220. }
  221. }
  222. return nil
  223. }