Skip to content

Commit d25a6ce

Browse files
elliott-beachdmitshur
authored andcommitted
Add support for fetching parent team (for nested teams). (#723)
Create mediaTypeNestedTeamsPreview const for https://developer.github.com/changes/2017-08-30-preview-nested-teams/. Add parent field to the Team struct. Set preview API media type in endpoints that return a team, so that the new parent field gets populated. Helps #714.
1 parent a021c14 commit d25a6ce

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

github/github.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ const (
102102

103103
// https://developer.github.com/changes/2017-07-26-team-review-request-thor-preview/
104104
mediaTypeTeamReviewPreview = "application/vnd.github.thor-preview+json"
105+
106+
// https://developer.github.com/changes/2017-08-30-preview-nested-teams/
107+
mediaTypeNestedTeamsPreview = "application/vnd.github.hellcat-preview+json"
105108
)
106109

107110
// A Client manages communication with the GitHub API.

github/orgs_teams.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Team struct {
3939
Organization *Organization `json:"organization,omitempty"`
4040
MembersURL *string `json:"members_url,omitempty"`
4141
RepositoriesURL *string `json:"repositories_url,omitempty"`
42+
Parent *Team `json:"parent,omitempty"`
4243

4344
// LDAPDN is only available in GitHub Enterprise and when the team
4445
// membership is synchronized with LDAP.
@@ -79,6 +80,9 @@ func (s *OrganizationsService) ListTeams(ctx context.Context, org string, opt *L
7980
return nil, nil, err
8081
}
8182

83+
// TODO: remove custom Accept header when this API fully launches.
84+
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
85+
8286
var teams []*Team
8387
resp, err := s.client.Do(ctx, req, &teams)
8488
if err != nil {
@@ -98,6 +102,9 @@ func (s *OrganizationsService) GetTeam(ctx context.Context, team int) (*Team, *R
98102
return nil, nil, err
99103
}
100104

105+
// TODO: remove custom Accept header when this API fully launches.
106+
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
107+
101108
t := new(Team)
102109
resp, err := s.client.Do(ctx, req, t)
103110
if err != nil {
@@ -117,6 +124,9 @@ func (s *OrganizationsService) CreateTeam(ctx context.Context, org string, team
117124
return nil, nil, err
118125
}
119126

127+
// TODO: remove custom Accept header when this API fully launches.
128+
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
129+
120130
t := new(Team)
121131
resp, err := s.client.Do(ctx, req, t)
122132
if err != nil {
@@ -136,6 +146,9 @@ func (s *OrganizationsService) EditTeam(ctx context.Context, id int, team *Team)
136146
return nil, nil, err
137147
}
138148

149+
// TODO: remove custom Accept header when this API fully launches.
150+
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
151+
139152
t := new(Team)
140153
resp, err := s.client.Do(ctx, req, t)
141154
if err != nil {
@@ -315,6 +328,9 @@ func (s *OrganizationsService) ListUserTeams(ctx context.Context, opt *ListOptio
315328
return nil, nil, err
316329
}
317330

331+
// TODO: remove custom Accept header when this API fully launches.
332+
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
333+
318334
var teams []*Team
319335
resp, err := s.client.Do(ctx, req, &teams)
320336
if err != nil {

github/orgs_teams_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestOrganizationsService_ListTeams(t *testing.T) {
2121

2222
mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) {
2323
testMethod(t, r, "GET")
24+
testHeader(t, r, "Accept", mediaTypeNestedTeamsPreview)
2425
testFormValues(t, r, values{"page": "2"})
2526
fmt.Fprint(w, `[{"id":1}]`)
2627
})
@@ -48,7 +49,8 @@ func TestOrganizationsService_GetTeam(t *testing.T) {
4849

4950
mux.HandleFunc("/teams/1", func(w http.ResponseWriter, r *http.Request) {
5051
testMethod(t, r, "GET")
51-
fmt.Fprint(w, `{"id":1, "name":"n", "description": "d", "url":"u", "slug": "s", "permission":"p", "ldap_dn":"cn=n,ou=groups,dc=example,dc=com"}`)
52+
testHeader(t, r, "Accept", mediaTypeNestedTeamsPreview)
53+
fmt.Fprint(w, `{"id":1, "name":"n", "description": "d", "url":"u", "slug": "s", "permission":"p", "ldap_dn":"cn=n,ou=groups,dc=example,dc=com", "parent":null}`)
5254
})
5355

5456
team, _, err := client.Organizations.GetTeam(context.Background(), 1)
@@ -62,17 +64,41 @@ func TestOrganizationsService_GetTeam(t *testing.T) {
6264
}
6365
}
6466

67+
func TestOrganizationService_GetTeam_nestedTeams(t *testing.T) {
68+
setup()
69+
defer teardown()
70+
71+
mux.HandleFunc("/teams/1", func(w http.ResponseWriter, r *http.Request) {
72+
testMethod(t, r, "GET")
73+
testHeader(t, r, "Accept", mediaTypeNestedTeamsPreview)
74+
fmt.Fprint(w, `{"id":1, "name":"n", "description": "d", "url":"u", "slug": "s", "permission":"p",
75+
"parent": {"id":2, "name":"n", "description": "d", "parent": null}}`)
76+
})
77+
78+
team, _, err := client.Organizations.GetTeam(context.Background(), 1)
79+
if err != nil {
80+
t.Errorf("Organizations.GetTeam returned error: %v", err)
81+
}
82+
83+
want := &Team{ID: Int(1), Name: String("n"), Description: String("d"), URL: String("u"), Slug: String("s"), Permission: String("p"),
84+
Parent: &Team{ID: Int(2), Name: String("n"), Description: String("d")},
85+
}
86+
if !reflect.DeepEqual(team, want) {
87+
t.Errorf("Organizations.GetTeam returned %+v, want %+v", team, want)
88+
}
89+
}
90+
6591
func TestOrganizationsService_CreateTeam(t *testing.T) {
6692
setup()
6793
defer teardown()
6894

6995
input := &Team{Name: String("n"), Privacy: String("closed")}
70-
7196
mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) {
7297
v := new(Team)
7398
json.NewDecoder(r.Body).Decode(v)
7499

75100
testMethod(t, r, "POST")
101+
testHeader(t, r, "Accept", mediaTypeNestedTeamsPreview)
76102
if !reflect.DeepEqual(v, input) {
77103
t.Errorf("Request body = %+v, want %+v", v, input)
78104
}
@@ -487,6 +513,7 @@ func TestOrganizationsService_ListUserTeams(t *testing.T) {
487513

488514
mux.HandleFunc("/user/teams", func(w http.ResponseWriter, r *http.Request) {
489515
testMethod(t, r, "GET")
516+
testHeader(t, r, "Accept", mediaTypeNestedTeamsPreview)
490517
testFormValues(t, r, values{"page": "1"})
491518
fmt.Fprint(w, `[{"id":1}]`)
492519
})

0 commit comments

Comments
 (0)