Skip to content

Request parent team when endpoints return a team #723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ const (

// https://developer.github.com/changes/2017-07-26-team-review-request-thor-preview/
mediaTypeTeamReviewPreview = "application/vnd.github.thor-preview+json"

// https://developer.github.com/changes/2017-08-30-preview-nested-teams/
mediaTypeNestedTeamsPreview = "application/vnd.github.hellcat-preview+json"
)

// A Client manages communication with the GitHub API.
Expand Down
16 changes: 16 additions & 0 deletions github/orgs_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Team struct {
Organization *Organization `json:"organization,omitempty"`
MembersURL *string `json:"members_url,omitempty"`
RepositoriesURL *string `json:"repositories_url,omitempty"`
Parent *Team `json:"parent,omitempty"`

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

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)

var teams []*Team
resp, err := s.client.Do(ctx, req, &teams)
if err != nil {
Expand All @@ -98,6 +102,9 @@ func (s *OrganizationsService) GetTeam(ctx context.Context, team int) (*Team, *R
return nil, nil, err
}

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)

t := new(Team)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
Expand All @@ -117,6 +124,9 @@ func (s *OrganizationsService) CreateTeam(ctx context.Context, org string, team
return nil, nil, err
}

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)

t := new(Team)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
Expand All @@ -136,6 +146,9 @@ func (s *OrganizationsService) EditTeam(ctx context.Context, id int, team *Team)
return nil, nil, err
}

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)

t := new(Team)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
Expand Down Expand Up @@ -315,6 +328,9 @@ func (s *OrganizationsService) ListUserTeams(ctx context.Context, opt *ListOptio
return nil, nil, err
}

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeNestedTeamsPreview)

var teams []*Team
resp, err := s.client.Do(ctx, req, &teams)
if err != nil {
Expand Down
31 changes: 29 additions & 2 deletions github/orgs_teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestOrganizationsService_ListTeams(t *testing.T) {

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

mux.HandleFunc("/teams/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
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"}`)
testHeader(t, r, "Accept", mediaTypeNestedTeamsPreview)
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}`)
})

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

func TestOrganizationService_GetTeam_nestedTeams(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/teams/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeNestedTeamsPreview)
fmt.Fprint(w, `{"id":1, "name":"n", "description": "d", "url":"u", "slug": "s", "permission":"p",
"parent": {"id":2, "name":"n", "description": "d", "parent": null}}`)
})

team, _, err := client.Organizations.GetTeam(context.Background(), 1)
if err != nil {
t.Errorf("Organizations.GetTeam returned error: %v", err)
}

want := &Team{ID: Int(1), Name: String("n"), Description: String("d"), URL: String("u"), Slug: String("s"), Permission: String("p"),
Parent: &Team{ID: Int(2), Name: String("n"), Description: String("d")},
}
if !reflect.DeepEqual(team, want) {
t.Errorf("Organizations.GetTeam returned %+v, want %+v", team, want)
}
}

func TestOrganizationsService_CreateTeam(t *testing.T) {
setup()
defer teardown()

input := &Team{Name: String("n"), Privacy: String("closed")}

mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) {
v := new(Team)
json.NewDecoder(r.Body).Decode(v)

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

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