From 1f336ccb6d9b2cbf0dd199e1b11449515e8cd7f0 Mon Sep 17 00:00:00 2001 From: Matt Dainty Date: Fri, 24 Feb 2023 11:55:05 +0000 Subject: [PATCH] Add ListExternalGroupsForTeamBySlug to Teams API --- github/teams.go | 22 ++++++++++++- github/teams_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/github/teams.go b/github/teams.go index 963a69b3d9..0ee7c20079 100644 --- a/github/teams.go +++ b/github/teams.go @@ -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) { @@ -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 diff --git a/github/teams_test.go b/github/teams_test.go index 93e658af09..d86fab7fd6 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -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()