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.

migrate.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.Wiki); 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. if err := uploader.CreateMilestones(milestones...); err != nil {
  81. return err
  82. }
  83. }
  84. if opts.Labels {
  85. log.Trace("migrating labels")
  86. labels, err := downloader.GetLabels()
  87. if err != nil {
  88. return err
  89. }
  90. if err := uploader.CreateLabels(labels...); err != nil {
  91. return err
  92. }
  93. }
  94. if opts.Releases {
  95. log.Trace("migrating releases")
  96. releases, err := downloader.GetReleases()
  97. if err != nil {
  98. return err
  99. }
  100. if err := uploader.CreateReleases(releases...); err != nil {
  101. return err
  102. }
  103. }
  104. if opts.Issues {
  105. log.Trace("migrating issues and comments")
  106. for i := 1; ; i++ {
  107. issues, isEnd, err := downloader.GetIssues(i, 100)
  108. if err != nil {
  109. return err
  110. }
  111. for _, issue := range issues {
  112. if !opts.IgnoreIssueAuthor {
  113. issue.Content = fmt.Sprintf("Author: @%s \n\n%s", issue.PosterName, issue.Content)
  114. }
  115. }
  116. if err := uploader.CreateIssues(issues...); err != nil {
  117. return err
  118. }
  119. if !opts.Comments {
  120. continue
  121. }
  122. var allComments = make([]*base.Comment, 0, 100)
  123. for _, issue := range issues {
  124. comments, err := downloader.GetComments(issue.Number)
  125. if err != nil {
  126. return err
  127. }
  128. for _, comment := range comments {
  129. if !opts.IgnoreIssueAuthor {
  130. comment.Content = fmt.Sprintf("Author: @%s \n\n%s", comment.PosterName, comment.Content)
  131. }
  132. }
  133. allComments = append(allComments, comments...)
  134. if len(allComments) >= 100 {
  135. if err := uploader.CreateComments(allComments...); err != nil {
  136. return err
  137. }
  138. allComments = make([]*base.Comment, 0, 100)
  139. }
  140. }
  141. if len(allComments) > 0 {
  142. if err := uploader.CreateComments(allComments...); err != nil {
  143. return err
  144. }
  145. }
  146. if isEnd {
  147. break
  148. }
  149. }
  150. }
  151. if opts.PullRequests {
  152. log.Trace("migrating pull requests and comments")
  153. for i := 1; ; i++ {
  154. prs, err := downloader.GetPullRequests(i, 100)
  155. if err != nil {
  156. return err
  157. }
  158. for _, pr := range prs {
  159. if !opts.IgnoreIssueAuthor {
  160. pr.Content = fmt.Sprintf("Author: @%s \n\n%s", pr.PosterName, pr.Content)
  161. }
  162. }
  163. if err := uploader.CreatePullRequests(prs...); err != nil {
  164. return err
  165. }
  166. if !opts.Comments {
  167. continue
  168. }
  169. var allComments = make([]*base.Comment, 0, 100)
  170. for _, pr := range prs {
  171. comments, err := downloader.GetComments(pr.Number)
  172. if err != nil {
  173. return err
  174. }
  175. for _, comment := range comments {
  176. if !opts.IgnoreIssueAuthor {
  177. comment.Content = fmt.Sprintf("Author: @%s \n\n%s", comment.PosterName, comment.Content)
  178. }
  179. }
  180. allComments = append(allComments, comments...)
  181. if len(allComments) >= 100 {
  182. if err := uploader.CreateComments(allComments...); err != nil {
  183. return err
  184. }
  185. allComments = make([]*base.Comment, 0, 100)
  186. }
  187. }
  188. if len(allComments) > 0 {
  189. if err := uploader.CreateComments(allComments...); err != nil {
  190. return err
  191. }
  192. }
  193. if len(prs) < 100 {
  194. break
  195. }
  196. }
  197. }
  198. return nil
  199. }