Skip to content

Commit

Permalink
Add ListExternalGroupsForTeamBySlug to Teams API (#2674)
Browse files Browse the repository at this point in the history
  • Loading branch information
bodgit committed Mar 6, 2023
1 parent 8b5397f commit 62e81bc
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
22 changes: 21 additions & 1 deletion github/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ type ListExternalGroupsOptions struct {
ListOptions
}

// ListExternalGroups lists external groups connected to a team on GitHub.
// ListExternalGroups lists external groups in an organization on GitHub.
//
// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/teams/external-groups#list-external-groups-in-an-organization
func (s *TeamsService) ListExternalGroups(ctx context.Context, org string, opts *ListExternalGroupsOptions) (*ExternalGroupList, *Response, error) {
Expand All @@ -937,6 +937,26 @@ func (s *TeamsService) ListExternalGroups(ctx context.Context, org string, opts
return externalGroups, resp, nil
}

// ListExternalGroupsForTeamBySlug lists external groups connected to a team on GitHub.
//
// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/teams/external-groups#list-a-connection-between-an-external-group-and-a-team
func (s *TeamsService) ListExternalGroupsForTeamBySlug(ctx context.Context, org, slug string) (*ExternalGroupList, *Response, error) {
u := fmt.Sprintf("orgs/%v/teams/%v/external-groups", org, slug)

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

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

return externalGroups, resp, nil
}

// UpdateConnectedExternalGroup updates the connection between an external group and a team.
//
// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/teams/external-groups#update-the-connection-between-an-external-group-and-a-team
Expand Down
73 changes: 73 additions & 0 deletions github/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,79 @@ func TestTeamsService_ListExternalGroups_notFound(t *testing.T) {
}
}

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

mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"groups": [
{
"group_id": 123,
"group_name": "Octocat admins",
"updated_at": "2006-01-02T15:04:05Z"
}
]
}`)
})

ctx := context.Background()
list, _, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t")
if err != nil {
t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned error: %v", err)
}

want := &ExternalGroupList{
Groups: []*ExternalGroup{
{
GroupID: Int64(123),
GroupName: String("Octocat admins"),
UpdatedAt: &Timestamp{Time: referenceTime},
},
},
}
if !cmp.Equal(list, want) {
t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned %+v, want %+v", list, want)
}

const methodName = "ListExternalGroupsForTeamBySlug"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Teams.ListExternalGroupsForTeamBySlug(ctx, "\n", "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

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

mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.WriteHeader(http.StatusNotFound)
})

ctx := context.Background()
eg, resp, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t")
if err == nil {
t.Errorf("Expected HTTP 404 response")
}
if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned status %d, want %d", got, want)
}
if eg != nil {
t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned %+v, want nil", eg)
}
}

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

0 comments on commit 62e81bc

Please sign in to comment.