1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "strings"
13 "time"
14 )
15
16
17
18
19
20 type TeamsService service
21
22
23
24 type Team struct {
25 ID *int64 `json:"id,omitempty"`
26 NodeID *string `json:"node_id,omitempty"`
27 Name *string `json:"name,omitempty"`
28 Description *string `json:"description,omitempty"`
29 URL *string `json:"url,omitempty"`
30 Slug *string `json:"slug,omitempty"`
31
32
33 Permission *string `json:"permission,omitempty"`
34
35
36
37
38
39
40 Privacy *string `json:"privacy,omitempty"`
41
42 MembersCount *int `json:"members_count,omitempty"`
43 ReposCount *int `json:"repos_count,omitempty"`
44 Organization *Organization `json:"organization,omitempty"`
45 MembersURL *string `json:"members_url,omitempty"`
46 RepositoriesURL *string `json:"repositories_url,omitempty"`
47 Parent *Team `json:"parent,omitempty"`
48
49
50
51 LDAPDN *string `json:"ldap_dn,omitempty"`
52 }
53
54 func (t Team) String() string {
55 return Stringify(t)
56 }
57
58
59 type Invitation struct {
60 ID *int64 `json:"id,omitempty"`
61 NodeID *string `json:"node_id,omitempty"`
62 Login *string `json:"login,omitempty"`
63 Email *string `json:"email,omitempty"`
64
65 Role *string `json:"role,omitempty"`
66 CreatedAt *time.Time `json:"created_at,omitempty"`
67 Inviter *User `json:"inviter,omitempty"`
68 TeamCount *int `json:"team_count,omitempty"`
69 InvitationTeamURL *string `json:"invitation_team_url,omitempty"`
70 }
71
72 func (i Invitation) String() string {
73 return Stringify(i)
74 }
75
76
77
78
79 func (s *TeamsService) ListTeams(ctx context.Context, org string, opts *ListOptions) ([]*Team, *Response, error) {
80 u := fmt.Sprintf("orgs/%v/teams", org)
81 u, err := addOptions(u, opts)
82 if err != nil {
83 return nil, nil, err
84 }
85
86 req, err := s.client.NewRequest("GET", u, nil)
87 if err != nil {
88 return nil, nil, err
89 }
90
91 var teams []*Team
92 resp, err := s.client.Do(ctx, req, &teams)
93 if err != nil {
94 return nil, resp, err
95 }
96
97 return teams, resp, nil
98 }
99
100
101
102
103 func (s *TeamsService) GetTeamByID(ctx context.Context, orgID, teamID int64) (*Team, *Response, error) {
104 u := fmt.Sprintf("organizations/%v/team/%v", orgID, teamID)
105 req, err := s.client.NewRequest("GET", u, nil)
106 if err != nil {
107 return nil, nil, err
108 }
109
110 t := new(Team)
111 resp, err := s.client.Do(ctx, req, t)
112 if err != nil {
113 return nil, resp, err
114 }
115
116 return t, resp, nil
117 }
118
119
120
121
122 func (s *TeamsService) GetTeamBySlug(ctx context.Context, org, slug string) (*Team, *Response, error) {
123 u := fmt.Sprintf("orgs/%v/teams/%v", org, slug)
124 req, err := s.client.NewRequest("GET", u, nil)
125 if err != nil {
126 return nil, nil, err
127 }
128
129 t := new(Team)
130 resp, err := s.client.Do(ctx, req, t)
131 if err != nil {
132 return nil, resp, err
133 }
134
135 return t, resp, nil
136 }
137
138
139 type NewTeam struct {
140 Name string `json:"name"`
141 Description *string `json:"description,omitempty"`
142 Maintainers []string `json:"maintainers,omitempty"`
143 RepoNames []string `json:"repo_names,omitempty"`
144 ParentTeamID *int64 `json:"parent_team_id,omitempty"`
145
146
147
148
149
150
151 Permission *string `json:"permission,omitempty"`
152
153
154
155
156
157
158 Privacy *string `json:"privacy,omitempty"`
159
160
161
162 LDAPDN *string `json:"ldap_dn,omitempty"`
163 }
164
165 func (s NewTeam) String() string {
166 return Stringify(s)
167 }
168
169
170
171
172 func (s *TeamsService) CreateTeam(ctx context.Context, org string, team NewTeam) (*Team, *Response, error) {
173 u := fmt.Sprintf("orgs/%v/teams", org)
174 req, err := s.client.NewRequest("POST", u, team)
175 if err != nil {
176 return nil, nil, err
177 }
178
179 t := new(Team)
180 resp, err := s.client.Do(ctx, req, t)
181 if err != nil {
182 return nil, resp, err
183 }
184
185 return t, resp, nil
186 }
187
188
189
190
191 type newTeamNoParent struct {
192 Name string `json:"name"`
193 Description *string `json:"description,omitempty"`
194 Maintainers []string `json:"maintainers,omitempty"`
195 RepoNames []string `json:"repo_names,omitempty"`
196 ParentTeamID *int64 `json:"parent_team_id"`
197 Privacy *string `json:"privacy,omitempty"`
198 LDAPDN *string `json:"ldap_dn,omitempty"`
199 }
200
201
202
203
204 func copyNewTeamWithoutParent(team *NewTeam) *newTeamNoParent {
205 return &newTeamNoParent{
206 Name: team.Name,
207 Description: team.Description,
208 Maintainers: team.Maintainers,
209 RepoNames: team.RepoNames,
210 Privacy: team.Privacy,
211 LDAPDN: team.LDAPDN,
212 }
213 }
214
215
216
217
218 func (s *TeamsService) EditTeamByID(ctx context.Context, orgID, teamID int64, team NewTeam, removeParent bool) (*Team, *Response, error) {
219 u := fmt.Sprintf("organizations/%v/team/%v", orgID, teamID)
220
221 var req *http.Request
222 var err error
223 if removeParent {
224 teamRemoveParent := copyNewTeamWithoutParent(&team)
225 req, err = s.client.NewRequest("PATCH", u, teamRemoveParent)
226 } else {
227 req, err = s.client.NewRequest("PATCH", u, team)
228 }
229 if err != nil {
230 return nil, nil, err
231 }
232
233 t := new(Team)
234 resp, err := s.client.Do(ctx, req, t)
235 if err != nil {
236 return nil, resp, err
237 }
238
239 return t, resp, nil
240 }
241
242
243
244
245 func (s *TeamsService) EditTeamBySlug(ctx context.Context, org, slug string, team NewTeam, removeParent bool) (*Team, *Response, error) {
246 u := fmt.Sprintf("orgs/%v/teams/%v", org, slug)
247
248 var req *http.Request
249 var err error
250 if removeParent {
251 teamRemoveParent := copyNewTeamWithoutParent(&team)
252 req, err = s.client.NewRequest("PATCH", u, teamRemoveParent)
253 } else {
254 req, err = s.client.NewRequest("PATCH", u, team)
255 }
256 if err != nil {
257 return nil, nil, err
258 }
259
260 t := new(Team)
261 resp, err := s.client.Do(ctx, req, t)
262 if err != nil {
263 return nil, resp, err
264 }
265
266 return t, resp, nil
267 }
268
269
270
271
272 func (s *TeamsService) DeleteTeamByID(ctx context.Context, orgID, teamID int64) (*Response, error) {
273 u := fmt.Sprintf("organizations/%v/team/%v", orgID, teamID)
274 req, err := s.client.NewRequest("DELETE", u, nil)
275 if err != nil {
276 return nil, err
277 }
278
279 return s.client.Do(ctx, req, nil)
280 }
281
282
283
284
285 func (s *TeamsService) DeleteTeamBySlug(ctx context.Context, org, slug string) (*Response, error) {
286 u := fmt.Sprintf("orgs/%v/teams/%v", org, slug)
287 req, err := s.client.NewRequest("DELETE", u, nil)
288 if err != nil {
289 return nil, err
290 }
291
292 return s.client.Do(ctx, req, nil)
293 }
294
295
296
297
298 func (s *TeamsService) ListChildTeamsByParentID(ctx context.Context, orgID, teamID int64, opts *ListOptions) ([]*Team, *Response, error) {
299 u := fmt.Sprintf("organizations/%v/team/%v/teams", orgID, teamID)
300 u, err := addOptions(u, opts)
301 if err != nil {
302 return nil, nil, err
303 }
304
305 req, err := s.client.NewRequest("GET", u, nil)
306 if err != nil {
307 return nil, nil, err
308 }
309
310 var teams []*Team
311 resp, err := s.client.Do(ctx, req, &teams)
312 if err != nil {
313 return nil, resp, err
314 }
315
316 return teams, resp, nil
317 }
318
319
320
321
322 func (s *TeamsService) ListChildTeamsByParentSlug(ctx context.Context, org, slug string, opts *ListOptions) ([]*Team, *Response, error) {
323 u := fmt.Sprintf("orgs/%v/teams/%v/teams", org, slug)
324 u, err := addOptions(u, opts)
325 if err != nil {
326 return nil, nil, err
327 }
328
329 req, err := s.client.NewRequest("GET", u, nil)
330 if err != nil {
331 return nil, nil, err
332 }
333
334 var teams []*Team
335 resp, err := s.client.Do(ctx, req, &teams)
336 if err != nil {
337 return nil, resp, err
338 }
339
340 return teams, resp, nil
341 }
342
343
344
345
346 func (s *TeamsService) ListTeamReposByID(ctx context.Context, orgID, teamID int64, opts *ListOptions) ([]*Repository, *Response, error) {
347 u := fmt.Sprintf("organizations/%v/team/%v/repos", orgID, teamID)
348 u, err := addOptions(u, opts)
349 if err != nil {
350 return nil, nil, err
351 }
352
353 req, err := s.client.NewRequest("GET", u, nil)
354 if err != nil {
355 return nil, nil, err
356 }
357
358
359 headers := []string{mediaTypeTopicsPreview}
360 req.Header.Set("Accept", strings.Join(headers, ", "))
361
362 var repos []*Repository
363 resp, err := s.client.Do(ctx, req, &repos)
364 if err != nil {
365 return nil, resp, err
366 }
367
368 return repos, resp, nil
369 }
370
371
372
373
374 func (s *TeamsService) ListTeamReposBySlug(ctx context.Context, org, slug string, opts *ListOptions) ([]*Repository, *Response, error) {
375 u := fmt.Sprintf("orgs/%v/teams/%v/repos", org, slug)
376 u, err := addOptions(u, opts)
377 if err != nil {
378 return nil, nil, err
379 }
380
381 req, err := s.client.NewRequest("GET", u, nil)
382 if err != nil {
383 return nil, nil, err
384 }
385
386
387 headers := []string{mediaTypeTopicsPreview}
388 req.Header.Set("Accept", strings.Join(headers, ", "))
389
390 var repos []*Repository
391 resp, err := s.client.Do(ctx, req, &repos)
392 if err != nil {
393 return nil, resp, err
394 }
395
396 return repos, resp, nil
397 }
398
399
400
401
402
403
404 func (s *TeamsService) IsTeamRepoByID(ctx context.Context, orgID, teamID int64, owner, repo string) (*Repository, *Response, error) {
405 u := fmt.Sprintf("organizations/%v/team/%v/repos/%v/%v", orgID, teamID, owner, repo)
406 req, err := s.client.NewRequest("GET", u, nil)
407 if err != nil {
408 return nil, nil, err
409 }
410
411 headers := []string{mediaTypeOrgPermissionRepo}
412 req.Header.Set("Accept", strings.Join(headers, ", "))
413
414 repository := new(Repository)
415 resp, err := s.client.Do(ctx, req, repository)
416 if err != nil {
417 return nil, resp, err
418 }
419
420 return repository, resp, nil
421 }
422
423
424
425
426
427
428 func (s *TeamsService) IsTeamRepoBySlug(ctx context.Context, org, slug, owner, repo string) (*Repository, *Response, error) {
429 u := fmt.Sprintf("orgs/%v/teams/%v/repos/%v/%v", org, slug, owner, repo)
430 req, err := s.client.NewRequest("GET", u, nil)
431 if err != nil {
432 return nil, nil, err
433 }
434
435 headers := []string{mediaTypeOrgPermissionRepo}
436 req.Header.Set("Accept", strings.Join(headers, ", "))
437
438 repository := new(Repository)
439 resp, err := s.client.Do(ctx, req, repository)
440 if err != nil {
441 return nil, resp, err
442 }
443
444 return repository, resp, nil
445 }
446
447
448
449 type TeamAddTeamRepoOptions struct {
450
451
452
453
454
455
456
457
458
459 Permission string `json:"permission,omitempty"`
460 }
461
462
463
464
465
466
467 func (s *TeamsService) AddTeamRepoByID(ctx context.Context, orgID, teamID int64, owner, repo string, opts *TeamAddTeamRepoOptions) (*Response, error) {
468 u := fmt.Sprintf("organizations/%v/team/%v/repos/%v/%v", orgID, teamID, owner, repo)
469 req, err := s.client.NewRequest("PUT", u, opts)
470 if err != nil {
471 return nil, err
472 }
473
474 return s.client.Do(ctx, req, nil)
475 }
476
477
478
479
480
481
482 func (s *TeamsService) AddTeamRepoBySlug(ctx context.Context, org, slug, owner, repo string, opts *TeamAddTeamRepoOptions) (*Response, error) {
483 u := fmt.Sprintf("orgs/%v/teams/%v/repos/%v/%v", org, slug, owner, repo)
484 req, err := s.client.NewRequest("PUT", u, opts)
485 if err != nil {
486 return nil, err
487 }
488
489 return s.client.Do(ctx, req, nil)
490 }
491
492
493
494
495
496
497 func (s *TeamsService) RemoveTeamRepoByID(ctx context.Context, orgID, teamID int64, owner, repo string) (*Response, error) {
498 u := fmt.Sprintf("organizations/%v/team/%v/repos/%v/%v", orgID, teamID, owner, repo)
499 req, err := s.client.NewRequest("DELETE", u, nil)
500 if err != nil {
501 return nil, err
502 }
503
504 return s.client.Do(ctx, req, nil)
505 }
506
507
508
509
510
511
512 func (s *TeamsService) RemoveTeamRepoBySlug(ctx context.Context, org, slug, owner, repo string) (*Response, error) {
513 u := fmt.Sprintf("orgs/%v/teams/%v/repos/%v/%v", org, slug, owner, repo)
514 req, err := s.client.NewRequest("DELETE", u, nil)
515 if err != nil {
516 return nil, err
517 }
518
519 return s.client.Do(ctx, req, nil)
520 }
521
522
523
524 func (s *TeamsService) ListUserTeams(ctx context.Context, opts *ListOptions) ([]*Team, *Response, error) {
525 u := "user/teams"
526 u, err := addOptions(u, opts)
527 if err != nil {
528 return nil, nil, err
529 }
530
531 req, err := s.client.NewRequest("GET", u, nil)
532 if err != nil {
533 return nil, nil, err
534 }
535
536 var teams []*Team
537 resp, err := s.client.Do(ctx, req, &teams)
538 if err != nil {
539 return nil, resp, err
540 }
541
542 return teams, resp, nil
543 }
544
545
546
547
548 func (s *TeamsService) ListTeamProjectsByID(ctx context.Context, orgID, teamID int64) ([]*Project, *Response, error) {
549 u := fmt.Sprintf("organizations/%v/team/%v/projects", orgID, teamID)
550
551 req, err := s.client.NewRequest("GET", u, nil)
552 if err != nil {
553 return nil, nil, err
554 }
555
556
557 acceptHeaders := []string{mediaTypeProjectsPreview}
558 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
559
560 var projects []*Project
561 resp, err := s.client.Do(ctx, req, &projects)
562 if err != nil {
563 return nil, resp, err
564 }
565
566 return projects, resp, nil
567 }
568
569
570
571
572 func (s *TeamsService) ListTeamProjectsBySlug(ctx context.Context, org, slug string) ([]*Project, *Response, error) {
573 u := fmt.Sprintf("orgs/%v/teams/%v/projects", org, slug)
574
575 req, err := s.client.NewRequest("GET", u, nil)
576 if err != nil {
577 return nil, nil, err
578 }
579
580
581 acceptHeaders := []string{mediaTypeProjectsPreview}
582 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
583
584 var projects []*Project
585 resp, err := s.client.Do(ctx, req, &projects)
586 if err != nil {
587 return nil, resp, err
588 }
589
590 return projects, resp, nil
591 }
592
593
594
595
596
597 func (s *TeamsService) ReviewTeamProjectsByID(ctx context.Context, orgID, teamID, projectID int64) (*Project, *Response, error) {
598 u := fmt.Sprintf("organizations/%v/team/%v/projects/%v", orgID, teamID, projectID)
599 req, err := s.client.NewRequest("GET", u, nil)
600 if err != nil {
601 return nil, nil, err
602 }
603
604
605 acceptHeaders := []string{mediaTypeProjectsPreview}
606 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
607
608 projects := &Project{}
609 resp, err := s.client.Do(ctx, req, &projects)
610 if err != nil {
611 return nil, resp, err
612 }
613
614 return projects, resp, nil
615 }
616
617
618
619
620
621 func (s *TeamsService) ReviewTeamProjectsBySlug(ctx context.Context, org, slug string, projectID int64) (*Project, *Response, error) {
622 u := fmt.Sprintf("orgs/%v/teams/%v/projects/%v", org, slug, projectID)
623 req, err := s.client.NewRequest("GET", u, nil)
624 if err != nil {
625 return nil, nil, err
626 }
627
628
629 acceptHeaders := []string{mediaTypeProjectsPreview}
630 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
631
632 projects := &Project{}
633 resp, err := s.client.Do(ctx, req, &projects)
634 if err != nil {
635 return nil, resp, err
636 }
637
638 return projects, resp, nil
639 }
640
641
642
643 type TeamProjectOptions struct {
644
645
646
647
648
649
650 Permission *string `json:"permission,omitempty"`
651 }
652
653
654
655
656
657
658 func (s *TeamsService) AddTeamProjectByID(ctx context.Context, orgID, teamID, projectID int64, opts *TeamProjectOptions) (*Response, error) {
659 u := fmt.Sprintf("organizations/%v/team/%v/projects/%v", orgID, teamID, projectID)
660 req, err := s.client.NewRequest("PUT", u, opts)
661 if err != nil {
662 return nil, err
663 }
664
665
666 acceptHeaders := []string{mediaTypeProjectsPreview}
667 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
668
669 return s.client.Do(ctx, req, nil)
670 }
671
672
673
674
675
676
677 func (s *TeamsService) AddTeamProjectBySlug(ctx context.Context, org, slug string, projectID int64, opts *TeamProjectOptions) (*Response, error) {
678 u := fmt.Sprintf("orgs/%v/teams/%v/projects/%v", org, slug, projectID)
679 req, err := s.client.NewRequest("PUT", u, opts)
680 if err != nil {
681 return nil, err
682 }
683
684
685 acceptHeaders := []string{mediaTypeProjectsPreview}
686 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
687
688 return s.client.Do(ctx, req, nil)
689 }
690
691
692
693
694
695
696
697
698
699 func (s *TeamsService) RemoveTeamProjectByID(ctx context.Context, orgID, teamID, projectID int64) (*Response, error) {
700 u := fmt.Sprintf("organizations/%v/team/%v/projects/%v", orgID, teamID, projectID)
701 req, err := s.client.NewRequest("DELETE", u, nil)
702 if err != nil {
703 return nil, err
704 }
705
706
707 acceptHeaders := []string{mediaTypeProjectsPreview}
708 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
709
710 return s.client.Do(ctx, req, nil)
711 }
712
713
714
715
716
717
718
719
720
721 func (s *TeamsService) RemoveTeamProjectBySlug(ctx context.Context, org, slug string, projectID int64) (*Response, error) {
722 u := fmt.Sprintf("orgs/%v/teams/%v/projects/%v", org, slug, projectID)
723 req, err := s.client.NewRequest("DELETE", u, nil)
724 if err != nil {
725 return nil, err
726 }
727
728
729 acceptHeaders := []string{mediaTypeProjectsPreview}
730 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
731
732 return s.client.Do(ctx, req, nil)
733 }
734
735
736 type IDPGroupList struct {
737 Groups []*IDPGroup `json:"groups"`
738 }
739
740
741 type IDPGroup struct {
742 GroupID *string `json:"group_id,omitempty"`
743 GroupName *string `json:"group_name,omitempty"`
744 GroupDescription *string `json:"group_description,omitempty"`
745 }
746
747
748
749
750 func (s *TeamsService) ListIDPGroupsInOrganization(ctx context.Context, org string, opts *ListCursorOptions) (*IDPGroupList, *Response, error) {
751 u := fmt.Sprintf("orgs/%v/team-sync/groups", org)
752 u, err := addOptions(u, opts)
753 if err != nil {
754 return nil, nil, err
755 }
756
757 req, err := s.client.NewRequest("GET", u, nil)
758 if err != nil {
759 return nil, nil, err
760 }
761
762 groups := new(IDPGroupList)
763 resp, err := s.client.Do(ctx, req, groups)
764 if err != nil {
765 return nil, resp, err
766 }
767 return groups, resp, nil
768 }
769
770
771
772
773
774 func (s *TeamsService) ListIDPGroupsForTeamByID(ctx context.Context, orgID, teamID int64) (*IDPGroupList, *Response, error) {
775 u := fmt.Sprintf("organizations/%v/team/%v/team-sync/group-mappings", orgID, teamID)
776
777 req, err := s.client.NewRequest("GET", u, nil)
778 if err != nil {
779 return nil, nil, err
780 }
781
782 groups := new(IDPGroupList)
783 resp, err := s.client.Do(ctx, req, groups)
784 if err != nil {
785 return nil, resp, err
786 }
787 return groups, resp, err
788 }
789
790
791
792
793
794 func (s *TeamsService) ListIDPGroupsForTeamBySlug(ctx context.Context, org, slug string) (*IDPGroupList, *Response, error) {
795 u := fmt.Sprintf("orgs/%v/teams/%v/team-sync/group-mappings", org, slug)
796
797 req, err := s.client.NewRequest("GET", u, nil)
798 if err != nil {
799 return nil, nil, err
800 }
801
802 groups := new(IDPGroupList)
803 resp, err := s.client.Do(ctx, req, groups)
804 if err != nil {
805 return nil, resp, err
806 }
807 return groups, resp, err
808 }
809
810
811
812
813
814 func (s *TeamsService) CreateOrUpdateIDPGroupConnectionsByID(ctx context.Context, orgID, teamID int64, opts IDPGroupList) (*IDPGroupList, *Response, error) {
815 u := fmt.Sprintf("organizations/%v/team/%v/team-sync/group-mappings", orgID, teamID)
816
817 req, err := s.client.NewRequest("PATCH", u, opts)
818 if err != nil {
819 return nil, nil, err
820 }
821
822 groups := new(IDPGroupList)
823 resp, err := s.client.Do(ctx, req, groups)
824 if err != nil {
825 return nil, resp, err
826 }
827
828 return groups, resp, nil
829 }
830
831
832
833
834
835 func (s *TeamsService) CreateOrUpdateIDPGroupConnectionsBySlug(ctx context.Context, org, slug string, opts IDPGroupList) (*IDPGroupList, *Response, error) {
836 u := fmt.Sprintf("orgs/%v/teams/%v/team-sync/group-mappings", org, slug)
837
838 req, err := s.client.NewRequest("PATCH", u, opts)
839 if err != nil {
840 return nil, nil, err
841 }
842
843 groups := new(IDPGroupList)
844 resp, err := s.client.Do(ctx, req, groups)
845 if err != nil {
846 return nil, resp, err
847 }
848
849 return groups, resp, nil
850 }
851
View as plain text