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.

204 lines
5.2 KiB

  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 base
  6. import (
  7. "context"
  8. "time"
  9. "code.gitea.io/gitea/modules/structs"
  10. )
  11. // Downloader downloads the site repo informations
  12. type Downloader interface {
  13. SetContext(context.Context)
  14. GetRepoInfo() (*Repository, error)
  15. GetTopics() ([]string, error)
  16. GetMilestones() ([]*Milestone, error)
  17. GetReleases() ([]*Release, error)
  18. GetLabels() ([]*Label, error)
  19. GetIssues(page, perPage int) ([]*Issue, bool, error)
  20. GetComments(issueNumber int64) ([]*Comment, error)
  21. GetPullRequests(page, perPage int) ([]*PullRequest, error)
  22. GetReviews(pullRequestNumber int64) ([]*Review, error)
  23. }
  24. // DownloaderFactory defines an interface to match a downloader implementation and create a downloader
  25. type DownloaderFactory interface {
  26. Match(opts MigrateOptions) (bool, error)
  27. New(opts MigrateOptions) (Downloader, error)
  28. GitServiceType() structs.GitServiceType
  29. }
  30. var (
  31. _ Downloader = &RetryDownloader{}
  32. )
  33. // RetryDownloader retry the downloads
  34. type RetryDownloader struct {
  35. Downloader
  36. RetryTimes int // the total execute times
  37. RetryDelay int // time to delay seconds
  38. }
  39. // NewRetryDownloader creates a retry downloader
  40. func NewRetryDownloader(downloader Downloader, retryTimes, retryDelay int) *RetryDownloader {
  41. return &RetryDownloader{
  42. Downloader: downloader,
  43. RetryTimes: retryTimes,
  44. RetryDelay: retryDelay,
  45. }
  46. }
  47. // SetContext set context
  48. func (d *RetryDownloader) SetContext(ctx context.Context) {
  49. d.Downloader.SetContext(ctx)
  50. }
  51. // GetRepoInfo returns a repository information with retry
  52. func (d *RetryDownloader) GetRepoInfo() (*Repository, error) {
  53. var (
  54. times = d.RetryTimes
  55. repo *Repository
  56. err error
  57. )
  58. for ; times > 0; times-- {
  59. if repo, err = d.Downloader.GetRepoInfo(); err == nil {
  60. return repo, nil
  61. }
  62. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  63. }
  64. return nil, err
  65. }
  66. // GetTopics returns a repository's topics with retry
  67. func (d *RetryDownloader) GetTopics() ([]string, error) {
  68. var (
  69. times = d.RetryTimes
  70. topics []string
  71. err error
  72. )
  73. for ; times > 0; times-- {
  74. if topics, err = d.Downloader.GetTopics(); err == nil {
  75. return topics, nil
  76. }
  77. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  78. }
  79. return nil, err
  80. }
  81. // GetMilestones returns a repository's milestones with retry
  82. func (d *RetryDownloader) GetMilestones() ([]*Milestone, error) {
  83. var (
  84. times = d.RetryTimes
  85. milestones []*Milestone
  86. err error
  87. )
  88. for ; times > 0; times-- {
  89. if milestones, err = d.Downloader.GetMilestones(); err == nil {
  90. return milestones, nil
  91. }
  92. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  93. }
  94. return nil, err
  95. }
  96. // GetReleases returns a repository's releases with retry
  97. func (d *RetryDownloader) GetReleases() ([]*Release, error) {
  98. var (
  99. times = d.RetryTimes
  100. releases []*Release
  101. err error
  102. )
  103. for ; times > 0; times-- {
  104. if releases, err = d.Downloader.GetReleases(); err == nil {
  105. return releases, nil
  106. }
  107. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  108. }
  109. return nil, err
  110. }
  111. // GetLabels returns a repository's labels with retry
  112. func (d *RetryDownloader) GetLabels() ([]*Label, error) {
  113. var (
  114. times = d.RetryTimes
  115. labels []*Label
  116. err error
  117. )
  118. for ; times > 0; times-- {
  119. if labels, err = d.Downloader.GetLabels(); err == nil {
  120. return labels, nil
  121. }
  122. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  123. }
  124. return nil, err
  125. }
  126. // GetIssues returns a repository's issues with retry
  127. func (d *RetryDownloader) GetIssues(page, perPage int) ([]*Issue, bool, error) {
  128. var (
  129. times = d.RetryTimes
  130. issues []*Issue
  131. isEnd bool
  132. err error
  133. )
  134. for ; times > 0; times-- {
  135. if issues, isEnd, err = d.Downloader.GetIssues(page, perPage); err == nil {
  136. return issues, isEnd, nil
  137. }
  138. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  139. }
  140. return nil, false, err
  141. }
  142. // GetComments returns a repository's comments with retry
  143. func (d *RetryDownloader) GetComments(issueNumber int64) ([]*Comment, error) {
  144. var (
  145. times = d.RetryTimes
  146. comments []*Comment
  147. err error
  148. )
  149. for ; times > 0; times-- {
  150. if comments, err = d.Downloader.GetComments(issueNumber); err == nil {
  151. return comments, nil
  152. }
  153. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  154. }
  155. return nil, err
  156. }
  157. // GetPullRequests returns a repository's pull requests with retry
  158. func (d *RetryDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, error) {
  159. var (
  160. times = d.RetryTimes
  161. prs []*PullRequest
  162. err error
  163. )
  164. for ; times > 0; times-- {
  165. if prs, err = d.Downloader.GetPullRequests(page, perPage); err == nil {
  166. return prs, nil
  167. }
  168. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  169. }
  170. return nil, err
  171. }
  172. // GetReviews returns pull requests reviews
  173. func (d *RetryDownloader) GetReviews(pullRequestNumber int64) ([]*Review, error) {
  174. var (
  175. times = d.RetryTimes
  176. reviews []*Review
  177. err error
  178. )
  179. for ; times > 0; times-- {
  180. if reviews, err = d.Downloader.GetReviews(pullRequestNumber); err == nil {
  181. return reviews, nil
  182. }
  183. time.Sleep(time.Second * time.Duration(d.RetryDelay))
  184. }
  185. return nil, err
  186. }