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 },
1278 Restrictions: &BranchRestrictionsRequest{
1279 Users: []string{"u"},
1280 Teams: []string{"t"},
1281 Apps: []string{"a"},
1282 },
1283 }
1284
1285 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1286 v := new(ProtectionRequest)
1287 json.NewDecoder(r.Body).Decode(v)
1288
1289 testMethod(t, r, "PUT")
1290 if !cmp.Equal(v, input) {
1291 t.Errorf("Request body = %+v, want %+v", v, input)
1292 }
1293
1294
1295 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1296 fmt.Fprintf(w, `{
1297 "required_status_checks":{
1298 "strict":true,
1299 "contexts":["continuous-integration"],
1300 "checks": [
1301 {
1302 "context": "continuous-integration",
1303 "app_id": null
1304 }
1305 ]
1306 },
1307 "required_pull_request_reviews":{
1308 "dismissal_restrictions":{
1309 "users":[{
1310 "id":3,
1311 "login":"uu"
1312 }],
1313 "teams":[{
1314 "id":4,
1315 "slug":"tt"
1316 }]
1317 },
1318 "dismiss_stale_reviews":true,
1319 "require_code_owner_reviews":true
1320 },
1321 "restrictions":{
1322 "users":[{"id":1,"login":"u"}],
1323 "teams":[{"id":2,"slug":"t"}],
1324 "apps":[{"id":3,"slug":"a"}]
1325 }
1326 }`)
1327 })
1328
1329 ctx := context.Background()
1330 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1331 if err != nil {
1332 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1333 }
1334
1335 want := &Protection{
1336 RequiredStatusChecks: &RequiredStatusChecks{
1337 Strict: true,
1338 Contexts: []string{"continuous-integration"},
1339 Checks: []*RequiredStatusCheck{
1340 {
1341 Context: "continuous-integration",
1342 },
1343 },
1344 },
1345 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1346 DismissStaleReviews: true,
1347 DismissalRestrictions: &DismissalRestrictions{
1348 Users: []*User{
1349 {Login: String("uu"), ID: Int64(3)},
1350 },
1351 Teams: []*Team{
1352 {Slug: String("tt"), ID: Int64(4)},
1353 },
1354 },
1355 RequireCodeOwnerReviews: true,
1356 },
1357 Restrictions: &BranchRestrictions{
1358 Users: []*User{
1359 {Login: String("u"), ID: Int64(1)},
1360 },
1361 Teams: []*Team{
1362 {Slug: String("t"), ID: Int64(2)},
1363 },
1364 Apps: []*App{
1365 {Slug: String("a"), ID: Int64(3)},
1366 },
1367 },
1368 }
1369 if !cmp.Equal(protection, want) {
1370 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1371 }
1372
1373 const methodName = "UpdateBranchProtection"
1374 testBadOptions(t, methodName, func() (err error) {
1375 _, _, err = client.Repositories.UpdateBranchProtection(ctx, "\n", "\n", "\n", input)
1376 return err
1377 })
1378
1379 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1380 got, resp, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1381 if got != nil {
1382 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1383 }
1384 return resp, err
1385 })
1386 }
1387
1388 func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
1389 client, mux, _, teardown := setup()
1390 defer teardown()
1391
1392 input := &ProtectionRequest{
1393 RequiredStatusChecks: &RequiredStatusChecks{
1394 Strict: true,
1395 Checks: []*RequiredStatusCheck{
1396 {
1397 Context: "continuous-integration",
1398 },
1399 },
1400 },
1401 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1402 DismissStaleReviews: true,
1403 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1404 Users: &[]string{"uu"},
1405 Teams: &[]string{"tt"},
1406 },
1407 },
1408 Restrictions: &BranchRestrictionsRequest{
1409 Users: []string{"u"},
1410 Teams: []string{"t"},
1411 Apps: []string{"a"},
1412 },
1413 }
1414
1415 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1416 v := new(ProtectionRequest)
1417 json.NewDecoder(r.Body).Decode(v)
1418
1419 testMethod(t, r, "PUT")
1420 if !cmp.Equal(v, input) {
1421 t.Errorf("Request body = %+v, want %+v", v, input)
1422 }
1423
1424
1425 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1426 fmt.Fprintf(w, `{
1427 "required_status_checks":{
1428 "strict":true,
1429 "contexts":["continuous-integration"],
1430 "checks": [
1431 {
1432 "context": "continuous-integration",
1433 "app_id": null
1434 }
1435 ]
1436 },
1437 "required_pull_request_reviews":{
1438 "dismissal_restrictions":{
1439 "users":[{
1440 "id":3,
1441 "login":"uu"
1442 }],
1443 "teams":[{
1444 "id":4,
1445 "slug":"tt"
1446 }]
1447 },
1448 "dismiss_stale_reviews":true,
1449 "require_code_owner_reviews":true
1450 },
1451 "restrictions":{
1452 "users":[{"id":1,"login":"u"}],
1453 "teams":[{"id":2,"slug":"t"}],
1454 "apps":[{"id":3,"slug":"a"}]
1455 }
1456 }`)
1457 })
1458
1459 ctx := context.Background()
1460 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1461 if err != nil {
1462 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1463 }
1464
1465 want := &Protection{
1466 RequiredStatusChecks: &RequiredStatusChecks{
1467 Strict: true,
1468 Contexts: []string{"continuous-integration"},
1469 Checks: []*RequiredStatusCheck{
1470 {
1471 Context: "continuous-integration",
1472 },
1473 },
1474 },
1475 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1476 DismissStaleReviews: true,
1477 DismissalRestrictions: &DismissalRestrictions{
1478 Users: []*User{
1479 {Login: String("uu"), ID: Int64(3)},
1480 },
1481 Teams: []*Team{
1482 {Slug: String("tt"), ID: Int64(4)},
1483 },
1484 },
1485 RequireCodeOwnerReviews: true,
1486 },
1487 Restrictions: &BranchRestrictions{
1488 Users: []*User{
1489 {Login: String("u"), ID: Int64(1)},
1490 },
1491 Teams: []*Team{
1492 {Slug: String("t"), ID: Int64(2)},
1493 },
1494 Apps: []*App{
1495 {Slug: String("a"), ID: Int64(3)},
1496 },
1497 },
1498 }
1499 if !cmp.Equal(protection, want) {
1500 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1501 }
1502 }
1503
1504 func TestRepositoriesService_RemoveBranchProtection(t *testing.T) {
1505 client, mux, _, teardown := setup()
1506 defer teardown()
1507
1508 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1509 testMethod(t, r, "DELETE")
1510 w.WriteHeader(http.StatusNoContent)
1511 })
1512
1513 ctx := context.Background()
1514 _, err := client.Repositories.RemoveBranchProtection(ctx, "o", "r", "b")
1515 if err != nil {
1516 t.Errorf("Repositories.RemoveBranchProtection returned error: %v", err)
1517 }
1518
1519 const methodName = "RemoveBranchProtection"
1520 testBadOptions(t, methodName, func() (err error) {
1521 _, err = client.Repositories.RemoveBranchProtection(ctx, "\n", "\n", "\n")
1522 return err
1523 })
1524
1525 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1526 return client.Repositories.RemoveBranchProtection(ctx, "o", "r", "b")
1527 })
1528 }
1529
1530 func TestRepositoriesService_ListLanguages_invalidOwner(t *testing.T) {
1531 client, _, _, teardown := setup()
1532 defer teardown()
1533
1534 ctx := context.Background()
1535 _, _, err := client.Repositories.ListLanguages(ctx, "%", "%")
1536 testURLParseError(t, err)
1537 }
1538
1539 func TestRepositoriesService_License(t *testing.T) {
1540 client, mux, _, teardown := setup()
1541 defer teardown()
1542
1543 mux.HandleFunc("/repos/o/r/license", func(w http.ResponseWriter, r *http.Request) {
1544 testMethod(t, r, "GET")
1545 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}}`)
1546 })
1547
1548 ctx := context.Background()
1549 got, _, err := client.Repositories.License(ctx, "o", "r")
1550 if err != nil {
1551 t.Errorf("Repositories.License returned error: %v", err)
1552 }
1553
1554 want := &RepositoryLicense{
1555 Name: String("LICENSE"),
1556 Path: String("LICENSE"),
1557 License: &License{
1558 Name: String("MIT License"),
1559 Key: String("mit"),
1560 SPDXID: String("MIT"),
1561 URL: String("https://api.github.com/licenses/mit"),
1562 Featured: Bool(true),
1563 },
1564 }
1565
1566 if !cmp.Equal(got, want) {
1567 t.Errorf("Repositories.License returned %+v, want %+v", got, want)
1568 }
1569
1570 const methodName = "License"
1571 testBadOptions(t, methodName, func() (err error) {
1572 _, _, err = client.Repositories.License(ctx, "\n", "\n")
1573 return err
1574 })
1575
1576 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1577 got, resp, err := client.Repositories.License(ctx, "o", "r")
1578 if got != nil {
1579 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1580 }
1581 return resp, err
1582 })
1583 }
1584
1585 func TestRepositoriesService_GetRequiredStatusChecks(t *testing.T) {
1586 client, mux, _, teardown := setup()
1587 defer teardown()
1588
1589 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1590 v := new(ProtectionRequest)
1591 json.NewDecoder(r.Body).Decode(v)
1592
1593 testMethod(t, r, "GET")
1594 fmt.Fprint(w, `{
1595 "strict": true,
1596 "contexts": ["x","y","z"],
1597 "checks": [
1598 {
1599 "context": "x",
1600 "app_id": null
1601 },
1602 {
1603 "context": "y",
1604 "app_id": null
1605 },
1606 {
1607 "context": "z",
1608 "app_id": null
1609 }
1610 ]
1611 }`)
1612 })
1613
1614 ctx := context.Background()
1615 checks, _, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1616 if err != nil {
1617 t.Errorf("Repositories.GetRequiredStatusChecks returned error: %v", err)
1618 }
1619
1620 want := &RequiredStatusChecks{
1621 Strict: true,
1622 Contexts: []string{"x", "y", "z"},
1623 Checks: []*RequiredStatusCheck{
1624 {
1625 Context: "x",
1626 },
1627 {
1628 Context: "y",
1629 },
1630 {
1631 Context: "z",
1632 },
1633 },
1634 }
1635 if !cmp.Equal(checks, want) {
1636 t.Errorf("Repositories.GetRequiredStatusChecks returned %+v, want %+v", checks, want)
1637 }
1638
1639 const methodName = "GetRequiredStatusChecks"
1640 testBadOptions(t, methodName, func() (err error) {
1641 _, _, err = client.Repositories.GetRequiredStatusChecks(ctx, "\n", "\n", "\n")
1642 return err
1643 })
1644
1645 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1646 got, resp, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1647 if got != nil {
1648 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1649 }
1650 return resp, err
1651 })
1652 }
1653
1654 func TestRepositoriesService_GetRequiredStatusChecks_branchNotProtected(t *testing.T) {
1655 client, mux, _, teardown := setup()
1656 defer teardown()
1657
1658 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1659 testMethod(t, r, "GET")
1660
1661 w.WriteHeader(http.StatusBadRequest)
1662 fmt.Fprintf(w, `{
1663 "message": %q,
1664 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
1665 }`, githubBranchNotProtected)
1666 })
1667
1668 ctx := context.Background()
1669 checks, _, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1670
1671 if checks != nil {
1672 t.Errorf("Repositories.GetRequiredStatusChecks returned non-nil status-checks data")
1673 }
1674
1675 if err != ErrBranchNotProtected {
1676 t.Errorf("Repositories.GetRequiredStatusChecks returned an invalid error: %v", err)
1677 }
1678 }
1679
1680 func TestRepositoriesService_UpdateRequiredStatusChecks_Contexts(t *testing.T) {
1681 client, mux, _, teardown := setup()
1682 defer teardown()
1683
1684 input := &RequiredStatusChecksRequest{
1685 Strict: Bool(true),
1686 Contexts: []string{"continuous-integration"},
1687 }
1688
1689 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1690 v := new(RequiredStatusChecksRequest)
1691 json.NewDecoder(r.Body).Decode(v)
1692
1693 testMethod(t, r, "PATCH")
1694 if !cmp.Equal(v, input) {
1695 t.Errorf("Request body = %+v, want %+v", v, input)
1696 }
1697 testHeader(t, r, "Accept", mediaTypeV3)
1698 fmt.Fprintf(w, `{
1699 "strict":true,
1700 "contexts":["continuous-integration"],
1701 "checks": [
1702 {
1703 "context": "continuous-integration",
1704 "app_id": null
1705 }
1706 ]
1707 }`)
1708 })
1709
1710 ctx := context.Background()
1711 statusChecks, _, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
1712 if err != nil {
1713 t.Errorf("Repositories.UpdateRequiredStatusChecks returned error: %v", err)
1714 }
1715
1716 want := &RequiredStatusChecks{
1717 Strict: true,
1718 Contexts: []string{"continuous-integration"},
1719 Checks: []*RequiredStatusCheck{
1720 {
1721 Context: "continuous-integration",
1722 },
1723 },
1724 }
1725 if !cmp.Equal(statusChecks, want) {
1726 t.Errorf("Repositories.UpdateRequiredStatusChecks returned %+v, want %+v", statusChecks, want)
1727 }
1728
1729 const methodName = "UpdateRequiredStatusChecks"
1730 testBadOptions(t, methodName, func() (err error) {
1731 _, _, err = client.Repositories.UpdateRequiredStatusChecks(ctx, "\n", "\n", "\n", input)
1732 return err
1733 })
1734
1735 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1736 got, resp, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
1737 if got != nil {
1738 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1739 }
1740 return resp, err
1741 })
1742 }
1743
1744 func TestRepositoriesService_UpdateRequiredStatusChecks_Checks(t *testing.T) {
1745 client, mux, _, teardown := setup()
1746 defer teardown()
1747
1748 appID := int64(123)
1749 noAppID := int64(-1)
1750 input := &RequiredStatusChecksRequest{
1751 Strict: Bool(true),
1752 Checks: []*RequiredStatusCheck{
1753 {
1754 Context: "continuous-integration",
1755 },
1756 {
1757 Context: "continuous-integration2",
1758 AppID: &appID,
1759 },
1760 {
1761 Context: "continuous-integration3",
1762 AppID: &noAppID,
1763 },
1764 },
1765 }
1766
1767 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1768 v := new(RequiredStatusChecksRequest)
1769 json.NewDecoder(r.Body).Decode(v)
1770
1771 testMethod(t, r, "PATCH")
1772 if !cmp.Equal(v, input) {
1773 t.Errorf("Request body = %+v, want %+v", v, input)
1774 }
1775 testHeader(t, r, "Accept", mediaTypeV3)
1776 fmt.Fprintf(w, `{
1777 "strict":true,
1778 "contexts":["continuous-integration"],
1779 "checks": [
1780 {
1781 "context": "continuous-integration",
1782 "app_id": null
1783 },
1784 {
1785 "context": "continuous-integration2",
1786 "app_id": 123
1787 },
1788 {
1789 "context": "continuous-integration3",
1790 "app_id": null
1791 }
1792 ]
1793 }`)
1794 })
1795
1796 ctx := context.Background()
1797 statusChecks, _, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
1798 if err != nil {
1799 t.Errorf("Repositories.UpdateRequiredStatusChecks returned error: %v", err)
1800 }
1801
1802 want := &RequiredStatusChecks{
1803 Strict: true,
1804 Contexts: []string{"continuous-integration"},
1805 Checks: []*RequiredStatusCheck{
1806 {
1807 Context: "continuous-integration",
1808 },
1809 {
1810 Context: "continuous-integration2",
1811 AppID: &appID,
1812 },
1813 {
1814 Context: "continuous-integration3",
1815 },
1816 },
1817 }
1818 if !cmp.Equal(statusChecks, want) {
1819 t.Errorf("Repositories.UpdateRequiredStatusChecks returned %+v, want %+v", statusChecks, want)
1820 }
1821 }
1822
1823 func TestRepositoriesService_RemoveRequiredStatusChecks(t *testing.T) {
1824 client, mux, _, teardown := setup()
1825 defer teardown()
1826
1827 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1828 testMethod(t, r, "DELETE")
1829 testHeader(t, r, "Accept", mediaTypeV3)
1830 w.WriteHeader(http.StatusNoContent)
1831 })
1832
1833 ctx := context.Background()
1834 _, err := client.Repositories.RemoveRequiredStatusChecks(ctx, "o", "r", "b")
1835 if err != nil {
1836 t.Errorf("Repositories.RemoveRequiredStatusChecks returned error: %v", err)
1837 }
1838
1839 const methodName = "RemoveRequiredStatusChecks"
1840 testBadOptions(t, methodName, func() (err error) {
1841 _, err = client.Repositories.RemoveRequiredStatusChecks(ctx, "\n", "\n", "\n")
1842 return err
1843 })
1844
1845 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1846 return client.Repositories.RemoveRequiredStatusChecks(ctx, "o", "r", "b")
1847 })
1848 }
1849
1850 func TestRepositoriesService_ListRequiredStatusChecksContexts(t *testing.T) {
1851 client, mux, _, teardown := setup()
1852 defer teardown()
1853
1854 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) {
1855 v := new(ProtectionRequest)
1856 json.NewDecoder(r.Body).Decode(v)
1857
1858 testMethod(t, r, "GET")
1859 fmt.Fprint(w, `["x", "y", "z"]`)
1860 })
1861
1862 ctx := context.Background()
1863 contexts, _, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
1864 if err != nil {
1865 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned error: %v", err)
1866 }
1867
1868 want := []string{"x", "y", "z"}
1869 if !cmp.Equal(contexts, want) {
1870 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned %+v, want %+v", contexts, want)
1871 }
1872
1873 const methodName = "ListRequiredStatusChecksContexts"
1874 testBadOptions(t, methodName, func() (err error) {
1875 _, _, err = client.Repositories.ListRequiredStatusChecksContexts(ctx, "\n", "\n", "\n")
1876 return err
1877 })
1878
1879 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1880 got, resp, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
1881 if got != nil {
1882 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1883 }
1884 return resp, err
1885 })
1886 }
1887
1888 func TestRepositoriesService_ListRequiredStatusChecksContexts_branchNotProtected(t *testing.T) {
1889 client, mux, _, teardown := setup()
1890 defer teardown()
1891
1892 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) {
1893 testMethod(t, r, "GET")
1894
1895 w.WriteHeader(http.StatusBadRequest)
1896 fmt.Fprintf(w, `{
1897 "message": %q,
1898 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
1899 }`, githubBranchNotProtected)
1900 })
1901
1902 ctx := context.Background()
1903 contexts, _, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
1904
1905 if contexts != nil {
1906 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned non-nil contexts data")
1907 }
1908
1909 if err != ErrBranchNotProtected {
1910 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned an invalid error: %v", err)
1911 }
1912 }
1913
1914 func TestRepositoriesService_GetPullRequestReviewEnforcement(t *testing.T) {
1915 client, mux, _, teardown := setup()
1916 defer teardown()
1917
1918 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
1919 testMethod(t, r, "GET")
1920
1921 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1922 fmt.Fprintf(w, `{
1923 "dismissal_restrictions":{
1924 "users":[{"id":1,"login":"u"}],
1925 "teams":[{"id":2,"slug":"t"}]
1926 },
1927 "dismiss_stale_reviews":true,
1928 "require_code_owner_reviews":true,
1929 "required_approving_review_count":1
1930 }`)
1931 })
1932
1933 ctx := context.Background()
1934 enforcement, _, err := client.Repositories.GetPullRequestReviewEnforcement(ctx, "o", "r", "b")
1935 if err != nil {
1936 t.Errorf("Repositories.GetPullRequestReviewEnforcement returned error: %v", err)
1937 }
1938
1939 want := &PullRequestReviewsEnforcement{
1940 DismissStaleReviews: true,
1941 DismissalRestrictions: &DismissalRestrictions{
1942 Users: []*User{
1943 {Login: String("u"), ID: Int64(1)},
1944 },
1945 Teams: []*Team{
1946 {Slug: String("t"), ID: Int64(2)},
1947 },
1948 },
1949 RequireCodeOwnerReviews: true,
1950 RequiredApprovingReviewCount: 1,
1951 }
1952
1953 if !cmp.Equal(enforcement, want) {
1954 t.Errorf("Repositories.GetPullRequestReviewEnforcement returned %+v, want %+v", enforcement, want)
1955 }
1956
1957 const methodName = "GetPullRequestReviewEnforcement"
1958 testBadOptions(t, methodName, func() (err error) {
1959 _, _, err = client.Repositories.GetPullRequestReviewEnforcement(ctx, "\n", "\n", "\n")
1960 return err
1961 })
1962
1963 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1964 got, resp, err := client.Repositories.GetPullRequestReviewEnforcement(ctx, "o", "r", "b")
1965 if got != nil {
1966 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1967 }
1968 return resp, err
1969 })
1970 }
1971
1972 func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) {
1973 client, mux, _, teardown := setup()
1974 defer teardown()
1975
1976 input := &PullRequestReviewsEnforcementUpdate{
1977 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1978 Users: &[]string{"u"},
1979 Teams: &[]string{"t"},
1980 },
1981 }
1982
1983 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
1984 v := new(PullRequestReviewsEnforcementUpdate)
1985 json.NewDecoder(r.Body).Decode(v)
1986
1987 testMethod(t, r, "PATCH")
1988 if !cmp.Equal(v, input) {
1989 t.Errorf("Request body = %+v, want %+v", v, input)
1990 }
1991
1992 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1993 fmt.Fprintf(w, `{
1994 "dismissal_restrictions":{
1995 "users":[{"id":1,"login":"u"}],
1996 "teams":[{"id":2,"slug":"t"}]
1997 },
1998 "dismiss_stale_reviews":true,
1999 "require_code_owner_reviews":true,
2000 "required_approving_review_count":3
2001 }`)
2002 })
2003
2004 ctx := context.Background()
2005 enforcement, _, err := client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "o", "r", "b", input)
2006 if err != nil {
2007 t.Errorf("Repositories.UpdatePullRequestReviewEnforcement returned error: %v", err)
2008 }
2009
2010 want := &PullRequestReviewsEnforcement{
2011 DismissStaleReviews: true,
2012 DismissalRestrictions: &DismissalRestrictions{
2013 Users: []*User{
2014 {Login: String("u"), ID: Int64(1)},
2015 },
2016 Teams: []*Team{
2017 {Slug: String("t"), ID: Int64(2)},
2018 },
2019 },
2020 RequireCodeOwnerReviews: true,
2021 RequiredApprovingReviewCount: 3,
2022 }
2023 if !cmp.Equal(enforcement, want) {
2024 t.Errorf("Repositories.UpdatePullRequestReviewEnforcement returned %+v, want %+v", enforcement, want)
2025 }
2026
2027 const methodName = "UpdatePullRequestReviewEnforcement"
2028 testBadOptions(t, methodName, func() (err error) {
2029 _, _, err = client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "\n", "\n", "\n", input)
2030 return err
2031 })
2032
2033 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2034 got, resp, err := client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "o", "r", "b", input)
2035 if got != nil {
2036 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2037 }
2038 return resp, err
2039 })
2040 }
2041
2042 func TestRepositoriesService_DisableDismissalRestrictions(t *testing.T) {
2043 client, mux, _, teardown := setup()
2044 defer teardown()
2045
2046 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2047 testMethod(t, r, "PATCH")
2048
2049 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
2050 testBody(t, r, `{"dismissal_restrictions":{}}`+"\n")
2051 fmt.Fprintf(w, `{"dismiss_stale_reviews":true,"require_code_owner_reviews":true,"required_approving_review_count":1}`)
2052 })
2053
2054 ctx := context.Background()
2055 enforcement, _, err := client.Repositories.DisableDismissalRestrictions(ctx, "o", "r", "b")
2056 if err != nil {
2057 t.Errorf("Repositories.DisableDismissalRestrictions returned error: %v", err)
2058 }
2059
2060 want := &PullRequestReviewsEnforcement{
2061 DismissStaleReviews: true,
2062 DismissalRestrictions: nil,
2063 RequireCodeOwnerReviews: true,
2064 RequiredApprovingReviewCount: 1,
2065 }
2066 if !cmp.Equal(enforcement, want) {
2067 t.Errorf("Repositories.DisableDismissalRestrictions returned %+v, want %+v", enforcement, want)
2068 }
2069
2070 const methodName = "DisableDismissalRestrictions"
2071 testBadOptions(t, methodName, func() (err error) {
2072 _, _, err = client.Repositories.DisableDismissalRestrictions(ctx, "\n", "\n", "\n")
2073 return err
2074 })
2075
2076 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2077 got, resp, err := client.Repositories.DisableDismissalRestrictions(ctx, "o", "r", "b")
2078 if got != nil {
2079 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2080 }
2081 return resp, err
2082 })
2083 }
2084
2085 func TestRepositoriesService_RemovePullRequestReviewEnforcement(t *testing.T) {
2086 client, mux, _, teardown := setup()
2087 defer teardown()
2088
2089 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2090 testMethod(t, r, "DELETE")
2091 w.WriteHeader(http.StatusNoContent)
2092 })
2093
2094 ctx := context.Background()
2095 _, err := client.Repositories.RemovePullRequestReviewEnforcement(ctx, "o", "r", "b")
2096 if err != nil {
2097 t.Errorf("Repositories.RemovePullRequestReviewEnforcement returned error: %v", err)
2098 }
2099
2100 const methodName = "RemovePullRequestReviewEnforcement"
2101 testBadOptions(t, methodName, func() (err error) {
2102 _, err = client.Repositories.RemovePullRequestReviewEnforcement(ctx, "\n", "\n", "\n")
2103 return err
2104 })
2105
2106 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2107 return client.Repositories.RemovePullRequestReviewEnforcement(ctx, "o", "r", "b")
2108 })
2109 }
2110
2111 func TestRepositoriesService_GetAdminEnforcement(t *testing.T) {
2112 client, mux, _, teardown := setup()
2113 defer teardown()
2114
2115 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2116 testMethod(t, r, "GET")
2117 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true}`)
2118 })
2119
2120 ctx := context.Background()
2121 enforcement, _, err := client.Repositories.GetAdminEnforcement(ctx, "o", "r", "b")
2122 if err != nil {
2123 t.Errorf("Repositories.GetAdminEnforcement returned error: %v", err)
2124 }
2125
2126 want := &AdminEnforcement{
2127 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
2128 Enabled: true,
2129 }
2130
2131 if !cmp.Equal(enforcement, want) {
2132 t.Errorf("Repositories.GetAdminEnforcement returned %+v, want %+v", enforcement, want)
2133 }
2134
2135 const methodName = "GetAdminEnforcement"
2136 testBadOptions(t, methodName, func() (err error) {
2137 _, _, err = client.Repositories.GetAdminEnforcement(ctx, "\n", "\n", "\n")
2138 return err
2139 })
2140
2141 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2142 got, resp, err := client.Repositories.GetAdminEnforcement(ctx, "o", "r", "b")
2143 if got != nil {
2144 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2145 }
2146 return resp, err
2147 })
2148 }
2149
2150 func TestRepositoriesService_AddAdminEnforcement(t *testing.T) {
2151 client, mux, _, teardown := setup()
2152 defer teardown()
2153
2154 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2155 testMethod(t, r, "POST")
2156 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true}`)
2157 })
2158
2159 ctx := context.Background()
2160 enforcement, _, err := client.Repositories.AddAdminEnforcement(ctx, "o", "r", "b")
2161 if err != nil {
2162 t.Errorf("Repositories.AddAdminEnforcement returned error: %v", err)
2163 }
2164
2165 want := &AdminEnforcement{
2166 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
2167 Enabled: true,
2168 }
2169 if !cmp.Equal(enforcement, want) {
2170 t.Errorf("Repositories.AddAdminEnforcement returned %+v, want %+v", enforcement, want)
2171 }
2172
2173 const methodName = "AddAdminEnforcement"
2174 testBadOptions(t, methodName, func() (err error) {
2175 _, _, err = client.Repositories.AddAdminEnforcement(ctx, "\n", "\n", "\n")
2176 return err
2177 })
2178
2179 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2180 got, resp, err := client.Repositories.AddAdminEnforcement(ctx, "o", "r", "b")
2181 if got != nil {
2182 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2183 }
2184 return resp, err
2185 })
2186 }
2187
2188 func TestRepositoriesService_RemoveAdminEnforcement(t *testing.T) {
2189 client, mux, _, teardown := setup()
2190 defer teardown()
2191
2192 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2193 testMethod(t, r, "DELETE")
2194 w.WriteHeader(http.StatusNoContent)
2195 })
2196
2197 ctx := context.Background()
2198 _, err := client.Repositories.RemoveAdminEnforcement(ctx, "o", "r", "b")
2199 if err != nil {
2200 t.Errorf("Repositories.RemoveAdminEnforcement returned error: %v", err)
2201 }
2202
2203 const methodName = "RemoveAdminEnforcement"
2204 testBadOptions(t, methodName, func() (err error) {
2205 _, err = client.Repositories.RemoveAdminEnforcement(ctx, "\n", "\n", "\n")
2206 return err
2207 })
2208
2209 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2210 return client.Repositories.RemoveAdminEnforcement(ctx, "o", "r", "b")
2211 })
2212 }
2213
2214 func TestRepositoriesService_GetSignaturesProtectedBranch(t *testing.T) {
2215 client, mux, _, teardown := setup()
2216 defer teardown()
2217
2218 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2219 testMethod(t, r, "GET")
2220 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2221 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/required_signatures","enabled":false}`)
2222 })
2223
2224 ctx := context.Background()
2225 signature, _, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", "b")
2226 if err != nil {
2227 t.Errorf("Repositories.GetSignaturesProtectedBranch returned error: %v", err)
2228 }
2229
2230 want := &SignaturesProtectedBranch{
2231 URL: String("/repos/o/r/branches/b/protection/required_signatures"),
2232 Enabled: Bool(false),
2233 }
2234
2235 if !cmp.Equal(signature, want) {
2236 t.Errorf("Repositories.GetSignaturesProtectedBranch returned %+v, want %+v", signature, want)
2237 }
2238
2239 const methodName = "GetSignaturesProtectedBranch"
2240 testBadOptions(t, methodName, func() (err error) {
2241 _, _, err = client.Repositories.GetSignaturesProtectedBranch(ctx, "\n", "\n", "\n")
2242 return err
2243 })
2244
2245 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2246 got, resp, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", "b")
2247 if got != nil {
2248 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2249 }
2250 return resp, err
2251 })
2252 }
2253
2254 func TestRepositoriesService_RequireSignaturesOnProtectedBranch(t *testing.T) {
2255 client, mux, _, teardown := setup()
2256 defer teardown()
2257
2258 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2259 testMethod(t, r, "POST")
2260 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2261 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/required_signatures","enabled":true}`)
2262 })
2263
2264 ctx := context.Background()
2265 signature, _, err := client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2266 if err != nil {
2267 t.Errorf("Repositories.RequireSignaturesOnProtectedBranch returned error: %v", err)
2268 }
2269
2270 want := &SignaturesProtectedBranch{
2271 URL: String("/repos/o/r/branches/b/protection/required_signatures"),
2272 Enabled: Bool(true),
2273 }
2274
2275 if !cmp.Equal(signature, want) {
2276 t.Errorf("Repositories.RequireSignaturesOnProtectedBranch returned %+v, want %+v", signature, want)
2277 }
2278
2279 const methodName = "RequireSignaturesOnProtectedBranch"
2280 testBadOptions(t, methodName, func() (err error) {
2281 _, _, err = client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "\n", "\n", "\n")
2282 return err
2283 })
2284
2285 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2286 got, resp, err := client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2287 if got != nil {
2288 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2289 }
2290 return resp, err
2291 })
2292 }
2293
2294 func TestRepositoriesService_OptionalSignaturesOnProtectedBranch(t *testing.T) {
2295 client, mux, _, teardown := setup()
2296 defer teardown()
2297
2298 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2299 testMethod(t, r, "DELETE")
2300 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2301 w.WriteHeader(http.StatusNoContent)
2302 })
2303
2304 ctx := context.Background()
2305 _, err := client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2306 if err != nil {
2307 t.Errorf("Repositories.OptionalSignaturesOnProtectedBranch returned error: %v", err)
2308 }
2309
2310 const methodName = "OptionalSignaturesOnProtectedBranch"
2311 testBadOptions(t, methodName, func() (err error) {
2312 _, err = client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "\n", "\n", "\n")
2313 return err
2314 })
2315
2316 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2317 return client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2318 })
2319 }
2320
2321 func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctions(t *testing.T) {
2322 req := PullRequestReviewsEnforcementRequest{}
2323
2324 got, err := json.Marshal(req)
2325 if err != nil {
2326 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2327 }
2328
2329 want := `{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2330 if want != string(got) {
2331 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2332 }
2333
2334 req = PullRequestReviewsEnforcementRequest{
2335 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{},
2336 }
2337
2338 got, err = json.Marshal(req)
2339 if err != nil {
2340 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2341 }
2342
2343 want = `{"dismissal_restrictions":{},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2344 if want != string(got) {
2345 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2346 }
2347
2348 req = PullRequestReviewsEnforcementRequest{
2349 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
2350 Users: &[]string{},
2351 Teams: &[]string{},
2352 },
2353 }
2354
2355 got, err = json.Marshal(req)
2356 if err != nil {
2357 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2358 }
2359
2360 want = `{"dismissal_restrictions":{"users":[],"teams":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2361 if want != string(got) {
2362 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2363 }
2364 }
2365
2366 func TestRepositoriesService_ListAllTopics(t *testing.T) {
2367 client, mux, _, teardown := setup()
2368 defer teardown()
2369
2370 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2371 testMethod(t, r, "GET")
2372 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2373 fmt.Fprint(w, `{"names":["go", "go-github", "github"]}`)
2374 })
2375
2376 ctx := context.Background()
2377 got, _, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2378 if err != nil {
2379 t.Fatalf("Repositories.ListAllTopics returned error: %v", err)
2380 }
2381
2382 want := []string{"go", "go-github", "github"}
2383 if !cmp.Equal(got, want) {
2384 t.Errorf("Repositories.ListAllTopics returned %+v, want %+v", got, want)
2385 }
2386
2387 const methodName = "ListAllTopics"
2388 testBadOptions(t, methodName, func() (err error) {
2389 _, _, err = client.Repositories.ListAllTopics(ctx, "\n", "\n")
2390 return err
2391 })
2392
2393 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2394 got, resp, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2395 if got != nil {
2396 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2397 }
2398 return resp, err
2399 })
2400 }
2401
2402 func TestRepositoriesService_ListAllTopics_emptyTopics(t *testing.T) {
2403 client, mux, _, teardown := setup()
2404 defer teardown()
2405
2406 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2407 testMethod(t, r, "GET")
2408 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2409 fmt.Fprint(w, `{"names":[]}`)
2410 })
2411
2412 ctx := context.Background()
2413 got, _, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2414 if err != nil {
2415 t.Fatalf("Repositories.ListAllTopics returned error: %v", err)
2416 }
2417
2418 want := []string{}
2419 if !cmp.Equal(got, want) {
2420 t.Errorf("Repositories.ListAllTopics returned %+v, want %+v", got, want)
2421 }
2422 }
2423
2424 func TestRepositoriesService_ReplaceAllTopics(t *testing.T) {
2425 client, mux, _, teardown := setup()
2426 defer teardown()
2427
2428 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2429 testMethod(t, r, "PUT")
2430 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2431 fmt.Fprint(w, `{"names":["go", "go-github", "github"]}`)
2432 })
2433
2434 ctx := context.Background()
2435 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{"go", "go-github", "github"})
2436 if err != nil {
2437 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2438 }
2439
2440 want := []string{"go", "go-github", "github"}
2441 if !cmp.Equal(got, want) {
2442 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2443 }
2444
2445 const methodName = "ReplaceAllTopics"
2446 testBadOptions(t, methodName, func() (err error) {
2447 _, _, err = client.Repositories.ReplaceAllTopics(ctx, "\n", "\n", []string{"\n", "\n", "\n"})
2448 return err
2449 })
2450
2451 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2452 got, resp, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{"go", "go-github", "github"})
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_ReplaceAllTopics_nilSlice(t *testing.T) {
2461 client, mux, _, teardown := setup()
2462 defer teardown()
2463
2464 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2465 testMethod(t, r, "PUT")
2466 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2467 testBody(t, r, `{"names":[]}`+"\n")
2468 fmt.Fprint(w, `{"names":[]}`)
2469 })
2470
2471 ctx := context.Background()
2472 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", nil)
2473 if err != nil {
2474 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2475 }
2476
2477 want := []string{}
2478 if !cmp.Equal(got, want) {
2479 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2480 }
2481 }
2482
2483 func TestRepositoriesService_ReplaceAllTopics_emptySlice(t *testing.T) {
2484 client, mux, _, teardown := setup()
2485 defer teardown()
2486
2487 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2488 testMethod(t, r, "PUT")
2489 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2490 testBody(t, r, `{"names":[]}`+"\n")
2491 fmt.Fprint(w, `{"names":[]}`)
2492 })
2493
2494 ctx := context.Background()
2495 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{})
2496 if err != nil {
2497 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2498 }
2499
2500 want := []string{}
2501 if !cmp.Equal(got, want) {
2502 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2503 }
2504 }
2505
2506 func TestRepositoriesService_ListApps(t *testing.T) {
2507 client, mux, _, teardown := setup()
2508 defer teardown()
2509
2510 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2511 testMethod(t, r, "GET")
2512 })
2513
2514 ctx := context.Background()
2515 _, _, err := client.Repositories.ListApps(ctx, "o", "r", "b")
2516 if err != nil {
2517 t.Errorf("Repositories.ListApps returned error: %v", err)
2518 }
2519
2520 const methodName = "ListApps"
2521 testBadOptions(t, methodName, func() (err error) {
2522 _, _, err = client.Repositories.ListApps(ctx, "\n", "\n", "\n")
2523 return err
2524 })
2525
2526 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2527 got, resp, err := client.Repositories.ListApps(ctx, "o", "r", "b")
2528 if got != nil {
2529 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2530 }
2531 return resp, err
2532 })
2533 }
2534
2535 func TestRepositoriesService_ReplaceAppRestrictions(t *testing.T) {
2536 client, mux, _, teardown := setup()
2537 defer teardown()
2538
2539 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2540 testMethod(t, r, "PUT")
2541 fmt.Fprint(w, `[{
2542 "name": "octocat"
2543 }]`)
2544 })
2545 input := []string{"octocat"}
2546 ctx := context.Background()
2547 got, _, err := client.Repositories.ReplaceAppRestrictions(ctx, "o", "r", "b", input)
2548 if err != nil {
2549 t.Errorf("Repositories.ReplaceAppRestrictions returned error: %v", err)
2550 }
2551 want := []*App{
2552 {Name: String("octocat")},
2553 }
2554 if !cmp.Equal(got, want) {
2555 t.Errorf("Repositories.ReplaceAppRestrictions returned %+v, want %+v", got, want)
2556 }
2557
2558 const methodName = "ReplaceAppRestrictions"
2559 testBadOptions(t, methodName, func() (err error) {
2560 _, _, err = client.Repositories.ReplaceAppRestrictions(ctx, "\n", "\n", "\n", input)
2561 return err
2562 })
2563
2564 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2565 got, resp, err := client.Repositories.ReplaceAppRestrictions(ctx, "o", "r", "b", input)
2566 if got != nil {
2567 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2568 }
2569 return resp, err
2570 })
2571 }
2572
2573 func TestRepositoriesService_AddAppRestrictions(t *testing.T) {
2574 client, mux, _, teardown := setup()
2575 defer teardown()
2576
2577 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2578 testMethod(t, r, "POST")
2579 fmt.Fprint(w, `[{
2580 "name": "octocat"
2581 }]`)
2582 })
2583 input := []string{"octocat"}
2584 ctx := context.Background()
2585 got, _, err := client.Repositories.AddAppRestrictions(ctx, "o", "r", "b", input)
2586 if err != nil {
2587 t.Errorf("Repositories.AddAppRestrictions returned error: %v", err)
2588 }
2589 want := []*App{
2590 {Name: String("octocat")},
2591 }
2592 if !cmp.Equal(got, want) {
2593 t.Errorf("Repositories.AddAppRestrictions returned %+v, want %+v", got, want)
2594 }
2595
2596 const methodName = "AddAppRestrictions"
2597 testBadOptions(t, methodName, func() (err error) {
2598 _, _, err = client.Repositories.AddAppRestrictions(ctx, "\n", "\n", "\n", input)
2599 return err
2600 })
2601
2602 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2603 got, resp, err := client.Repositories.AddAppRestrictions(ctx, "o", "r", "b", input)
2604 if got != nil {
2605 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2606 }
2607 return resp, err
2608 })
2609 }
2610
2611 func TestRepositoriesService_RemoveAppRestrictions(t *testing.T) {
2612 client, mux, _, teardown := setup()
2613 defer teardown()
2614
2615 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2616 testMethod(t, r, "DELETE")
2617 fmt.Fprint(w, `[]`)
2618 })
2619 input := []string{"octocat"}
2620 ctx := context.Background()
2621 got, _, err := client.Repositories.RemoveAppRestrictions(ctx, "o", "r", "b", input)
2622 if err != nil {
2623 t.Errorf("Repositories.RemoveAppRestrictions returned error: %v", err)
2624 }
2625 want := []*App{}
2626 if !cmp.Equal(got, want) {
2627 t.Errorf("Repositories.RemoveAppRestrictions returned %+v, want %+v", got, want)
2628 }
2629
2630 const methodName = "RemoveAppRestrictions"
2631 testBadOptions(t, methodName, func() (err error) {
2632 _, _, err = client.Repositories.RemoveAppRestrictions(ctx, "\n", "\n", "\n", input)
2633 return err
2634 })
2635
2636 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2637 got, resp, err := client.Repositories.RemoveAppRestrictions(ctx, "o", "r", "b", input)
2638 if got != nil {
2639 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2640 }
2641 return resp, err
2642 })
2643 }
2644
2645 func TestRepositoriesService_Transfer(t *testing.T) {
2646 client, mux, _, teardown := setup()
2647 defer teardown()
2648
2649 input := TransferRequest{NewOwner: "a", TeamID: []int64{123}}
2650
2651 mux.HandleFunc("/repos/o/r/transfer", func(w http.ResponseWriter, r *http.Request) {
2652 var v TransferRequest
2653 json.NewDecoder(r.Body).Decode(&v)
2654
2655 testMethod(t, r, "POST")
2656 if !cmp.Equal(v, input) {
2657 t.Errorf("Request body = %+v, want %+v", v, input)
2658 }
2659
2660 fmt.Fprint(w, `{"owner":{"login":"a"}}`)
2661 })
2662
2663 ctx := context.Background()
2664 got, _, err := client.Repositories.Transfer(ctx, "o", "r", input)
2665 if err != nil {
2666 t.Errorf("Repositories.Transfer returned error: %v", err)
2667 }
2668
2669 want := &Repository{Owner: &User{Login: String("a")}}
2670 if !cmp.Equal(got, want) {
2671 t.Errorf("Repositories.Transfer returned %+v, want %+v", got, want)
2672 }
2673
2674 const methodName = "Transfer"
2675 testBadOptions(t, methodName, func() (err error) {
2676 _, _, err = client.Repositories.Transfer(ctx, "\n", "\n", input)
2677 return err
2678 })
2679
2680 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2681 got, resp, err := client.Repositories.Transfer(ctx, "o", "r", input)
2682 if got != nil {
2683 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2684 }
2685 return resp, err
2686 })
2687 }
2688
2689 func TestRepositoriesService_Dispatch(t *testing.T) {
2690 client, mux, _, teardown := setup()
2691 defer teardown()
2692
2693 var input DispatchRequestOptions
2694
2695 mux.HandleFunc("/repos/o/r/dispatches", func(w http.ResponseWriter, r *http.Request) {
2696 var v DispatchRequestOptions
2697 json.NewDecoder(r.Body).Decode(&v)
2698
2699 testMethod(t, r, "POST")
2700 if !cmp.Equal(v, input) {
2701 t.Errorf("Request body = %+v, want %+v", v, input)
2702 }
2703
2704 fmt.Fprint(w, `{"owner":{"login":"a"}}`)
2705 })
2706
2707 ctx := context.Background()
2708
2709 testCases := []interface{}{
2710 nil,
2711 struct {
2712 Foo string
2713 }{
2714 Foo: "test",
2715 },
2716 struct {
2717 Bar int
2718 }{
2719 Bar: 42,
2720 },
2721 struct {
2722 Foo string
2723 Bar int
2724 Baz bool
2725 }{
2726 Foo: "test",
2727 Bar: 42,
2728 Baz: false,
2729 },
2730 }
2731
2732 for _, tc := range testCases {
2733 if tc == nil {
2734 input = DispatchRequestOptions{EventType: "go"}
2735 } else {
2736 bytes, _ := json.Marshal(tc)
2737 payload := json.RawMessage(bytes)
2738 input = DispatchRequestOptions{EventType: "go", ClientPayload: &payload}
2739 }
2740
2741 got, _, err := client.Repositories.Dispatch(ctx, "o", "r", input)
2742 if err != nil {
2743 t.Errorf("Repositories.Dispatch returned error: %v", err)
2744 }
2745
2746 want := &Repository{Owner: &User{Login: String("a")}}
2747 if !cmp.Equal(got, want) {
2748 t.Errorf("Repositories.Dispatch returned %+v, want %+v", got, want)
2749 }
2750 }
2751
2752 const methodName = "Dispatch"
2753 testBadOptions(t, methodName, func() (err error) {
2754 _, _, err = client.Repositories.Dispatch(ctx, "\n", "\n", input)
2755 return err
2756 })
2757
2758 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2759 got, resp, err := client.Repositories.Dispatch(ctx, "o", "r", input)
2760 if got != nil {
2761 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2762 }
2763 return resp, err
2764 })
2765 }
2766
2767 func TestAdvancedSecurity_Marshal(t *testing.T) {
2768 testJSONMarshal(t, &AdvancedSecurity{}, "{}")
2769
2770 u := &AdvancedSecurity{
2771 Status: String("status"),
2772 }
2773
2774 want := `{
2775 "status": "status"
2776 }`
2777
2778 testJSONMarshal(t, u, want)
2779 }
2780
2781 func TestAuthorizedActorsOnly_Marshal(t *testing.T) {
2782 testJSONMarshal(t, &AuthorizedActorsOnly{}, "{}")
2783
2784 u := &AuthorizedActorsOnly{
2785 From: Bool(true),
2786 }
2787
2788 want := `{
2789 "from" : true
2790 }`
2791
2792 testJSONMarshal(t, u, want)
2793 }
2794
View as plain text