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.

codebase_test.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2021 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. "fmt"
  8. "net/url"
  9. "os"
  10. "testing"
  11. "time"
  12. base "code.gitea.io/gitea/modules/migration"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestCodebaseDownloadRepo(t *testing.T) {
  16. // Skip tests if Codebase token is not found
  17. cloneUser := os.Getenv("CODEBASE_CLONE_USER")
  18. clonePassword := os.Getenv("CODEBASE_CLONE_PASSWORD")
  19. apiUser := os.Getenv("CODEBASE_API_USER")
  20. apiPassword := os.Getenv("CODEBASE_API_TOKEN")
  21. if apiUser == "" || apiPassword == "" {
  22. t.Skip("skipped test because a CODEBASE_ variable was not in the environment")
  23. }
  24. cloneAddr := "https://gitea-test.codebasehq.com/gitea-test/test.git"
  25. u, _ := url.Parse(cloneAddr)
  26. if cloneUser != "" {
  27. u.User = url.UserPassword(cloneUser, clonePassword)
  28. }
  29. factory := &CodebaseDownloaderFactory{}
  30. downloader, err := factory.New(context.Background(), base.MigrateOptions{
  31. CloneAddr: u.String(),
  32. AuthUsername: apiUser,
  33. AuthPassword: apiPassword,
  34. })
  35. if err != nil {
  36. t.Fatal(fmt.Sprintf("Error creating Codebase downloader: %v", err))
  37. }
  38. repo, err := downloader.GetRepoInfo()
  39. assert.NoError(t, err)
  40. assertRepositoryEqual(t, &base.Repository{
  41. Name: "test",
  42. Owner: "",
  43. Description: "Repository Description",
  44. CloneURL: "git@codebasehq.com:gitea-test/gitea-test/test.git",
  45. OriginalURL: cloneAddr,
  46. }, repo)
  47. milestones, err := downloader.GetMilestones()
  48. assert.NoError(t, err)
  49. assertMilestonesEqual(t, []*base.Milestone{
  50. {
  51. Title: "Milestone1",
  52. Deadline: timePtr(time.Date(2021, time.September, 16, 0, 0, 0, 0, time.UTC)),
  53. },
  54. {
  55. Title: "Milestone2",
  56. Deadline: timePtr(time.Date(2021, time.September, 17, 0, 0, 0, 0, time.UTC)),
  57. Closed: timePtr(time.Date(2021, time.September, 17, 0, 0, 0, 0, time.UTC)),
  58. State: "closed",
  59. },
  60. }, milestones)
  61. labels, err := downloader.GetLabels()
  62. assert.NoError(t, err)
  63. assert.Len(t, labels, 4)
  64. issues, isEnd, err := downloader.GetIssues(1, 2)
  65. assert.NoError(t, err)
  66. assert.True(t, isEnd)
  67. assertIssuesEqual(t, []*base.Issue{
  68. {
  69. Number: 2,
  70. Title: "Open Ticket",
  71. Content: "Open Ticket Message",
  72. PosterName: "gitea-test-43",
  73. PosterEmail: "gitea-codebase@smack.email",
  74. State: "open",
  75. Created: time.Date(2021, time.September, 26, 19, 19, 14, 0, time.UTC),
  76. Updated: time.Date(2021, time.September, 26, 19, 19, 34, 0, time.UTC),
  77. Labels: []*base.Label{
  78. {
  79. Name: "Feature",
  80. },
  81. },
  82. },
  83. {
  84. Number: 1,
  85. Title: "Closed Ticket",
  86. Content: "Closed Ticket Message",
  87. PosterName: "gitea-test-43",
  88. PosterEmail: "gitea-codebase@smack.email",
  89. State: "closed",
  90. Milestone: "Milestone1",
  91. Created: time.Date(2021, time.September, 26, 19, 18, 33, 0, time.UTC),
  92. Updated: time.Date(2021, time.September, 26, 19, 18, 55, 0, time.UTC),
  93. Labels: []*base.Label{
  94. {
  95. Name: "Bug",
  96. },
  97. },
  98. },
  99. }, issues)
  100. comments, _, err := downloader.GetComments(base.GetCommentOptions{
  101. Context: issues[0].Context,
  102. })
  103. assert.NoError(t, err)
  104. assertCommentsEqual(t, []*base.Comment{
  105. {
  106. IssueIndex: 2,
  107. PosterName: "gitea-test-43",
  108. PosterEmail: "gitea-codebase@smack.email",
  109. Created: time.Date(2021, time.September, 26, 19, 19, 34, 0, time.UTC),
  110. Updated: time.Date(2021, time.September, 26, 19, 19, 34, 0, time.UTC),
  111. Content: "open comment",
  112. },
  113. }, comments)
  114. prs, _, err := downloader.GetPullRequests(1, 1)
  115. assert.NoError(t, err)
  116. assertPullRequestsEqual(t, []*base.PullRequest{
  117. {
  118. Number: 3,
  119. Title: "Readme Change",
  120. Content: "Merge Request comment",
  121. PosterName: "gitea-test-43",
  122. PosterEmail: "gitea-codebase@smack.email",
  123. State: "open",
  124. Created: time.Date(2021, time.September, 26, 20, 25, 47, 0, time.UTC),
  125. Updated: time.Date(2021, time.September, 26, 20, 25, 47, 0, time.UTC),
  126. Head: base.PullRequestBranch{
  127. Ref: "readme-mr",
  128. SHA: "1287f206b888d4d13540e0a8e1c07458f5420059",
  129. RepoName: "test",
  130. },
  131. Base: base.PullRequestBranch{
  132. Ref: "master",
  133. SHA: "f32b0a9dfd09a60f616f29158f772cedd89942d2",
  134. RepoName: "test",
  135. },
  136. },
  137. }, prs)
  138. rvs, err := downloader.GetReviews(prs[0].Context)
  139. assert.NoError(t, err)
  140. assert.Empty(t, rvs)
  141. }