A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

457 lines
13 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 migrations
  6. import (
  7. "context"
  8. "os"
  9. "testing"
  10. "time"
  11. "code.gitea.io/gitea/modules/migrations/base"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func assertMilestoneEqual(t *testing.T, description, title, dueOn, created, updated, closed, state string, ms *base.Milestone) {
  15. var tmPtr *time.Time
  16. if dueOn != "" {
  17. tm, err := time.Parse("2006-01-02 15:04:05 -0700 MST", dueOn)
  18. assert.NoError(t, err)
  19. tmPtr = &tm
  20. }
  21. var (
  22. createdTM time.Time
  23. updatedTM *time.Time
  24. closedTM *time.Time
  25. )
  26. if created != "" {
  27. var err error
  28. createdTM, err = time.Parse("2006-01-02 15:04:05 -0700 MST", created)
  29. assert.NoError(t, err)
  30. }
  31. if updated != "" {
  32. updatedTemp, err := time.Parse("2006-01-02 15:04:05 -0700 MST", updated)
  33. assert.NoError(t, err)
  34. updatedTM = &updatedTemp
  35. }
  36. if closed != "" {
  37. closedTemp, err := time.Parse("2006-01-02 15:04:05 -0700 MST", closed)
  38. assert.NoError(t, err)
  39. closedTM = &closedTemp
  40. }
  41. assert.EqualValues(t, &base.Milestone{
  42. Description: description,
  43. Title: title,
  44. Deadline: tmPtr,
  45. State: state,
  46. Created: createdTM,
  47. Updated: updatedTM,
  48. Closed: closedTM,
  49. }, ms)
  50. }
  51. func assertLabelEqual(t *testing.T, name, color, description string, label *base.Label) {
  52. assert.EqualValues(t, &base.Label{
  53. Name: name,
  54. Color: color,
  55. Description: description,
  56. }, label)
  57. }
  58. func TestGitHubDownloadRepo(t *testing.T) {
  59. GithubLimitRateRemaining = 3 //Wait at 3 remaining since we could have 3 CI in //
  60. downloader := NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", os.Getenv("GITHUB_READ_TOKEN"), "go-gitea", "test_repo")
  61. err := downloader.RefreshRate()
  62. assert.NoError(t, err)
  63. repo, err := downloader.GetRepoInfo()
  64. assert.NoError(t, err)
  65. assert.EqualValues(t, &base.Repository{
  66. Name: "test_repo",
  67. Owner: "go-gitea",
  68. Description: "Test repository for testing migration from github to gitea",
  69. CloneURL: "https://github.com/go-gitea/test_repo.git",
  70. OriginalURL: "https://github.com/go-gitea/test_repo",
  71. DefaultBranch: "master",
  72. }, repo)
  73. topics, err := downloader.GetTopics()
  74. assert.NoError(t, err)
  75. assert.Contains(t, topics, "gitea")
  76. milestones, err := downloader.GetMilestones()
  77. assert.NoError(t, err)
  78. assert.True(t, len(milestones) >= 2)
  79. for _, milestone := range milestones {
  80. switch milestone.Title {
  81. case "1.0.0":
  82. assertMilestoneEqual(t, "Milestone 1.0.0", "1.0.0", "2019-11-11 08:00:00 +0000 UTC",
  83. "2019-11-12 19:37:08 +0000 UTC",
  84. "2019-11-12 21:56:17 +0000 UTC",
  85. "2019-11-12 19:45:49 +0000 UTC",
  86. "closed", milestone)
  87. case "1.1.0":
  88. assertMilestoneEqual(t, "Milestone 1.1.0", "1.1.0", "2019-11-12 08:00:00 +0000 UTC",
  89. "2019-11-12 19:37:25 +0000 UTC",
  90. "2019-11-12 21:39:27 +0000 UTC",
  91. "2019-11-12 19:45:46 +0000 UTC",
  92. "closed", milestone)
  93. }
  94. }
  95. labels, err := downloader.GetLabels()
  96. assert.NoError(t, err)
  97. assert.True(t, len(labels) >= 8)
  98. for _, l := range labels {
  99. switch l.Name {
  100. case "bug":
  101. assertLabelEqual(t, "bug", "d73a4a", "Something isn't working", l)
  102. case "documentation":
  103. assertLabelEqual(t, "documentation", "0075ca", "Improvements or additions to documentation", l)
  104. case "duplicate":
  105. assertLabelEqual(t, "duplicate", "cfd3d7", "This issue or pull request already exists", l)
  106. case "enhancement":
  107. assertLabelEqual(t, "enhancement", "a2eeef", "New feature or request", l)
  108. case "good first issue":
  109. assertLabelEqual(t, "good first issue", "7057ff", "Good for newcomers", l)
  110. case "help wanted":
  111. assertLabelEqual(t, "help wanted", "008672", "Extra attention is needed", l)
  112. case "invalid":
  113. assertLabelEqual(t, "invalid", "e4e669", "This doesn't seem right", l)
  114. case "question":
  115. assertLabelEqual(t, "question", "d876e3", "Further information is requested", l)
  116. }
  117. }
  118. releases, err := downloader.GetReleases()
  119. assert.NoError(t, err)
  120. assert.EqualValues(t, []*base.Release{
  121. {
  122. TagName: "v0.9.99",
  123. TargetCommitish: "master",
  124. Name: "First Release",
  125. Body: "A test release",
  126. Created: time.Date(2019, 11, 9, 16, 49, 21, 0, time.UTC),
  127. Published: time.Date(2019, 11, 12, 20, 12, 10, 0, time.UTC),
  128. PublisherID: 1669571,
  129. PublisherName: "mrsdizzie",
  130. },
  131. }, releases[len(releases)-1:])
  132. // downloader.GetIssues()
  133. issues, isEnd, err := downloader.GetIssues(1, 2)
  134. assert.NoError(t, err)
  135. assert.EqualValues(t, 2, len(issues))
  136. assert.False(t, isEnd)
  137. var (
  138. closed1 = time.Date(2019, 11, 12, 20, 22, 22, 0, time.UTC)
  139. closed2 = time.Date(2019, 11, 12, 21, 1, 31, 0, time.UTC)
  140. )
  141. assert.EqualValues(t, []*base.Issue{
  142. {
  143. Number: 1,
  144. Title: "Please add an animated gif icon to the merge button",
  145. Content: "I just want the merge button to hurt my eyes a little. \xF0\x9F\x98\x9D ",
  146. Milestone: "1.0.0",
  147. PosterID: 18600385,
  148. PosterName: "guillep2k",
  149. State: "closed",
  150. Created: time.Date(2019, 11, 9, 17, 0, 29, 0, time.UTC),
  151. Updated: time.Date(2019, 11, 12, 20, 29, 53, 0, time.UTC),
  152. Labels: []*base.Label{
  153. {
  154. Name: "bug",
  155. Color: "d73a4a",
  156. Description: "Something isn't working",
  157. },
  158. {
  159. Name: "good first issue",
  160. Color: "7057ff",
  161. Description: "Good for newcomers",
  162. },
  163. },
  164. Reactions: []*base.Reaction{
  165. {
  166. UserID: 1669571,
  167. UserName: "mrsdizzie",
  168. Content: "+1",
  169. },
  170. },
  171. Closed: &closed1,
  172. },
  173. {
  174. Number: 2,
  175. Title: "Test issue",
  176. Content: "This is test issue 2, do not touch!",
  177. Milestone: "1.1.0",
  178. PosterID: 1669571,
  179. PosterName: "mrsdizzie",
  180. State: "closed",
  181. Created: time.Date(2019, 11, 12, 21, 0, 6, 0, time.UTC),
  182. Updated: time.Date(2019, 11, 12, 22, 7, 14, 0, time.UTC),
  183. Labels: []*base.Label{
  184. {
  185. Name: "duplicate",
  186. Color: "cfd3d7",
  187. Description: "This issue or pull request already exists",
  188. },
  189. },
  190. Reactions: []*base.Reaction{
  191. {
  192. UserID: 1669571,
  193. UserName: "mrsdizzie",
  194. Content: "heart",
  195. },
  196. {
  197. UserID: 1669571,
  198. UserName: "mrsdizzie",
  199. Content: "laugh",
  200. },
  201. {
  202. UserID: 1669571,
  203. UserName: "mrsdizzie",
  204. Content: "-1",
  205. },
  206. {
  207. UserID: 1669571,
  208. UserName: "mrsdizzie",
  209. Content: "confused",
  210. },
  211. {
  212. UserID: 1669571,
  213. UserName: "mrsdizzie",
  214. Content: "hooray",
  215. },
  216. {
  217. UserID: 1669571,
  218. UserName: "mrsdizzie",
  219. Content: "+1",
  220. },
  221. },
  222. Closed: &closed2,
  223. },
  224. }, issues)
  225. // downloader.GetComments()
  226. comments, err := downloader.GetComments(2)
  227. assert.NoError(t, err)
  228. assert.EqualValues(t, 2, len(comments))
  229. assert.EqualValues(t, []*base.Comment{
  230. {
  231. IssueIndex: 2,
  232. PosterID: 1669571,
  233. PosterName: "mrsdizzie",
  234. Created: time.Date(2019, 11, 12, 21, 0, 13, 0, time.UTC),
  235. Updated: time.Date(2019, 11, 12, 21, 0, 13, 0, time.UTC),
  236. Content: "This is a comment",
  237. Reactions: []*base.Reaction{
  238. {
  239. UserID: 1669571,
  240. UserName: "mrsdizzie",
  241. Content: "+1",
  242. },
  243. },
  244. },
  245. {
  246. IssueIndex: 2,
  247. PosterID: 1669571,
  248. PosterName: "mrsdizzie",
  249. Created: time.Date(2019, 11, 12, 22, 7, 14, 0, time.UTC),
  250. Updated: time.Date(2019, 11, 12, 22, 7, 14, 0, time.UTC),
  251. Content: "A second comment",
  252. Reactions: nil,
  253. },
  254. }, comments[:2])
  255. // downloader.GetPullRequests()
  256. prs, _, err := downloader.GetPullRequests(1, 2)
  257. assert.NoError(t, err)
  258. assert.EqualValues(t, 2, len(prs))
  259. closed1 = time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)
  260. var merged1 = time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)
  261. assert.EqualValues(t, []*base.PullRequest{
  262. {
  263. Number: 3,
  264. Title: "Update README.md",
  265. Content: "add warning to readme",
  266. Milestone: "1.1.0",
  267. PosterID: 1669571,
  268. PosterName: "mrsdizzie",
  269. State: "closed",
  270. Created: time.Date(2019, 11, 12, 21, 21, 43, 0, time.UTC),
  271. Updated: time.Date(2019, 11, 12, 21, 39, 28, 0, time.UTC),
  272. Labels: []*base.Label{
  273. {
  274. Name: "documentation",
  275. Color: "0075ca",
  276. Description: "Improvements or additions to documentation",
  277. },
  278. },
  279. PatchURL: "https://github.com/go-gitea/test_repo/pull/3.patch",
  280. Head: base.PullRequestBranch{
  281. Ref: "master",
  282. CloneURL: "https://github.com/mrsdizzie/test_repo.git",
  283. SHA: "076160cf0b039f13e5eff19619932d181269414b",
  284. RepoName: "test_repo",
  285. OwnerName: "mrsdizzie",
  286. },
  287. Base: base.PullRequestBranch{
  288. Ref: "master",
  289. SHA: "72866af952e98d02a73003501836074b286a78f6",
  290. OwnerName: "go-gitea",
  291. RepoName: "test_repo",
  292. },
  293. Closed: &closed1,
  294. Merged: true,
  295. MergedTime: &merged1,
  296. MergeCommitSHA: "f32b0a9dfd09a60f616f29158f772cedd89942d2",
  297. },
  298. {
  299. Number: 4,
  300. Title: "Test branch",
  301. Content: "do not merge this PR",
  302. Milestone: "1.0.0",
  303. PosterID: 1669571,
  304. PosterName: "mrsdizzie",
  305. State: "open",
  306. Created: time.Date(2019, 11, 12, 21, 54, 18, 0, time.UTC),
  307. Updated: time.Date(2020, 1, 4, 11, 30, 1, 0, time.UTC),
  308. Labels: []*base.Label{
  309. {
  310. Name: "bug",
  311. Color: "d73a4a",
  312. Description: "Something isn't working",
  313. },
  314. },
  315. PatchURL: "https://github.com/go-gitea/test_repo/pull/4.patch",
  316. Head: base.PullRequestBranch{
  317. Ref: "test-branch",
  318. SHA: "2be9101c543658591222acbee3eb799edfc3853d",
  319. RepoName: "test_repo",
  320. OwnerName: "mrsdizzie",
  321. CloneURL: "https://github.com/mrsdizzie/test_repo.git",
  322. },
  323. Base: base.PullRequestBranch{
  324. Ref: "master",
  325. SHA: "f32b0a9dfd09a60f616f29158f772cedd89942d2",
  326. OwnerName: "go-gitea",
  327. RepoName: "test_repo",
  328. },
  329. Merged: false,
  330. MergeCommitSHA: "565d1208f5fffdc1c5ae1a2436491eb9a5e4ebae",
  331. Reactions: []*base.Reaction{
  332. {
  333. UserID: 81045,
  334. UserName: "lunny",
  335. Content: "heart",
  336. },
  337. {
  338. UserID: 81045,
  339. UserName: "lunny",
  340. Content: "+1",
  341. },
  342. },
  343. },
  344. }, prs)
  345. reviews, err := downloader.GetReviews(3)
  346. assert.NoError(t, err)
  347. assert.EqualValues(t, []*base.Review{
  348. {
  349. ID: 315859956,
  350. IssueIndex: 3,
  351. ReviewerID: 42128690,
  352. ReviewerName: "jolheiser",
  353. CommitID: "076160cf0b039f13e5eff19619932d181269414b",
  354. CreatedAt: time.Date(2019, 11, 12, 21, 35, 24, 0, time.UTC),
  355. State: base.ReviewStateApproved,
  356. },
  357. {
  358. ID: 315860062,
  359. IssueIndex: 3,
  360. ReviewerID: 1824502,
  361. ReviewerName: "zeripath",
  362. CommitID: "076160cf0b039f13e5eff19619932d181269414b",
  363. CreatedAt: time.Date(2019, 11, 12, 21, 35, 36, 0, time.UTC),
  364. State: base.ReviewStateApproved,
  365. },
  366. {
  367. ID: 315861440,
  368. IssueIndex: 3,
  369. ReviewerID: 165205,
  370. ReviewerName: "lafriks",
  371. CommitID: "076160cf0b039f13e5eff19619932d181269414b",
  372. CreatedAt: time.Date(2019, 11, 12, 21, 38, 00, 0, time.UTC),
  373. State: base.ReviewStateApproved,
  374. },
  375. }, reviews)
  376. reviews, err = downloader.GetReviews(4)
  377. assert.NoError(t, err)
  378. assert.EqualValues(t, []*base.Review{
  379. {
  380. ID: 338338740,
  381. IssueIndex: 4,
  382. ReviewerID: 81045,
  383. ReviewerName: "lunny",
  384. CommitID: "2be9101c543658591222acbee3eb799edfc3853d",
  385. CreatedAt: time.Date(2020, 01, 04, 05, 33, 18, 0, time.UTC),
  386. State: base.ReviewStateApproved,
  387. Comments: []*base.ReviewComment{
  388. {
  389. ID: 363017488,
  390. Content: "This is a good pull request.",
  391. TreePath: "README.md",
  392. DiffHunk: "@@ -1,2 +1,4 @@\n # test_repo\n Test repository for testing migration from github to gitea\n+",
  393. Position: 3,
  394. CommitID: "2be9101c543658591222acbee3eb799edfc3853d",
  395. PosterID: 81045,
  396. CreatedAt: time.Date(2020, 01, 04, 05, 33, 06, 0, time.UTC),
  397. UpdatedAt: time.Date(2020, 01, 04, 05, 33, 18, 0, time.UTC),
  398. },
  399. },
  400. },
  401. {
  402. ID: 338339651,
  403. IssueIndex: 4,
  404. ReviewerID: 81045,
  405. ReviewerName: "lunny",
  406. CommitID: "2be9101c543658591222acbee3eb799edfc3853d",
  407. CreatedAt: time.Date(2020, 01, 04, 06, 07, 06, 0, time.UTC),
  408. State: base.ReviewStateChangesRequested,
  409. Content: "Don't add more reviews",
  410. },
  411. {
  412. ID: 338349019,
  413. IssueIndex: 4,
  414. ReviewerID: 81045,
  415. ReviewerName: "lunny",
  416. CommitID: "2be9101c543658591222acbee3eb799edfc3853d",
  417. CreatedAt: time.Date(2020, 01, 04, 11, 21, 41, 0, time.UTC),
  418. State: base.ReviewStateCommented,
  419. Comments: []*base.ReviewComment{
  420. {
  421. ID: 363029944,
  422. Content: "test a single comment.",
  423. TreePath: "LICENSE",
  424. DiffHunk: "@@ -19,3 +19,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n+",
  425. Position: 4,
  426. CommitID: "2be9101c543658591222acbee3eb799edfc3853d",
  427. PosterID: 81045,
  428. CreatedAt: time.Date(2020, 01, 04, 11, 21, 41, 0, time.UTC),
  429. UpdatedAt: time.Date(2020, 01, 04, 11, 21, 41, 0, time.UTC),
  430. },
  431. },
  432. },
  433. }, reviews)
  434. }