gitea源码

github_test.go 13KB

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