1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "errors"
12 "fmt"
13 "net/http"
14 "net/url"
15 "strings"
16 "testing"
17
18 "github.com/google/go-cmp/cmp"
19 )
20
21 func TestRepositoriesService_List_authenticatedUser(t *testing.T) {
22 client, mux, _, teardown := setup()
23 defer teardown()
24
25 wantAcceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview}
26 mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) {
27 testMethod(t, r, "GET")
28 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
29 fmt.Fprint(w, `[{"id":1},{"id":2}]`)
30 })
31
32 ctx := context.Background()
33 got, _, err := client.Repositories.List(ctx, "", nil)
34 if err != nil {
35 t.Errorf("Repositories.List returned error: %v", err)
36 }
37
38 want := []*Repository{{ID: Int64(1)}, {ID: Int64(2)}}
39 if !cmp.Equal(got, want) {
40 t.Errorf("Repositories.List returned %+v, want %+v", got, want)
41 }
42
43 const methodName = "List"
44 testBadOptions(t, methodName, func() (err error) {
45 _, _, err = client.Repositories.List(ctx, "\n", &RepositoryListOptions{})
46 return err
47 })
48
49 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
50 got, resp, err := client.Repositories.List(ctx, "", nil)
51 if got != nil {
52 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
53 }
54 return resp, err
55 })
56 }
57
58 func TestRepositoriesService_List_specifiedUser(t *testing.T) {
59 client, mux, _, teardown := setup()
60 defer teardown()
61
62 wantAcceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview}
63 mux.HandleFunc("/users/u/repos", func(w http.ResponseWriter, r *http.Request) {
64 testMethod(t, r, "GET")
65 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
66 testFormValues(t, r, values{
67 "visibility": "public",
68 "affiliation": "owner,collaborator",
69 "sort": "created",
70 "direction": "asc",
71 "page": "2",
72 })
73 fmt.Fprint(w, `[{"id":1}]`)
74 })
75
76 opt := &RepositoryListOptions{
77 Visibility: "public",
78 Affiliation: "owner,collaborator",
79 Sort: "created",
80 Direction: "asc",
81 ListOptions: ListOptions{Page: 2},
82 }
83 ctx := context.Background()
84 repos, _, err := client.Repositories.List(ctx, "u", opt)
85 if err != nil {
86 t.Errorf("Repositories.List returned error: %v", err)
87 }
88
89 want := []*Repository{{ID: Int64(1)}}
90 if !cmp.Equal(repos, want) {
91 t.Errorf("Repositories.List returned %+v, want %+v", repos, want)
92 }
93 }
94
95 func TestRepositoriesService_List_specifiedUser_type(t *testing.T) {
96 client, mux, _, teardown := setup()
97 defer teardown()
98
99 wantAcceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview}
100 mux.HandleFunc("/users/u/repos", func(w http.ResponseWriter, r *http.Request) {
101 testMethod(t, r, "GET")
102 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
103 testFormValues(t, r, values{
104 "type": "owner",
105 })
106 fmt.Fprint(w, `[{"id":1}]`)
107 })
108
109 opt := &RepositoryListOptions{
110 Type: "owner",
111 }
112 ctx := context.Background()
113 repos, _, err := client.Repositories.List(ctx, "u", opt)
114 if err != nil {
115 t.Errorf("Repositories.List returned error: %v", err)
116 }
117
118 want := []*Repository{{ID: Int64(1)}}
119 if !cmp.Equal(repos, want) {
120 t.Errorf("Repositories.List returned %+v, want %+v", repos, want)
121 }
122 }
123
124 func TestRepositoriesService_List_invalidUser(t *testing.T) {
125 client, _, _, teardown := setup()
126 defer teardown()
127
128 ctx := context.Background()
129 _, _, err := client.Repositories.List(ctx, "%", nil)
130 testURLParseError(t, err)
131 }
132
133 func TestRepositoriesService_ListByOrg(t *testing.T) {
134 client, mux, _, teardown := setup()
135 defer teardown()
136
137 wantAcceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview}
138 mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) {
139 testMethod(t, r, "GET")
140 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
141 testFormValues(t, r, values{
142 "type": "forks",
143 "page": "2",
144 })
145 fmt.Fprint(w, `[{"id":1}]`)
146 })
147
148 ctx := context.Background()
149 opt := &RepositoryListByOrgOptions{
150 Type: "forks",
151 ListOptions: ListOptions{Page: 2},
152 }
153 got, _, err := client.Repositories.ListByOrg(ctx, "o", opt)
154 if err != nil {
155 t.Errorf("Repositories.ListByOrg returned error: %v", err)
156 }
157
158 want := []*Repository{{ID: Int64(1)}}
159 if !cmp.Equal(got, want) {
160 t.Errorf("Repositories.ListByOrg returned %+v, want %+v", got, want)
161 }
162
163 const methodName = "ListByOrg"
164 testBadOptions(t, methodName, func() (err error) {
165 _, _, err = client.Repositories.ListByOrg(ctx, "\n", opt)
166 return err
167 })
168
169 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
170 got, resp, err := client.Repositories.ListByOrg(ctx, "o", opt)
171 if got != nil {
172 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
173 }
174 return resp, err
175 })
176 }
177
178 func TestRepositoriesService_ListByOrg_invalidOrg(t *testing.T) {
179 client, _, _, teardown := setup()
180 defer teardown()
181
182 ctx := context.Background()
183 _, _, err := client.Repositories.ListByOrg(ctx, "%", nil)
184 testURLParseError(t, err)
185 }
186
187 func TestRepositoriesService_ListAll(t *testing.T) {
188 client, mux, _, teardown := setup()
189 defer teardown()
190
191 mux.HandleFunc("/repositories", func(w http.ResponseWriter, r *http.Request) {
192 testMethod(t, r, "GET")
193 testFormValues(t, r, values{
194 "since": "1",
195 })
196 fmt.Fprint(w, `[{"id":1}]`)
197 })
198
199 ctx := context.Background()
200 opt := &RepositoryListAllOptions{1}
201 got, _, err := client.Repositories.ListAll(ctx, opt)
202 if err != nil {
203 t.Errorf("Repositories.ListAll returned error: %v", err)
204 }
205
206 want := []*Repository{{ID: Int64(1)}}
207 if !cmp.Equal(got, want) {
208 t.Errorf("Repositories.ListAll returned %+v, want %+v", got, want)
209 }
210
211 const methodName = "ListAll"
212 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
213 got, resp, err := client.Repositories.ListAll(ctx, &RepositoryListAllOptions{1})
214 if got != nil {
215 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
216 }
217 return resp, err
218 })
219 }
220
221 func TestRepositoriesService_Create_user(t *testing.T) {
222 client, mux, _, teardown := setup()
223 defer teardown()
224
225 input := &Repository{
226 Name: String("n"),
227 Archived: Bool(true),
228 }
229
230 wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
231 mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) {
232 v := new(createRepoRequest)
233 json.NewDecoder(r.Body).Decode(v)
234
235 testMethod(t, r, "POST")
236 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
237 want := &createRepoRequest{Name: String("n")}
238 if !cmp.Equal(v, want) {
239 t.Errorf("Request body = %+v, want %+v", v, want)
240 }
241
242 fmt.Fprint(w, `{"id":1}`)
243 })
244
245 ctx := context.Background()
246 got, _, err := client.Repositories.Create(ctx, "", input)
247 if err != nil {
248 t.Errorf("Repositories.Create returned error: %v", err)
249 }
250
251 want := &Repository{ID: Int64(1)}
252 if !cmp.Equal(got, want) {
253 t.Errorf("Repositories.Create returned %+v, want %+v", got, want)
254 }
255
256 const methodName = "Create"
257 testBadOptions(t, methodName, func() (err error) {
258 _, _, err = client.Repositories.Create(ctx, "\n", input)
259 return err
260 })
261
262 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
263 got, resp, err := client.Repositories.Create(ctx, "", input)
264 if got != nil {
265 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
266 }
267 return resp, err
268 })
269 }
270
271 func TestRepositoriesService_Create_org(t *testing.T) {
272 client, mux, _, teardown := setup()
273 defer teardown()
274
275 input := &Repository{
276 Name: String("n"),
277 Archived: Bool(true),
278 }
279
280 wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
281 mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) {
282 v := new(createRepoRequest)
283 json.NewDecoder(r.Body).Decode(v)
284
285 testMethod(t, r, "POST")
286 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
287 want := &createRepoRequest{Name: String("n")}
288 if !cmp.Equal(v, want) {
289 t.Errorf("Request body = %+v, want %+v", v, want)
290 }
291
292 fmt.Fprint(w, `{"id":1}`)
293 })
294
295 ctx := context.Background()
296 repo, _, err := client.Repositories.Create(ctx, "o", input)
297 if err != nil {
298 t.Errorf("Repositories.Create returned error: %v", err)
299 }
300
301 want := &Repository{ID: Int64(1)}
302 if !cmp.Equal(repo, want) {
303 t.Errorf("Repositories.Create returned %+v, want %+v", repo, want)
304 }
305 }
306
307 func TestRepositoriesService_CreateFromTemplate(t *testing.T) {
308 client, mux, _, teardown := setup()
309 defer teardown()
310
311 templateRepoReq := &TemplateRepoRequest{
312 Name: String("n"),
313 }
314
315 mux.HandleFunc("/repos/to/tr/generate", func(w http.ResponseWriter, r *http.Request) {
316 v := new(TemplateRepoRequest)
317 json.NewDecoder(r.Body).Decode(v)
318
319 testMethod(t, r, "POST")
320 testHeader(t, r, "Accept", mediaTypeRepositoryTemplatePreview)
321 want := &TemplateRepoRequest{Name: String("n")}
322 if !cmp.Equal(v, want) {
323 t.Errorf("Request body = %+v, want %+v", v, want)
324 }
325
326 fmt.Fprint(w, `{"id":1,"name":"n"}`)
327 })
328
329 ctx := context.Background()
330 got, _, err := client.Repositories.CreateFromTemplate(ctx, "to", "tr", templateRepoReq)
331 if err != nil {
332 t.Errorf("Repositories.CreateFromTemplate returned error: %v", err)
333 }
334
335 want := &Repository{ID: Int64(1), Name: String("n")}
336 if !cmp.Equal(got, want) {
337 t.Errorf("Repositories.CreateFromTemplate returned %+v, want %+v", got, want)
338 }
339
340 const methodName = "CreateFromTemplate"
341 testBadOptions(t, methodName, func() (err error) {
342 _, _, err = client.Repositories.CreateFromTemplate(ctx, "\n", "\n", templateRepoReq)
343 return err
344 })
345
346 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
347 got, resp, err := client.Repositories.CreateFromTemplate(ctx, "to", "tr", templateRepoReq)
348 if got != nil {
349 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
350 }
351 return resp, err
352 })
353 }
354
355 func TestRepositoriesService_Get(t *testing.T) {
356 client, mux, _, teardown := setup()
357 defer teardown()
358
359 wantAcceptHeaders := []string{mediaTypeCodesOfConductPreview, mediaTypeTopicsPreview, mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
360 mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) {
361 testMethod(t, r, "GET")
362 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
363 fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"},"security_and_analysis":{"advanced_security":{"status":"enabled"},"secret_scanning":{"status":"enabled"}}}`)
364 })
365
366 ctx := context.Background()
367 got, _, err := client.Repositories.Get(ctx, "o", "r")
368 if err != nil {
369 t.Errorf("Repositories.Get returned error: %v", err)
370 }
371
372 want := &Repository{ID: Int64(1), Name: String("n"), Description: String("d"), Owner: &User{Login: String("l")}, License: &License{Key: String("mit")}, SecurityAndAnalysis: &SecurityAndAnalysis{AdvancedSecurity: &AdvancedSecurity{Status: String("enabled")}, SecretScanning: &SecretScanning{String("enabled")}}}
373 if !cmp.Equal(got, want) {
374 t.Errorf("Repositories.Get returned %+v, want %+v", got, want)
375 }
376
377 const methodName = "Get"
378 testBadOptions(t, methodName, func() (err error) {
379 _, _, err = client.Repositories.Get(ctx, "\n", "\n")
380 return err
381 })
382
383 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
384 got, resp, err := client.Repositories.Get(ctx, "o", "r")
385 if got != nil {
386 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
387 }
388 return resp, err
389 })
390 }
391
392 func TestRepositoriesService_GetCodeOfConduct(t *testing.T) {
393 client, mux, _, teardown := setup()
394 defer teardown()
395
396 mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) {
397 testMethod(t, r, "GET")
398 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview)
399 fmt.Fprint(w, `{
400 "code_of_conduct": {
401 "key": "key",
402 "name": "name",
403 "url": "url",
404 "body": "body"
405 }}`,
406 )
407 })
408
409 ctx := context.Background()
410 got, _, err := client.Repositories.GetCodeOfConduct(ctx, "o", "r")
411 if err != nil {
412 t.Errorf("Repositories.GetCodeOfConduct returned error: %v", err)
413 }
414
415 want := &CodeOfConduct{
416 Key: String("key"),
417 Name: String("name"),
418 URL: String("url"),
419 Body: String("body"),
420 }
421
422 if !cmp.Equal(got, want) {
423 t.Errorf("Repositories.GetCodeOfConduct returned %+v, want %+v", got, want)
424 }
425
426 const methodName = "GetCodeOfConduct"
427 testBadOptions(t, methodName, func() (err error) {
428 _, _, err = client.Repositories.GetCodeOfConduct(ctx, "\n", "\n")
429 return err
430 })
431
432 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
433 got, resp, err := client.Repositories.GetCodeOfConduct(ctx, "o", "r")
434 if got != nil {
435 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
436 }
437 return resp, err
438 })
439 }
440
441 func TestRepositoriesService_GetByID(t *testing.T) {
442 client, mux, _, teardown := setup()
443 defer teardown()
444
445 mux.HandleFunc("/repositories/1", func(w http.ResponseWriter, r *http.Request) {
446 testMethod(t, r, "GET")
447 fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"}}`)
448 })
449
450 ctx := context.Background()
451 got, _, err := client.Repositories.GetByID(ctx, 1)
452 if err != nil {
453 t.Fatalf("Repositories.GetByID returned error: %v", err)
454 }
455
456 want := &Repository{ID: Int64(1), Name: String("n"), Description: String("d"), Owner: &User{Login: String("l")}, License: &License{Key: String("mit")}}
457 if !cmp.Equal(got, want) {
458 t.Errorf("Repositories.GetByID returned %+v, want %+v", got, want)
459 }
460
461 const methodName = "GetByID"
462 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
463 got, resp, err := client.Repositories.GetByID(ctx, 1)
464 if got != nil {
465 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
466 }
467 return resp, err
468 })
469 }
470
471 func TestRepositoriesService_Edit(t *testing.T) {
472 client, mux, _, teardown := setup()
473 defer teardown()
474
475 i := true
476 input := &Repository{HasIssues: &i}
477
478 wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
479 mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) {
480 v := new(Repository)
481 json.NewDecoder(r.Body).Decode(v)
482
483 testMethod(t, r, "PATCH")
484 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
485 if !cmp.Equal(v, input) {
486 t.Errorf("Request body = %+v, want %+v", v, input)
487 }
488 fmt.Fprint(w, `{"id":1}`)
489 })
490
491 ctx := context.Background()
492 got, _, err := client.Repositories.Edit(ctx, "o", "r", input)
493 if err != nil {
494 t.Errorf("Repositories.Edit returned error: %v", err)
495 }
496
497 want := &Repository{ID: Int64(1)}
498 if !cmp.Equal(got, want) {
499 t.Errorf("Repositories.Edit returned %+v, want %+v", got, want)
500 }
501
502 const methodName = "Edit"
503 testBadOptions(t, methodName, func() (err error) {
504 _, _, err = client.Repositories.Edit(ctx, "\n", "\n", input)
505 return err
506 })
507
508 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
509 got, resp, err := client.Repositories.Edit(ctx, "o", "r", input)
510 if got != nil {
511 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
512 }
513 return resp, err
514 })
515 }
516
517 func TestRepositoriesService_Delete(t *testing.T) {
518 client, mux, _, teardown := setup()
519 defer teardown()
520
521 mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) {
522 testMethod(t, r, "DELETE")
523 })
524
525 ctx := context.Background()
526 _, err := client.Repositories.Delete(ctx, "o", "r")
527 if err != nil {
528 t.Errorf("Repositories.Delete returned error: %v", err)
529 }
530
531 const methodName = "Delete"
532 testBadOptions(t, methodName, func() (err error) {
533 _, err = client.Repositories.Delete(ctx, "\n", "\n")
534 return err
535 })
536
537 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
538 return client.Repositories.Delete(ctx, "o", "r")
539 })
540 }
541
542 func TestRepositoriesService_Get_invalidOwner(t *testing.T) {
543 client, _, _, teardown := setup()
544 defer teardown()
545
546 ctx := context.Background()
547 _, _, err := client.Repositories.Get(ctx, "%", "r")
548 testURLParseError(t, err)
549 }
550
551 func TestRepositoriesService_Edit_invalidOwner(t *testing.T) {
552 client, _, _, teardown := setup()
553 defer teardown()
554
555 ctx := context.Background()
556 _, _, err := client.Repositories.Edit(ctx, "%", "r", nil)
557 testURLParseError(t, err)
558 }
559
560 func TestRepositoriesService_GetVulnerabilityAlerts(t *testing.T) {
561 client, mux, _, teardown := setup()
562 defer teardown()
563
564 mux.HandleFunc("/repos/o/r/vulnerability-alerts", func(w http.ResponseWriter, r *http.Request) {
565 testMethod(t, r, "GET")
566 testHeader(t, r, "Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
567
568 w.WriteHeader(http.StatusNoContent)
569 })
570
571 ctx := context.Background()
572 vulnerabilityAlertsEnabled, _, err := client.Repositories.GetVulnerabilityAlerts(ctx, "o", "r")
573 if err != nil {
574 t.Errorf("Repositories.GetVulnerabilityAlerts returned error: %v", err)
575 }
576
577 if want := true; vulnerabilityAlertsEnabled != want {
578 t.Errorf("Repositories.GetVulnerabilityAlerts returned %+v, want %+v", vulnerabilityAlertsEnabled, want)
579 }
580
581 const methodName = "GetVulnerabilityAlerts"
582 testBadOptions(t, methodName, func() (err error) {
583 _, _, err = client.Repositories.GetVulnerabilityAlerts(ctx, "\n", "\n")
584 return err
585 })
586
587 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
588 got, resp, err := client.Repositories.GetVulnerabilityAlerts(ctx, "o", "r")
589 if got {
590 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
591 }
592 return resp, err
593 })
594 }
595
596 func TestRepositoriesService_EnableVulnerabilityAlerts(t *testing.T) {
597 client, mux, _, teardown := setup()
598 defer teardown()
599
600 mux.HandleFunc("/repos/o/r/vulnerability-alerts", func(w http.ResponseWriter, r *http.Request) {
601 testMethod(t, r, "PUT")
602 testHeader(t, r, "Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
603
604 w.WriteHeader(http.StatusNoContent)
605 })
606
607 ctx := context.Background()
608 if _, err := client.Repositories.EnableVulnerabilityAlerts(ctx, "o", "r"); err != nil {
609 t.Errorf("Repositories.EnableVulnerabilityAlerts returned error: %v", err)
610 }
611
612 const methodName = "EnableVulnerabilityAlerts"
613 testBadOptions(t, methodName, func() (err error) {
614 _, err = client.Repositories.EnableVulnerabilityAlerts(ctx, "\n", "\n")
615 return err
616 })
617
618 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
619 return client.Repositories.EnableVulnerabilityAlerts(ctx, "o", "r")
620 })
621 }
622
623 func TestRepositoriesService_DisableVulnerabilityAlerts(t *testing.T) {
624 client, mux, _, teardown := setup()
625 defer teardown()
626
627 mux.HandleFunc("/repos/o/r/vulnerability-alerts", func(w http.ResponseWriter, r *http.Request) {
628 testMethod(t, r, "DELETE")
629 testHeader(t, r, "Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
630
631 w.WriteHeader(http.StatusNoContent)
632 })
633
634 ctx := context.Background()
635 if _, err := client.Repositories.DisableVulnerabilityAlerts(ctx, "o", "r"); err != nil {
636 t.Errorf("Repositories.DisableVulnerabilityAlerts returned error: %v", err)
637 }
638
639 const methodName = "DisableVulnerabilityAlerts"
640 testBadOptions(t, methodName, func() (err error) {
641 _, err = client.Repositories.DisableVulnerabilityAlerts(ctx, "\n", "\n")
642 return err
643 })
644
645 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
646 return client.Repositories.DisableVulnerabilityAlerts(ctx, "o", "r")
647 })
648 }
649
650 func TestRepositoriesService_EnableAutomatedSecurityFixes(t *testing.T) {
651 client, mux, _, teardown := setup()
652 defer teardown()
653
654 mux.HandleFunc("/repos/o/r/automated-security-fixes", func(w http.ResponseWriter, r *http.Request) {
655 testMethod(t, r, "PUT")
656 testHeader(t, r, "Accept", mediaTypeRequiredAutomatedSecurityFixesPreview)
657
658 w.WriteHeader(http.StatusNoContent)
659 })
660
661 ctx := context.Background()
662 if _, err := client.Repositories.EnableAutomatedSecurityFixes(ctx, "o", "r"); err != nil {
663 t.Errorf("Repositories.EnableAutomatedSecurityFixes returned error: %v", err)
664 }
665 }
666
667 func TestRepositoriesService_DisableAutomatedSecurityFixes(t *testing.T) {
668 client, mux, _, teardown := setup()
669 defer teardown()
670
671 mux.HandleFunc("/repos/o/r/automated-security-fixes", func(w http.ResponseWriter, r *http.Request) {
672 testMethod(t, r, "DELETE")
673 testHeader(t, r, "Accept", mediaTypeRequiredAutomatedSecurityFixesPreview)
674
675 w.WriteHeader(http.StatusNoContent)
676 })
677
678 ctx := context.Background()
679 if _, err := client.Repositories.DisableAutomatedSecurityFixes(ctx, "o", "r"); err != nil {
680 t.Errorf("Repositories.DisableAutomatedSecurityFixes returned error: %v", err)
681 }
682 }
683
684 func TestRepositoriesService_ListContributors(t *testing.T) {
685 client, mux, _, teardown := setup()
686 defer teardown()
687
688 mux.HandleFunc("/repos/o/r/contributors", func(w http.ResponseWriter, r *http.Request) {
689 testMethod(t, r, "GET")
690 testFormValues(t, r, values{
691 "anon": "true",
692 "page": "2",
693 })
694 fmt.Fprint(w, `[{"contributions":42}]`)
695 })
696
697 opts := &ListContributorsOptions{Anon: "true", ListOptions: ListOptions{Page: 2}}
698 ctx := context.Background()
699 contributors, _, err := client.Repositories.ListContributors(ctx, "o", "r", opts)
700 if err != nil {
701 t.Errorf("Repositories.ListContributors returned error: %v", err)
702 }
703
704 want := []*Contributor{{Contributions: Int(42)}}
705 if !cmp.Equal(contributors, want) {
706 t.Errorf("Repositories.ListContributors returned %+v, want %+v", contributors, want)
707 }
708
709 const methodName = "ListContributors"
710 testBadOptions(t, methodName, func() (err error) {
711 _, _, err = client.Repositories.ListContributors(ctx, "\n", "\n", opts)
712 return err
713 })
714
715 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
716 got, resp, err := client.Repositories.ListContributors(ctx, "o", "r", opts)
717 if got != nil {
718 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
719 }
720 return resp, err
721 })
722 }
723
724 func TestRepositoriesService_ListLanguages(t *testing.T) {
725 client, mux, _, teardown := setup()
726 defer teardown()
727
728 mux.HandleFunc("/repos/o/r/languages", func(w http.ResponseWriter, r *http.Request) {
729 testMethod(t, r, "GET")
730 fmt.Fprint(w, `{"go":1}`)
731 })
732
733 ctx := context.Background()
734 languages, _, err := client.Repositories.ListLanguages(ctx, "o", "r")
735 if err != nil {
736 t.Errorf("Repositories.ListLanguages returned error: %v", err)
737 }
738
739 want := map[string]int{"go": 1}
740 if !cmp.Equal(languages, want) {
741 t.Errorf("Repositories.ListLanguages returned %+v, want %+v", languages, want)
742 }
743
744 const methodName = "ListLanguages"
745 testBadOptions(t, methodName, func() (err error) {
746 _, _, err = client.Repositories.ListLanguages(ctx, "\n", "\n")
747 return err
748 })
749
750 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
751 got, resp, err := client.Repositories.ListLanguages(ctx, "o", "r")
752 if got != nil {
753 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
754 }
755 return resp, err
756 })
757 }
758
759 func TestRepositoriesService_ListTeams(t *testing.T) {
760 client, mux, _, teardown := setup()
761 defer teardown()
762
763 mux.HandleFunc("/repos/o/r/teams", func(w http.ResponseWriter, r *http.Request) {
764 testMethod(t, r, "GET")
765 testFormValues(t, r, values{"page": "2"})
766 fmt.Fprint(w, `[{"id":1}]`)
767 })
768
769 opt := &ListOptions{Page: 2}
770 ctx := context.Background()
771 teams, _, err := client.Repositories.ListTeams(ctx, "o", "r", opt)
772 if err != nil {
773 t.Errorf("Repositories.ListTeams returned error: %v", err)
774 }
775
776 want := []*Team{{ID: Int64(1)}}
777 if !cmp.Equal(teams, want) {
778 t.Errorf("Repositories.ListTeams returned %+v, want %+v", teams, want)
779 }
780
781 const methodName = "ListTeams"
782 testBadOptions(t, methodName, func() (err error) {
783 _, _, err = client.Repositories.ListTeams(ctx, "\n", "\n", opt)
784 return err
785 })
786
787 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
788 got, resp, err := client.Repositories.ListTeams(ctx, "o", "r", opt)
789 if got != nil {
790 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
791 }
792 return resp, err
793 })
794 }
795
796 func TestRepositoriesService_ListTags(t *testing.T) {
797 client, mux, _, teardown := setup()
798 defer teardown()
799
800 mux.HandleFunc("/repos/o/r/tags", func(w http.ResponseWriter, r *http.Request) {
801 testMethod(t, r, "GET")
802 testFormValues(t, r, values{"page": "2"})
803 fmt.Fprint(w, `[{"name":"n", "commit" : {"sha" : "s", "url" : "u"}, "zipball_url": "z", "tarball_url": "t"}]`)
804 })
805
806 opt := &ListOptions{Page: 2}
807 ctx := context.Background()
808 tags, _, err := client.Repositories.ListTags(ctx, "o", "r", opt)
809 if err != nil {
810 t.Errorf("Repositories.ListTags returned error: %v", err)
811 }
812
813 want := []*RepositoryTag{
814 {
815 Name: String("n"),
816 Commit: &Commit{
817 SHA: String("s"),
818 URL: String("u"),
819 },
820 ZipballURL: String("z"),
821 TarballURL: String("t"),
822 },
823 }
824 if !cmp.Equal(tags, want) {
825 t.Errorf("Repositories.ListTags returned %+v, want %+v", tags, want)
826 }
827
828 const methodName = "ListTags"
829 testBadOptions(t, methodName, func() (err error) {
830 _, _, err = client.Repositories.ListTags(ctx, "\n", "\n", opt)
831 return err
832 })
833
834 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
835 got, resp, err := client.Repositories.ListTags(ctx, "o", "r", opt)
836 if got != nil {
837 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
838 }
839 return resp, err
840 })
841 }
842
843 func TestRepositoriesService_ListBranches(t *testing.T) {
844 client, mux, _, teardown := setup()
845 defer teardown()
846
847 mux.HandleFunc("/repos/o/r/branches", func(w http.ResponseWriter, r *http.Request) {
848 testMethod(t, r, "GET")
849 testFormValues(t, r, values{"page": "2"})
850 fmt.Fprint(w, `[{"name":"master", "commit" : {"sha" : "a57781", "url" : "https://api.github.com/repos/o/r/commits/a57781"}}]`)
851 })
852
853 opt := &BranchListOptions{
854 Protected: nil,
855 ListOptions: ListOptions{Page: 2},
856 }
857 ctx := context.Background()
858 branches, _, err := client.Repositories.ListBranches(ctx, "o", "r", opt)
859 if err != nil {
860 t.Errorf("Repositories.ListBranches returned error: %v", err)
861 }
862
863 want := []*Branch{{Name: String("master"), Commit: &RepositoryCommit{SHA: String("a57781"), URL: String("https://api.github.com/repos/o/r/commits/a57781")}}}
864 if !cmp.Equal(branches, want) {
865 t.Errorf("Repositories.ListBranches returned %+v, want %+v", branches, want)
866 }
867
868 const methodName = "ListBranches"
869 testBadOptions(t, methodName, func() (err error) {
870 _, _, err = client.Repositories.ListBranches(ctx, "\n", "\n", opt)
871 return err
872 })
873
874 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
875 got, resp, err := client.Repositories.ListBranches(ctx, "o", "r", opt)
876 if got != nil {
877 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
878 }
879 return resp, err
880 })
881 }
882
883 func TestRepositoriesService_GetBranch(t *testing.T) {
884 client, mux, _, teardown := setup()
885 defer teardown()
886
887 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) {
888 testMethod(t, r, "GET")
889 fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true}`)
890 })
891
892 ctx := context.Background()
893 branch, _, err := client.Repositories.GetBranch(ctx, "o", "r", "b", false)
894 if err != nil {
895 t.Errorf("Repositories.GetBranch returned error: %v", err)
896 }
897
898 want := &Branch{
899 Name: String("n"),
900 Commit: &RepositoryCommit{
901 SHA: String("s"),
902 Commit: &Commit{
903 Message: String("m"),
904 },
905 },
906 Protected: Bool(true),
907 }
908
909 if !cmp.Equal(branch, want) {
910 t.Errorf("Repositories.GetBranch returned %+v, want %+v", branch, want)
911 }
912
913 const methodName = "GetBranch"
914 testBadOptions(t, methodName, func() (err error) {
915 _, _, err = client.Repositories.GetBranch(ctx, "\n", "\n", "\n", false)
916 return err
917 })
918 }
919
920 func TestRepositoriesService_GetBranch_BadJSONResponse(t *testing.T) {
921 client, mux, _, teardown := setup()
922 defer teardown()
923
924 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) {
925 testMethod(t, r, "GET")
926 fmt.Fprint(w, `{"name":"n", "commit":{"sha":...truncated`)
927 })
928
929 ctx := context.Background()
930 if _, _, err := client.Repositories.GetBranch(ctx, "o", "r", "b", false); err == nil {
931 t.Error("Repositories.GetBranch returned no error; wanted JSON error")
932 }
933 }
934
935 func TestRepositoriesService_GetBranch_StatusMovedPermanently_followRedirects(t *testing.T) {
936 client, mux, serverURL, teardown := setup()
937 defer teardown()
938
939 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) {
940 testMethod(t, r, "GET")
941 redirectURL, _ := url.Parse(serverURL + baseURLPath + "/repos/o/r/branches/br")
942 http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently)
943 })
944 mux.HandleFunc("/repos/o/r/branches/br", func(w http.ResponseWriter, r *http.Request) {
945 testMethod(t, r, "GET")
946 fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true}`)
947 })
948 ctx := context.Background()
949 branch, resp, err := client.Repositories.GetBranch(ctx, "o", "r", "b", true)
950 if err != nil {
951 t.Errorf("Repositories.GetBranch returned error: %v", err)
952 }
953 if resp.StatusCode != http.StatusOK {
954 t.Errorf("Repositories.GetBranch returned status: %d, want %d", resp.StatusCode, http.StatusOK)
955 }
956
957 want := &Branch{
958 Name: String("n"),
959 Commit: &RepositoryCommit{
960 SHA: String("s"),
961 Commit: &Commit{
962 Message: String("m"),
963 },
964 },
965 Protected: Bool(true),
966 }
967 if !cmp.Equal(branch, want) {
968 t.Errorf("Repositories.GetBranch returned %+v, want %+v", branch, want)
969 }
970 }
971
972 func TestRepositoriesService_GetBranch_notFound(t *testing.T) {
973 client, mux, _, teardown := setup()
974 defer teardown()
975
976 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) {
977 testMethod(t, r, "GET")
978 http.Error(w, "branch not found", http.StatusNotFound)
979 })
980 ctx := context.Background()
981 _, resp, err := client.Repositories.GetBranch(ctx, "o", "r", "b", true)
982 if err == nil {
983 t.Error("Repositories.GetBranch returned error: nil")
984 }
985 if resp.StatusCode != http.StatusNotFound {
986 t.Errorf("Repositories.GetBranch returned status: %d, want %d", resp.StatusCode, http.StatusNotFound)
987 }
988
989
990 client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
991 return nil, errors.New("failed to get branch")
992 })
993
994 const methodName = "GetBranch"
995 testBadOptions(t, methodName, func() (err error) {
996 _, _, err = client.Repositories.GetBranch(ctx, "\n", "\n", "\n", true)
997 return err
998 })
999 }
1000
1001 func TestRepositoriesService_RenameBranch(t *testing.T) {
1002 client, mux, _, teardown := setup()
1003 defer teardown()
1004
1005 renameBranchReq := "nn"
1006
1007 mux.HandleFunc("/repos/o/r/branches/b/rename", func(w http.ResponseWriter, r *http.Request) {
1008 v := new(renameBranchRequest)
1009 json.NewDecoder(r.Body).Decode(v)
1010
1011 testMethod(t, r, "POST")
1012 want := &renameBranchRequest{NewName: "nn"}
1013 if !cmp.Equal(v, want) {
1014 t.Errorf("Request body = %+v, want %+v", v, want)
1015 }
1016
1017 fmt.Fprint(w, `{"protected":true,"name":"nn"}`)
1018 })
1019
1020 ctx := context.Background()
1021 got, _, err := client.Repositories.RenameBranch(ctx, "o", "r", "b", renameBranchReq)
1022 if err != nil {
1023 t.Errorf("Repositories.RenameBranch returned error: %v", err)
1024 }
1025
1026 want := &Branch{Name: String("nn"), Protected: Bool(true)}
1027 if !cmp.Equal(got, want) {
1028 t.Errorf("Repositories.RenameBranch returned %+v, want %+v", got, want)
1029 }
1030
1031 const methodName = "RenameBranch"
1032 testBadOptions(t, methodName, func() (err error) {
1033 _, _, err = client.Repositories.RenameBranch(ctx, "\n", "\n", "\n", renameBranchReq)
1034 return err
1035 })
1036
1037 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1038 got, resp, err := client.Repositories.RenameBranch(ctx, "o", "r", "b", renameBranchReq)
1039 if got != nil {
1040 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1041 }
1042 return resp, err
1043 })
1044 }
1045
1046 func TestRepositoriesService_GetBranchProtection(t *testing.T) {
1047 client, mux, _, teardown := setup()
1048 defer teardown()
1049
1050 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1051 v := new(ProtectionRequest)
1052 json.NewDecoder(r.Body).Decode(v)
1053
1054 testMethod(t, r, "GET")
1055
1056 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1057 fmt.Fprintf(w, `{
1058 "required_status_checks":{
1059 "strict":true,
1060 "contexts":["continuous-integration"],
1061 "checks": [
1062 {
1063 "context": "continuous-integration",
1064 "app_id": null
1065 }
1066 ]
1067 },
1068 "required_pull_request_reviews":{
1069 "dismissal_restrictions":{
1070 "users":[{
1071 "id":3,
1072 "login":"u"
1073 }],
1074 "teams":[{
1075 "id":4,
1076 "slug":"t"
1077 }]
1078 },
1079 "dismiss_stale_reviews":true,
1080 "require_code_owner_reviews":true,
1081 "required_approving_review_count":1
1082 },
1083 "enforce_admins":{
1084 "url":"/repos/o/r/branches/b/protection/enforce_admins",
1085 "enabled":true
1086 },
1087 "restrictions":{
1088 "users":[{"id":1,"login":"u"}],
1089 "teams":[{"id":2,"slug":"t"}]
1090 },
1091 "required_conversation_resolution": {
1092 "enabled": true
1093 }
1094 }`)
1095 })
1096
1097 ctx := context.Background()
1098 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b")
1099 if err != nil {
1100 t.Errorf("Repositories.GetBranchProtection returned error: %v", err)
1101 }
1102
1103 want := &Protection{
1104 RequiredStatusChecks: &RequiredStatusChecks{
1105 Strict: true,
1106 Contexts: []string{"continuous-integration"},
1107 Checks: []*RequiredStatusCheck{
1108 {
1109 Context: "continuous-integration",
1110 },
1111 },
1112 },
1113 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1114 DismissStaleReviews: true,
1115 DismissalRestrictions: &DismissalRestrictions{
1116 Users: []*User{
1117 {Login: String("u"), ID: Int64(3)},
1118 },
1119 Teams: []*Team{
1120 {Slug: String("t"), ID: Int64(4)},
1121 },
1122 },
1123 RequireCodeOwnerReviews: true,
1124 RequiredApprovingReviewCount: 1,
1125 },
1126 EnforceAdmins: &AdminEnforcement{
1127 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
1128 Enabled: true,
1129 },
1130 Restrictions: &BranchRestrictions{
1131 Users: []*User{
1132 {Login: String("u"), ID: Int64(1)},
1133 },
1134 Teams: []*Team{
1135 {Slug: String("t"), ID: Int64(2)},
1136 },
1137 },
1138 RequiredConversationResolution: &RequiredConversationResolution{
1139 Enabled: true,
1140 },
1141 }
1142 if !cmp.Equal(protection, want) {
1143 t.Errorf("Repositories.GetBranchProtection returned %+v, want %+v", protection, want)
1144 }
1145
1146 const methodName = "GetBranchProtection"
1147 testBadOptions(t, methodName, func() (err error) {
1148 _, _, err = client.Repositories.GetBranchProtection(ctx, "\n", "\n", "\n")
1149 return err
1150 })
1151
1152 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1153 got, resp, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b")
1154 if got != nil {
1155 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1156 }
1157 return resp, err
1158 })
1159 }
1160
1161 func TestRepositoriesService_GetBranchProtection_noDismissalRestrictions(t *testing.T) {
1162 client, mux, _, teardown := setup()
1163 defer teardown()
1164
1165 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1166 testMethod(t, r, "GET")
1167
1168 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1169 fmt.Fprintf(w, `{
1170 "required_status_checks":{
1171 "strict":true,
1172 "contexts":["continuous-integration"],
1173 "checks": [
1174 {
1175 "context": "continuous-integration",
1176 "app_id": null
1177 }
1178 ]
1179 },
1180 "required_pull_request_reviews":{
1181 "dismiss_stale_reviews":true,
1182 "require_code_owner_reviews":true,
1183 "required_approving_review_count":1
1184 },
1185 "enforce_admins":{
1186 "url":"/repos/o/r/branches/b/protection/enforce_admins",
1187 "enabled":true
1188 },
1189 "restrictions":{
1190 "users":[{"id":1,"login":"u"}],
1191 "teams":[{"id":2,"slug":"t"}]
1192 }
1193 }`)
1194 })
1195
1196 ctx := context.Background()
1197 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b")
1198 if err != nil {
1199 t.Errorf("Repositories.GetBranchProtection returned error: %v", err)
1200 }
1201
1202 want := &Protection{
1203 RequiredStatusChecks: &RequiredStatusChecks{
1204 Strict: true,
1205 Contexts: []string{"continuous-integration"},
1206 Checks: []*RequiredStatusCheck{
1207 {
1208 Context: "continuous-integration",
1209 },
1210 },
1211 },
1212 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1213 DismissStaleReviews: true,
1214 DismissalRestrictions: nil,
1215 RequireCodeOwnerReviews: true,
1216 RequiredApprovingReviewCount: 1,
1217 },
1218 EnforceAdmins: &AdminEnforcement{
1219 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
1220 Enabled: true,
1221 },
1222 Restrictions: &BranchRestrictions{
1223 Users: []*User{
1224 {Login: String("u"), ID: Int64(1)},
1225 },
1226 Teams: []*Team{
1227 {Slug: String("t"), ID: Int64(2)},
1228 },
1229 },
1230 }
1231 if !cmp.Equal(protection, want) {
1232 t.Errorf("Repositories.GetBranchProtection returned %+v, want %+v", protection, want)
1233 }
1234 }
1235
1236 func TestRepositoriesService_GetBranchProtection_branchNotProtected(t *testing.T) {
1237 client, mux, _, teardown := setup()
1238 defer teardown()
1239
1240 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1241 testMethod(t, r, "GET")
1242
1243 w.WriteHeader(http.StatusBadRequest)
1244 fmt.Fprintf(w, `{
1245 "message": %q,
1246 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
1247 }`, githubBranchNotProtected)
1248 })
1249
1250 ctx := context.Background()
1251 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b")
1252
1253 if protection != nil {
1254 t.Errorf("Repositories.GetBranchProtection returned non-nil protection data")
1255 }
1256
1257 if err != ErrBranchNotProtected {
1258 t.Errorf("Repositories.GetBranchProtection returned an invalid error: %v", err)
1259 }
1260 }
1261
1262 func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) {
1263 client, mux, _, teardown := setup()
1264 defer teardown()
1265
1266 input := &ProtectionRequest{
1267 RequiredStatusChecks: &RequiredStatusChecks{
1268 Strict: true,
1269 Contexts: []string{"continuous-integration"},
1270 },
1271 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1272 DismissStaleReviews: true,
1273 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1274 Users: &[]string{"uu"},
1275 Teams: &[]string{"tt"},
1276 },
1277 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
1278 Users: []string{"uuu"},
1279 Teams: []string{"ttt"},
1280 Apps: []string{"aaa"},
1281 },
1282 },
1283 Restrictions: &BranchRestrictionsRequest{
1284 Users: []string{"u"},
1285 Teams: []string{"t"},
1286 Apps: []string{"a"},
1287 },
1288 }
1289
1290 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1291 v := new(ProtectionRequest)
1292 json.NewDecoder(r.Body).Decode(v)
1293
1294 testMethod(t, r, "PUT")
1295 if !cmp.Equal(v, input) {
1296 t.Errorf("Request body = %+v, want %+v", v, input)
1297 }
1298
1299
1300 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1301 fmt.Fprintf(w, `{
1302 "required_status_checks":{
1303 "strict":true,
1304 "contexts":["continuous-integration"],
1305 "checks": [
1306 {
1307 "context": "continuous-integration",
1308 "app_id": null
1309 }
1310 ]
1311 },
1312 "required_pull_request_reviews":{
1313 "dismissal_restrictions":{
1314 "users":[{
1315 "id":3,
1316 "login":"uu"
1317 }],
1318 "teams":[{
1319 "id":4,
1320 "slug":"tt"
1321 }]
1322 },
1323 "dismiss_stale_reviews":true,
1324 "require_code_owner_reviews":true,
1325 "bypass_pull_request_allowances": {
1326 "users":[{"id":10,"login":"uuu"}],
1327 "teams":[{"id":20,"slug":"ttt"}],
1328 "apps":[{"id":30,"slug":"aaa"}]
1329 }
1330 },
1331 "restrictions":{
1332 "users":[{"id":1,"login":"u"}],
1333 "teams":[{"id":2,"slug":"t"}],
1334 "apps":[{"id":3,"slug":"a"}]
1335 }
1336 }`)
1337 })
1338
1339 ctx := context.Background()
1340 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1341 if err != nil {
1342 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1343 }
1344
1345 want := &Protection{
1346 RequiredStatusChecks: &RequiredStatusChecks{
1347 Strict: true,
1348 Contexts: []string{"continuous-integration"},
1349 Checks: []*RequiredStatusCheck{
1350 {
1351 Context: "continuous-integration",
1352 },
1353 },
1354 },
1355 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1356 DismissStaleReviews: true,
1357 DismissalRestrictions: &DismissalRestrictions{
1358 Users: []*User{
1359 {Login: String("uu"), ID: Int64(3)},
1360 },
1361 Teams: []*Team{
1362 {Slug: String("tt"), ID: Int64(4)},
1363 },
1364 },
1365 RequireCodeOwnerReviews: true,
1366 BypassPullRequestAllowances: &BypassPullRequestAllowances{
1367 Users: []*User{
1368 {Login: String("uuu"), ID: Int64(10)},
1369 },
1370 Teams: []*Team{
1371 {Slug: String("ttt"), ID: Int64(20)},
1372 },
1373 Apps: []*App{
1374 {Slug: String("aaa"), ID: Int64(30)},
1375 },
1376 },
1377 },
1378 Restrictions: &BranchRestrictions{
1379 Users: []*User{
1380 {Login: String("u"), ID: Int64(1)},
1381 },
1382 Teams: []*Team{
1383 {Slug: String("t"), ID: Int64(2)},
1384 },
1385 Apps: []*App{
1386 {Slug: String("a"), ID: Int64(3)},
1387 },
1388 },
1389 }
1390 if !cmp.Equal(protection, want) {
1391 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1392 }
1393
1394 const methodName = "UpdateBranchProtection"
1395 testBadOptions(t, methodName, func() (err error) {
1396 _, _, err = client.Repositories.UpdateBranchProtection(ctx, "\n", "\n", "\n", input)
1397 return err
1398 })
1399
1400 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1401 got, resp, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1402 if got != nil {
1403 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1404 }
1405 return resp, err
1406 })
1407 }
1408
1409 func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
1410 client, mux, _, teardown := setup()
1411 defer teardown()
1412
1413 input := &ProtectionRequest{
1414 RequiredStatusChecks: &RequiredStatusChecks{
1415 Strict: true,
1416 Checks: []*RequiredStatusCheck{
1417 {
1418 Context: "continuous-integration",
1419 },
1420 },
1421 },
1422 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1423 DismissStaleReviews: true,
1424 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1425 Users: &[]string{"uu"},
1426 Teams: &[]string{"tt"},
1427 },
1428 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
1429 Users: []string{"uuu"},
1430 Teams: []string{"ttt"},
1431 Apps: []string{"aaa"},
1432 },
1433 },
1434 Restrictions: &BranchRestrictionsRequest{
1435 Users: []string{"u"},
1436 Teams: []string{"t"},
1437 Apps: []string{"a"},
1438 },
1439 }
1440
1441 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1442 v := new(ProtectionRequest)
1443 json.NewDecoder(r.Body).Decode(v)
1444
1445 testMethod(t, r, "PUT")
1446 if !cmp.Equal(v, input) {
1447 t.Errorf("Request body = %+v, want %+v", v, input)
1448 }
1449
1450
1451 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1452 fmt.Fprintf(w, `{
1453 "required_status_checks":{
1454 "strict":true,
1455 "contexts":["continuous-integration"],
1456 "checks": [
1457 {
1458 "context": "continuous-integration",
1459 "app_id": null
1460 }
1461 ]
1462 },
1463 "required_pull_request_reviews":{
1464 "dismissal_restrictions":{
1465 "users":[{
1466 "id":3,
1467 "login":"uu"
1468 }],
1469 "teams":[{
1470 "id":4,
1471 "slug":"tt"
1472 }]
1473 },
1474 "dismiss_stale_reviews":true,
1475 "require_code_owner_reviews":true,
1476 "bypass_pull_request_allowances": {
1477 "users":[{"id":10,"login":"uuu"}],
1478 "teams":[{"id":20,"slug":"ttt"}],
1479 "apps":[{"id":30,"slug":"aaa"}]
1480 }
1481 },
1482 "restrictions":{
1483 "users":[{"id":1,"login":"u"}],
1484 "teams":[{"id":2,"slug":"t"}],
1485 "apps":[{"id":3,"slug":"a"}]
1486 }
1487 }`)
1488 })
1489
1490 ctx := context.Background()
1491 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1492 if err != nil {
1493 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1494 }
1495
1496 want := &Protection{
1497 RequiredStatusChecks: &RequiredStatusChecks{
1498 Strict: true,
1499 Contexts: []string{"continuous-integration"},
1500 Checks: []*RequiredStatusCheck{
1501 {
1502 Context: "continuous-integration",
1503 },
1504 },
1505 },
1506 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1507 DismissStaleReviews: true,
1508 DismissalRestrictions: &DismissalRestrictions{
1509 Users: []*User{
1510 {Login: String("uu"), ID: Int64(3)},
1511 },
1512 Teams: []*Team{
1513 {Slug: String("tt"), ID: Int64(4)},
1514 },
1515 },
1516 RequireCodeOwnerReviews: true,
1517 BypassPullRequestAllowances: &BypassPullRequestAllowances{
1518 Users: []*User{
1519 {Login: String("uuu"), ID: Int64(10)},
1520 },
1521 Teams: []*Team{
1522 {Slug: String("ttt"), ID: Int64(20)},
1523 },
1524 Apps: []*App{
1525 {Slug: String("aaa"), ID: Int64(30)},
1526 },
1527 },
1528 },
1529 Restrictions: &BranchRestrictions{
1530 Users: []*User{
1531 {Login: String("u"), ID: Int64(1)},
1532 },
1533 Teams: []*Team{
1534 {Slug: String("t"), ID: Int64(2)},
1535 },
1536 Apps: []*App{
1537 {Slug: String("a"), ID: Int64(3)},
1538 },
1539 },
1540 }
1541 if !cmp.Equal(protection, want) {
1542 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1543 }
1544 }
1545
1546 func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T) {
1547 client, mux, _, teardown := setup()
1548 defer teardown()
1549
1550 input := &ProtectionRequest{
1551 RequiredStatusChecks: &RequiredStatusChecks{
1552 Strict: true,
1553 Checks: []*RequiredStatusCheck{},
1554 },
1555 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1556 DismissStaleReviews: true,
1557 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1558 Users: &[]string{"uu"},
1559 Teams: &[]string{"tt"},
1560 },
1561 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
1562 Users: []string{"uuu"},
1563 Teams: []string{"ttt"},
1564 Apps: []string{"aaa"},
1565 },
1566 },
1567 Restrictions: &BranchRestrictionsRequest{
1568 Users: []string{"u"},
1569 Teams: []string{"t"},
1570 Apps: []string{"a"},
1571 },
1572 }
1573
1574 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1575 v := new(ProtectionRequest)
1576 json.NewDecoder(r.Body).Decode(v)
1577
1578 testMethod(t, r, "PUT")
1579 if !cmp.Equal(v, input) {
1580 t.Errorf("Request body = %+v, want %+v", v, input)
1581 }
1582
1583
1584 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1585 fmt.Fprintf(w, `{
1586 "required_status_checks":{
1587 "strict":true,
1588 "contexts":[],
1589 "checks": []
1590 },
1591 "required_pull_request_reviews":{
1592 "dismissal_restrictions":{
1593 "users":[{
1594 "id":3,
1595 "login":"uu"
1596 }],
1597 "teams":[{
1598 "id":4,
1599 "slug":"tt"
1600 }]
1601 },
1602 "dismiss_stale_reviews":true,
1603 "require_code_owner_reviews":true,
1604 "bypass_pull_request_allowances": {
1605 "users":[{"id":10,"login":"uuu"}],
1606 "teams":[{"id":20,"slug":"ttt"}],
1607 "apps":[{"id":30,"slug":"aaa"}]
1608 }
1609 },
1610 "restrictions":{
1611 "users":[{"id":1,"login":"u"}],
1612 "teams":[{"id":2,"slug":"t"}],
1613 "apps":[{"id":3,"slug":"a"}]
1614 }
1615 }`)
1616 })
1617
1618 ctx := context.Background()
1619 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1620 if err != nil {
1621 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1622 }
1623
1624 want := &Protection{
1625 RequiredStatusChecks: &RequiredStatusChecks{
1626 Strict: true,
1627 Contexts: []string{},
1628 Checks: []*RequiredStatusCheck{},
1629 },
1630 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1631 DismissStaleReviews: true,
1632 DismissalRestrictions: &DismissalRestrictions{
1633 Users: []*User{
1634 {Login: String("uu"), ID: Int64(3)},
1635 },
1636 Teams: []*Team{
1637 {Slug: String("tt"), ID: Int64(4)},
1638 },
1639 },
1640 RequireCodeOwnerReviews: true,
1641 BypassPullRequestAllowances: &BypassPullRequestAllowances{
1642 Users: []*User{
1643 {Login: String("uuu"), ID: Int64(10)},
1644 },
1645 Teams: []*Team{
1646 {Slug: String("ttt"), ID: Int64(20)},
1647 },
1648 Apps: []*App{
1649 {Slug: String("aaa"), ID: Int64(30)},
1650 },
1651 },
1652 },
1653 Restrictions: &BranchRestrictions{
1654 Users: []*User{
1655 {Login: String("u"), ID: Int64(1)},
1656 },
1657 Teams: []*Team{
1658 {Slug: String("t"), ID: Int64(2)},
1659 },
1660 Apps: []*App{
1661 {Slug: String("a"), ID: Int64(3)},
1662 },
1663 },
1664 }
1665 if !cmp.Equal(protection, want) {
1666 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1667 }
1668 }
1669
1670 func TestRepositoriesService_RemoveBranchProtection(t *testing.T) {
1671 client, mux, _, teardown := setup()
1672 defer teardown()
1673
1674 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1675 testMethod(t, r, "DELETE")
1676 w.WriteHeader(http.StatusNoContent)
1677 })
1678
1679 ctx := context.Background()
1680 _, err := client.Repositories.RemoveBranchProtection(ctx, "o", "r", "b")
1681 if err != nil {
1682 t.Errorf("Repositories.RemoveBranchProtection returned error: %v", err)
1683 }
1684
1685 const methodName = "RemoveBranchProtection"
1686 testBadOptions(t, methodName, func() (err error) {
1687 _, err = client.Repositories.RemoveBranchProtection(ctx, "\n", "\n", "\n")
1688 return err
1689 })
1690
1691 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1692 return client.Repositories.RemoveBranchProtection(ctx, "o", "r", "b")
1693 })
1694 }
1695
1696 func TestRepositoriesService_ListLanguages_invalidOwner(t *testing.T) {
1697 client, _, _, teardown := setup()
1698 defer teardown()
1699
1700 ctx := context.Background()
1701 _, _, err := client.Repositories.ListLanguages(ctx, "%", "%")
1702 testURLParseError(t, err)
1703 }
1704
1705 func TestRepositoriesService_License(t *testing.T) {
1706 client, mux, _, teardown := setup()
1707 defer teardown()
1708
1709 mux.HandleFunc("/repos/o/r/license", func(w http.ResponseWriter, r *http.Request) {
1710 testMethod(t, r, "GET")
1711 fmt.Fprint(w, `{"name": "LICENSE", "path": "LICENSE", "license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","featured":true}}`)
1712 })
1713
1714 ctx := context.Background()
1715 got, _, err := client.Repositories.License(ctx, "o", "r")
1716 if err != nil {
1717 t.Errorf("Repositories.License returned error: %v", err)
1718 }
1719
1720 want := &RepositoryLicense{
1721 Name: String("LICENSE"),
1722 Path: String("LICENSE"),
1723 License: &License{
1724 Name: String("MIT License"),
1725 Key: String("mit"),
1726 SPDXID: String("MIT"),
1727 URL: String("https://api.github.com/licenses/mit"),
1728 Featured: Bool(true),
1729 },
1730 }
1731
1732 if !cmp.Equal(got, want) {
1733 t.Errorf("Repositories.License returned %+v, want %+v", got, want)
1734 }
1735
1736 const methodName = "License"
1737 testBadOptions(t, methodName, func() (err error) {
1738 _, _, err = client.Repositories.License(ctx, "\n", "\n")
1739 return err
1740 })
1741
1742 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1743 got, resp, err := client.Repositories.License(ctx, "o", "r")
1744 if got != nil {
1745 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1746 }
1747 return resp, err
1748 })
1749 }
1750
1751 func TestRepositoriesService_GetRequiredStatusChecks(t *testing.T) {
1752 client, mux, _, teardown := setup()
1753 defer teardown()
1754
1755 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1756 v := new(ProtectionRequest)
1757 json.NewDecoder(r.Body).Decode(v)
1758
1759 testMethod(t, r, "GET")
1760 fmt.Fprint(w, `{
1761 "strict": true,
1762 "contexts": ["x","y","z"],
1763 "checks": [
1764 {
1765 "context": "x",
1766 "app_id": null
1767 },
1768 {
1769 "context": "y",
1770 "app_id": null
1771 },
1772 {
1773 "context": "z",
1774 "app_id": null
1775 }
1776 ]
1777 }`)
1778 })
1779
1780 ctx := context.Background()
1781 checks, _, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1782 if err != nil {
1783 t.Errorf("Repositories.GetRequiredStatusChecks returned error: %v", err)
1784 }
1785
1786 want := &RequiredStatusChecks{
1787 Strict: true,
1788 Contexts: []string{"x", "y", "z"},
1789 Checks: []*RequiredStatusCheck{
1790 {
1791 Context: "x",
1792 },
1793 {
1794 Context: "y",
1795 },
1796 {
1797 Context: "z",
1798 },
1799 },
1800 }
1801 if !cmp.Equal(checks, want) {
1802 t.Errorf("Repositories.GetRequiredStatusChecks returned %+v, want %+v", checks, want)
1803 }
1804
1805 const methodName = "GetRequiredStatusChecks"
1806 testBadOptions(t, methodName, func() (err error) {
1807 _, _, err = client.Repositories.GetRequiredStatusChecks(ctx, "\n", "\n", "\n")
1808 return err
1809 })
1810
1811 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1812 got, resp, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1813 if got != nil {
1814 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1815 }
1816 return resp, err
1817 })
1818 }
1819
1820 func TestRepositoriesService_GetRequiredStatusChecks_branchNotProtected(t *testing.T) {
1821 client, mux, _, teardown := setup()
1822 defer teardown()
1823
1824 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1825 testMethod(t, r, "GET")
1826
1827 w.WriteHeader(http.StatusBadRequest)
1828 fmt.Fprintf(w, `{
1829 "message": %q,
1830 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
1831 }`, githubBranchNotProtected)
1832 })
1833
1834 ctx := context.Background()
1835 checks, _, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1836
1837 if checks != nil {
1838 t.Errorf("Repositories.GetRequiredStatusChecks returned non-nil status-checks data")
1839 }
1840
1841 if err != ErrBranchNotProtected {
1842 t.Errorf("Repositories.GetRequiredStatusChecks returned an invalid error: %v", err)
1843 }
1844 }
1845
1846 func TestRepositoriesService_UpdateRequiredStatusChecks_Contexts(t *testing.T) {
1847 client, mux, _, teardown := setup()
1848 defer teardown()
1849
1850 input := &RequiredStatusChecksRequest{
1851 Strict: Bool(true),
1852 Contexts: []string{"continuous-integration"},
1853 }
1854
1855 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1856 v := new(RequiredStatusChecksRequest)
1857 json.NewDecoder(r.Body).Decode(v)
1858
1859 testMethod(t, r, "PATCH")
1860 if !cmp.Equal(v, input) {
1861 t.Errorf("Request body = %+v, want %+v", v, input)
1862 }
1863 testHeader(t, r, "Accept", mediaTypeV3)
1864 fmt.Fprintf(w, `{
1865 "strict":true,
1866 "contexts":["continuous-integration"],
1867 "checks": [
1868 {
1869 "context": "continuous-integration",
1870 "app_id": null
1871 }
1872 ]
1873 }`)
1874 })
1875
1876 ctx := context.Background()
1877 statusChecks, _, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
1878 if err != nil {
1879 t.Errorf("Repositories.UpdateRequiredStatusChecks returned error: %v", err)
1880 }
1881
1882 want := &RequiredStatusChecks{
1883 Strict: true,
1884 Contexts: []string{"continuous-integration"},
1885 Checks: []*RequiredStatusCheck{
1886 {
1887 Context: "continuous-integration",
1888 },
1889 },
1890 }
1891 if !cmp.Equal(statusChecks, want) {
1892 t.Errorf("Repositories.UpdateRequiredStatusChecks returned %+v, want %+v", statusChecks, want)
1893 }
1894
1895 const methodName = "UpdateRequiredStatusChecks"
1896 testBadOptions(t, methodName, func() (err error) {
1897 _, _, err = client.Repositories.UpdateRequiredStatusChecks(ctx, "\n", "\n", "\n", input)
1898 return err
1899 })
1900
1901 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1902 got, resp, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
1903 if got != nil {
1904 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1905 }
1906 return resp, err
1907 })
1908 }
1909
1910 func TestRepositoriesService_UpdateRequiredStatusChecks_Checks(t *testing.T) {
1911 client, mux, _, teardown := setup()
1912 defer teardown()
1913
1914 appID := int64(123)
1915 noAppID := int64(-1)
1916 input := &RequiredStatusChecksRequest{
1917 Strict: Bool(true),
1918 Checks: []*RequiredStatusCheck{
1919 {
1920 Context: "continuous-integration",
1921 },
1922 {
1923 Context: "continuous-integration2",
1924 AppID: &appID,
1925 },
1926 {
1927 Context: "continuous-integration3",
1928 AppID: &noAppID,
1929 },
1930 },
1931 }
1932
1933 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1934 v := new(RequiredStatusChecksRequest)
1935 json.NewDecoder(r.Body).Decode(v)
1936
1937 testMethod(t, r, "PATCH")
1938 if !cmp.Equal(v, input) {
1939 t.Errorf("Request body = %+v, want %+v", v, input)
1940 }
1941 testHeader(t, r, "Accept", mediaTypeV3)
1942 fmt.Fprintf(w, `{
1943 "strict":true,
1944 "contexts":["continuous-integration"],
1945 "checks": [
1946 {
1947 "context": "continuous-integration",
1948 "app_id": null
1949 },
1950 {
1951 "context": "continuous-integration2",
1952 "app_id": 123
1953 },
1954 {
1955 "context": "continuous-integration3",
1956 "app_id": null
1957 }
1958 ]
1959 }`)
1960 })
1961
1962 ctx := context.Background()
1963 statusChecks, _, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
1964 if err != nil {
1965 t.Errorf("Repositories.UpdateRequiredStatusChecks returned error: %v", err)
1966 }
1967
1968 want := &RequiredStatusChecks{
1969 Strict: true,
1970 Contexts: []string{"continuous-integration"},
1971 Checks: []*RequiredStatusCheck{
1972 {
1973 Context: "continuous-integration",
1974 },
1975 {
1976 Context: "continuous-integration2",
1977 AppID: &appID,
1978 },
1979 {
1980 Context: "continuous-integration3",
1981 },
1982 },
1983 }
1984 if !cmp.Equal(statusChecks, want) {
1985 t.Errorf("Repositories.UpdateRequiredStatusChecks returned %+v, want %+v", statusChecks, want)
1986 }
1987 }
1988
1989 func TestRepositoriesService_RemoveRequiredStatusChecks(t *testing.T) {
1990 client, mux, _, teardown := setup()
1991 defer teardown()
1992
1993 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1994 testMethod(t, r, "DELETE")
1995 testHeader(t, r, "Accept", mediaTypeV3)
1996 w.WriteHeader(http.StatusNoContent)
1997 })
1998
1999 ctx := context.Background()
2000 _, err := client.Repositories.RemoveRequiredStatusChecks(ctx, "o", "r", "b")
2001 if err != nil {
2002 t.Errorf("Repositories.RemoveRequiredStatusChecks returned error: %v", err)
2003 }
2004
2005 const methodName = "RemoveRequiredStatusChecks"
2006 testBadOptions(t, methodName, func() (err error) {
2007 _, err = client.Repositories.RemoveRequiredStatusChecks(ctx, "\n", "\n", "\n")
2008 return err
2009 })
2010
2011 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2012 return client.Repositories.RemoveRequiredStatusChecks(ctx, "o", "r", "b")
2013 })
2014 }
2015
2016 func TestRepositoriesService_ListRequiredStatusChecksContexts(t *testing.T) {
2017 client, mux, _, teardown := setup()
2018 defer teardown()
2019
2020 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) {
2021 v := new(ProtectionRequest)
2022 json.NewDecoder(r.Body).Decode(v)
2023
2024 testMethod(t, r, "GET")
2025 fmt.Fprint(w, `["x", "y", "z"]`)
2026 })
2027
2028 ctx := context.Background()
2029 contexts, _, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
2030 if err != nil {
2031 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned error: %v", err)
2032 }
2033
2034 want := []string{"x", "y", "z"}
2035 if !cmp.Equal(contexts, want) {
2036 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned %+v, want %+v", contexts, want)
2037 }
2038
2039 const methodName = "ListRequiredStatusChecksContexts"
2040 testBadOptions(t, methodName, func() (err error) {
2041 _, _, err = client.Repositories.ListRequiredStatusChecksContexts(ctx, "\n", "\n", "\n")
2042 return err
2043 })
2044
2045 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2046 got, resp, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
2047 if got != nil {
2048 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2049 }
2050 return resp, err
2051 })
2052 }
2053
2054 func TestRepositoriesService_ListRequiredStatusChecksContexts_branchNotProtected(t *testing.T) {
2055 client, mux, _, teardown := setup()
2056 defer teardown()
2057
2058 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) {
2059 testMethod(t, r, "GET")
2060
2061 w.WriteHeader(http.StatusBadRequest)
2062 fmt.Fprintf(w, `{
2063 "message": %q,
2064 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
2065 }`, githubBranchNotProtected)
2066 })
2067
2068 ctx := context.Background()
2069 contexts, _, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
2070
2071 if contexts != nil {
2072 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned non-nil contexts data")
2073 }
2074
2075 if err != ErrBranchNotProtected {
2076 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned an invalid error: %v", err)
2077 }
2078 }
2079
2080 func TestRepositoriesService_GetPullRequestReviewEnforcement(t *testing.T) {
2081 client, mux, _, teardown := setup()
2082 defer teardown()
2083
2084 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2085 testMethod(t, r, "GET")
2086
2087 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
2088 fmt.Fprintf(w, `{
2089 "dismissal_restrictions":{
2090 "users":[{"id":1,"login":"u"}],
2091 "teams":[{"id":2,"slug":"t"}]
2092 },
2093 "dismiss_stale_reviews":true,
2094 "require_code_owner_reviews":true,
2095 "required_approving_review_count":1
2096 }`)
2097 })
2098
2099 ctx := context.Background()
2100 enforcement, _, err := client.Repositories.GetPullRequestReviewEnforcement(ctx, "o", "r", "b")
2101 if err != nil {
2102 t.Errorf("Repositories.GetPullRequestReviewEnforcement returned error: %v", err)
2103 }
2104
2105 want := &PullRequestReviewsEnforcement{
2106 DismissStaleReviews: true,
2107 DismissalRestrictions: &DismissalRestrictions{
2108 Users: []*User{
2109 {Login: String("u"), ID: Int64(1)},
2110 },
2111 Teams: []*Team{
2112 {Slug: String("t"), ID: Int64(2)},
2113 },
2114 },
2115 RequireCodeOwnerReviews: true,
2116 RequiredApprovingReviewCount: 1,
2117 }
2118
2119 if !cmp.Equal(enforcement, want) {
2120 t.Errorf("Repositories.GetPullRequestReviewEnforcement returned %+v, want %+v", enforcement, want)
2121 }
2122
2123 const methodName = "GetPullRequestReviewEnforcement"
2124 testBadOptions(t, methodName, func() (err error) {
2125 _, _, err = client.Repositories.GetPullRequestReviewEnforcement(ctx, "\n", "\n", "\n")
2126 return err
2127 })
2128
2129 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2130 got, resp, err := client.Repositories.GetPullRequestReviewEnforcement(ctx, "o", "r", "b")
2131 if got != nil {
2132 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2133 }
2134 return resp, err
2135 })
2136 }
2137
2138 func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) {
2139 client, mux, _, teardown := setup()
2140 defer teardown()
2141
2142 input := &PullRequestReviewsEnforcementUpdate{
2143 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
2144 Users: &[]string{"u"},
2145 Teams: &[]string{"t"},
2146 },
2147 }
2148
2149 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2150 v := new(PullRequestReviewsEnforcementUpdate)
2151 json.NewDecoder(r.Body).Decode(v)
2152
2153 testMethod(t, r, "PATCH")
2154 if !cmp.Equal(v, input) {
2155 t.Errorf("Request body = %+v, want %+v", v, input)
2156 }
2157
2158 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
2159 fmt.Fprintf(w, `{
2160 "dismissal_restrictions":{
2161 "users":[{"id":1,"login":"u"}],
2162 "teams":[{"id":2,"slug":"t"}]
2163 },
2164 "dismiss_stale_reviews":true,
2165 "require_code_owner_reviews":true,
2166 "required_approving_review_count":3
2167 }`)
2168 })
2169
2170 ctx := context.Background()
2171 enforcement, _, err := client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "o", "r", "b", input)
2172 if err != nil {
2173 t.Errorf("Repositories.UpdatePullRequestReviewEnforcement returned error: %v", err)
2174 }
2175
2176 want := &PullRequestReviewsEnforcement{
2177 DismissStaleReviews: true,
2178 DismissalRestrictions: &DismissalRestrictions{
2179 Users: []*User{
2180 {Login: String("u"), ID: Int64(1)},
2181 },
2182 Teams: []*Team{
2183 {Slug: String("t"), ID: Int64(2)},
2184 },
2185 },
2186 RequireCodeOwnerReviews: true,
2187 RequiredApprovingReviewCount: 3,
2188 }
2189 if !cmp.Equal(enforcement, want) {
2190 t.Errorf("Repositories.UpdatePullRequestReviewEnforcement returned %+v, want %+v", enforcement, want)
2191 }
2192
2193 const methodName = "UpdatePullRequestReviewEnforcement"
2194 testBadOptions(t, methodName, func() (err error) {
2195 _, _, err = client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "\n", "\n", "\n", input)
2196 return err
2197 })
2198
2199 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2200 got, resp, err := client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "o", "r", "b", input)
2201 if got != nil {
2202 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2203 }
2204 return resp, err
2205 })
2206 }
2207
2208 func TestRepositoriesService_DisableDismissalRestrictions(t *testing.T) {
2209 client, mux, _, teardown := setup()
2210 defer teardown()
2211
2212 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2213 testMethod(t, r, "PATCH")
2214
2215 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
2216 testBody(t, r, `{"dismissal_restrictions":{}}`+"\n")
2217 fmt.Fprintf(w, `{"dismiss_stale_reviews":true,"require_code_owner_reviews":true,"required_approving_review_count":1}`)
2218 })
2219
2220 ctx := context.Background()
2221 enforcement, _, err := client.Repositories.DisableDismissalRestrictions(ctx, "o", "r", "b")
2222 if err != nil {
2223 t.Errorf("Repositories.DisableDismissalRestrictions returned error: %v", err)
2224 }
2225
2226 want := &PullRequestReviewsEnforcement{
2227 DismissStaleReviews: true,
2228 DismissalRestrictions: nil,
2229 RequireCodeOwnerReviews: true,
2230 RequiredApprovingReviewCount: 1,
2231 }
2232 if !cmp.Equal(enforcement, want) {
2233 t.Errorf("Repositories.DisableDismissalRestrictions returned %+v, want %+v", enforcement, want)
2234 }
2235
2236 const methodName = "DisableDismissalRestrictions"
2237 testBadOptions(t, methodName, func() (err error) {
2238 _, _, err = client.Repositories.DisableDismissalRestrictions(ctx, "\n", "\n", "\n")
2239 return err
2240 })
2241
2242 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2243 got, resp, err := client.Repositories.DisableDismissalRestrictions(ctx, "o", "r", "b")
2244 if got != nil {
2245 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2246 }
2247 return resp, err
2248 })
2249 }
2250
2251 func TestRepositoriesService_RemovePullRequestReviewEnforcement(t *testing.T) {
2252 client, mux, _, teardown := setup()
2253 defer teardown()
2254
2255 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2256 testMethod(t, r, "DELETE")
2257 w.WriteHeader(http.StatusNoContent)
2258 })
2259
2260 ctx := context.Background()
2261 _, err := client.Repositories.RemovePullRequestReviewEnforcement(ctx, "o", "r", "b")
2262 if err != nil {
2263 t.Errorf("Repositories.RemovePullRequestReviewEnforcement returned error: %v", err)
2264 }
2265
2266 const methodName = "RemovePullRequestReviewEnforcement"
2267 testBadOptions(t, methodName, func() (err error) {
2268 _, err = client.Repositories.RemovePullRequestReviewEnforcement(ctx, "\n", "\n", "\n")
2269 return err
2270 })
2271
2272 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2273 return client.Repositories.RemovePullRequestReviewEnforcement(ctx, "o", "r", "b")
2274 })
2275 }
2276
2277 func TestRepositoriesService_GetAdminEnforcement(t *testing.T) {
2278 client, mux, _, teardown := setup()
2279 defer teardown()
2280
2281 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2282 testMethod(t, r, "GET")
2283 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true}`)
2284 })
2285
2286 ctx := context.Background()
2287 enforcement, _, err := client.Repositories.GetAdminEnforcement(ctx, "o", "r", "b")
2288 if err != nil {
2289 t.Errorf("Repositories.GetAdminEnforcement returned error: %v", err)
2290 }
2291
2292 want := &AdminEnforcement{
2293 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
2294 Enabled: true,
2295 }
2296
2297 if !cmp.Equal(enforcement, want) {
2298 t.Errorf("Repositories.GetAdminEnforcement returned %+v, want %+v", enforcement, want)
2299 }
2300
2301 const methodName = "GetAdminEnforcement"
2302 testBadOptions(t, methodName, func() (err error) {
2303 _, _, err = client.Repositories.GetAdminEnforcement(ctx, "\n", "\n", "\n")
2304 return err
2305 })
2306
2307 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2308 got, resp, err := client.Repositories.GetAdminEnforcement(ctx, "o", "r", "b")
2309 if got != nil {
2310 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2311 }
2312 return resp, err
2313 })
2314 }
2315
2316 func TestRepositoriesService_AddAdminEnforcement(t *testing.T) {
2317 client, mux, _, teardown := setup()
2318 defer teardown()
2319
2320 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2321 testMethod(t, r, "POST")
2322 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true}`)
2323 })
2324
2325 ctx := context.Background()
2326 enforcement, _, err := client.Repositories.AddAdminEnforcement(ctx, "o", "r", "b")
2327 if err != nil {
2328 t.Errorf("Repositories.AddAdminEnforcement returned error: %v", err)
2329 }
2330
2331 want := &AdminEnforcement{
2332 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
2333 Enabled: true,
2334 }
2335 if !cmp.Equal(enforcement, want) {
2336 t.Errorf("Repositories.AddAdminEnforcement returned %+v, want %+v", enforcement, want)
2337 }
2338
2339 const methodName = "AddAdminEnforcement"
2340 testBadOptions(t, methodName, func() (err error) {
2341 _, _, err = client.Repositories.AddAdminEnforcement(ctx, "\n", "\n", "\n")
2342 return err
2343 })
2344
2345 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2346 got, resp, err := client.Repositories.AddAdminEnforcement(ctx, "o", "r", "b")
2347 if got != nil {
2348 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2349 }
2350 return resp, err
2351 })
2352 }
2353
2354 func TestRepositoriesService_RemoveAdminEnforcement(t *testing.T) {
2355 client, mux, _, teardown := setup()
2356 defer teardown()
2357
2358 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2359 testMethod(t, r, "DELETE")
2360 w.WriteHeader(http.StatusNoContent)
2361 })
2362
2363 ctx := context.Background()
2364 _, err := client.Repositories.RemoveAdminEnforcement(ctx, "o", "r", "b")
2365 if err != nil {
2366 t.Errorf("Repositories.RemoveAdminEnforcement returned error: %v", err)
2367 }
2368
2369 const methodName = "RemoveAdminEnforcement"
2370 testBadOptions(t, methodName, func() (err error) {
2371 _, err = client.Repositories.RemoveAdminEnforcement(ctx, "\n", "\n", "\n")
2372 return err
2373 })
2374
2375 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2376 return client.Repositories.RemoveAdminEnforcement(ctx, "o", "r", "b")
2377 })
2378 }
2379
2380 func TestRepositoriesService_GetSignaturesProtectedBranch(t *testing.T) {
2381 client, mux, _, teardown := setup()
2382 defer teardown()
2383
2384 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2385 testMethod(t, r, "GET")
2386 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2387 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/required_signatures","enabled":false}`)
2388 })
2389
2390 ctx := context.Background()
2391 signature, _, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", "b")
2392 if err != nil {
2393 t.Errorf("Repositories.GetSignaturesProtectedBranch returned error: %v", err)
2394 }
2395
2396 want := &SignaturesProtectedBranch{
2397 URL: String("/repos/o/r/branches/b/protection/required_signatures"),
2398 Enabled: Bool(false),
2399 }
2400
2401 if !cmp.Equal(signature, want) {
2402 t.Errorf("Repositories.GetSignaturesProtectedBranch returned %+v, want %+v", signature, want)
2403 }
2404
2405 const methodName = "GetSignaturesProtectedBranch"
2406 testBadOptions(t, methodName, func() (err error) {
2407 _, _, err = client.Repositories.GetSignaturesProtectedBranch(ctx, "\n", "\n", "\n")
2408 return err
2409 })
2410
2411 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2412 got, resp, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", "b")
2413 if got != nil {
2414 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2415 }
2416 return resp, err
2417 })
2418 }
2419
2420 func TestRepositoriesService_RequireSignaturesOnProtectedBranch(t *testing.T) {
2421 client, mux, _, teardown := setup()
2422 defer teardown()
2423
2424 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2425 testMethod(t, r, "POST")
2426 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2427 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/required_signatures","enabled":true}`)
2428 })
2429
2430 ctx := context.Background()
2431 signature, _, err := client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2432 if err != nil {
2433 t.Errorf("Repositories.RequireSignaturesOnProtectedBranch returned error: %v", err)
2434 }
2435
2436 want := &SignaturesProtectedBranch{
2437 URL: String("/repos/o/r/branches/b/protection/required_signatures"),
2438 Enabled: Bool(true),
2439 }
2440
2441 if !cmp.Equal(signature, want) {
2442 t.Errorf("Repositories.RequireSignaturesOnProtectedBranch returned %+v, want %+v", signature, want)
2443 }
2444
2445 const methodName = "RequireSignaturesOnProtectedBranch"
2446 testBadOptions(t, methodName, func() (err error) {
2447 _, _, err = client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "\n", "\n", "\n")
2448 return err
2449 })
2450
2451 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2452 got, resp, err := client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2453 if got != nil {
2454 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2455 }
2456 return resp, err
2457 })
2458 }
2459
2460 func TestRepositoriesService_OptionalSignaturesOnProtectedBranch(t *testing.T) {
2461 client, mux, _, teardown := setup()
2462 defer teardown()
2463
2464 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2465 testMethod(t, r, "DELETE")
2466 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2467 w.WriteHeader(http.StatusNoContent)
2468 })
2469
2470 ctx := context.Background()
2471 _, err := client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2472 if err != nil {
2473 t.Errorf("Repositories.OptionalSignaturesOnProtectedBranch returned error: %v", err)
2474 }
2475
2476 const methodName = "OptionalSignaturesOnProtectedBranch"
2477 testBadOptions(t, methodName, func() (err error) {
2478 _, err = client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "\n", "\n", "\n")
2479 return err
2480 })
2481
2482 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2483 return client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2484 })
2485 }
2486
2487 func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctions(t *testing.T) {
2488 req := PullRequestReviewsEnforcementRequest{}
2489
2490 got, err := json.Marshal(req)
2491 if err != nil {
2492 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2493 }
2494
2495 want := `{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2496 if want != string(got) {
2497 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2498 }
2499
2500 req = PullRequestReviewsEnforcementRequest{
2501 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{},
2502 }
2503
2504 got, err = json.Marshal(req)
2505 if err != nil {
2506 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2507 }
2508
2509 want = `{"dismissal_restrictions":{},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2510 if want != string(got) {
2511 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2512 }
2513
2514 req = PullRequestReviewsEnforcementRequest{
2515 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
2516 Users: &[]string{},
2517 Teams: &[]string{},
2518 },
2519 }
2520
2521 got, err = json.Marshal(req)
2522 if err != nil {
2523 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2524 }
2525
2526 want = `{"dismissal_restrictions":{"users":[],"teams":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2527 if want != string(got) {
2528 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2529 }
2530 }
2531
2532 func TestRepositoriesService_ListAllTopics(t *testing.T) {
2533 client, mux, _, teardown := setup()
2534 defer teardown()
2535
2536 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2537 testMethod(t, r, "GET")
2538 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2539 fmt.Fprint(w, `{"names":["go", "go-github", "github"]}`)
2540 })
2541
2542 ctx := context.Background()
2543 got, _, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2544 if err != nil {
2545 t.Fatalf("Repositories.ListAllTopics returned error: %v", err)
2546 }
2547
2548 want := []string{"go", "go-github", "github"}
2549 if !cmp.Equal(got, want) {
2550 t.Errorf("Repositories.ListAllTopics returned %+v, want %+v", got, want)
2551 }
2552
2553 const methodName = "ListAllTopics"
2554 testBadOptions(t, methodName, func() (err error) {
2555 _, _, err = client.Repositories.ListAllTopics(ctx, "\n", "\n")
2556 return err
2557 })
2558
2559 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2560 got, resp, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2561 if got != nil {
2562 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2563 }
2564 return resp, err
2565 })
2566 }
2567
2568 func TestRepositoriesService_ListAllTopics_emptyTopics(t *testing.T) {
2569 client, mux, _, teardown := setup()
2570 defer teardown()
2571
2572 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2573 testMethod(t, r, "GET")
2574 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2575 fmt.Fprint(w, `{"names":[]}`)
2576 })
2577
2578 ctx := context.Background()
2579 got, _, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2580 if err != nil {
2581 t.Fatalf("Repositories.ListAllTopics returned error: %v", err)
2582 }
2583
2584 want := []string{}
2585 if !cmp.Equal(got, want) {
2586 t.Errorf("Repositories.ListAllTopics returned %+v, want %+v", got, want)
2587 }
2588 }
2589
2590 func TestRepositoriesService_ReplaceAllTopics(t *testing.T) {
2591 client, mux, _, teardown := setup()
2592 defer teardown()
2593
2594 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2595 testMethod(t, r, "PUT")
2596 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2597 fmt.Fprint(w, `{"names":["go", "go-github", "github"]}`)
2598 })
2599
2600 ctx := context.Background()
2601 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{"go", "go-github", "github"})
2602 if err != nil {
2603 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2604 }
2605
2606 want := []string{"go", "go-github", "github"}
2607 if !cmp.Equal(got, want) {
2608 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2609 }
2610
2611 const methodName = "ReplaceAllTopics"
2612 testBadOptions(t, methodName, func() (err error) {
2613 _, _, err = client.Repositories.ReplaceAllTopics(ctx, "\n", "\n", []string{"\n", "\n", "\n"})
2614 return err
2615 })
2616
2617 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2618 got, resp, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{"go", "go-github", "github"})
2619 if got != nil {
2620 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2621 }
2622 return resp, err
2623 })
2624 }
2625
2626 func TestRepositoriesService_ReplaceAllTopics_nilSlice(t *testing.T) {
2627 client, mux, _, teardown := setup()
2628 defer teardown()
2629
2630 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2631 testMethod(t, r, "PUT")
2632 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2633 testBody(t, r, `{"names":[]}`+"\n")
2634 fmt.Fprint(w, `{"names":[]}`)
2635 })
2636
2637 ctx := context.Background()
2638 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", nil)
2639 if err != nil {
2640 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2641 }
2642
2643 want := []string{}
2644 if !cmp.Equal(got, want) {
2645 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2646 }
2647 }
2648
2649 func TestRepositoriesService_ReplaceAllTopics_emptySlice(t *testing.T) {
2650 client, mux, _, teardown := setup()
2651 defer teardown()
2652
2653 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2654 testMethod(t, r, "PUT")
2655 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2656 testBody(t, r, `{"names":[]}`+"\n")
2657 fmt.Fprint(w, `{"names":[]}`)
2658 })
2659
2660 ctx := context.Background()
2661 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{})
2662 if err != nil {
2663 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2664 }
2665
2666 want := []string{}
2667 if !cmp.Equal(got, want) {
2668 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2669 }
2670 }
2671
2672 func TestRepositoriesService_ListApps(t *testing.T) {
2673 client, mux, _, teardown := setup()
2674 defer teardown()
2675
2676 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2677 testMethod(t, r, "GET")
2678 })
2679
2680 ctx := context.Background()
2681 _, _, err := client.Repositories.ListApps(ctx, "o", "r", "b")
2682 if err != nil {
2683 t.Errorf("Repositories.ListApps returned error: %v", err)
2684 }
2685
2686 const methodName = "ListApps"
2687 testBadOptions(t, methodName, func() (err error) {
2688 _, _, err = client.Repositories.ListApps(ctx, "\n", "\n", "\n")
2689 return err
2690 })
2691
2692 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2693 got, resp, err := client.Repositories.ListApps(ctx, "o", "r", "b")
2694 if got != nil {
2695 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2696 }
2697 return resp, err
2698 })
2699 }
2700
2701 func TestRepositoriesService_ReplaceAppRestrictions(t *testing.T) {
2702 client, mux, _, teardown := setup()
2703 defer teardown()
2704
2705 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2706 testMethod(t, r, "PUT")
2707 fmt.Fprint(w, `[{
2708 "name": "octocat"
2709 }]`)
2710 })
2711 input := []string{"octocat"}
2712 ctx := context.Background()
2713 got, _, err := client.Repositories.ReplaceAppRestrictions(ctx, "o", "r", "b", input)
2714 if err != nil {
2715 t.Errorf("Repositories.ReplaceAppRestrictions returned error: %v", err)
2716 }
2717 want := []*App{
2718 {Name: String("octocat")},
2719 }
2720 if !cmp.Equal(got, want) {
2721 t.Errorf("Repositories.ReplaceAppRestrictions returned %+v, want %+v", got, want)
2722 }
2723
2724 const methodName = "ReplaceAppRestrictions"
2725 testBadOptions(t, methodName, func() (err error) {
2726 _, _, err = client.Repositories.ReplaceAppRestrictions(ctx, "\n", "\n", "\n", input)
2727 return err
2728 })
2729
2730 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2731 got, resp, err := client.Repositories.ReplaceAppRestrictions(ctx, "o", "r", "b", input)
2732 if got != nil {
2733 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2734 }
2735 return resp, err
2736 })
2737 }
2738
2739 func TestRepositoriesService_AddAppRestrictions(t *testing.T) {
2740 client, mux, _, teardown := setup()
2741 defer teardown()
2742
2743 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2744 testMethod(t, r, "POST")
2745 fmt.Fprint(w, `[{
2746 "name": "octocat"
2747 }]`)
2748 })
2749 input := []string{"octocat"}
2750 ctx := context.Background()
2751 got, _, err := client.Repositories.AddAppRestrictions(ctx, "o", "r", "b", input)
2752 if err != nil {
2753 t.Errorf("Repositories.AddAppRestrictions returned error: %v", err)
2754 }
2755 want := []*App{
2756 {Name: String("octocat")},
2757 }
2758 if !cmp.Equal(got, want) {
2759 t.Errorf("Repositories.AddAppRestrictions returned %+v, want %+v", got, want)
2760 }
2761
2762 const methodName = "AddAppRestrictions"
2763 testBadOptions(t, methodName, func() (err error) {
2764 _, _, err = client.Repositories.AddAppRestrictions(ctx, "\n", "\n", "\n", input)
2765 return err
2766 })
2767
2768 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2769 got, resp, err := client.Repositories.AddAppRestrictions(ctx, "o", "r", "b", input)
2770 if got != nil {
2771 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2772 }
2773 return resp, err
2774 })
2775 }
2776
2777 func TestRepositoriesService_RemoveAppRestrictions(t *testing.T) {
2778 client, mux, _, teardown := setup()
2779 defer teardown()
2780
2781 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2782 testMethod(t, r, "DELETE")
2783 fmt.Fprint(w, `[]`)
2784 })
2785 input := []string{"octocat"}
2786 ctx := context.Background()
2787 got, _, err := client.Repositories.RemoveAppRestrictions(ctx, "o", "r", "b", input)
2788 if err != nil {
2789 t.Errorf("Repositories.RemoveAppRestrictions returned error: %v", err)
2790 }
2791 want := []*App{}
2792 if !cmp.Equal(got, want) {
2793 t.Errorf("Repositories.RemoveAppRestrictions returned %+v, want %+v", got, want)
2794 }
2795
2796 const methodName = "RemoveAppRestrictions"
2797 testBadOptions(t, methodName, func() (err error) {
2798 _, _, err = client.Repositories.RemoveAppRestrictions(ctx, "\n", "\n", "\n", input)
2799 return err
2800 })
2801
2802 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2803 got, resp, err := client.Repositories.RemoveAppRestrictions(ctx, "o", "r", "b", input)
2804 if got != nil {
2805 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2806 }
2807 return resp, err
2808 })
2809 }
2810
2811 func TestRepositoriesService_Transfer(t *testing.T) {
2812 client, mux, _, teardown := setup()
2813 defer teardown()
2814
2815 input := TransferRequest{NewOwner: "a", TeamID: []int64{123}}
2816
2817 mux.HandleFunc("/repos/o/r/transfer", func(w http.ResponseWriter, r *http.Request) {
2818 var v TransferRequest
2819 json.NewDecoder(r.Body).Decode(&v)
2820
2821 testMethod(t, r, "POST")
2822 if !cmp.Equal(v, input) {
2823 t.Errorf("Request body = %+v, want %+v", v, input)
2824 }
2825
2826 fmt.Fprint(w, `{"owner":{"login":"a"}}`)
2827 })
2828
2829 ctx := context.Background()
2830 got, _, err := client.Repositories.Transfer(ctx, "o", "r", input)
2831 if err != nil {
2832 t.Errorf("Repositories.Transfer returned error: %v", err)
2833 }
2834
2835 want := &Repository{Owner: &User{Login: String("a")}}
2836 if !cmp.Equal(got, want) {
2837 t.Errorf("Repositories.Transfer returned %+v, want %+v", got, want)
2838 }
2839
2840 const methodName = "Transfer"
2841 testBadOptions(t, methodName, func() (err error) {
2842 _, _, err = client.Repositories.Transfer(ctx, "\n", "\n", input)
2843 return err
2844 })
2845
2846 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2847 got, resp, err := client.Repositories.Transfer(ctx, "o", "r", input)
2848 if got != nil {
2849 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2850 }
2851 return resp, err
2852 })
2853 }
2854
2855 func TestRepositoriesService_Dispatch(t *testing.T) {
2856 client, mux, _, teardown := setup()
2857 defer teardown()
2858
2859 var input DispatchRequestOptions
2860
2861 mux.HandleFunc("/repos/o/r/dispatches", func(w http.ResponseWriter, r *http.Request) {
2862 var v DispatchRequestOptions
2863 json.NewDecoder(r.Body).Decode(&v)
2864
2865 testMethod(t, r, "POST")
2866 if !cmp.Equal(v, input) {
2867 t.Errorf("Request body = %+v, want %+v", v, input)
2868 }
2869
2870 fmt.Fprint(w, `{"owner":{"login":"a"}}`)
2871 })
2872
2873 ctx := context.Background()
2874
2875 testCases := []interface{}{
2876 nil,
2877 struct {
2878 Foo string
2879 }{
2880 Foo: "test",
2881 },
2882 struct {
2883 Bar int
2884 }{
2885 Bar: 42,
2886 },
2887 struct {
2888 Foo string
2889 Bar int
2890 Baz bool
2891 }{
2892 Foo: "test",
2893 Bar: 42,
2894 Baz: false,
2895 },
2896 }
2897
2898 for _, tc := range testCases {
2899 if tc == nil {
2900 input = DispatchRequestOptions{EventType: "go"}
2901 } else {
2902 bytes, _ := json.Marshal(tc)
2903 payload := json.RawMessage(bytes)
2904 input = DispatchRequestOptions{EventType: "go", ClientPayload: &payload}
2905 }
2906
2907 got, _, err := client.Repositories.Dispatch(ctx, "o", "r", input)
2908 if err != nil {
2909 t.Errorf("Repositories.Dispatch returned error: %v", err)
2910 }
2911
2912 want := &Repository{Owner: &User{Login: String("a")}}
2913 if !cmp.Equal(got, want) {
2914 t.Errorf("Repositories.Dispatch returned %+v, want %+v", got, want)
2915 }
2916 }
2917
2918 const methodName = "Dispatch"
2919 testBadOptions(t, methodName, func() (err error) {
2920 _, _, err = client.Repositories.Dispatch(ctx, "\n", "\n", input)
2921 return err
2922 })
2923
2924 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2925 got, resp, err := client.Repositories.Dispatch(ctx, "o", "r", input)
2926 if got != nil {
2927 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2928 }
2929 return resp, err
2930 })
2931 }
2932
2933 func TestAdvancedSecurity_Marshal(t *testing.T) {
2934 testJSONMarshal(t, &AdvancedSecurity{}, "{}")
2935
2936 u := &AdvancedSecurity{
2937 Status: String("status"),
2938 }
2939
2940 want := `{
2941 "status": "status"
2942 }`
2943
2944 testJSONMarshal(t, u, want)
2945 }
2946
2947 func TestAuthorizedActorsOnly_Marshal(t *testing.T) {
2948 testJSONMarshal(t, &AuthorizedActorsOnly{}, "{}")
2949
2950 u := &AuthorizedActorsOnly{
2951 From: Bool(true),
2952 }
2953
2954 want := `{
2955 "from" : true
2956 }`
2957
2958 testJSONMarshal(t, u, want)
2959 }
2960
View as plain text