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