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"},"secret_scanning_push_protection":{"status":"enabled"},"dependabot_security_updates":{"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")}, SecretScanningPushProtection: &SecretScanningPushProtection{String("enabled")}, DependabotSecurityUpdates: &DependabotSecurityUpdates{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
657 w.WriteHeader(http.StatusNoContent)
658 })
659
660 ctx := context.Background()
661 if _, err := client.Repositories.EnableAutomatedSecurityFixes(ctx, "o", "r"); err != nil {
662 t.Errorf("Repositories.EnableAutomatedSecurityFixes returned error: %v", err)
663 }
664 }
665
666 func TestRepositoriesService_GetAutomatedSecurityFixes(t *testing.T) {
667 client, mux, _, teardown := setup()
668 defer teardown()
669
670 mux.HandleFunc("/repos/o/r/automated-security-fixes", func(w http.ResponseWriter, r *http.Request) {
671 testMethod(t, r, "GET")
672 fmt.Fprint(w, `{"enabled": true, "paused": false}`)
673 })
674
675 ctx := context.Background()
676 fixes, _, err := client.Repositories.GetAutomatedSecurityFixes(ctx, "o", "r")
677 if err != nil {
678 t.Errorf("Repositories.GetAutomatedSecurityFixes returned errpr: #{err}")
679 }
680
681 want := &AutomatedSecurityFixes{
682 Enabled: Bool(true),
683 Paused: Bool(false),
684 }
685 if !cmp.Equal(fixes, want) {
686 t.Errorf("Repositories.GetAutomatedSecurityFixes returned #{fixes}, want #{want}")
687 }
688
689 const methodName = "GetAutomatedSecurityFixes"
690 testBadOptions(t, methodName, func() (err error) {
691 _, _, err = client.Repositories.GetAutomatedSecurityFixes(ctx, "\n", "\n")
692 return err
693 })
694 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
695 got, resp, err := client.Repositories.GetAutomatedSecurityFixes(ctx, "o", "r")
696 if got != nil {
697 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
698 }
699 return resp, err
700 })
701 }
702
703 func TestRepositoriesService_DisableAutomatedSecurityFixes(t *testing.T) {
704 client, mux, _, teardown := setup()
705 defer teardown()
706
707 mux.HandleFunc("/repos/o/r/automated-security-fixes", func(w http.ResponseWriter, r *http.Request) {
708 testMethod(t, r, "DELETE")
709
710 w.WriteHeader(http.StatusNoContent)
711 })
712
713 ctx := context.Background()
714 if _, err := client.Repositories.DisableAutomatedSecurityFixes(ctx, "o", "r"); err != nil {
715 t.Errorf("Repositories.DisableAutomatedSecurityFixes returned error: %v", err)
716 }
717 }
718
719 func TestRepositoriesService_ListContributors(t *testing.T) {
720 client, mux, _, teardown := setup()
721 defer teardown()
722
723 mux.HandleFunc("/repos/o/r/contributors", func(w http.ResponseWriter, r *http.Request) {
724 testMethod(t, r, "GET")
725 testFormValues(t, r, values{
726 "anon": "true",
727 "page": "2",
728 })
729 fmt.Fprint(w, `[{"contributions":42}]`)
730 })
731
732 opts := &ListContributorsOptions{Anon: "true", ListOptions: ListOptions{Page: 2}}
733 ctx := context.Background()
734 contributors, _, err := client.Repositories.ListContributors(ctx, "o", "r", opts)
735 if err != nil {
736 t.Errorf("Repositories.ListContributors returned error: %v", err)
737 }
738
739 want := []*Contributor{{Contributions: Int(42)}}
740 if !cmp.Equal(contributors, want) {
741 t.Errorf("Repositories.ListContributors returned %+v, want %+v", contributors, want)
742 }
743
744 const methodName = "ListContributors"
745 testBadOptions(t, methodName, func() (err error) {
746 _, _, err = client.Repositories.ListContributors(ctx, "\n", "\n", opts)
747 return err
748 })
749
750 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
751 got, resp, err := client.Repositories.ListContributors(ctx, "o", "r", opts)
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_ListLanguages(t *testing.T) {
760 client, mux, _, teardown := setup()
761 defer teardown()
762
763 mux.HandleFunc("/repos/o/r/languages", func(w http.ResponseWriter, r *http.Request) {
764 testMethod(t, r, "GET")
765 fmt.Fprint(w, `{"go":1}`)
766 })
767
768 ctx := context.Background()
769 languages, _, err := client.Repositories.ListLanguages(ctx, "o", "r")
770 if err != nil {
771 t.Errorf("Repositories.ListLanguages returned error: %v", err)
772 }
773
774 want := map[string]int{"go": 1}
775 if !cmp.Equal(languages, want) {
776 t.Errorf("Repositories.ListLanguages returned %+v, want %+v", languages, want)
777 }
778
779 const methodName = "ListLanguages"
780 testBadOptions(t, methodName, func() (err error) {
781 _, _, err = client.Repositories.ListLanguages(ctx, "\n", "\n")
782 return err
783 })
784
785 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
786 got, resp, err := client.Repositories.ListLanguages(ctx, "o", "r")
787 if got != nil {
788 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
789 }
790 return resp, err
791 })
792 }
793
794 func TestRepositoriesService_ListTeams(t *testing.T) {
795 client, mux, _, teardown := setup()
796 defer teardown()
797
798 mux.HandleFunc("/repos/o/r/teams", func(w http.ResponseWriter, r *http.Request) {
799 testMethod(t, r, "GET")
800 testFormValues(t, r, values{"page": "2"})
801 fmt.Fprint(w, `[{"id":1}]`)
802 })
803
804 opt := &ListOptions{Page: 2}
805 ctx := context.Background()
806 teams, _, err := client.Repositories.ListTeams(ctx, "o", "r", opt)
807 if err != nil {
808 t.Errorf("Repositories.ListTeams returned error: %v", err)
809 }
810
811 want := []*Team{{ID: Int64(1)}}
812 if !cmp.Equal(teams, want) {
813 t.Errorf("Repositories.ListTeams returned %+v, want %+v", teams, want)
814 }
815
816 const methodName = "ListTeams"
817 testBadOptions(t, methodName, func() (err error) {
818 _, _, err = client.Repositories.ListTeams(ctx, "\n", "\n", opt)
819 return err
820 })
821
822 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
823 got, resp, err := client.Repositories.ListTeams(ctx, "o", "r", opt)
824 if got != nil {
825 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
826 }
827 return resp, err
828 })
829 }
830
831 func TestRepositoriesService_ListTags(t *testing.T) {
832 client, mux, _, teardown := setup()
833 defer teardown()
834
835 mux.HandleFunc("/repos/o/r/tags", func(w http.ResponseWriter, r *http.Request) {
836 testMethod(t, r, "GET")
837 testFormValues(t, r, values{"page": "2"})
838 fmt.Fprint(w, `[{"name":"n", "commit" : {"sha" : "s", "url" : "u"}, "zipball_url": "z", "tarball_url": "t"}]`)
839 })
840
841 opt := &ListOptions{Page: 2}
842 ctx := context.Background()
843 tags, _, err := client.Repositories.ListTags(ctx, "o", "r", opt)
844 if err != nil {
845 t.Errorf("Repositories.ListTags returned error: %v", err)
846 }
847
848 want := []*RepositoryTag{
849 {
850 Name: String("n"),
851 Commit: &Commit{
852 SHA: String("s"),
853 URL: String("u"),
854 },
855 ZipballURL: String("z"),
856 TarballURL: String("t"),
857 },
858 }
859 if !cmp.Equal(tags, want) {
860 t.Errorf("Repositories.ListTags returned %+v, want %+v", tags, want)
861 }
862
863 const methodName = "ListTags"
864 testBadOptions(t, methodName, func() (err error) {
865 _, _, err = client.Repositories.ListTags(ctx, "\n", "\n", opt)
866 return err
867 })
868
869 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
870 got, resp, err := client.Repositories.ListTags(ctx, "o", "r", opt)
871 if got != nil {
872 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
873 }
874 return resp, err
875 })
876 }
877
878 func TestRepositoriesService_ListBranches(t *testing.T) {
879 client, mux, _, teardown := setup()
880 defer teardown()
881
882 mux.HandleFunc("/repos/o/r/branches", func(w http.ResponseWriter, r *http.Request) {
883 testMethod(t, r, "GET")
884 testFormValues(t, r, values{"page": "2"})
885 fmt.Fprint(w, `[{"name":"master", "commit" : {"sha" : "a57781", "url" : "https://api.github.com/repos/o/r/commits/a57781"}}]`)
886 })
887
888 opt := &BranchListOptions{
889 Protected: nil,
890 ListOptions: ListOptions{Page: 2},
891 }
892 ctx := context.Background()
893 branches, _, err := client.Repositories.ListBranches(ctx, "o", "r", opt)
894 if err != nil {
895 t.Errorf("Repositories.ListBranches returned error: %v", err)
896 }
897
898 want := []*Branch{{Name: String("master"), Commit: &RepositoryCommit{SHA: String("a57781"), URL: String("https://api.github.com/repos/o/r/commits/a57781")}}}
899 if !cmp.Equal(branches, want) {
900 t.Errorf("Repositories.ListBranches returned %+v, want %+v", branches, want)
901 }
902
903 const methodName = "ListBranches"
904 testBadOptions(t, methodName, func() (err error) {
905 _, _, err = client.Repositories.ListBranches(ctx, "\n", "\n", opt)
906 return err
907 })
908
909 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
910 got, resp, err := client.Repositories.ListBranches(ctx, "o", "r", opt)
911 if got != nil {
912 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
913 }
914 return resp, err
915 })
916 }
917
918 func TestRepositoriesService_GetBranch(t *testing.T) {
919 client, mux, _, teardown := setup()
920 defer teardown()
921
922 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) {
923 testMethod(t, r, "GET")
924 fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true}`)
925 })
926
927 ctx := context.Background()
928 branch, _, err := client.Repositories.GetBranch(ctx, "o", "r", "b", false)
929 if err != nil {
930 t.Errorf("Repositories.GetBranch returned error: %v", err)
931 }
932
933 want := &Branch{
934 Name: String("n"),
935 Commit: &RepositoryCommit{
936 SHA: String("s"),
937 Commit: &Commit{
938 Message: String("m"),
939 },
940 },
941 Protected: Bool(true),
942 }
943
944 if !cmp.Equal(branch, want) {
945 t.Errorf("Repositories.GetBranch returned %+v, want %+v", branch, want)
946 }
947
948 const methodName = "GetBranch"
949 testBadOptions(t, methodName, func() (err error) {
950 _, _, err = client.Repositories.GetBranch(ctx, "\n", "\n", "\n", false)
951 return err
952 })
953 }
954
955 func TestRepositoriesService_GetBranch_BadJSONResponse(t *testing.T) {
956 client, mux, _, teardown := setup()
957 defer teardown()
958
959 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) {
960 testMethod(t, r, "GET")
961 fmt.Fprint(w, `{"name":"n", "commit":{"sha":...truncated`)
962 })
963
964 ctx := context.Background()
965 if _, _, err := client.Repositories.GetBranch(ctx, "o", "r", "b", false); err == nil {
966 t.Error("Repositories.GetBranch returned no error; wanted JSON error")
967 }
968 }
969
970 func TestRepositoriesService_GetBranch_StatusMovedPermanently_followRedirects(t *testing.T) {
971 client, mux, serverURL, teardown := setup()
972 defer teardown()
973
974 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) {
975 testMethod(t, r, "GET")
976 redirectURL, _ := url.Parse(serverURL + baseURLPath + "/repos/o/r/branches/br")
977 http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently)
978 })
979 mux.HandleFunc("/repos/o/r/branches/br", func(w http.ResponseWriter, r *http.Request) {
980 testMethod(t, r, "GET")
981 fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true}`)
982 })
983 ctx := context.Background()
984 branch, resp, err := client.Repositories.GetBranch(ctx, "o", "r", "b", true)
985 if err != nil {
986 t.Errorf("Repositories.GetBranch returned error: %v", err)
987 }
988 if resp.StatusCode != http.StatusOK {
989 t.Errorf("Repositories.GetBranch returned status: %d, want %d", resp.StatusCode, http.StatusOK)
990 }
991
992 want := &Branch{
993 Name: String("n"),
994 Commit: &RepositoryCommit{
995 SHA: String("s"),
996 Commit: &Commit{
997 Message: String("m"),
998 },
999 },
1000 Protected: Bool(true),
1001 }
1002 if !cmp.Equal(branch, want) {
1003 t.Errorf("Repositories.GetBranch returned %+v, want %+v", branch, want)
1004 }
1005 }
1006
1007 func TestRepositoriesService_GetBranch_notFound(t *testing.T) {
1008 client, mux, _, teardown := setup()
1009 defer teardown()
1010
1011 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) {
1012 testMethod(t, r, "GET")
1013 http.Error(w, "branch not found", http.StatusNotFound)
1014 })
1015 ctx := context.Background()
1016 _, resp, err := client.Repositories.GetBranch(ctx, "o", "r", "b", true)
1017 if err == nil {
1018 t.Error("Repositories.GetBranch returned error: nil")
1019 }
1020 if resp.StatusCode != http.StatusNotFound {
1021 t.Errorf("Repositories.GetBranch returned status: %d, want %d", resp.StatusCode, http.StatusNotFound)
1022 }
1023
1024
1025 client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
1026 return nil, errors.New("failed to get branch")
1027 })
1028
1029 const methodName = "GetBranch"
1030 testBadOptions(t, methodName, func() (err error) {
1031 _, _, err = client.Repositories.GetBranch(ctx, "\n", "\n", "\n", true)
1032 return err
1033 })
1034 }
1035
1036 func TestRepositoriesService_RenameBranch(t *testing.T) {
1037 client, mux, _, teardown := setup()
1038 defer teardown()
1039
1040 renameBranchReq := "nn"
1041
1042 mux.HandleFunc("/repos/o/r/branches/b/rename", func(w http.ResponseWriter, r *http.Request) {
1043 v := new(renameBranchRequest)
1044 json.NewDecoder(r.Body).Decode(v)
1045
1046 testMethod(t, r, "POST")
1047 want := &renameBranchRequest{NewName: "nn"}
1048 if !cmp.Equal(v, want) {
1049 t.Errorf("Request body = %+v, want %+v", v, want)
1050 }
1051
1052 fmt.Fprint(w, `{"protected":true,"name":"nn"}`)
1053 })
1054
1055 ctx := context.Background()
1056 got, _, err := client.Repositories.RenameBranch(ctx, "o", "r", "b", renameBranchReq)
1057 if err != nil {
1058 t.Errorf("Repositories.RenameBranch returned error: %v", err)
1059 }
1060
1061 want := &Branch{Name: String("nn"), Protected: Bool(true)}
1062 if !cmp.Equal(got, want) {
1063 t.Errorf("Repositories.RenameBranch returned %+v, want %+v", got, want)
1064 }
1065
1066 const methodName = "RenameBranch"
1067 testBadOptions(t, methodName, func() (err error) {
1068 _, _, err = client.Repositories.RenameBranch(ctx, "\n", "\n", "\n", renameBranchReq)
1069 return err
1070 })
1071
1072 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1073 got, resp, err := client.Repositories.RenameBranch(ctx, "o", "r", "b", renameBranchReq)
1074 if got != nil {
1075 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1076 }
1077 return resp, err
1078 })
1079 }
1080
1081 func TestRepositoriesService_GetBranchProtection(t *testing.T) {
1082 client, mux, _, teardown := setup()
1083 defer teardown()
1084
1085 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1086 v := new(ProtectionRequest)
1087 json.NewDecoder(r.Body).Decode(v)
1088
1089 testMethod(t, r, "GET")
1090
1091 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1092 fmt.Fprintf(w, `{
1093 "required_status_checks":{
1094 "strict":true,
1095 "contexts":["continuous-integration"],
1096 "checks": [
1097 {
1098 "context": "continuous-integration",
1099 "app_id": null
1100 }
1101 ]
1102 },
1103 "required_pull_request_reviews":{
1104 "dismissal_restrictions":{
1105 "users":[{
1106 "id":3,
1107 "login":"u"
1108 }],
1109 "teams":[{
1110 "id":4,
1111 "slug":"t"
1112 }],
1113 "apps":[{
1114 "id":5,
1115 "slug":"a"
1116 }]
1117 },
1118 "dismiss_stale_reviews":true,
1119 "require_code_owner_reviews":true,
1120 "require_last_push_approval":false,
1121 "required_approving_review_count":1
1122 },
1123 "enforce_admins":{
1124 "url":"/repos/o/r/branches/b/protection/enforce_admins",
1125 "enabled":true
1126 },
1127 "restrictions":{
1128 "users":[{"id":1,"login":"u"}],
1129 "teams":[{"id":2,"slug":"t"}],
1130 "apps":[{"id":3,"slug":"a"}]
1131 },
1132 "required_conversation_resolution": {
1133 "enabled": true
1134 },
1135 "block_creations": {
1136 "enabled": false
1137 },
1138 "lock_branch": {
1139 "enabled": false
1140 },
1141 "allow_fork_syncing": {
1142 "enabled": false
1143 }
1144 }`)
1145 })
1146
1147 ctx := context.Background()
1148 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b")
1149 if err != nil {
1150 t.Errorf("Repositories.GetBranchProtection returned error: %v", err)
1151 }
1152
1153 want := &Protection{
1154 RequiredStatusChecks: &RequiredStatusChecks{
1155 Strict: true,
1156 Contexts: []string{"continuous-integration"},
1157 Checks: []*RequiredStatusCheck{
1158 {
1159 Context: "continuous-integration",
1160 },
1161 },
1162 },
1163 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1164 DismissStaleReviews: true,
1165 DismissalRestrictions: &DismissalRestrictions{
1166 Users: []*User{
1167 {Login: String("u"), ID: Int64(3)},
1168 },
1169 Teams: []*Team{
1170 {Slug: String("t"), ID: Int64(4)},
1171 },
1172 Apps: []*App{
1173 {Slug: String("a"), ID: Int64(5)},
1174 },
1175 },
1176 RequireCodeOwnerReviews: true,
1177 RequiredApprovingReviewCount: 1,
1178 RequireLastPushApproval: false,
1179 },
1180 EnforceAdmins: &AdminEnforcement{
1181 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
1182 Enabled: true,
1183 },
1184 Restrictions: &BranchRestrictions{
1185 Users: []*User{
1186 {Login: String("u"), ID: Int64(1)},
1187 },
1188 Teams: []*Team{
1189 {Slug: String("t"), ID: Int64(2)},
1190 },
1191 Apps: []*App{
1192 {Slug: String("a"), ID: Int64(3)},
1193 },
1194 },
1195 RequiredConversationResolution: &RequiredConversationResolution{
1196 Enabled: true,
1197 },
1198 BlockCreations: &BlockCreations{
1199 Enabled: Bool(false),
1200 },
1201 LockBranch: &LockBranch{
1202 Enabled: Bool(false),
1203 },
1204 AllowForkSyncing: &AllowForkSyncing{
1205 Enabled: Bool(false),
1206 },
1207 }
1208 if !cmp.Equal(protection, want) {
1209 t.Errorf("Repositories.GetBranchProtection returned %+v, want %+v", protection, want)
1210 }
1211
1212 const methodName = "GetBranchProtection"
1213 testBadOptions(t, methodName, func() (err error) {
1214 _, _, err = client.Repositories.GetBranchProtection(ctx, "\n", "\n", "\n")
1215 return err
1216 })
1217
1218 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1219 got, resp, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b")
1220 if got != nil {
1221 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1222 }
1223 return resp, err
1224 })
1225 }
1226
1227 func TestRepositoriesService_GetBranchProtection_noDismissalRestrictions(t *testing.T) {
1228 client, mux, _, teardown := setup()
1229 defer teardown()
1230
1231 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1232 testMethod(t, r, "GET")
1233
1234 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1235 fmt.Fprintf(w, `{
1236 "required_status_checks":{
1237 "strict":true,
1238 "contexts":["continuous-integration"],
1239 "checks": [
1240 {
1241 "context": "continuous-integration",
1242 "app_id": null
1243 }
1244 ]
1245 },
1246 "required_pull_request_reviews":{
1247 "dismiss_stale_reviews":true,
1248 "require_code_owner_reviews":true,
1249 "required_approving_review_count":1
1250 },
1251 "enforce_admins":{
1252 "url":"/repos/o/r/branches/b/protection/enforce_admins",
1253 "enabled":true
1254 },
1255 "restrictions":{
1256 "users":[{"id":1,"login":"u"}],
1257 "teams":[{"id":2,"slug":"t"}]
1258 }
1259 }`)
1260 })
1261
1262 ctx := context.Background()
1263 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b")
1264 if err != nil {
1265 t.Errorf("Repositories.GetBranchProtection returned error: %v", err)
1266 }
1267
1268 want := &Protection{
1269 RequiredStatusChecks: &RequiredStatusChecks{
1270 Strict: true,
1271 Contexts: []string{"continuous-integration"},
1272 Checks: []*RequiredStatusCheck{
1273 {
1274 Context: "continuous-integration",
1275 },
1276 },
1277 },
1278 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1279 DismissStaleReviews: true,
1280 DismissalRestrictions: nil,
1281 RequireCodeOwnerReviews: true,
1282 RequiredApprovingReviewCount: 1,
1283 },
1284 EnforceAdmins: &AdminEnforcement{
1285 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
1286 Enabled: true,
1287 },
1288 Restrictions: &BranchRestrictions{
1289 Users: []*User{
1290 {Login: String("u"), ID: Int64(1)},
1291 },
1292 Teams: []*Team{
1293 {Slug: String("t"), ID: Int64(2)},
1294 },
1295 },
1296 }
1297 if !cmp.Equal(protection, want) {
1298 t.Errorf("Repositories.GetBranchProtection returned %+v, want %+v", protection, want)
1299 }
1300 }
1301
1302 func TestRepositoriesService_GetBranchProtection_branchNotProtected(t *testing.T) {
1303 client, mux, _, teardown := setup()
1304 defer teardown()
1305
1306 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1307 testMethod(t, r, "GET")
1308
1309 w.WriteHeader(http.StatusBadRequest)
1310 fmt.Fprintf(w, `{
1311 "message": %q,
1312 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
1313 }`, githubBranchNotProtected)
1314 })
1315
1316 ctx := context.Background()
1317 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b")
1318
1319 if protection != nil {
1320 t.Errorf("Repositories.GetBranchProtection returned non-nil protection data")
1321 }
1322
1323 if err != ErrBranchNotProtected {
1324 t.Errorf("Repositories.GetBranchProtection returned an invalid error: %v", err)
1325 }
1326 }
1327
1328 func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) {
1329 client, mux, _, teardown := setup()
1330 defer teardown()
1331
1332 input := &ProtectionRequest{
1333 RequiredStatusChecks: &RequiredStatusChecks{
1334 Strict: true,
1335 Contexts: []string{"continuous-integration"},
1336 },
1337 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1338 DismissStaleReviews: true,
1339 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1340 Users: &[]string{"uu"},
1341 Teams: &[]string{"tt"},
1342 Apps: &[]string{"aa"},
1343 },
1344 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
1345 Users: []string{"uuu"},
1346 Teams: []string{"ttt"},
1347 Apps: []string{"aaa"},
1348 },
1349 },
1350 Restrictions: &BranchRestrictionsRequest{
1351 Users: []string{"u"},
1352 Teams: []string{"t"},
1353 Apps: []string{"a"},
1354 },
1355 BlockCreations: Bool(true),
1356 LockBranch: Bool(true),
1357 AllowForkSyncing: Bool(true),
1358 }
1359
1360 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1361 v := new(ProtectionRequest)
1362 json.NewDecoder(r.Body).Decode(v)
1363
1364 testMethod(t, r, "PUT")
1365 if !cmp.Equal(v, input) {
1366 t.Errorf("Request body = %+v, want %+v", v, input)
1367 }
1368
1369
1370 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1371 fmt.Fprintf(w, `{
1372 "required_status_checks":{
1373 "strict":true,
1374 "contexts":["continuous-integration"],
1375 "checks": [
1376 {
1377 "context": "continuous-integration",
1378 "app_id": null
1379 }
1380 ]
1381 },
1382 "required_pull_request_reviews":{
1383 "dismissal_restrictions":{
1384 "users":[{
1385 "id":3,
1386 "login":"uu"
1387 }],
1388 "teams":[{
1389 "id":4,
1390 "slug":"tt"
1391 }],
1392 "apps":[{
1393 "id":5,
1394 "slug":"aa"
1395 }]
1396 },
1397 "dismiss_stale_reviews":true,
1398 "require_code_owner_reviews":true,
1399 "bypass_pull_request_allowances": {
1400 "users":[{"id":10,"login":"uuu"}],
1401 "teams":[{"id":20,"slug":"ttt"}],
1402 "apps":[{"id":30,"slug":"aaa"}]
1403 }
1404 },
1405 "restrictions":{
1406 "users":[{"id":1,"login":"u"}],
1407 "teams":[{"id":2,"slug":"t"}],
1408 "apps":[{"id":3,"slug":"a"}]
1409 },
1410 "block_creations": {
1411 "enabled": true
1412 },
1413 "lock_branch": {
1414 "enabled": true
1415 },
1416 "allow_fork_syncing": {
1417 "enabled": true
1418 }
1419 }`)
1420 })
1421
1422 ctx := context.Background()
1423 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1424 if err != nil {
1425 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1426 }
1427
1428 want := &Protection{
1429 RequiredStatusChecks: &RequiredStatusChecks{
1430 Strict: true,
1431 Contexts: []string{"continuous-integration"},
1432 Checks: []*RequiredStatusCheck{
1433 {
1434 Context: "continuous-integration",
1435 },
1436 },
1437 },
1438 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1439 DismissStaleReviews: true,
1440 DismissalRestrictions: &DismissalRestrictions{
1441 Users: []*User{
1442 {Login: String("uu"), ID: Int64(3)},
1443 },
1444 Teams: []*Team{
1445 {Slug: String("tt"), ID: Int64(4)},
1446 },
1447 Apps: []*App{
1448 {Slug: String("aa"), ID: Int64(5)},
1449 },
1450 },
1451 RequireCodeOwnerReviews: true,
1452 BypassPullRequestAllowances: &BypassPullRequestAllowances{
1453 Users: []*User{
1454 {Login: String("uuu"), ID: Int64(10)},
1455 },
1456 Teams: []*Team{
1457 {Slug: String("ttt"), ID: Int64(20)},
1458 },
1459 Apps: []*App{
1460 {Slug: String("aaa"), ID: Int64(30)},
1461 },
1462 },
1463 },
1464 Restrictions: &BranchRestrictions{
1465 Users: []*User{
1466 {Login: String("u"), ID: Int64(1)},
1467 },
1468 Teams: []*Team{
1469 {Slug: String("t"), ID: Int64(2)},
1470 },
1471 Apps: []*App{
1472 {Slug: String("a"), ID: Int64(3)},
1473 },
1474 },
1475 BlockCreations: &BlockCreations{
1476 Enabled: Bool(true),
1477 },
1478 LockBranch: &LockBranch{
1479 Enabled: Bool(true),
1480 },
1481 AllowForkSyncing: &AllowForkSyncing{
1482 Enabled: Bool(true),
1483 },
1484 }
1485 if !cmp.Equal(protection, want) {
1486 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1487 }
1488
1489 const methodName = "UpdateBranchProtection"
1490 testBadOptions(t, methodName, func() (err error) {
1491 _, _, err = client.Repositories.UpdateBranchProtection(ctx, "\n", "\n", "\n", input)
1492 return err
1493 })
1494
1495 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1496 got, resp, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1497 if got != nil {
1498 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1499 }
1500 return resp, err
1501 })
1502 }
1503
1504 func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
1505 client, mux, _, teardown := setup()
1506 defer teardown()
1507
1508 input := &ProtectionRequest{
1509 RequiredStatusChecks: &RequiredStatusChecks{
1510 Strict: true,
1511 Checks: []*RequiredStatusCheck{
1512 {
1513 Context: "continuous-integration",
1514 },
1515 },
1516 },
1517 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1518 DismissStaleReviews: true,
1519 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1520 Users: &[]string{"uu"},
1521 Teams: &[]string{"tt"},
1522 Apps: &[]string{"aa"},
1523 },
1524 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
1525 Users: []string{"uuu"},
1526 Teams: []string{"ttt"},
1527 Apps: []string{"aaa"},
1528 },
1529 },
1530 Restrictions: &BranchRestrictionsRequest{
1531 Users: []string{"u"},
1532 Teams: []string{"t"},
1533 Apps: []string{"a"},
1534 },
1535 }
1536
1537 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1538 v := new(ProtectionRequest)
1539 json.NewDecoder(r.Body).Decode(v)
1540
1541 testMethod(t, r, "PUT")
1542 if !cmp.Equal(v, input) {
1543 t.Errorf("Request body = %+v, want %+v", v, input)
1544 }
1545
1546
1547 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1548 fmt.Fprintf(w, `{
1549 "required_status_checks":{
1550 "strict":true,
1551 "contexts":["continuous-integration"],
1552 "checks": [
1553 {
1554 "context": "continuous-integration",
1555 "app_id": null
1556 }
1557 ]
1558 },
1559 "required_pull_request_reviews":{
1560 "dismissal_restrictions":{
1561 "users":[{
1562 "id":3,
1563 "login":"uu"
1564 }],
1565 "teams":[{
1566 "id":4,
1567 "slug":"tt"
1568 }],
1569 "apps":[{
1570 "id":5,
1571 "slug":"aa"
1572 }]
1573 },
1574 "dismiss_stale_reviews":true,
1575 "require_code_owner_reviews":true,
1576 "bypass_pull_request_allowances": {
1577 "users":[{"id":10,"login":"uuu"}],
1578 "teams":[{"id":20,"slug":"ttt"}],
1579 "apps":[{"id":30,"slug":"aaa"}]
1580 }
1581 },
1582 "restrictions":{
1583 "users":[{"id":1,"login":"u"}],
1584 "teams":[{"id":2,"slug":"t"}],
1585 "apps":[{"id":3,"slug":"a"}]
1586 }
1587 }`)
1588 })
1589
1590 ctx := context.Background()
1591 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1592 if err != nil {
1593 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1594 }
1595
1596 want := &Protection{
1597 RequiredStatusChecks: &RequiredStatusChecks{
1598 Strict: true,
1599 Contexts: []string{"continuous-integration"},
1600 Checks: []*RequiredStatusCheck{
1601 {
1602 Context: "continuous-integration",
1603 },
1604 },
1605 },
1606 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1607 DismissStaleReviews: true,
1608 DismissalRestrictions: &DismissalRestrictions{
1609 Users: []*User{
1610 {Login: String("uu"), ID: Int64(3)},
1611 },
1612 Teams: []*Team{
1613 {Slug: String("tt"), ID: Int64(4)},
1614 },
1615 Apps: []*App{
1616 {Slug: String("aa"), ID: Int64(5)},
1617 },
1618 },
1619 RequireCodeOwnerReviews: true,
1620 BypassPullRequestAllowances: &BypassPullRequestAllowances{
1621 Users: []*User{
1622 {Login: String("uuu"), ID: Int64(10)},
1623 },
1624 Teams: []*Team{
1625 {Slug: String("ttt"), ID: Int64(20)},
1626 },
1627 Apps: []*App{
1628 {Slug: String("aaa"), ID: Int64(30)},
1629 },
1630 },
1631 },
1632 Restrictions: &BranchRestrictions{
1633 Users: []*User{
1634 {Login: String("u"), ID: Int64(1)},
1635 },
1636 Teams: []*Team{
1637 {Slug: String("t"), ID: Int64(2)},
1638 },
1639 Apps: []*App{
1640 {Slug: String("a"), ID: Int64(3)},
1641 },
1642 },
1643 }
1644 if !cmp.Equal(protection, want) {
1645 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1646 }
1647 }
1648
1649 func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T) {
1650 client, mux, _, teardown := setup()
1651 defer teardown()
1652
1653 input := &ProtectionRequest{
1654 RequiredStatusChecks: &RequiredStatusChecks{
1655 Strict: true,
1656 Checks: []*RequiredStatusCheck{},
1657 },
1658 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1659 DismissStaleReviews: true,
1660 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
1661 Users: &[]string{"uu"},
1662 Teams: &[]string{"tt"},
1663 Apps: &[]string{"aa"},
1664 },
1665 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
1666 Users: []string{"uuu"},
1667 Teams: []string{"ttt"},
1668 Apps: []string{"aaa"},
1669 },
1670 },
1671 Restrictions: &BranchRestrictionsRequest{
1672 Users: []string{"u"},
1673 Teams: []string{"t"},
1674 Apps: []string{"a"},
1675 },
1676 }
1677
1678 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1679 v := new(ProtectionRequest)
1680 json.NewDecoder(r.Body).Decode(v)
1681
1682 testMethod(t, r, "PUT")
1683 if !cmp.Equal(v, input) {
1684 t.Errorf("Request body = %+v, want %+v", v, input)
1685 }
1686
1687
1688 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
1689 fmt.Fprintf(w, `{
1690 "required_status_checks":{
1691 "strict":true,
1692 "contexts":[],
1693 "checks": []
1694 },
1695 "required_pull_request_reviews":{
1696 "dismissal_restrictions":{
1697 "users":[{
1698 "id":3,
1699 "login":"uu"
1700 }],
1701 "teams":[{
1702 "id":4,
1703 "slug":"tt"
1704 }],
1705 "apps":[{
1706 "id":5,
1707 "slug":"aa"
1708 }]
1709 },
1710 "dismiss_stale_reviews":true,
1711 "require_code_owner_reviews":true,
1712 "require_last_push_approval":false,
1713 "bypass_pull_request_allowances": {
1714 "users":[{"id":10,"login":"uuu"}],
1715 "teams":[{"id":20,"slug":"ttt"}],
1716 "apps":[{"id":30,"slug":"aaa"}]
1717 }
1718 },
1719 "restrictions":{
1720 "users":[{"id":1,"login":"u"}],
1721 "teams":[{"id":2,"slug":"t"}],
1722 "apps":[{"id":3,"slug":"a"}]
1723 }
1724 }`)
1725 })
1726
1727 ctx := context.Background()
1728 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1729 if err != nil {
1730 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1731 }
1732
1733 want := &Protection{
1734 RequiredStatusChecks: &RequiredStatusChecks{
1735 Strict: true,
1736 Contexts: []string{},
1737 Checks: []*RequiredStatusCheck{},
1738 },
1739 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1740 DismissStaleReviews: true,
1741 DismissalRestrictions: &DismissalRestrictions{
1742 Users: []*User{
1743 {Login: String("uu"), ID: Int64(3)},
1744 },
1745 Teams: []*Team{
1746 {Slug: String("tt"), ID: Int64(4)},
1747 },
1748 Apps: []*App{
1749 {Slug: String("aa"), ID: Int64(5)},
1750 },
1751 },
1752 RequireCodeOwnerReviews: true,
1753 BypassPullRequestAllowances: &BypassPullRequestAllowances{
1754 Users: []*User{
1755 {Login: String("uuu"), ID: Int64(10)},
1756 },
1757 Teams: []*Team{
1758 {Slug: String("ttt"), ID: Int64(20)},
1759 },
1760 Apps: []*App{
1761 {Slug: String("aaa"), ID: Int64(30)},
1762 },
1763 },
1764 },
1765 Restrictions: &BranchRestrictions{
1766 Users: []*User{
1767 {Login: String("u"), ID: Int64(1)},
1768 },
1769 Teams: []*Team{
1770 {Slug: String("t"), ID: Int64(2)},
1771 },
1772 Apps: []*App{
1773 {Slug: String("a"), ID: Int64(3)},
1774 },
1775 },
1776 }
1777 if !cmp.Equal(protection, want) {
1778 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1779 }
1780 }
1781
1782 func TestRepositoriesService_UpdateBranchProtection_RequireLastPushApproval(t *testing.T) {
1783 client, mux, _, teardown := setup()
1784 defer teardown()
1785
1786 input := &ProtectionRequest{
1787 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
1788 RequireLastPushApproval: Bool(true),
1789 },
1790 }
1791
1792 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1793 v := new(ProtectionRequest)
1794 json.NewDecoder(r.Body).Decode(v)
1795
1796 testMethod(t, r, "PUT")
1797 if !cmp.Equal(v, input) {
1798 t.Errorf("Request body = %+v, want %+v", v, input)
1799 }
1800
1801 fmt.Fprintf(w, `{
1802 "required_pull_request_reviews":{
1803 "require_last_push_approval":true
1804 }
1805 }`)
1806 })
1807
1808 ctx := context.Background()
1809 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
1810 if err != nil {
1811 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
1812 }
1813
1814 want := &Protection{
1815 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
1816 RequireLastPushApproval: true,
1817 },
1818 }
1819 if !cmp.Equal(protection, want) {
1820 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
1821 }
1822 }
1823
1824 func TestRepositoriesService_RemoveBranchProtection(t *testing.T) {
1825 client, mux, _, teardown := setup()
1826 defer teardown()
1827
1828 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
1829 testMethod(t, r, "DELETE")
1830 w.WriteHeader(http.StatusNoContent)
1831 })
1832
1833 ctx := context.Background()
1834 _, err := client.Repositories.RemoveBranchProtection(ctx, "o", "r", "b")
1835 if err != nil {
1836 t.Errorf("Repositories.RemoveBranchProtection returned error: %v", err)
1837 }
1838
1839 const methodName = "RemoveBranchProtection"
1840 testBadOptions(t, methodName, func() (err error) {
1841 _, err = client.Repositories.RemoveBranchProtection(ctx, "\n", "\n", "\n")
1842 return err
1843 })
1844
1845 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1846 return client.Repositories.RemoveBranchProtection(ctx, "o", "r", "b")
1847 })
1848 }
1849
1850 func TestRepositoriesService_ListLanguages_invalidOwner(t *testing.T) {
1851 client, _, _, teardown := setup()
1852 defer teardown()
1853
1854 ctx := context.Background()
1855 _, _, err := client.Repositories.ListLanguages(ctx, "%", "%")
1856 testURLParseError(t, err)
1857 }
1858
1859 func TestRepositoriesService_License(t *testing.T) {
1860 client, mux, _, teardown := setup()
1861 defer teardown()
1862
1863 mux.HandleFunc("/repos/o/r/license", func(w http.ResponseWriter, r *http.Request) {
1864 testMethod(t, r, "GET")
1865 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}}`)
1866 })
1867
1868 ctx := context.Background()
1869 got, _, err := client.Repositories.License(ctx, "o", "r")
1870 if err != nil {
1871 t.Errorf("Repositories.License returned error: %v", err)
1872 }
1873
1874 want := &RepositoryLicense{
1875 Name: String("LICENSE"),
1876 Path: String("LICENSE"),
1877 License: &License{
1878 Name: String("MIT License"),
1879 Key: String("mit"),
1880 SPDXID: String("MIT"),
1881 URL: String("https://api.github.com/licenses/mit"),
1882 Featured: Bool(true),
1883 },
1884 }
1885
1886 if !cmp.Equal(got, want) {
1887 t.Errorf("Repositories.License returned %+v, want %+v", got, want)
1888 }
1889
1890 const methodName = "License"
1891 testBadOptions(t, methodName, func() (err error) {
1892 _, _, err = client.Repositories.License(ctx, "\n", "\n")
1893 return err
1894 })
1895
1896 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1897 got, resp, err := client.Repositories.License(ctx, "o", "r")
1898 if got != nil {
1899 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1900 }
1901 return resp, err
1902 })
1903 }
1904
1905 func TestRepositoriesService_GetRequiredStatusChecks(t *testing.T) {
1906 client, mux, _, teardown := setup()
1907 defer teardown()
1908
1909 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1910 v := new(ProtectionRequest)
1911 json.NewDecoder(r.Body).Decode(v)
1912
1913 testMethod(t, r, "GET")
1914 fmt.Fprint(w, `{
1915 "strict": true,
1916 "contexts": ["x","y","z"],
1917 "checks": [
1918 {
1919 "context": "x",
1920 "app_id": null
1921 },
1922 {
1923 "context": "y",
1924 "app_id": null
1925 },
1926 {
1927 "context": "z",
1928 "app_id": null
1929 }
1930 ]
1931 }`)
1932 })
1933
1934 ctx := context.Background()
1935 checks, _, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1936 if err != nil {
1937 t.Errorf("Repositories.GetRequiredStatusChecks returned error: %v", err)
1938 }
1939
1940 want := &RequiredStatusChecks{
1941 Strict: true,
1942 Contexts: []string{"x", "y", "z"},
1943 Checks: []*RequiredStatusCheck{
1944 {
1945 Context: "x",
1946 },
1947 {
1948 Context: "y",
1949 },
1950 {
1951 Context: "z",
1952 },
1953 },
1954 }
1955 if !cmp.Equal(checks, want) {
1956 t.Errorf("Repositories.GetRequiredStatusChecks returned %+v, want %+v", checks, want)
1957 }
1958
1959 const methodName = "GetRequiredStatusChecks"
1960 testBadOptions(t, methodName, func() (err error) {
1961 _, _, err = client.Repositories.GetRequiredStatusChecks(ctx, "\n", "\n", "\n")
1962 return err
1963 })
1964
1965 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1966 got, resp, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1967 if got != nil {
1968 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1969 }
1970 return resp, err
1971 })
1972 }
1973
1974 func TestRepositoriesService_GetRequiredStatusChecks_branchNotProtected(t *testing.T) {
1975 client, mux, _, teardown := setup()
1976 defer teardown()
1977
1978 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
1979 testMethod(t, r, "GET")
1980
1981 w.WriteHeader(http.StatusBadRequest)
1982 fmt.Fprintf(w, `{
1983 "message": %q,
1984 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
1985 }`, githubBranchNotProtected)
1986 })
1987
1988 ctx := context.Background()
1989 checks, _, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b")
1990
1991 if checks != nil {
1992 t.Errorf("Repositories.GetRequiredStatusChecks returned non-nil status-checks data")
1993 }
1994
1995 if err != ErrBranchNotProtected {
1996 t.Errorf("Repositories.GetRequiredStatusChecks returned an invalid error: %v", err)
1997 }
1998 }
1999
2000 func TestRepositoriesService_UpdateRequiredStatusChecks_Contexts(t *testing.T) {
2001 client, mux, _, teardown := setup()
2002 defer teardown()
2003
2004 input := &RequiredStatusChecksRequest{
2005 Strict: Bool(true),
2006 Contexts: []string{"continuous-integration"},
2007 }
2008
2009 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
2010 v := new(RequiredStatusChecksRequest)
2011 json.NewDecoder(r.Body).Decode(v)
2012
2013 testMethod(t, r, "PATCH")
2014 if !cmp.Equal(v, input) {
2015 t.Errorf("Request body = %+v, want %+v", v, input)
2016 }
2017 testHeader(t, r, "Accept", mediaTypeV3)
2018 fmt.Fprintf(w, `{
2019 "strict":true,
2020 "contexts":["continuous-integration"],
2021 "checks": [
2022 {
2023 "context": "continuous-integration",
2024 "app_id": null
2025 }
2026 ]
2027 }`)
2028 })
2029
2030 ctx := context.Background()
2031 statusChecks, _, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
2032 if err != nil {
2033 t.Errorf("Repositories.UpdateRequiredStatusChecks returned error: %v", err)
2034 }
2035
2036 want := &RequiredStatusChecks{
2037 Strict: true,
2038 Contexts: []string{"continuous-integration"},
2039 Checks: []*RequiredStatusCheck{
2040 {
2041 Context: "continuous-integration",
2042 },
2043 },
2044 }
2045 if !cmp.Equal(statusChecks, want) {
2046 t.Errorf("Repositories.UpdateRequiredStatusChecks returned %+v, want %+v", statusChecks, want)
2047 }
2048
2049 const methodName = "UpdateRequiredStatusChecks"
2050 testBadOptions(t, methodName, func() (err error) {
2051 _, _, err = client.Repositories.UpdateRequiredStatusChecks(ctx, "\n", "\n", "\n", input)
2052 return err
2053 })
2054
2055 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2056 got, resp, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
2057 if got != nil {
2058 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2059 }
2060 return resp, err
2061 })
2062 }
2063
2064 func TestRepositoriesService_UpdateRequiredStatusChecks_Checks(t *testing.T) {
2065 client, mux, _, teardown := setup()
2066 defer teardown()
2067
2068 appID := int64(123)
2069 noAppID := int64(-1)
2070 input := &RequiredStatusChecksRequest{
2071 Strict: Bool(true),
2072 Checks: []*RequiredStatusCheck{
2073 {
2074 Context: "continuous-integration",
2075 },
2076 {
2077 Context: "continuous-integration2",
2078 AppID: &appID,
2079 },
2080 {
2081 Context: "continuous-integration3",
2082 AppID: &noAppID,
2083 },
2084 },
2085 }
2086
2087 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
2088 v := new(RequiredStatusChecksRequest)
2089 json.NewDecoder(r.Body).Decode(v)
2090
2091 testMethod(t, r, "PATCH")
2092 if !cmp.Equal(v, input) {
2093 t.Errorf("Request body = %+v, want %+v", v, input)
2094 }
2095 testHeader(t, r, "Accept", mediaTypeV3)
2096 fmt.Fprintf(w, `{
2097 "strict":true,
2098 "contexts":["continuous-integration"],
2099 "checks": [
2100 {
2101 "context": "continuous-integration",
2102 "app_id": null
2103 },
2104 {
2105 "context": "continuous-integration2",
2106 "app_id": 123
2107 },
2108 {
2109 "context": "continuous-integration3",
2110 "app_id": null
2111 }
2112 ]
2113 }`)
2114 })
2115
2116 ctx := context.Background()
2117 statusChecks, _, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input)
2118 if err != nil {
2119 t.Errorf("Repositories.UpdateRequiredStatusChecks returned error: %v", err)
2120 }
2121
2122 want := &RequiredStatusChecks{
2123 Strict: true,
2124 Contexts: []string{"continuous-integration"},
2125 Checks: []*RequiredStatusCheck{
2126 {
2127 Context: "continuous-integration",
2128 },
2129 {
2130 Context: "continuous-integration2",
2131 AppID: &appID,
2132 },
2133 {
2134 Context: "continuous-integration3",
2135 },
2136 },
2137 }
2138 if !cmp.Equal(statusChecks, want) {
2139 t.Errorf("Repositories.UpdateRequiredStatusChecks returned %+v, want %+v", statusChecks, want)
2140 }
2141 }
2142
2143 func TestRepositoriesService_RemoveRequiredStatusChecks(t *testing.T) {
2144 client, mux, _, teardown := setup()
2145 defer teardown()
2146
2147 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) {
2148 testMethod(t, r, "DELETE")
2149 testHeader(t, r, "Accept", mediaTypeV3)
2150 w.WriteHeader(http.StatusNoContent)
2151 })
2152
2153 ctx := context.Background()
2154 _, err := client.Repositories.RemoveRequiredStatusChecks(ctx, "o", "r", "b")
2155 if err != nil {
2156 t.Errorf("Repositories.RemoveRequiredStatusChecks returned error: %v", err)
2157 }
2158
2159 const methodName = "RemoveRequiredStatusChecks"
2160 testBadOptions(t, methodName, func() (err error) {
2161 _, err = client.Repositories.RemoveRequiredStatusChecks(ctx, "\n", "\n", "\n")
2162 return err
2163 })
2164
2165 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2166 return client.Repositories.RemoveRequiredStatusChecks(ctx, "o", "r", "b")
2167 })
2168 }
2169
2170 func TestRepositoriesService_ListRequiredStatusChecksContexts(t *testing.T) {
2171 client, mux, _, teardown := setup()
2172 defer teardown()
2173
2174 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) {
2175 v := new(ProtectionRequest)
2176 json.NewDecoder(r.Body).Decode(v)
2177
2178 testMethod(t, r, "GET")
2179 fmt.Fprint(w, `["x", "y", "z"]`)
2180 })
2181
2182 ctx := context.Background()
2183 contexts, _, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
2184 if err != nil {
2185 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned error: %v", err)
2186 }
2187
2188 want := []string{"x", "y", "z"}
2189 if !cmp.Equal(contexts, want) {
2190 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned %+v, want %+v", contexts, want)
2191 }
2192
2193 const methodName = "ListRequiredStatusChecksContexts"
2194 testBadOptions(t, methodName, func() (err error) {
2195 _, _, err = client.Repositories.ListRequiredStatusChecksContexts(ctx, "\n", "\n", "\n")
2196 return err
2197 })
2198
2199 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2200 got, resp, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
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_ListRequiredStatusChecksContexts_branchNotProtected(t *testing.T) {
2209 client, mux, _, teardown := setup()
2210 defer teardown()
2211
2212 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) {
2213 testMethod(t, r, "GET")
2214
2215 w.WriteHeader(http.StatusBadRequest)
2216 fmt.Fprintf(w, `{
2217 "message": %q,
2218 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
2219 }`, githubBranchNotProtected)
2220 })
2221
2222 ctx := context.Background()
2223 contexts, _, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b")
2224
2225 if contexts != nil {
2226 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned non-nil contexts data")
2227 }
2228
2229 if err != ErrBranchNotProtected {
2230 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned an invalid error: %v", err)
2231 }
2232 }
2233
2234 func TestRepositoriesService_GetPullRequestReviewEnforcement(t *testing.T) {
2235 client, mux, _, teardown := setup()
2236 defer teardown()
2237
2238 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2239 testMethod(t, r, "GET")
2240
2241 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
2242 fmt.Fprintf(w, `{
2243 "dismissal_restrictions":{
2244 "users":[{"id":1,"login":"u"}],
2245 "teams":[{"id":2,"slug":"t"}],
2246 "apps":[{"id":3,"slug":"a"}]
2247 },
2248 "dismiss_stale_reviews":true,
2249 "require_code_owner_reviews":true,
2250 "required_approving_review_count":1
2251 }`)
2252 })
2253
2254 ctx := context.Background()
2255 enforcement, _, err := client.Repositories.GetPullRequestReviewEnforcement(ctx, "o", "r", "b")
2256 if err != nil {
2257 t.Errorf("Repositories.GetPullRequestReviewEnforcement returned error: %v", err)
2258 }
2259
2260 want := &PullRequestReviewsEnforcement{
2261 DismissStaleReviews: true,
2262 DismissalRestrictions: &DismissalRestrictions{
2263 Users: []*User{
2264 {Login: String("u"), ID: Int64(1)},
2265 },
2266 Teams: []*Team{
2267 {Slug: String("t"), ID: Int64(2)},
2268 },
2269 Apps: []*App{
2270 {Slug: String("a"), ID: Int64(3)},
2271 },
2272 },
2273 RequireCodeOwnerReviews: true,
2274 RequiredApprovingReviewCount: 1,
2275 }
2276
2277 if !cmp.Equal(enforcement, want) {
2278 t.Errorf("Repositories.GetPullRequestReviewEnforcement returned %+v, want %+v", enforcement, want)
2279 }
2280
2281 const methodName = "GetPullRequestReviewEnforcement"
2282 testBadOptions(t, methodName, func() (err error) {
2283 _, _, err = client.Repositories.GetPullRequestReviewEnforcement(ctx, "\n", "\n", "\n")
2284 return err
2285 })
2286
2287 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2288 got, resp, err := client.Repositories.GetPullRequestReviewEnforcement(ctx, "o", "r", "b")
2289 if got != nil {
2290 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2291 }
2292 return resp, err
2293 })
2294 }
2295
2296 func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) {
2297 client, mux, _, teardown := setup()
2298 defer teardown()
2299
2300 input := &PullRequestReviewsEnforcementUpdate{
2301 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
2302 Users: &[]string{"u"},
2303 Teams: &[]string{"t"},
2304 Apps: &[]string{"a"},
2305 },
2306 }
2307
2308 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2309 v := new(PullRequestReviewsEnforcementUpdate)
2310 json.NewDecoder(r.Body).Decode(v)
2311
2312 testMethod(t, r, "PATCH")
2313 if !cmp.Equal(v, input) {
2314 t.Errorf("Request body = %+v, want %+v", v, input)
2315 }
2316
2317 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
2318 fmt.Fprintf(w, `{
2319 "dismissal_restrictions":{
2320 "users":[{"id":1,"login":"u"}],
2321 "teams":[{"id":2,"slug":"t"}],
2322 "apps":[{"id":3,"slug":"a"}]
2323 },
2324 "dismiss_stale_reviews":true,
2325 "require_code_owner_reviews":true,
2326 "required_approving_review_count":3
2327 }`)
2328 })
2329
2330 ctx := context.Background()
2331 enforcement, _, err := client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "o", "r", "b", input)
2332 if err != nil {
2333 t.Errorf("Repositories.UpdatePullRequestReviewEnforcement returned error: %v", err)
2334 }
2335
2336 want := &PullRequestReviewsEnforcement{
2337 DismissStaleReviews: true,
2338 DismissalRestrictions: &DismissalRestrictions{
2339 Users: []*User{
2340 {Login: String("u"), ID: Int64(1)},
2341 },
2342 Teams: []*Team{
2343 {Slug: String("t"), ID: Int64(2)},
2344 },
2345 Apps: []*App{
2346 {Slug: String("a"), ID: Int64(3)},
2347 },
2348 },
2349 RequireCodeOwnerReviews: true,
2350 RequiredApprovingReviewCount: 3,
2351 }
2352 if !cmp.Equal(enforcement, want) {
2353 t.Errorf("Repositories.UpdatePullRequestReviewEnforcement returned %+v, want %+v", enforcement, want)
2354 }
2355
2356 const methodName = "UpdatePullRequestReviewEnforcement"
2357 testBadOptions(t, methodName, func() (err error) {
2358 _, _, err = client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "\n", "\n", "\n", input)
2359 return err
2360 })
2361
2362 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2363 got, resp, err := client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "o", "r", "b", input)
2364 if got != nil {
2365 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2366 }
2367 return resp, err
2368 })
2369 }
2370
2371 func TestRepositoriesService_DisableDismissalRestrictions(t *testing.T) {
2372 client, mux, _, teardown := setup()
2373 defer teardown()
2374
2375 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2376 testMethod(t, r, "PATCH")
2377
2378 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
2379 testBody(t, r, `{"dismissal_restrictions":{}}`+"\n")
2380 fmt.Fprintf(w, `{"dismiss_stale_reviews":true,"require_code_owner_reviews":true,"required_approving_review_count":1}`)
2381 })
2382
2383 ctx := context.Background()
2384 enforcement, _, err := client.Repositories.DisableDismissalRestrictions(ctx, "o", "r", "b")
2385 if err != nil {
2386 t.Errorf("Repositories.DisableDismissalRestrictions returned error: %v", err)
2387 }
2388
2389 want := &PullRequestReviewsEnforcement{
2390 DismissStaleReviews: true,
2391 DismissalRestrictions: nil,
2392 RequireCodeOwnerReviews: true,
2393 RequiredApprovingReviewCount: 1,
2394 }
2395 if !cmp.Equal(enforcement, want) {
2396 t.Errorf("Repositories.DisableDismissalRestrictions returned %+v, want %+v", enforcement, want)
2397 }
2398
2399 const methodName = "DisableDismissalRestrictions"
2400 testBadOptions(t, methodName, func() (err error) {
2401 _, _, err = client.Repositories.DisableDismissalRestrictions(ctx, "\n", "\n", "\n")
2402 return err
2403 })
2404
2405 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2406 got, resp, err := client.Repositories.DisableDismissalRestrictions(ctx, "o", "r", "b")
2407 if got != nil {
2408 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2409 }
2410 return resp, err
2411 })
2412 }
2413
2414 func TestRepositoriesService_RemovePullRequestReviewEnforcement(t *testing.T) {
2415 client, mux, _, teardown := setup()
2416 defer teardown()
2417
2418 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) {
2419 testMethod(t, r, "DELETE")
2420 w.WriteHeader(http.StatusNoContent)
2421 })
2422
2423 ctx := context.Background()
2424 _, err := client.Repositories.RemovePullRequestReviewEnforcement(ctx, "o", "r", "b")
2425 if err != nil {
2426 t.Errorf("Repositories.RemovePullRequestReviewEnforcement returned error: %v", err)
2427 }
2428
2429 const methodName = "RemovePullRequestReviewEnforcement"
2430 testBadOptions(t, methodName, func() (err error) {
2431 _, err = client.Repositories.RemovePullRequestReviewEnforcement(ctx, "\n", "\n", "\n")
2432 return err
2433 })
2434
2435 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2436 return client.Repositories.RemovePullRequestReviewEnforcement(ctx, "o", "r", "b")
2437 })
2438 }
2439
2440 func TestRepositoriesService_GetAdminEnforcement(t *testing.T) {
2441 client, mux, _, teardown := setup()
2442 defer teardown()
2443
2444 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2445 testMethod(t, r, "GET")
2446 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true}`)
2447 })
2448
2449 ctx := context.Background()
2450 enforcement, _, err := client.Repositories.GetAdminEnforcement(ctx, "o", "r", "b")
2451 if err != nil {
2452 t.Errorf("Repositories.GetAdminEnforcement returned error: %v", err)
2453 }
2454
2455 want := &AdminEnforcement{
2456 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
2457 Enabled: true,
2458 }
2459
2460 if !cmp.Equal(enforcement, want) {
2461 t.Errorf("Repositories.GetAdminEnforcement returned %+v, want %+v", enforcement, want)
2462 }
2463
2464 const methodName = "GetAdminEnforcement"
2465 testBadOptions(t, methodName, func() (err error) {
2466 _, _, err = client.Repositories.GetAdminEnforcement(ctx, "\n", "\n", "\n")
2467 return err
2468 })
2469
2470 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2471 got, resp, err := client.Repositories.GetAdminEnforcement(ctx, "o", "r", "b")
2472 if got != nil {
2473 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2474 }
2475 return resp, err
2476 })
2477 }
2478
2479 func TestRepositoriesService_AddAdminEnforcement(t *testing.T) {
2480 client, mux, _, teardown := setup()
2481 defer teardown()
2482
2483 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2484 testMethod(t, r, "POST")
2485 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true}`)
2486 })
2487
2488 ctx := context.Background()
2489 enforcement, _, err := client.Repositories.AddAdminEnforcement(ctx, "o", "r", "b")
2490 if err != nil {
2491 t.Errorf("Repositories.AddAdminEnforcement returned error: %v", err)
2492 }
2493
2494 want := &AdminEnforcement{
2495 URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
2496 Enabled: true,
2497 }
2498 if !cmp.Equal(enforcement, want) {
2499 t.Errorf("Repositories.AddAdminEnforcement returned %+v, want %+v", enforcement, want)
2500 }
2501
2502 const methodName = "AddAdminEnforcement"
2503 testBadOptions(t, methodName, func() (err error) {
2504 _, _, err = client.Repositories.AddAdminEnforcement(ctx, "\n", "\n", "\n")
2505 return err
2506 })
2507
2508 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2509 got, resp, err := client.Repositories.AddAdminEnforcement(ctx, "o", "r", "b")
2510 if got != nil {
2511 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2512 }
2513 return resp, err
2514 })
2515 }
2516
2517 func TestRepositoriesService_RemoveAdminEnforcement(t *testing.T) {
2518 client, mux, _, teardown := setup()
2519 defer teardown()
2520
2521 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) {
2522 testMethod(t, r, "DELETE")
2523 w.WriteHeader(http.StatusNoContent)
2524 })
2525
2526 ctx := context.Background()
2527 _, err := client.Repositories.RemoveAdminEnforcement(ctx, "o", "r", "b")
2528 if err != nil {
2529 t.Errorf("Repositories.RemoveAdminEnforcement returned error: %v", err)
2530 }
2531
2532 const methodName = "RemoveAdminEnforcement"
2533 testBadOptions(t, methodName, func() (err error) {
2534 _, err = client.Repositories.RemoveAdminEnforcement(ctx, "\n", "\n", "\n")
2535 return err
2536 })
2537
2538 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2539 return client.Repositories.RemoveAdminEnforcement(ctx, "o", "r", "b")
2540 })
2541 }
2542
2543 func TestRepositoriesService_GetSignaturesProtectedBranch(t *testing.T) {
2544 client, mux, _, teardown := setup()
2545 defer teardown()
2546
2547 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2548 testMethod(t, r, "GET")
2549 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2550 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/required_signatures","enabled":false}`)
2551 })
2552
2553 ctx := context.Background()
2554 signature, _, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", "b")
2555 if err != nil {
2556 t.Errorf("Repositories.GetSignaturesProtectedBranch returned error: %v", err)
2557 }
2558
2559 want := &SignaturesProtectedBranch{
2560 URL: String("/repos/o/r/branches/b/protection/required_signatures"),
2561 Enabled: Bool(false),
2562 }
2563
2564 if !cmp.Equal(signature, want) {
2565 t.Errorf("Repositories.GetSignaturesProtectedBranch returned %+v, want %+v", signature, want)
2566 }
2567
2568 const methodName = "GetSignaturesProtectedBranch"
2569 testBadOptions(t, methodName, func() (err error) {
2570 _, _, err = client.Repositories.GetSignaturesProtectedBranch(ctx, "\n", "\n", "\n")
2571 return err
2572 })
2573
2574 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2575 got, resp, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", "b")
2576 if got != nil {
2577 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2578 }
2579 return resp, err
2580 })
2581 }
2582
2583 func TestRepositoriesService_RequireSignaturesOnProtectedBranch(t *testing.T) {
2584 client, mux, _, teardown := setup()
2585 defer teardown()
2586
2587 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2588 testMethod(t, r, "POST")
2589 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2590 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/required_signatures","enabled":true}`)
2591 })
2592
2593 ctx := context.Background()
2594 signature, _, err := client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2595 if err != nil {
2596 t.Errorf("Repositories.RequireSignaturesOnProtectedBranch returned error: %v", err)
2597 }
2598
2599 want := &SignaturesProtectedBranch{
2600 URL: String("/repos/o/r/branches/b/protection/required_signatures"),
2601 Enabled: Bool(true),
2602 }
2603
2604 if !cmp.Equal(signature, want) {
2605 t.Errorf("Repositories.RequireSignaturesOnProtectedBranch returned %+v, want %+v", signature, want)
2606 }
2607
2608 const methodName = "RequireSignaturesOnProtectedBranch"
2609 testBadOptions(t, methodName, func() (err error) {
2610 _, _, err = client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "\n", "\n", "\n")
2611 return err
2612 })
2613
2614 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2615 got, resp, err := client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2616 if got != nil {
2617 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2618 }
2619 return resp, err
2620 })
2621 }
2622
2623 func TestRepositoriesService_OptionalSignaturesOnProtectedBranch(t *testing.T) {
2624 client, mux, _, teardown := setup()
2625 defer teardown()
2626
2627 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) {
2628 testMethod(t, r, "DELETE")
2629 testHeader(t, r, "Accept", mediaTypeSignaturePreview)
2630 w.WriteHeader(http.StatusNoContent)
2631 })
2632
2633 ctx := context.Background()
2634 _, err := client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2635 if err != nil {
2636 t.Errorf("Repositories.OptionalSignaturesOnProtectedBranch returned error: %v", err)
2637 }
2638
2639 const methodName = "OptionalSignaturesOnProtectedBranch"
2640 testBadOptions(t, methodName, func() (err error) {
2641 _, err = client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "\n", "\n", "\n")
2642 return err
2643 })
2644
2645 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2646 return client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "o", "r", "b")
2647 })
2648 }
2649
2650 func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctions(t *testing.T) {
2651 req := PullRequestReviewsEnforcementRequest{}
2652
2653 got, err := json.Marshal(req)
2654 if err != nil {
2655 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2656 }
2657
2658 want := `{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2659 if want != string(got) {
2660 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2661 }
2662
2663 req = PullRequestReviewsEnforcementRequest{
2664 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{},
2665 }
2666
2667 got, err = json.Marshal(req)
2668 if err != nil {
2669 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2670 }
2671
2672 want = `{"dismissal_restrictions":{},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
2673 if want != string(got) {
2674 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2675 }
2676
2677 req = PullRequestReviewsEnforcementRequest{
2678 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
2679 Users: &[]string{},
2680 Teams: &[]string{},
2681 Apps: &[]string{},
2682 },
2683 RequireLastPushApproval: Bool(true),
2684 }
2685
2686 got, err = json.Marshal(req)
2687 if err != nil {
2688 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
2689 }
2690
2691 want = `{"dismissal_restrictions":{"users":[],"teams":[],"apps":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0,"require_last_push_approval":true}`
2692 if want != string(got) {
2693 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
2694 }
2695 }
2696
2697 func TestRepositoriesService_ListAllTopics(t *testing.T) {
2698 client, mux, _, teardown := setup()
2699 defer teardown()
2700
2701 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2702 testMethod(t, r, "GET")
2703 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2704 fmt.Fprint(w, `{"names":["go", "go-github", "github"]}`)
2705 })
2706
2707 ctx := context.Background()
2708 got, _, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2709 if err != nil {
2710 t.Fatalf("Repositories.ListAllTopics returned error: %v", err)
2711 }
2712
2713 want := []string{"go", "go-github", "github"}
2714 if !cmp.Equal(got, want) {
2715 t.Errorf("Repositories.ListAllTopics returned %+v, want %+v", got, want)
2716 }
2717
2718 const methodName = "ListAllTopics"
2719 testBadOptions(t, methodName, func() (err error) {
2720 _, _, err = client.Repositories.ListAllTopics(ctx, "\n", "\n")
2721 return err
2722 })
2723
2724 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2725 got, resp, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2726 if got != nil {
2727 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2728 }
2729 return resp, err
2730 })
2731 }
2732
2733 func TestRepositoriesService_ListAllTopics_emptyTopics(t *testing.T) {
2734 client, mux, _, teardown := setup()
2735 defer teardown()
2736
2737 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2738 testMethod(t, r, "GET")
2739 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2740 fmt.Fprint(w, `{"names":[]}`)
2741 })
2742
2743 ctx := context.Background()
2744 got, _, err := client.Repositories.ListAllTopics(ctx, "o", "r")
2745 if err != nil {
2746 t.Fatalf("Repositories.ListAllTopics returned error: %v", err)
2747 }
2748
2749 want := []string{}
2750 if !cmp.Equal(got, want) {
2751 t.Errorf("Repositories.ListAllTopics returned %+v, want %+v", got, want)
2752 }
2753 }
2754
2755 func TestRepositoriesService_ReplaceAllTopics(t *testing.T) {
2756 client, mux, _, teardown := setup()
2757 defer teardown()
2758
2759 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2760 testMethod(t, r, "PUT")
2761 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2762 fmt.Fprint(w, `{"names":["go", "go-github", "github"]}`)
2763 })
2764
2765 ctx := context.Background()
2766 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{"go", "go-github", "github"})
2767 if err != nil {
2768 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2769 }
2770
2771 want := []string{"go", "go-github", "github"}
2772 if !cmp.Equal(got, want) {
2773 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2774 }
2775
2776 const methodName = "ReplaceAllTopics"
2777 testBadOptions(t, methodName, func() (err error) {
2778 _, _, err = client.Repositories.ReplaceAllTopics(ctx, "\n", "\n", []string{"\n", "\n", "\n"})
2779 return err
2780 })
2781
2782 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2783 got, resp, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{"go", "go-github", "github"})
2784 if got != nil {
2785 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2786 }
2787 return resp, err
2788 })
2789 }
2790
2791 func TestRepositoriesService_ReplaceAllTopics_nilSlice(t *testing.T) {
2792 client, mux, _, teardown := setup()
2793 defer teardown()
2794
2795 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2796 testMethod(t, r, "PUT")
2797 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2798 testBody(t, r, `{"names":[]}`+"\n")
2799 fmt.Fprint(w, `{"names":[]}`)
2800 })
2801
2802 ctx := context.Background()
2803 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", nil)
2804 if err != nil {
2805 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2806 }
2807
2808 want := []string{}
2809 if !cmp.Equal(got, want) {
2810 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2811 }
2812 }
2813
2814 func TestRepositoriesService_ReplaceAllTopics_emptySlice(t *testing.T) {
2815 client, mux, _, teardown := setup()
2816 defer teardown()
2817
2818 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) {
2819 testMethod(t, r, "PUT")
2820 testHeader(t, r, "Accept", mediaTypeTopicsPreview)
2821 testBody(t, r, `{"names":[]}`+"\n")
2822 fmt.Fprint(w, `{"names":[]}`)
2823 })
2824
2825 ctx := context.Background()
2826 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{})
2827 if err != nil {
2828 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err)
2829 }
2830
2831 want := []string{}
2832 if !cmp.Equal(got, want) {
2833 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want)
2834 }
2835 }
2836
2837 func TestRepositoriesService_ListAppRestrictions(t *testing.T) {
2838 client, mux, _, teardown := setup()
2839 defer teardown()
2840
2841 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2842 testMethod(t, r, "GET")
2843 })
2844
2845 ctx := context.Background()
2846 _, _, err := client.Repositories.ListAppRestrictions(ctx, "o", "r", "b")
2847 if err != nil {
2848 t.Errorf("Repositories.ListAppRestrictions returned error: %v", err)
2849 }
2850
2851 const methodName = "ListAppRestrictions"
2852 testBadOptions(t, methodName, func() (err error) {
2853 _, _, err = client.Repositories.ListAppRestrictions(ctx, "\n", "\n", "\n")
2854 return err
2855 })
2856
2857 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2858 got, resp, err := client.Repositories.ListAppRestrictions(ctx, "o", "r", "b")
2859 if got != nil {
2860 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2861 }
2862 return resp, err
2863 })
2864 }
2865
2866 func TestRepositoriesService_ReplaceAppRestrictions(t *testing.T) {
2867 client, mux, _, teardown := setup()
2868 defer teardown()
2869
2870 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2871 testMethod(t, r, "PUT")
2872 fmt.Fprint(w, `[{
2873 "name": "octocat"
2874 }]`)
2875 })
2876 input := []string{"octocat"}
2877 ctx := context.Background()
2878 got, _, err := client.Repositories.ReplaceAppRestrictions(ctx, "o", "r", "b", input)
2879 if err != nil {
2880 t.Errorf("Repositories.ReplaceAppRestrictions returned error: %v", err)
2881 }
2882 want := []*App{
2883 {Name: String("octocat")},
2884 }
2885 if !cmp.Equal(got, want) {
2886 t.Errorf("Repositories.ReplaceAppRestrictions returned %+v, want %+v", got, want)
2887 }
2888
2889 const methodName = "ReplaceAppRestrictions"
2890 testBadOptions(t, methodName, func() (err error) {
2891 _, _, err = client.Repositories.ReplaceAppRestrictions(ctx, "\n", "\n", "\n", input)
2892 return err
2893 })
2894
2895 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2896 got, resp, err := client.Repositories.ReplaceAppRestrictions(ctx, "o", "r", "b", input)
2897 if got != nil {
2898 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2899 }
2900 return resp, err
2901 })
2902 }
2903
2904 func TestRepositoriesService_AddAppRestrictions(t *testing.T) {
2905 client, mux, _, teardown := setup()
2906 defer teardown()
2907
2908 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2909 testMethod(t, r, "POST")
2910 fmt.Fprint(w, `[{
2911 "name": "octocat"
2912 }]`)
2913 })
2914 input := []string{"octocat"}
2915 ctx := context.Background()
2916 got, _, err := client.Repositories.AddAppRestrictions(ctx, "o", "r", "b", input)
2917 if err != nil {
2918 t.Errorf("Repositories.AddAppRestrictions returned error: %v", err)
2919 }
2920 want := []*App{
2921 {Name: String("octocat")},
2922 }
2923 if !cmp.Equal(got, want) {
2924 t.Errorf("Repositories.AddAppRestrictions returned %+v, want %+v", got, want)
2925 }
2926
2927 const methodName = "AddAppRestrictions"
2928 testBadOptions(t, methodName, func() (err error) {
2929 _, _, err = client.Repositories.AddAppRestrictions(ctx, "\n", "\n", "\n", input)
2930 return err
2931 })
2932
2933 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2934 got, resp, err := client.Repositories.AddAppRestrictions(ctx, "o", "r", "b", input)
2935 if got != nil {
2936 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2937 }
2938 return resp, err
2939 })
2940 }
2941
2942 func TestRepositoriesService_RemoveAppRestrictions(t *testing.T) {
2943 client, mux, _, teardown := setup()
2944 defer teardown()
2945
2946 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) {
2947 testMethod(t, r, "DELETE")
2948 fmt.Fprint(w, `[]`)
2949 })
2950 input := []string{"octocat"}
2951 ctx := context.Background()
2952 got, _, err := client.Repositories.RemoveAppRestrictions(ctx, "o", "r", "b", input)
2953 if err != nil {
2954 t.Errorf("Repositories.RemoveAppRestrictions returned error: %v", err)
2955 }
2956 want := []*App{}
2957 if !cmp.Equal(got, want) {
2958 t.Errorf("Repositories.RemoveAppRestrictions returned %+v, want %+v", got, want)
2959 }
2960
2961 const methodName = "RemoveAppRestrictions"
2962 testBadOptions(t, methodName, func() (err error) {
2963 _, _, err = client.Repositories.RemoveAppRestrictions(ctx, "\n", "\n", "\n", input)
2964 return err
2965 })
2966
2967 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2968 got, resp, err := client.Repositories.RemoveAppRestrictions(ctx, "o", "r", "b", input)
2969 if got != nil {
2970 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
2971 }
2972 return resp, err
2973 })
2974 }
2975
2976 func TestRepositoriesService_ListTeamRestrictions(t *testing.T) {
2977 client, mux, _, teardown := setup()
2978 defer teardown()
2979
2980 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/teams", func(w http.ResponseWriter, r *http.Request) {
2981 testMethod(t, r, "GET")
2982 })
2983
2984 ctx := context.Background()
2985 _, _, err := client.Repositories.ListTeamRestrictions(ctx, "o", "r", "b")
2986 if err != nil {
2987 t.Errorf("Repositories.ListTeamRestrictions returned error: %v", err)
2988 }
2989
2990 const methodName = "ListTeamRestrictions"
2991 testBadOptions(t, methodName, func() (err error) {
2992 _, _, err = client.Repositories.ListTeamRestrictions(ctx, "\n", "\n", "\n")
2993 return err
2994 })
2995
2996 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
2997 got, resp, err := client.Repositories.ListTeamRestrictions(ctx, "o", "r", "b")
2998 if got != nil {
2999 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3000 }
3001 return resp, err
3002 })
3003 }
3004
3005 func TestRepositoriesService_ReplaceTeamRestrictions(t *testing.T) {
3006 client, mux, _, teardown := setup()
3007 defer teardown()
3008
3009 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/teams", func(w http.ResponseWriter, r *http.Request) {
3010 testMethod(t, r, "PUT")
3011 fmt.Fprint(w, `[{
3012 "name": "octocat"
3013 }]`)
3014 })
3015 input := []string{"octocat"}
3016 ctx := context.Background()
3017 got, _, err := client.Repositories.ReplaceTeamRestrictions(ctx, "o", "r", "b", input)
3018 if err != nil {
3019 t.Errorf("Repositories.ReplaceTeamRestrictions returned error: %v", err)
3020 }
3021 want := []*Team{
3022 {Name: String("octocat")},
3023 }
3024 if !cmp.Equal(got, want) {
3025 t.Errorf("Repositories.ReplaceTeamRestrictions returned %+v, want %+v", got, want)
3026 }
3027
3028 const methodName = "ReplaceTeamRestrictions"
3029 testBadOptions(t, methodName, func() (err error) {
3030 _, _, err = client.Repositories.ReplaceTeamRestrictions(ctx, "\n", "\n", "\n", input)
3031 return err
3032 })
3033
3034 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3035 got, resp, err := client.Repositories.ReplaceTeamRestrictions(ctx, "o", "r", "b", input)
3036 if got != nil {
3037 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3038 }
3039 return resp, err
3040 })
3041 }
3042
3043 func TestRepositoriesService_AddTeamRestrictions(t *testing.T) {
3044 client, mux, _, teardown := setup()
3045 defer teardown()
3046
3047 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/teams", func(w http.ResponseWriter, r *http.Request) {
3048 testMethod(t, r, "POST")
3049 fmt.Fprint(w, `[{
3050 "name": "octocat"
3051 }]`)
3052 })
3053 input := []string{"octocat"}
3054 ctx := context.Background()
3055 got, _, err := client.Repositories.AddTeamRestrictions(ctx, "o", "r", "b", input)
3056 if err != nil {
3057 t.Errorf("Repositories.AddTeamRestrictions returned error: %v", err)
3058 }
3059 want := []*Team{
3060 {Name: String("octocat")},
3061 }
3062 if !cmp.Equal(got, want) {
3063 t.Errorf("Repositories.AddTeamRestrictions returned %+v, want %+v", got, want)
3064 }
3065
3066 const methodName = "AddTeamRestrictions"
3067 testBadOptions(t, methodName, func() (err error) {
3068 _, _, err = client.Repositories.AddTeamRestrictions(ctx, "\n", "\n", "\n", input)
3069 return err
3070 })
3071
3072 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3073 got, resp, err := client.Repositories.AddTeamRestrictions(ctx, "o", "r", "b", input)
3074 if got != nil {
3075 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3076 }
3077 return resp, err
3078 })
3079 }
3080
3081 func TestRepositoriesService_RemoveTeamRestrictions(t *testing.T) {
3082 client, mux, _, teardown := setup()
3083 defer teardown()
3084
3085 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/teams", func(w http.ResponseWriter, r *http.Request) {
3086 testMethod(t, r, "DELETE")
3087 fmt.Fprint(w, `[]`)
3088 })
3089 input := []string{"octocat"}
3090 ctx := context.Background()
3091 got, _, err := client.Repositories.RemoveTeamRestrictions(ctx, "o", "r", "b", input)
3092 if err != nil {
3093 t.Errorf("Repositories.RemoveTeamRestrictions returned error: %v", err)
3094 }
3095 want := []*Team{}
3096 if !cmp.Equal(got, want) {
3097 t.Errorf("Repositories.RemoveTeamRestrictions returned %+v, want %+v", got, want)
3098 }
3099
3100 const methodName = "RemoveTeamRestrictions"
3101 testBadOptions(t, methodName, func() (err error) {
3102 _, _, err = client.Repositories.RemoveTeamRestrictions(ctx, "\n", "\n", "\n", input)
3103 return err
3104 })
3105
3106 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3107 got, resp, err := client.Repositories.RemoveTeamRestrictions(ctx, "o", "r", "b", input)
3108 if got != nil {
3109 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3110 }
3111 return resp, err
3112 })
3113 }
3114
3115 func TestRepositoriesService_ListUserRestrictions(t *testing.T) {
3116 client, mux, _, teardown := setup()
3117 defer teardown()
3118
3119 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/users", func(w http.ResponseWriter, r *http.Request) {
3120 testMethod(t, r, "GET")
3121 })
3122
3123 ctx := context.Background()
3124 _, _, err := client.Repositories.ListUserRestrictions(ctx, "o", "r", "b")
3125 if err != nil {
3126 t.Errorf("Repositories.ListUserRestrictions returned error: %v", err)
3127 }
3128
3129 const methodName = "ListUserRestrictions"
3130 testBadOptions(t, methodName, func() (err error) {
3131 _, _, err = client.Repositories.ListUserRestrictions(ctx, "\n", "\n", "\n")
3132 return err
3133 })
3134
3135 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3136 got, resp, err := client.Repositories.ListUserRestrictions(ctx, "o", "r", "b")
3137 if got != nil {
3138 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3139 }
3140 return resp, err
3141 })
3142 }
3143
3144 func TestRepositoriesService_ReplaceUserRestrictions(t *testing.T) {
3145 client, mux, _, teardown := setup()
3146 defer teardown()
3147
3148 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/users", func(w http.ResponseWriter, r *http.Request) {
3149 testMethod(t, r, "PUT")
3150 fmt.Fprint(w, `[{
3151 "name": "octocat"
3152 }]`)
3153 })
3154 input := []string{"octocat"}
3155 ctx := context.Background()
3156 got, _, err := client.Repositories.ReplaceUserRestrictions(ctx, "o", "r", "b", input)
3157 if err != nil {
3158 t.Errorf("Repositories.ReplaceUserRestrictions returned error: %v", err)
3159 }
3160 want := []*User{
3161 {Name: String("octocat")},
3162 }
3163 if !cmp.Equal(got, want) {
3164 t.Errorf("Repositories.ReplaceUserRestrictions returned %+v, want %+v", got, want)
3165 }
3166
3167 const methodName = "ReplaceUserRestrictions"
3168 testBadOptions(t, methodName, func() (err error) {
3169 _, _, err = client.Repositories.ReplaceUserRestrictions(ctx, "\n", "\n", "\n", input)
3170 return err
3171 })
3172
3173 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3174 got, resp, err := client.Repositories.ReplaceUserRestrictions(ctx, "o", "r", "b", input)
3175 if got != nil {
3176 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3177 }
3178 return resp, err
3179 })
3180 }
3181
3182 func TestRepositoriesService_AddUserRestrictions(t *testing.T) {
3183 client, mux, _, teardown := setup()
3184 defer teardown()
3185
3186 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/users", func(w http.ResponseWriter, r *http.Request) {
3187 testMethod(t, r, "POST")
3188 fmt.Fprint(w, `[{
3189 "name": "octocat"
3190 }]`)
3191 })
3192 input := []string{"octocat"}
3193 ctx := context.Background()
3194 got, _, err := client.Repositories.AddUserRestrictions(ctx, "o", "r", "b", input)
3195 if err != nil {
3196 t.Errorf("Repositories.AddUserRestrictions returned error: %v", err)
3197 }
3198 want := []*User{
3199 {Name: String("octocat")},
3200 }
3201 if !cmp.Equal(got, want) {
3202 t.Errorf("Repositories.AddUserRestrictions returned %+v, want %+v", got, want)
3203 }
3204
3205 const methodName = "AddUserRestrictions"
3206 testBadOptions(t, methodName, func() (err error) {
3207 _, _, err = client.Repositories.AddUserRestrictions(ctx, "\n", "\n", "\n", input)
3208 return err
3209 })
3210
3211 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3212 got, resp, err := client.Repositories.AddUserRestrictions(ctx, "o", "r", "b", input)
3213 if got != nil {
3214 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3215 }
3216 return resp, err
3217 })
3218 }
3219
3220 func TestRepositoriesService_RemoveUserRestrictions(t *testing.T) {
3221 client, mux, _, teardown := setup()
3222 defer teardown()
3223
3224 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/users", func(w http.ResponseWriter, r *http.Request) {
3225 testMethod(t, r, "DELETE")
3226 fmt.Fprint(w, `[]`)
3227 })
3228 input := []string{"octocat"}
3229 ctx := context.Background()
3230 got, _, err := client.Repositories.RemoveUserRestrictions(ctx, "o", "r", "b", input)
3231 if err != nil {
3232 t.Errorf("Repositories.RemoveUserRestrictions returned error: %v", err)
3233 }
3234 want := []*User{}
3235 if !cmp.Equal(got, want) {
3236 t.Errorf("Repositories.RemoveUserRestrictions returned %+v, want %+v", got, want)
3237 }
3238
3239 const methodName = "RemoveUserRestrictions"
3240 testBadOptions(t, methodName, func() (err error) {
3241 _, _, err = client.Repositories.RemoveUserRestrictions(ctx, "\n", "\n", "\n", input)
3242 return err
3243 })
3244
3245 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3246 got, resp, err := client.Repositories.RemoveUserRestrictions(ctx, "o", "r", "b", input)
3247 if got != nil {
3248 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3249 }
3250 return resp, err
3251 })
3252 }
3253
3254 func TestRepositoriesService_Transfer(t *testing.T) {
3255 client, mux, _, teardown := setup()
3256 defer teardown()
3257
3258 input := TransferRequest{NewOwner: "a", NewName: String("b"), TeamID: []int64{123}}
3259
3260 mux.HandleFunc("/repos/o/r/transfer", func(w http.ResponseWriter, r *http.Request) {
3261 var v TransferRequest
3262 json.NewDecoder(r.Body).Decode(&v)
3263
3264 testMethod(t, r, "POST")
3265 if !cmp.Equal(v, input) {
3266 t.Errorf("Request body = %+v, want %+v", v, input)
3267 }
3268
3269 fmt.Fprint(w, `{"owner":{"login":"a"}}`)
3270 })
3271
3272 ctx := context.Background()
3273 got, _, err := client.Repositories.Transfer(ctx, "o", "r", input)
3274 if err != nil {
3275 t.Errorf("Repositories.Transfer returned error: %v", err)
3276 }
3277
3278 want := &Repository{Owner: &User{Login: String("a")}}
3279 if !cmp.Equal(got, want) {
3280 t.Errorf("Repositories.Transfer returned %+v, want %+v", got, want)
3281 }
3282
3283 const methodName = "Transfer"
3284 testBadOptions(t, methodName, func() (err error) {
3285 _, _, err = client.Repositories.Transfer(ctx, "\n", "\n", input)
3286 return err
3287 })
3288
3289 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3290 got, resp, err := client.Repositories.Transfer(ctx, "o", "r", input)
3291 if got != nil {
3292 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3293 }
3294 return resp, err
3295 })
3296 }
3297
3298 func TestRepositoriesService_Dispatch(t *testing.T) {
3299 client, mux, _, teardown := setup()
3300 defer teardown()
3301
3302 var input DispatchRequestOptions
3303
3304 mux.HandleFunc("/repos/o/r/dispatches", func(w http.ResponseWriter, r *http.Request) {
3305 var v DispatchRequestOptions
3306 json.NewDecoder(r.Body).Decode(&v)
3307
3308 testMethod(t, r, "POST")
3309 if !cmp.Equal(v, input) {
3310 t.Errorf("Request body = %+v, want %+v", v, input)
3311 }
3312
3313 fmt.Fprint(w, `{"owner":{"login":"a"}}`)
3314 })
3315
3316 ctx := context.Background()
3317
3318 testCases := []interface{}{
3319 nil,
3320 struct {
3321 Foo string
3322 }{
3323 Foo: "test",
3324 },
3325 struct {
3326 Bar int
3327 }{
3328 Bar: 42,
3329 },
3330 struct {
3331 Foo string
3332 Bar int
3333 Baz bool
3334 }{
3335 Foo: "test",
3336 Bar: 42,
3337 Baz: false,
3338 },
3339 }
3340
3341 for _, tc := range testCases {
3342 if tc == nil {
3343 input = DispatchRequestOptions{EventType: "go"}
3344 } else {
3345 bytes, _ := json.Marshal(tc)
3346 payload := json.RawMessage(bytes)
3347 input = DispatchRequestOptions{EventType: "go", ClientPayload: &payload}
3348 }
3349
3350 got, _, err := client.Repositories.Dispatch(ctx, "o", "r", input)
3351 if err != nil {
3352 t.Errorf("Repositories.Dispatch returned error: %v", err)
3353 }
3354
3355 want := &Repository{Owner: &User{Login: String("a")}}
3356 if !cmp.Equal(got, want) {
3357 t.Errorf("Repositories.Dispatch returned %+v, want %+v", got, want)
3358 }
3359 }
3360
3361 const methodName = "Dispatch"
3362 testBadOptions(t, methodName, func() (err error) {
3363 _, _, err = client.Repositories.Dispatch(ctx, "\n", "\n", input)
3364 return err
3365 })
3366
3367 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3368 got, resp, err := client.Repositories.Dispatch(ctx, "o", "r", input)
3369 if got != nil {
3370 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
3371 }
3372 return resp, err
3373 })
3374 }
3375
3376 func TestAdvancedSecurity_Marshal(t *testing.T) {
3377 testJSONMarshal(t, &AdvancedSecurity{}, "{}")
3378
3379 u := &AdvancedSecurity{
3380 Status: String("status"),
3381 }
3382
3383 want := `{
3384 "status": "status"
3385 }`
3386
3387 testJSONMarshal(t, u, want)
3388 }
3389
3390 func TestAuthorizedActorsOnly_Marshal(t *testing.T) {
3391 testJSONMarshal(t, &AuthorizedActorsOnly{}, "{}")
3392
3393 u := &AuthorizedActorsOnly{
3394 From: Bool(true),
3395 }
3396
3397 want := `{
3398 "from" : true
3399 }`
3400
3401 testJSONMarshal(t, u, want)
3402 }
3403
3404 func TestDispatchRequestOptions_Marshal(t *testing.T) {
3405 testJSONMarshal(t, &DispatchRequestOptions{}, "{}")
3406
3407 cp := json.RawMessage(`{"testKey":"testValue"}`)
3408 u := &DispatchRequestOptions{
3409 EventType: "test_event_type",
3410 ClientPayload: &cp,
3411 }
3412
3413 want := `{
3414 "event_type": "test_event_type",
3415 "client_payload": {
3416 "testKey": "testValue"
3417 }
3418 }`
3419
3420 testJSONMarshal(t, u, want)
3421 }
3422
3423 func TestTransferRequest_Marshal(t *testing.T) {
3424 testJSONMarshal(t, &TransferRequest{}, "{}")
3425
3426 u := &TransferRequest{
3427 NewOwner: "testOwner",
3428 NewName: String("testName"),
3429 TeamID: []int64{1, 2},
3430 }
3431
3432 want := `{
3433 "new_owner": "testOwner",
3434 "new_name": "testName",
3435 "team_ids": [1,2]
3436 }`
3437
3438 testJSONMarshal(t, u, want)
3439 }
3440
3441 func TestSignaturesProtectedBranch_Marshal(t *testing.T) {
3442 testJSONMarshal(t, &SignaturesProtectedBranch{}, "{}")
3443
3444 u := &SignaturesProtectedBranch{
3445 URL: String("https://www.testURL.in"),
3446 Enabled: Bool(false),
3447 }
3448
3449 want := `{
3450 "url": "https://www.testURL.in",
3451 "enabled": false
3452 }`
3453
3454 testJSONMarshal(t, u, want)
3455
3456 u2 := &SignaturesProtectedBranch{
3457 URL: String("testURL"),
3458 Enabled: Bool(true),
3459 }
3460
3461 want2 := `{
3462 "url": "testURL",
3463 "enabled": true
3464 }`
3465
3466 testJSONMarshal(t, u2, want2)
3467 }
3468
3469 func TestDismissalRestrictionsRequest_Marshal(t *testing.T) {
3470 testJSONMarshal(t, &DismissalRestrictionsRequest{}, "{}")
3471
3472 u := &DismissalRestrictionsRequest{
3473 Users: &[]string{"user1", "user2"},
3474 Teams: &[]string{"team1", "team2"},
3475 Apps: &[]string{"app1", "app2"},
3476 }
3477
3478 want := `{
3479 "users": ["user1","user2"],
3480 "teams": ["team1","team2"],
3481 "apps": ["app1","app2"]
3482 }`
3483
3484 testJSONMarshal(t, u, want)
3485 }
3486
3487 func TestAdminEnforcement_Marshal(t *testing.T) {
3488 testJSONMarshal(t, &AdminEnforcement{}, "{}")
3489
3490 u := &AdminEnforcement{
3491 URL: String("https://www.test-url.in"),
3492 Enabled: false,
3493 }
3494
3495 want := `{
3496 "url": "https://www.test-url.in",
3497 "enabled": false
3498 }`
3499
3500 testJSONMarshal(t, u, want)
3501 }
3502
3503 func TestPullRequestReviewsEnforcementUpdate_Marshal(t *testing.T) {
3504 testJSONMarshal(t, &PullRequestReviewsEnforcementUpdate{}, "{}")
3505
3506 u := &PullRequestReviewsEnforcementUpdate{
3507 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
3508 Users: []string{"user1", "user2"},
3509 Teams: []string{"team1", "team2"},
3510 Apps: []string{"app1", "app2"},
3511 },
3512 DismissStaleReviews: Bool(false),
3513 RequireCodeOwnerReviews: Bool(true),
3514 RequiredApprovingReviewCount: 2,
3515 }
3516
3517 want := `{
3518 "bypass_pull_request_allowances": {
3519 "users": ["user1","user2"],
3520 "teams": ["team1","team2"],
3521 "apps": ["app1","app2"]
3522 },
3523 "dismiss_stale_reviews": false,
3524 "require_code_owner_reviews": true,
3525 "required_approving_review_count": 2
3526 }`
3527
3528 testJSONMarshal(t, u, want)
3529 }
3530
3531 func TestRequiredStatusCheck_Marshal(t *testing.T) {
3532 testJSONMarshal(t, &RequiredStatusCheck{}, "{}")
3533
3534 u := &RequiredStatusCheck{
3535 Context: "ctx",
3536 AppID: Int64(1),
3537 }
3538
3539 want := `{
3540 "context": "ctx",
3541 "app_id": 1
3542 }`
3543
3544 testJSONMarshal(t, u, want)
3545 }
3546
3547 func TestRepositoryTag_Marshal(t *testing.T) {
3548 testJSONMarshal(t, &RepositoryTag{}, "{}")
3549
3550 u := &RepositoryTag{
3551 Name: String("v0.1"),
3552 Commit: &Commit{
3553 SHA: String("sha"),
3554 URL: String("url"),
3555 },
3556 ZipballURL: String("zball"),
3557 TarballURL: String("tball"),
3558 }
3559
3560 want := `{
3561 "name": "v0.1",
3562 "commit": {
3563 "sha": "sha",
3564 "url": "url"
3565 },
3566 "zipball_url": "zball",
3567 "tarball_url": "tball"
3568 }`
3569
3570 testJSONMarshal(t, u, want)
3571 }
3572
3573 func TestRepositoriesService_EnablePrivateReporting(t *testing.T) {
3574 client, mux, _, teardown := setup()
3575 defer teardown()
3576
3577 mux.HandleFunc("/repos/owner/repo/private-vulnerability-reporting", func(w http.ResponseWriter, r *http.Request) {
3578 testMethod(t, r, "PUT")
3579 w.WriteHeader(http.StatusNoContent)
3580 })
3581
3582 ctx := context.Background()
3583 _, err := client.Repositories.EnablePrivateReporting(ctx, "owner", "repo")
3584 if err != nil {
3585 t.Errorf("Repositories.EnablePrivateReporting returned error: %v", err)
3586 }
3587
3588 const methodName = "EnablePrivateReporting"
3589 testBadOptions(t, methodName, func() (err error) {
3590 _, err = client.Repositories.EnablePrivateReporting(ctx, "\n", "\n")
3591 return err
3592 })
3593
3594 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3595 return client.Repositories.EnablePrivateReporting(ctx, "owner", "repo")
3596 })
3597 }
3598
3599 func TestRepositoriesService_DisablePrivateReporting(t *testing.T) {
3600 client, mux, _, teardown := setup()
3601 defer teardown()
3602
3603 mux.HandleFunc("/repos/owner/repo/private-vulnerability-reporting", func(w http.ResponseWriter, r *http.Request) {
3604 testMethod(t, r, "DELETE")
3605 w.WriteHeader(http.StatusNoContent)
3606 })
3607
3608 ctx := context.Background()
3609 _, err := client.Repositories.DisablePrivateReporting(ctx, "owner", "repo")
3610 if err != nil {
3611 t.Errorf("Repositories.DisablePrivateReporting returned error: %v", err)
3612 }
3613
3614 const methodName = "DisablePrivateReporting"
3615 testBadOptions(t, methodName, func() (err error) {
3616 _, err = client.Repositories.DisablePrivateReporting(ctx, "\n", "\n")
3617 return err
3618 })
3619
3620 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
3621 return client.Repositories.DisablePrivateReporting(ctx, "owner", "repo")
3622 })
3623 }
3624
View as plain text