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

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