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.

downloader.go 4.8 kB

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