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.

gitea_downloader_test.go 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "context"
  7. "net/http"
  8. "os"
  9. "sort"
  10. "testing"
  11. "time"
  12. "code.gitea.io/gitea/modules/migrations/base"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestGiteaDownloadRepo(t *testing.T) {
  16. // Skip tests if Gitea token is not found
  17. giteaToken := os.Getenv("GITEA_TOKEN")
  18. if giteaToken == "" {
  19. t.Skip("skipped test because GITEA_TOKEN was not in the environment")
  20. }
  21. resp, err := http.Get("https://gitea.com/gitea")
  22. if err != nil || resp.StatusCode != 200 {
  23. t.Skipf("Can't reach https://gitea.com, skipping %s", t.Name())
  24. }
  25. downloader, err := NewGiteaDownloader(context.Background(), "https://gitea.com", "gitea/test_repo", "", "", giteaToken)
  26. if downloader == nil {
  27. t.Fatal("NewGitlabDownloader is nil")
  28. }
  29. if !assert.NoError(t, err) {
  30. t.Fatal("NewGitlabDownloader error occur")
  31. }
  32. repo, err := downloader.GetRepoInfo()
  33. assert.NoError(t, err)
  34. assertRepositoryEqual(t, &base.Repository{
  35. Name: "test_repo",
  36. Owner: "gitea",
  37. IsPrivate: false,
  38. Description: "Test repository for testing migration from gitea to gitea",
  39. CloneURL: "https://gitea.com/gitea/test_repo.git",
  40. OriginalURL: "https://gitea.com/gitea/test_repo",
  41. DefaultBranch: "master",
  42. }, repo)
  43. topics, err := downloader.GetTopics()
  44. assert.NoError(t, err)
  45. sort.Strings(topics)
  46. assert.EqualValues(t, []string{"ci", "gitea", "migration", "test"}, topics)
  47. labels, err := downloader.GetLabels()
  48. assert.NoError(t, err)
  49. assertLabelsEqual(t, []*base.Label{
  50. {
  51. Name: "Bug",
  52. Color: "e11d21",
  53. },
  54. {
  55. Name: "Enhancement",
  56. Color: "207de5",
  57. },
  58. {
  59. Name: "Feature",
  60. Color: "0052cc",
  61. Description: "a feature request",
  62. },
  63. {
  64. Name: "Invalid",
  65. Color: "d4c5f9",
  66. },
  67. {
  68. Name: "Question",
  69. Color: "fbca04",
  70. },
  71. {
  72. Name: "Valid",
  73. Color: "53e917",
  74. },
  75. }, labels)
  76. milestones, err := downloader.GetMilestones()
  77. assert.NoError(t, err)
  78. assertMilestonesEqual(t, []*base.Milestone{
  79. {
  80. Title: "V2 Finalize",
  81. Created: time.Unix(0, 0),
  82. Deadline: timePtr(time.Unix(1599263999, 0)),
  83. Updated: timePtr(time.Unix(0, 0)),
  84. State: "open",
  85. },
  86. {
  87. Title: "V1",
  88. Description: "Generate Content",
  89. Created: time.Unix(0, 0),
  90. Updated: timePtr(time.Unix(0, 0)),
  91. Closed: timePtr(time.Unix(1598985406, 0)),
  92. State: "closed",
  93. },
  94. }, milestones)
  95. releases, err := downloader.GetReleases()
  96. assert.NoError(t, err)
  97. assertReleasesEqual(t, []*base.Release{
  98. {
  99. Name: "Second Release",
  100. TagName: "v2-rc1",
  101. TargetCommitish: "master",
  102. Body: "this repo has:\r\n* reactions\r\n* wiki\r\n* issues (open/closed)\r\n* pulls (open/closed/merged) (external/internal)\r\n* pull reviews\r\n* projects\r\n* milestones\r\n* labels\r\n* releases\r\n\r\nto test migration against",
  103. Draft: false,
  104. Prerelease: true,
  105. Created: time.Date(2020, 9, 1, 18, 2, 43, 0, time.UTC),
  106. Published: time.Date(2020, 9, 1, 18, 2, 43, 0, time.UTC),
  107. PublisherID: 689,
  108. PublisherName: "6543",
  109. PublisherEmail: "6543@obermui.de",
  110. },
  111. {
  112. Name: "First Release",
  113. TagName: "V1",
  114. TargetCommitish: "master",
  115. Body: "as title",
  116. Draft: false,
  117. Prerelease: false,
  118. Created: time.Date(2020, 9, 1, 17, 30, 32, 0, time.UTC),
  119. Published: time.Date(2020, 9, 1, 17, 30, 32, 0, time.UTC),
  120. PublisherID: 689,
  121. PublisherName: "6543",
  122. PublisherEmail: "6543@obermui.de",
  123. },
  124. }, releases)
  125. issues, isEnd, err := downloader.GetIssues(1, 50)
  126. assert.NoError(t, err)
  127. assert.True(t, isEnd)
  128. assert.Len(t, issues, 7)
  129. assert.EqualValues(t, "open", issues[0].State)
  130. issues, isEnd, err = downloader.GetIssues(3, 2)
  131. assert.NoError(t, err)
  132. assert.False(t, isEnd)
  133. assertIssuesEqual(t, []*base.Issue{
  134. {
  135. Number: 4,
  136. Title: "what is this repo about?",
  137. Content: "",
  138. Milestone: "V1",
  139. PosterID: -1,
  140. PosterName: "Ghost",
  141. PosterEmail: "",
  142. State: "closed",
  143. IsLocked: true,
  144. Created: time.Unix(1598975321, 0),
  145. Updated: time.Unix(1598975400, 0),
  146. Labels: []*base.Label{{
  147. Name: "Question",
  148. Color: "fbca04",
  149. Description: "",
  150. }},
  151. Reactions: []*base.Reaction{
  152. {
  153. UserID: 689,
  154. UserName: "6543",
  155. Content: "gitea",
  156. },
  157. {
  158. UserID: 689,
  159. UserName: "6543",
  160. Content: "laugh",
  161. },
  162. },
  163. Closed: timePtr(time.Date(2020, 9, 1, 15, 49, 34, 0, time.UTC)),
  164. },
  165. {
  166. Number: 2,
  167. Title: "Spam",
  168. Content: ":(",
  169. Milestone: "",
  170. PosterID: 689,
  171. PosterName: "6543",
  172. PosterEmail: "6543@obermui.de",
  173. State: "closed",
  174. IsLocked: false,
  175. Created: time.Unix(1598919780, 0),
  176. Updated: time.Unix(1598969497, 0),
  177. Labels: []*base.Label{{
  178. Name: "Invalid",
  179. Color: "d4c5f9",
  180. Description: "",
  181. }},
  182. Closed: timePtr(time.Unix(1598969497, 0)),
  183. },
  184. }, issues)
  185. comments, _, err := downloader.GetComments(base.GetCommentOptions{
  186. Context: base.BasicIssueContext(4),
  187. })
  188. assert.NoError(t, err)
  189. assertCommentsEqual(t, []*base.Comment{
  190. {
  191. IssueIndex: 4,
  192. PosterID: 689,
  193. PosterName: "6543",
  194. PosterEmail: "6543@obermui.de",
  195. Created: time.Unix(1598975370, 0),
  196. Updated: time.Unix(1599070865, 0),
  197. Content: "a really good question!\n\nIt is the used as TESTSET for gitea2gitea repo migration function",
  198. },
  199. {
  200. IssueIndex: 4,
  201. PosterID: -1,
  202. PosterName: "Ghost",
  203. PosterEmail: "",
  204. Created: time.Unix(1598975393, 0),
  205. Updated: time.Unix(1598975393, 0),
  206. Content: "Oh!",
  207. },
  208. }, comments)
  209. prs, isEnd, err := downloader.GetPullRequests(1, 50)
  210. assert.NoError(t, err)
  211. assert.True(t, isEnd)
  212. assert.Len(t, prs, 6)
  213. prs, isEnd, err = downloader.GetPullRequests(1, 3)
  214. assert.NoError(t, err)
  215. assert.False(t, isEnd)
  216. assert.Len(t, prs, 3)
  217. assertPullRequestEqual(t, &base.PullRequest{
  218. Number: 12,
  219. PosterID: 689,
  220. PosterName: "6543",
  221. PosterEmail: "6543@obermui.de",
  222. Title: "Dont Touch",
  223. Content: "\r\nadd dont touch note",
  224. Milestone: "V2 Finalize",
  225. State: "closed",
  226. IsLocked: false,
  227. Created: time.Unix(1598982759, 0),
  228. Updated: time.Unix(1599023425, 0),
  229. Closed: timePtr(time.Unix(1598982934, 0)),
  230. Assignees: []string{"techknowlogick"},
  231. Base: base.PullRequestBranch{
  232. CloneURL: "",
  233. Ref: "master",
  234. SHA: "827aa28a907853e5ddfa40c8f9bc52471a2685fd",
  235. RepoName: "test_repo",
  236. OwnerName: "gitea",
  237. },
  238. Head: base.PullRequestBranch{
  239. CloneURL: "https://gitea.com/6543-forks/test_repo.git",
  240. Ref: "refs/pull/12/head",
  241. SHA: "b6ab5d9ae000b579a5fff03f92c486da4ddf48b6",
  242. RepoName: "test_repo",
  243. OwnerName: "6543-forks",
  244. },
  245. Merged: true,
  246. MergedTime: timePtr(time.Unix(1598982934, 0)),
  247. MergeCommitSHA: "827aa28a907853e5ddfa40c8f9bc52471a2685fd",
  248. PatchURL: "https://gitea.com/gitea/test_repo/pulls/12.patch",
  249. }, prs[1])
  250. reviews, err := downloader.GetReviews(base.BasicIssueContext(7))
  251. assert.NoError(t, err)
  252. assertReviewsEqual(t, []*base.Review{
  253. {
  254. ID: 1770,
  255. IssueIndex: 7,
  256. ReviewerID: 689,
  257. ReviewerName: "6543",
  258. CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03",
  259. CreatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
  260. State: "COMMENT", // TODO
  261. Comments: []*base.ReviewComment{
  262. {
  263. ID: 116561,
  264. InReplyTo: 0,
  265. Content: "is one `\\newline` to less?",
  266. TreePath: "README.md",
  267. DiffHunk: "@@ -2,3 +2,3 @@\n \n-Test repository for testing migration from gitea 2 gitea\n\\ No newline at end of file\n+Test repository for testing migration from gitea 2 gitea",
  268. Position: 0,
  269. Line: 4,
  270. CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03",
  271. PosterID: 689,
  272. Reactions: nil,
  273. CreatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
  274. UpdatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
  275. },
  276. },
  277. },
  278. {
  279. ID: 1771,
  280. IssueIndex: 7,
  281. ReviewerID: 9,
  282. ReviewerName: "techknowlogick",
  283. CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03",
  284. CreatedAt: time.Date(2020, 9, 1, 17, 6, 47, 0, time.UTC),
  285. State: "REQUEST_CHANGES", // TODO
  286. Content: "I think this needs some changes",
  287. },
  288. {
  289. ID: 1772,
  290. IssueIndex: 7,
  291. ReviewerID: 9,
  292. ReviewerName: "techknowlogick",
  293. CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03",
  294. CreatedAt: time.Date(2020, 9, 1, 17, 19, 51, 0, time.UTC),
  295. State: base.ReviewStateApproved,
  296. Official: true,
  297. Content: "looks good",
  298. },
  299. }, reviews)
  300. }