github.com/google/go-github/v33@v33.0.0/github/pulls_reviews_test.go (about) 1 // Copyright 2016 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "encoding/json" 11 "fmt" 12 "net/http" 13 "reflect" 14 "testing" 15 ) 16 17 func TestReviewers_marshall(t *testing.T) { 18 testJSONMarshal(t, &Reviewers{}, "{}") 19 20 u := &Reviewers{ 21 Users: []*User{{ 22 Login: String("l"), 23 ID: Int64(1), 24 AvatarURL: String("a"), 25 GravatarID: String("g"), 26 Name: String("n"), 27 Company: String("c"), 28 Blog: String("b"), 29 Location: String("l"), 30 Email: String("e"), 31 Hireable: Bool(true), 32 PublicRepos: Int(1), 33 Followers: Int(1), 34 Following: Int(1), 35 CreatedAt: &Timestamp{referenceTime}, 36 URL: String("u"), 37 }}, 38 Teams: []*Team{{ 39 ID: Int64(1), 40 NodeID: String("node"), 41 Name: String("n"), 42 Description: String("d"), 43 URL: String("u"), 44 Slug: String("s"), 45 Permission: String("p"), 46 Privacy: String("priv"), 47 MembersCount: Int(1), 48 ReposCount: Int(1), 49 Organization: nil, 50 MembersURL: String("m"), 51 RepositoriesURL: String("r"), 52 Parent: nil, 53 LDAPDN: String("l"), 54 }}, 55 } 56 57 want := `{ 58 "users" : [ 59 { 60 "login": "l", 61 "id": 1, 62 "avatar_url": "a", 63 "gravatar_id": "g", 64 "name": "n", 65 "company": "c", 66 "blog": "b", 67 "location": "l", 68 "email": "e", 69 "hireable": true, 70 "public_repos": 1, 71 "followers": 1, 72 "following": 1, 73 "created_at": ` + referenceTimeStr + `, 74 "url": "u" 75 } 76 ], 77 "teams" : [ 78 { 79 "id": 1, 80 "node_id": "node", 81 "name": "n", 82 "description": "d", 83 "url": "u", 84 "slug": "s", 85 "permission": "p", 86 "privacy": "priv", 87 "members_count": 1, 88 "repos_count": 1, 89 "members_url": "m", 90 "repositories_url": "r", 91 "ldap_dn": "l" 92 } 93 ] 94 }` 95 96 testJSONMarshal(t, u, want) 97 } 98 99 func TestPullRequestsService_ListReviews(t *testing.T) { 100 client, mux, _, teardown := setup() 101 defer teardown() 102 103 mux.HandleFunc("/repos/o/r/pulls/1/reviews", func(w http.ResponseWriter, r *http.Request) { 104 testMethod(t, r, "GET") 105 testFormValues(t, r, values{ 106 "page": "2", 107 }) 108 fmt.Fprint(w, `[{"id":1},{"id":2}]`) 109 }) 110 111 opt := &ListOptions{Page: 2} 112 reviews, _, err := client.PullRequests.ListReviews(context.Background(), "o", "r", 1, opt) 113 if err != nil { 114 t.Errorf("PullRequests.ListReviews returned error: %v", err) 115 } 116 117 want := []*PullRequestReview{ 118 {ID: Int64(1)}, 119 {ID: Int64(2)}, 120 } 121 if !reflect.DeepEqual(reviews, want) { 122 t.Errorf("PullRequests.ListReviews returned %+v, want %+v", reviews, want) 123 } 124 } 125 126 func TestPullRequestsService_ListReviews_invalidOwner(t *testing.T) { 127 client, _, _, teardown := setup() 128 defer teardown() 129 130 _, _, err := client.PullRequests.ListReviews(context.Background(), "%", "r", 1, nil) 131 testURLParseError(t, err) 132 } 133 134 func TestPullRequestsService_GetReview(t *testing.T) { 135 client, mux, _, teardown := setup() 136 defer teardown() 137 138 mux.HandleFunc("/repos/o/r/pulls/1/reviews/1", func(w http.ResponseWriter, r *http.Request) { 139 testMethod(t, r, "GET") 140 fmt.Fprint(w, `{"id":1}`) 141 }) 142 143 review, _, err := client.PullRequests.GetReview(context.Background(), "o", "r", 1, 1) 144 if err != nil { 145 t.Errorf("PullRequests.GetReview returned error: %v", err) 146 } 147 148 want := &PullRequestReview{ID: Int64(1)} 149 if !reflect.DeepEqual(review, want) { 150 t.Errorf("PullRequests.GetReview returned %+v, want %+v", review, want) 151 } 152 } 153 154 func TestPullRequestsService_GetReview_invalidOwner(t *testing.T) { 155 client, _, _, teardown := setup() 156 defer teardown() 157 158 _, _, err := client.PullRequests.GetReview(context.Background(), "%", "r", 1, 1) 159 testURLParseError(t, err) 160 } 161 162 func TestPullRequestsService_DeletePendingReview(t *testing.T) { 163 client, mux, _, teardown := setup() 164 defer teardown() 165 166 mux.HandleFunc("/repos/o/r/pulls/1/reviews/1", func(w http.ResponseWriter, r *http.Request) { 167 testMethod(t, r, "DELETE") 168 fmt.Fprint(w, `{"id":1}`) 169 }) 170 171 review, _, err := client.PullRequests.DeletePendingReview(context.Background(), "o", "r", 1, 1) 172 if err != nil { 173 t.Errorf("PullRequests.DeletePendingReview returned error: %v", err) 174 } 175 176 want := &PullRequestReview{ID: Int64(1)} 177 if !reflect.DeepEqual(review, want) { 178 t.Errorf("PullRequests.DeletePendingReview returned %+v, want %+v", review, want) 179 } 180 } 181 182 func TestPullRequestsService_DeletePendingReview_invalidOwner(t *testing.T) { 183 client, _, _, teardown := setup() 184 defer teardown() 185 186 _, _, err := client.PullRequests.DeletePendingReview(context.Background(), "%", "r", 1, 1) 187 testURLParseError(t, err) 188 } 189 190 func TestPullRequestsService_ListReviewComments(t *testing.T) { 191 client, mux, _, teardown := setup() 192 defer teardown() 193 194 mux.HandleFunc("/repos/o/r/pulls/1/reviews/1/comments", func(w http.ResponseWriter, r *http.Request) { 195 testMethod(t, r, "GET") 196 fmt.Fprint(w, `[{"id":1},{"id":2}]`) 197 }) 198 199 comments, _, err := client.PullRequests.ListReviewComments(context.Background(), "o", "r", 1, 1, nil) 200 if err != nil { 201 t.Errorf("PullRequests.ListReviewComments returned error: %v", err) 202 } 203 204 want := []*PullRequestComment{ 205 {ID: Int64(1)}, 206 {ID: Int64(2)}, 207 } 208 if !reflect.DeepEqual(comments, want) { 209 t.Errorf("PullRequests.ListReviewComments returned %+v, want %+v", comments, want) 210 } 211 } 212 213 func TestPullRequestsService_ListReviewComments_withOptions(t *testing.T) { 214 client, mux, _, teardown := setup() 215 defer teardown() 216 217 mux.HandleFunc("/repos/o/r/pulls/1/reviews/1/comments", func(w http.ResponseWriter, r *http.Request) { 218 testMethod(t, r, "GET") 219 testFormValues(t, r, values{ 220 "page": "2", 221 }) 222 fmt.Fprint(w, `[]`) 223 }) 224 225 _, _, err := client.PullRequests.ListReviewComments(context.Background(), "o", "r", 1, 1, &ListOptions{Page: 2}) 226 if err != nil { 227 t.Errorf("PullRequests.ListReviewComments returned error: %v", err) 228 } 229 } 230 231 func TestPullRequestReviewRequest_isComfortFadePreview(t *testing.T) { 232 path := "path/to/file.go" 233 body := "this is a comment body" 234 left, right := "LEFT", "RIGHT" 235 pos1, pos2, pos3 := 1, 2, 3 236 line1, line2, line3 := 11, 22, 33 237 238 tests := []struct { 239 name string 240 review *PullRequestReviewRequest 241 wantErr error 242 wantBool bool 243 }{{ 244 name: "empty review", 245 review: &PullRequestReviewRequest{}, 246 wantBool: false, 247 }, { 248 name: "old-style review", 249 review: &PullRequestReviewRequest{ 250 Comments: []*DraftReviewComment{{ 251 Path: &path, 252 Body: &body, 253 Position: &pos1, 254 }, { 255 Path: &path, 256 Body: &body, 257 Position: &pos2, 258 }, { 259 Path: &path, 260 Body: &body, 261 Position: &pos3, 262 }}, 263 }, 264 wantBool: false, 265 }, { 266 name: "new-style review", 267 review: &PullRequestReviewRequest{ 268 Comments: []*DraftReviewComment{{ 269 Path: &path, 270 Body: &body, 271 Side: &right, 272 Line: &line1, 273 }, { 274 Path: &path, 275 Body: &body, 276 Side: &left, 277 Line: &line2, 278 }, { 279 Path: &path, 280 Body: &body, 281 Side: &right, 282 Line: &line3, 283 }}, 284 }, 285 wantBool: true, 286 }, { 287 name: "blended comment", 288 review: &PullRequestReviewRequest{ 289 Comments: []*DraftReviewComment{{ 290 Path: &path, 291 Body: &body, 292 Position: &pos1, // can't have both styles. 293 Side: &right, 294 Line: &line1, 295 }}, 296 }, 297 wantErr: ErrMixedCommentStyles, 298 }, { 299 name: "position then line", 300 review: &PullRequestReviewRequest{ 301 Comments: []*DraftReviewComment{{ 302 Path: &path, 303 Body: &body, 304 Position: &pos1, 305 }, { 306 Path: &path, 307 Body: &body, 308 Side: &right, 309 Line: &line1, 310 }}, 311 }, 312 wantErr: ErrMixedCommentStyles, 313 }, { 314 name: "line then position", 315 review: &PullRequestReviewRequest{ 316 Comments: []*DraftReviewComment{{ 317 Path: &path, 318 Body: &body, 319 Side: &right, 320 Line: &line1, 321 }, { 322 Path: &path, 323 Body: &body, 324 Position: &pos1, 325 }}, 326 }, 327 wantErr: ErrMixedCommentStyles, 328 }} 329 330 for _, tc := range tests { 331 t.Run(tc.name, func(t *testing.T) { 332 gotBool, gotErr := tc.review.isComfortFadePreview() 333 if tc.wantErr != nil { 334 if gotErr != tc.wantErr { 335 t.Errorf("isComfortFadePreview() = %v, wanted %v", gotErr, tc.wantErr) 336 } 337 } else { 338 if gotBool != tc.wantBool { 339 t.Errorf("isComfortFadePreview() = %v, wanted %v", gotBool, tc.wantBool) 340 } 341 } 342 }) 343 } 344 } 345 346 func TestPullRequestsService_ListReviewComments_invalidOwner(t *testing.T) { 347 client, _, _, teardown := setup() 348 defer teardown() 349 350 _, _, err := client.PullRequests.ListReviewComments(context.Background(), "%", "r", 1, 1, nil) 351 testURLParseError(t, err) 352 } 353 354 func TestPullRequestsService_CreateReview(t *testing.T) { 355 client, mux, _, teardown := setup() 356 defer teardown() 357 358 input := &PullRequestReviewRequest{ 359 CommitID: String("commit_id"), 360 Body: String("b"), 361 Event: String("APPROVE"), 362 } 363 364 mux.HandleFunc("/repos/o/r/pulls/1/reviews", func(w http.ResponseWriter, r *http.Request) { 365 v := new(PullRequestReviewRequest) 366 json.NewDecoder(r.Body).Decode(v) 367 368 testMethod(t, r, "POST") 369 if !reflect.DeepEqual(v, input) { 370 t.Errorf("Request body = %+v, want %+v", v, input) 371 } 372 373 fmt.Fprint(w, `{"id":1}`) 374 }) 375 376 review, _, err := client.PullRequests.CreateReview(context.Background(), "o", "r", 1, input) 377 if err != nil { 378 t.Errorf("PullRequests.CreateReview returned error: %v", err) 379 } 380 381 want := &PullRequestReview{ID: Int64(1)} 382 if !reflect.DeepEqual(review, want) { 383 t.Errorf("PullRequests.CreateReview returned %+v, want %+v", review, want) 384 } 385 } 386 387 func TestPullRequestsService_CreateReview_invalidOwner(t *testing.T) { 388 client, _, _, teardown := setup() 389 defer teardown() 390 391 _, _, err := client.PullRequests.CreateReview(context.Background(), "%", "r", 1, &PullRequestReviewRequest{}) 392 testURLParseError(t, err) 393 } 394 395 func TestPullRequestsService_UpdateReview(t *testing.T) { 396 client, mux, _, teardown := setup() 397 defer teardown() 398 399 mux.HandleFunc("/repos/o/r/pulls/1/reviews/1", func(w http.ResponseWriter, r *http.Request) { 400 testMethod(t, r, "PUT") 401 fmt.Fprintf(w, `{"id":1}`) 402 }) 403 404 got, _, err := client.PullRequests.UpdateReview(context.Background(), "o", "r", 1, 1, "updated_body") 405 if err != nil { 406 t.Errorf("PullRequests.UpdateReview returned error: %v", err) 407 } 408 409 want := &PullRequestReview{ID: Int64(1)} 410 if !reflect.DeepEqual(got, want) { 411 t.Errorf("PullRequests.UpdateReview = %+v, want %+v", got, want) 412 } 413 } 414 415 func TestPullRequestsService_SubmitReview(t *testing.T) { 416 client, mux, _, teardown := setup() 417 defer teardown() 418 419 input := &PullRequestReviewRequest{ 420 Body: String("b"), 421 Event: String("APPROVE"), 422 } 423 424 mux.HandleFunc("/repos/o/r/pulls/1/reviews/1/events", func(w http.ResponseWriter, r *http.Request) { 425 v := new(PullRequestReviewRequest) 426 json.NewDecoder(r.Body).Decode(v) 427 428 testMethod(t, r, "POST") 429 if !reflect.DeepEqual(v, input) { 430 t.Errorf("Request body = %+v, want %+v", v, input) 431 } 432 433 fmt.Fprint(w, `{"id":1}`) 434 }) 435 436 review, _, err := client.PullRequests.SubmitReview(context.Background(), "o", "r", 1, 1, input) 437 if err != nil { 438 t.Errorf("PullRequests.SubmitReview returned error: %v", err) 439 } 440 441 want := &PullRequestReview{ID: Int64(1)} 442 if !reflect.DeepEqual(review, want) { 443 t.Errorf("PullRequests.SubmitReview returned %+v, want %+v", review, want) 444 } 445 } 446 447 func TestPullRequestsService_SubmitReview_invalidOwner(t *testing.T) { 448 client, _, _, teardown := setup() 449 defer teardown() 450 451 _, _, err := client.PullRequests.SubmitReview(context.Background(), "%", "r", 1, 1, &PullRequestReviewRequest{}) 452 testURLParseError(t, err) 453 } 454 455 func TestPullRequestsService_DismissReview(t *testing.T) { 456 client, mux, _, teardown := setup() 457 defer teardown() 458 459 input := &PullRequestReviewDismissalRequest{Message: String("m")} 460 461 mux.HandleFunc("/repos/o/r/pulls/1/reviews/1/dismissals", func(w http.ResponseWriter, r *http.Request) { 462 v := new(PullRequestReviewDismissalRequest) 463 json.NewDecoder(r.Body).Decode(v) 464 465 testMethod(t, r, "PUT") 466 if !reflect.DeepEqual(v, input) { 467 t.Errorf("Request body = %+v, want %+v", v, input) 468 } 469 470 fmt.Fprint(w, `{"id":1}`) 471 }) 472 473 review, _, err := client.PullRequests.DismissReview(context.Background(), "o", "r", 1, 1, input) 474 if err != nil { 475 t.Errorf("PullRequests.DismissReview returned error: %v", err) 476 } 477 478 want := &PullRequestReview{ID: Int64(1)} 479 if !reflect.DeepEqual(review, want) { 480 t.Errorf("PullRequests.DismissReview returned %+v, want %+v", review, want) 481 } 482 } 483 484 func TestPullRequestsService_DismissReview_invalidOwner(t *testing.T) { 485 client, _, _, teardown := setup() 486 defer teardown() 487 488 _, _, err := client.PullRequests.DismissReview(context.Background(), "%", "r", 1, 1, &PullRequestReviewDismissalRequest{}) 489 testURLParseError(t, err) 490 }