Skip to content
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

Added Support preview Team Sync API for GitHub Enterprise Cloud users #1212

Merged
merged 3 commits into from
Jul 8, 2019
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
24 changes: 24 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ const (

// https://developer.github.com/changes/2019-04-11-pulls-branches-for-commit/
mediaTypeListPullsOrBranchesForCommitPreview = "application/vnd.github.groot-preview+json"

// https://developer.github.com/changes/2019-06-12-team-sync/
mediaTypeTeamSyncPreview = "application/vnd.github.team-sync-preview+json"
)

// A Client manages communication with the GitHub API.
Expand Down
84 changes: 84 additions & 0 deletions github/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,87 @@ func (s *TeamsService) RemoveTeamProject(ctx context.Context, teamID int64, proj

return s.client.Do(ctx, req, nil)
}

// IDPGroupList represents a list of external identity provider (IDP) groups.
type IDPGroupList struct {
Groups []*IDPGroup `json:"groups,omitempty"`
}

// IDPGroup represents an external identity provider (IDP) group.
type IDPGroup struct {
GroupID *string `json:"group_id,omitempty"`
GroupName *string `json:"group_name,omitempty"`
GroupDescription *string `json:"group_description,omitempty"`
}

// ListIDPGroupsInOrganization lists IDP groups available in an organization.
//
// GitHub API docs: https://developer.github.com/v3/teams/team_sync/#list-idp-groups-in-an-organization
func (s *TeamsService) ListIDPGroupsInOrganization(ctx context.Context, org string, opt *ListOptions) (*IDPGroupList, *Response, error) {
u := fmt.Sprintf("orgs/%v/team-sync/groups", org)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

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

groups := new(IDPGroupList)
resp, err := s.client.Do(ctx, req, groups)
if err != nil {
return nil, resp, err
}
return groups, resp, nil
}

// ListIDPGroupsForTeam lists IDP groups connected to a team on GitHub.
//
// GitHub API docs: https://developer.github.com/v3/teams/team_sync/#list-idp-groups-for-a-team
func (s *TeamsService) ListIDPGroupsForTeam(ctx context.Context, teamID string) (*IDPGroupList, *Response, error) {
u := fmt.Sprintf("teams/%v/team-sync/group-mappings", teamID)

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

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

groups := new(IDPGroupList)
resp, err := s.client.Do(ctx, req, groups)
if err != nil {
return nil, resp, err
}
return groups, resp, err
}

// CreateOrUpdateIDPGroupConnections creates, updates, or removes a connection between a team
// and an IDP group.
//
// GitHub API docs: https://developer.github.com/v3/teams/team_sync/#create-or-update-idp-group-connections
func (s *TeamsService) CreateOrUpdateIDPGroupConnections(ctx context.Context, teamID string, opt IDPGroupList) (*IDPGroupList, *Response, error) {
u := fmt.Sprintf("teams/%v/team-sync/group-mappings", teamID)

req, err := s.client.NewRequest("PATCH", u, opt)
if err != nil {
return nil, nil, err
}

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

groups := new(IDPGroupList)
resp, err := s.client.Do(ctx, req, groups)
if err != nil {
return nil, resp, err
}

return groups, resp, nil
}
101 changes: 101 additions & 0 deletions github/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,104 @@ func TestTeamsService_RemoveTeamProject(t *testing.T) {
t.Errorf("Teams.RemoveTeamProject returned error: %v", err)
}
}

func TestTeamsService_ListIDPGroupsInOrganization(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/team-sync/groups", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeTeamSyncPreview)
testFormValues(t, r, values{
"page": "2",
})
fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`)
})

opt := &ListOptions{Page: 2}
groups, _, err := client.Teams.ListIDPGroupsInOrganization(context.Background(), "o", opt)
if err != nil {
t.Errorf("Teams.ListIDPGroupsInOrganization returned error: %v", err)
}

want := &IDPGroupList{
Groups: []*IDPGroup{
{
GroupID: String("1"),
GroupName: String("n"),
GroupDescription: String("d"),
},
},
}
if !reflect.DeepEqual(groups, want) {
t.Errorf("Teams.ListIDPGroupsInOrganization returned %+v. want %+v", groups, want)
}
}

func TestTeamsService_ListIDPGroupsForTeam(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/teams/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeTeamSyncPreview)
fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`)
})

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

want := &IDPGroupList{
Groups: []*IDPGroup{
{
GroupID: String("1"),
GroupName: String("n"),
GroupDescription: String("d"),
},
},
}
if !reflect.DeepEqual(groups, want) {
t.Errorf("Teams.ListIDPGroupsForTeam returned %+v. want %+v", groups, want)
}
}

func TestTeamsService_CreateOrUpdateIDPGroupConnections(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/teams/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
testHeader(t, r, "Accept", mediaTypeTeamSyncPreview)
fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`)
})

input := IDPGroupList{
Groups: []*IDPGroup{
{
GroupID: String("1"),
GroupName: String("n"),
GroupDescription: String("d"),
},
},
}

groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnections(context.Background(), "1", input)
if err != nil {
t.Errorf("Teams.CreateOrUpdateIDPGroupConnections returned error: %v", err)
}

want := &IDPGroupList{
Groups: []*IDPGroup{
{
GroupID: String("1"),
GroupName: String("n"),
GroupDescription: String("d"),
},
},
}
if !reflect.DeepEqual(groups, want) {
t.Errorf("Teams.CreateOrUpdateIDPGroupConnections returned %+v. want %+v", groups, want)
}
}