github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/services/pull_request/gitea_test.go (about) 1 package pull_request 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 "code.gitea.io/sdk/gitea" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func giteaMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) { 16 return func(w http.ResponseWriter, r *http.Request) { 17 w.Header().Set("Content-Type", "application/json") 18 fmt.Println(r.RequestURI) 19 switch r.RequestURI { 20 case "/api/v1/version": 21 _, err := io.WriteString(w, `{"version":"1.17.0+dev-452-g1f0541780"}`) 22 if err != nil { 23 t.Fail() 24 } 25 case "/api/v1/repos/test-argocd/pr-test/pulls?limit=0&page=1&state=open": 26 _, err := io.WriteString(w, `[{ 27 "id": 50721, 28 "url": "https://gitea.com/test-argocd/pr-test/pulls/1", 29 "number": 1, 30 "user": { 31 "id": 4476, 32 "login": "graytshirt", 33 "full_name": "Dan", 34 "email": "graytshirt@noreply.gitea.io", 35 "avatar_url": "https://secure.gravatar.com/avatar/2446c67bcd59d71f6ae3cf376ec2ae37?d=identicon", 36 "language": "", 37 "is_admin": false, 38 "last_login": "0001-01-01T00:00:00Z", 39 "created": "2020-04-07T01:14:36+08:00", 40 "restricted": false, 41 "active": false, 42 "prohibit_login": false, 43 "location": "", 44 "website": "", 45 "description": "", 46 "visibility": "public", 47 "followers_count": 0, 48 "following_count": 4, 49 "starred_repos_count": 1, 50 "username": "graytshirt" 51 }, 52 "title": "add an empty file", 53 "body": "", 54 "labels": [], 55 "milestone": null, 56 "assignee": null, 57 "assignees": null, 58 "state": "open", 59 "is_locked": false, 60 "comments": 0, 61 "html_url": "https://gitea.com/test-argocd/pr-test/pulls/1", 62 "diff_url": "https://gitea.com/test-argocd/pr-test/pulls/1.diff", 63 "patch_url": "https://gitea.com/test-argocd/pr-test/pulls/1.patch", 64 "mergeable": true, 65 "merged": false, 66 "merged_at": null, 67 "merge_commit_sha": null, 68 "merged_by": null, 69 "base": { 70 "label": "main", 71 "ref": "main", 72 "sha": "72687815ccba81ef014a96201cc2e846a68789d8", 73 "repo_id": 21618, 74 "repo": { 75 "id": 21618, 76 "owner": { 77 "id": 31480, 78 "login": "test-argocd", 79 "full_name": "", 80 "email": "", 81 "avatar_url": "https://gitea.com/avatars/22d1b1d3f61abf95951c4a958731d848", 82 "language": "", 83 "is_admin": false, 84 "last_login": "0001-01-01T00:00:00Z", 85 "created": "2022-04-06T02:28:06+08:00", 86 "restricted": false, 87 "active": false, 88 "prohibit_login": false, 89 "location": "", 90 "website": "", 91 "description": "", 92 "visibility": "public", 93 "followers_count": 0, 94 "following_count": 0, 95 "starred_repos_count": 0, 96 "username": "test-argocd" 97 }, 98 "name": "pr-test", 99 "full_name": "test-argocd/pr-test", 100 "description": "", 101 "empty": false, 102 "private": false, 103 "fork": false, 104 "template": false, 105 "parent": null, 106 "mirror": false, 107 "size": 28, 108 "language": "", 109 "languages_url": "https://gitea.com/api/v1/repos/test-argocd/pr-test/languages", 110 "html_url": "https://gitea.com/test-argocd/pr-test", 111 "ssh_url": "git@gitea.com:test-argocd/pr-test.git", 112 "clone_url": "https://gitea.com/test-argocd/pr-test.git", 113 "original_url": "", 114 "website": "", 115 "stars_count": 0, 116 "forks_count": 0, 117 "watchers_count": 1, 118 "open_issues_count": 0, 119 "open_pr_counter": 1, 120 "release_counter": 0, 121 "default_branch": "main", 122 "archived": false, 123 "created_at": "2022-04-06T02:32:09+08:00", 124 "updated_at": "2022-04-06T02:33:12+08:00", 125 "permissions": { 126 "admin": false, 127 "push": false, 128 "pull": true 129 }, 130 "has_issues": true, 131 "internal_tracker": { 132 "enable_time_tracker": true, 133 "allow_only_contributors_to_track_time": true, 134 "enable_issue_dependencies": true 135 }, 136 "has_wiki": true, 137 "has_pull_requests": true, 138 "has_projects": true, 139 "ignore_whitespace_conflicts": false, 140 "allow_merge_commits": true, 141 "allow_rebase": true, 142 "allow_rebase_explicit": true, 143 "allow_squash_merge": true, 144 "default_merge_style": "merge", 145 "avatar_url": "", 146 "internal": false, 147 "mirror_interval": "", 148 "mirror_updated": "0001-01-01T00:00:00Z", 149 "repo_transfer": null 150 } 151 }, 152 "head": { 153 "label": "test", 154 "ref": "test", 155 "sha": "7bbaf62d92ddfafd9cc8b340c619abaec32bc09f", 156 "repo_id": 21618, 157 "repo": { 158 "id": 21618, 159 "owner": { 160 "id": 31480, 161 "login": "test-argocd", 162 "full_name": "", 163 "email": "", 164 "avatar_url": "https://gitea.com/avatars/22d1b1d3f61abf95951c4a958731d848", 165 "language": "", 166 "is_admin": false, 167 "last_login": "0001-01-01T00:00:00Z", 168 "created": "2022-04-06T02:28:06+08:00", 169 "restricted": false, 170 "active": false, 171 "prohibit_login": false, 172 "location": "", 173 "website": "", 174 "description": "", 175 "visibility": "public", 176 "followers_count": 0, 177 "following_count": 0, 178 "starred_repos_count": 0, 179 "username": "test-argocd" 180 }, 181 "name": "pr-test", 182 "full_name": "test-argocd/pr-test", 183 "description": "", 184 "empty": false, 185 "private": false, 186 "fork": false, 187 "template": false, 188 "parent": null, 189 "mirror": false, 190 "size": 28, 191 "language": "", 192 "languages_url": "https://gitea.com/api/v1/repos/test-argocd/pr-test/languages", 193 "html_url": "https://gitea.com/test-argocd/pr-test", 194 "ssh_url": "git@gitea.com:test-argocd/pr-test.git", 195 "clone_url": "https://gitea.com/test-argocd/pr-test.git", 196 "original_url": "", 197 "website": "", 198 "stars_count": 0, 199 "forks_count": 0, 200 "watchers_count": 1, 201 "open_issues_count": 0, 202 "open_pr_counter": 1, 203 "release_counter": 0, 204 "default_branch": "main", 205 "archived": false, 206 "created_at": "2022-04-06T02:32:09+08:00", 207 "updated_at": "2022-04-06T02:33:12+08:00", 208 "permissions": { 209 "admin": false, 210 "push": false, 211 "pull": true 212 }, 213 "has_issues": true, 214 "internal_tracker": { 215 "enable_time_tracker": true, 216 "allow_only_contributors_to_track_time": true, 217 "enable_issue_dependencies": true 218 }, 219 "has_wiki": true, 220 "has_pull_requests": true, 221 "has_projects": true, 222 "ignore_whitespace_conflicts": false, 223 "allow_merge_commits": true, 224 "allow_rebase": true, 225 "allow_rebase_explicit": true, 226 "allow_squash_merge": true, 227 "default_merge_style": "merge", 228 "avatar_url": "", 229 "internal": false, 230 "mirror_interval": "", 231 "mirror_updated": "0001-01-01T00:00:00Z", 232 "repo_transfer": null 233 } 234 }, 235 "merge_base": "72687815ccba81ef014a96201cc2e846a68789d8", 236 "due_date": null, 237 "created_at": "2022-04-06T02:34:24+08:00", 238 "updated_at": "2022-04-06T02:34:24+08:00", 239 "closed_at": null 240 }]`) 241 if err != nil { 242 t.Fail() 243 } 244 } 245 } 246 } 247 248 func TestGiteaList(t *testing.T) { 249 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 250 giteaMockHandler(t)(w, r) 251 })) 252 host, err := NewGiteaService(context.Background(), "", ts.URL, "test-argocd", "pr-test", false) 253 assert.Nil(t, err) 254 prs, err := host.List(context.Background()) 255 assert.Nil(t, err) 256 assert.Equal(t, len(prs), 1) 257 assert.Equal(t, prs[0].Number, 1) 258 assert.Equal(t, prs[0].Branch, "test") 259 assert.Equal(t, prs[0].TargetBranch, "main") 260 assert.Equal(t, prs[0].HeadSHA, "7bbaf62d92ddfafd9cc8b340c619abaec32bc09f") 261 } 262 263 func TestGetGiteaPRLabelNames(t *testing.T) { 264 Tests := []struct { 265 Name string 266 PullLabels []*gitea.Label 267 ExpectedResult []string 268 }{ 269 { 270 Name: "PR has labels", 271 PullLabels: []*gitea.Label{ 272 {Name: "label1"}, 273 {Name: "label2"}, 274 {Name: "label3"}, 275 }, 276 ExpectedResult: []string{"label1", "label2", "label3"}, 277 }, 278 { 279 Name: "PR does not have labels", 280 PullLabels: []*gitea.Label{}, 281 ExpectedResult: nil, 282 }, 283 } 284 for _, test := range Tests { 285 t.Run(test.Name, func(t *testing.T) { 286 labels := getGiteaPRLabelNames(test.PullLabels) 287 assert.Equal(t, test.ExpectedResult, labels) 288 }) 289 } 290 }