From 77395b08d744d31ba44d4f47f8ef37c7602616b9 Mon Sep 17 00:00:00 2001 From: kabesan Date: Mon, 6 Dec 2021 01:00:48 +0900 Subject: [PATCH 1/9] Add support External Groups. --- github/teams.go | 113 ++++++++++++++ github/teams_test.go | 349 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 462 insertions(+) diff --git a/github/teams.go b/github/teams.go index 7a8a5d8a7b7..917c3e36d48 100644 --- a/github/teams.go +++ b/github/teams.go @@ -854,3 +854,116 @@ func (s *TeamsService) CreateOrUpdateIDPGroupConnectionsBySlug(ctx context.Conte return groups, resp, nil } + +// ExternalGroupMember represents a member of external group. +type ExternalGroupMember struct { + ID int64 `json:"member_id,omitempty"` + Login string `json:"member_login,omitempty"` + Name string `json:"member_name,omitempty"` + Email string `json:"member_email,omitempty"` +} + +// ExternalGroupTeam represents a team connected to external group +type ExternalGroupTeam struct { + ID int64 `json:"team_id,omitempty"` + Name string `json:"team_name,omitempty"` +} + +// ExternalGroup represents an external group. +type ExternalGroup struct { + GroupID *int64 `json:"group_id,omitempty"` + GroupName *string `json:"group_name,omitempty"` + UpdatedAt *string `json:"updated_at,omitempty"` + Teams []*ExternalGroupTeam `json:"teams,omitempty"` + Members []*ExternalGroupMember `json:"members,omitempty"` +} + +// ExternalGroupList represents a list of external groups. +type ExternalGroupList struct { + Groups []*ExternalGroup `json:"groups"` +} + +// GetExternalGroup fetches a external group +// +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/reference/teams#get-an-external-group +func (s *TeamsService) GetExternalGroup(ctx context.Context, org string, groupID int64) (*ExternalGroup, *Response, error) { + u := fmt.Sprintf("orgs/%v/external-group/%v", org, groupID) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + externalGroup := new(ExternalGroup) + resp, err := s.client.Do(ctx, req, externalGroup) + if err != nil { + return nil, resp, err + } + + return externalGroup, resp, nil +} + +// ListExternalGroupsOptions specifies the optional parameters to the +// TeamsService.ListExternalGroups method. +type ListExternalGroupsOptions struct { + DisplayName string `url:"display_name,omitempty"` + + ListOptions +} + +// ListExternalGroups Lists external groups connected to a team on GitHub +// +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/reference/teams#list-external-groups-in-an-organization +func (s *TeamsService) ListExternalGroups(ctx context.Context, org string, opts *ListExternalGroupsOptions) (*ExternalGroupList, *Response, error) { + u := fmt.Sprintf("orgs/%v/external-groups", org) + u, err := addOptions(u, opts) + if err != nil { + return nil, nil, err + } + + 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 on external group and a team +// +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/reference/teams#update-the-connection-between-an-external-group-and-a-team +func (s *TeamsService) UpdateConnectedExternalGroup(ctx context.Context, org, slug string, eg *ExternalGroup) (*ExternalGroup, *Response, error) { + u := fmt.Sprintf("orgs/%v/teams/%v/external-groups", org, slug) + + req, err := s.client.NewRequest("PATCH", u, eg) + if err != nil { + return nil, nil, err + } + + externalGroup := new(ExternalGroup) + resp, err := s.client.Do(ctx, req, externalGroup) + if err != nil { + return nil, resp, err + } + + return externalGroup, resp, nil +} + +// RemoveConnectedExternalGroup removes the connection between on external group and a team +// +// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/reference/teams#remove-the-connection-between-an-external-group-and-a-team +func (s *TeamsService) RemoveConnectedExternalGroup(ctx context.Context, org, slug string) (*Response, error) { + u := fmt.Sprintf("orgs/%v/teams/%v/external-groups", org, slug) + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/github/teams_test.go b/github/teams_test.go index 26cf2c030fb..16a92d4113d 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -1745,3 +1745,352 @@ func TestIDPGroup_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } + +func TestTeamsService_GetExternalGroup(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/external-group/123", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{ + "group_id": 123, + "group_name": "Octocat admins", + "updated_at": "2021-01-24T11:31:04-06:00", + "teams": [ + { + "team_id": 1, + "team_name": "team-test" + }, + { + "team_id": 2, + "team_name": "team-test2" + } + ], + "members": [ + { + "member_id": 1, + "member_login": "mona-lisa_eocsaxrs", + "member_name": "Mona Lisa", + "member_email": "mona_lisa@github.com" + }, + { + "member_id": 2, + "member_login": "octo-lisa_eocsaxrs", + "member_name": "Octo Lisa", + "member_email": "octo_lisa@github.com" + } + ] + }`) + }) + + ctx := context.Background() + externalGroup, _, err := client.Teams.GetExternalGroup(ctx, "o", 123) + if err != nil { + t.Errorf("Teams.GetExternalGroup returned error: %v", err) + } + + want := &ExternalGroup{ + GroupID: Int64(123), + GroupName: String("Octocat admins"), + UpdatedAt: String("2021-01-24T11:31:04-06:00"), + Teams: []*ExternalGroupTeam{ + { + ID: 1, + Name: "team-test", + }, + { + ID: 2, + Name: "team-test2", + }, + }, + Members: []*ExternalGroupMember{ + { + ID: 1, + Login: "mona-lisa_eocsaxrs", + Name: "Mona Lisa", + Email: "mona_lisa@github.com", + }, + { + ID: 2, + Login: "octo-lisa_eocsaxrs", + Name: "Octo Lisa", + Email: "octo_lisa@github.com", + }, + }, + } + if !cmp.Equal(externalGroup, want) { + t.Errorf("Teams.GetExternalGroup returned %+v, want %+v", externalGroup, want) + } + + const methodName = "GetExternalGroup" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Teams.GetExternalGroup(ctx, "", -1) + return err + }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestTeamsService_GetExternalGroup_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/external-group/123", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + eg, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Teams.GetExternalGroup returned status %d, want %d", got, want) + } + if eg != nil { + t.Errorf("Teams.GetExternalGroup returned %+v, want nil", eg) + } +} + +func TestTeamsService_ListExternalGroups(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/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": "2021-01-24T11:31:04-06:00" + } + ] + }`) + }) + + ctx := context.Background() + opts := &ListExternalGroupsOptions{ + DisplayName: "Octocat", + } + list, _, err := client.Teams.ListExternalGroups(ctx, "o", opts) + if err != nil { + t.Errorf("Teams.ListExternalGroups returned error: %v", err) + } + + want := &ExternalGroupList{ + Groups: []*ExternalGroup{ + { + GroupID: Int64(123), + GroupName: String("Octocat admins"), + UpdatedAt: String("2021-01-24T11:31:04-06:00"), + }, + }, + } + if !cmp.Equal(list, want) { + t.Errorf("Teams.ListExternalGroups returned %+v, want %+v", list, want) + } + + const methodName = "ListExternalGroups" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Teams.ListExternalGroups(ctx, "", nil) + return err + }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Teams.ListExternalGroups(ctx, "o", nil) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestTeamsService_ListExternalGroups_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/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.ListExternalGroups(ctx, "o", nil) + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Teams.ListExternalGroups returned status %d, want %d", got, want) + } + if eg != nil { + t.Errorf("Teams.ListExternalGroups returned %+v, want nil", eg) + } +} + +func TestTeamsService_UpdateConnectedExternalGroup(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, "PATCH") + fmt.Fprint(w, `{ + "group_id": 123, + "group_name": "Octocat admins", + "updated_at": "2021-01-24T11:31:04-06:00", + "teams": [ + { + "team_id": 1, + "team_name": "team-test" + }, + { + "team_id": 2, + "team_name": "team-test2" + } + ], + "members": [ + { + "member_id": 1, + "member_login": "mona-lisa_eocsaxrs", + "member_name": "Mona Lisa", + "member_email": "mona_lisa@github.com" + }, + { + "member_id": 2, + "member_login": "octo-lisa_eocsaxrs", + "member_name": "Octo Lisa", + "member_email": "octo_lisa@github.com" + } + ] + }`) + }) + + ctx := context.Background() + body := &ExternalGroup{ + GroupID: Int64(123), + } + externalGroup, _, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) + if err != nil { + t.Errorf("Teams.UpdateConnectedExternalGroup returned error: %v", err) + } + + want := &ExternalGroup{ + GroupID: Int64(123), + GroupName: String("Octocat admins"), + UpdatedAt: String("2021-01-24T11:31:04-06:00"), + Teams: []*ExternalGroupTeam{ + { + ID: 1, + Name: "team-test", + }, + { + ID: 2, + Name: "team-test2", + }, + }, + Members: []*ExternalGroupMember{ + { + ID: 1, + Login: "mona-lisa_eocsaxrs", + Name: "Mona Lisa", + Email: "mona_lisa@github.com", + }, + { + ID: 2, + Login: "octo-lisa_eocsaxrs", + Name: "Octo Lisa", + Email: "octo_lisa@github.com", + }, + }, + } + if !cmp.Equal(externalGroup, want) { + t.Errorf("Teams.GetExternalGroup returned %+v, want %+v", externalGroup, want) + } + + const methodName = "UpdateConnectedExternalGroup" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, "", "", body) + return err + }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestTeamsService_UpdateConnectedExternalGroup_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, "PATCH") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + body := &ExternalGroup{ + GroupID: Int64(123), + } + eg, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Teams.UpdateConnectedExternalGroup returned status %d, want %d", got, want) + } + if eg != nil { + t.Errorf("Teams.UpdateConnectedExternalGroup returned %+v, want nil", eg) + } +} + +func TestTeamsService_RemoveConnectedExternalGroup(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, "DELETE") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + _, err := client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") + if err != nil { + t.Errorf("Teams.RemoveConnectedExternalGroup returned error: %v", err) + } + + const methodName = "RemoveConnectedExternalGroup" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Teams.RemoveConnectedExternalGroup(ctx, "", "") + return err + }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") + }) +} + +func TestTeamsService_RemoveConnectedExternalGroup_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, "DELETE") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + resp, err := client.Teams.RemoveConnectedExternalGroup(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.GetExternalGroup returned status %d, want %d", got, want) + } +} From cb5ad819262410463342fbeebda4f44216b3de06 Mon Sep 17 00:00:00 2001 From: Hiroki Ito Date: Mon, 6 Dec 2021 14:54:29 +0900 Subject: [PATCH 2/9] Fix comments Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/teams.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/github/teams.go b/github/teams.go index 917c3e36d48..8bd0eab5a0d 100644 --- a/github/teams.go +++ b/github/teams.go @@ -855,7 +855,7 @@ func (s *TeamsService) CreateOrUpdateIDPGroupConnectionsBySlug(ctx context.Conte return groups, resp, nil } -// ExternalGroupMember represents a member of external group. +// ExternalGroupMember represents a member of an external group. type ExternalGroupMember struct { ID int64 `json:"member_id,omitempty"` Login string `json:"member_login,omitempty"` @@ -863,7 +863,7 @@ type ExternalGroupMember struct { Email string `json:"member_email,omitempty"` } -// ExternalGroupTeam represents a team connected to external group +// ExternalGroupTeam represents a team connected to an external group. type ExternalGroupTeam struct { ID int64 `json:"team_id,omitempty"` Name string `json:"team_name,omitempty"` @@ -883,7 +883,7 @@ type ExternalGroupList struct { Groups []*ExternalGroup `json:"groups"` } -// GetExternalGroup fetches a external group +// GetExternalGroup fetches an external group. // // GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/reference/teams#get-an-external-group func (s *TeamsService) GetExternalGroup(ctx context.Context, org string, groupID int64) (*ExternalGroup, *Response, error) { @@ -910,7 +910,7 @@ type ListExternalGroupsOptions struct { ListOptions } -// ListExternalGroups Lists external groups connected to a team on GitHub +// ListExternalGroups lists external groups connected to a team on GitHub. // // GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/reference/teams#list-external-groups-in-an-organization func (s *TeamsService) ListExternalGroups(ctx context.Context, org string, opts *ListExternalGroupsOptions) (*ExternalGroupList, *Response, error) { @@ -934,7 +934,7 @@ func (s *TeamsService) ListExternalGroups(ctx context.Context, org string, opts return externalGroups, resp, nil } -// UpdateConnectedExternalGroup updates the connection between on external group and a team +// UpdateConnectedExternalGroup updates the connection between an external group and a team. // // GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/reference/teams#update-the-connection-between-an-external-group-and-a-team func (s *TeamsService) UpdateConnectedExternalGroup(ctx context.Context, org, slug string, eg *ExternalGroup) (*ExternalGroup, *Response, error) { @@ -954,7 +954,7 @@ func (s *TeamsService) UpdateConnectedExternalGroup(ctx context.Context, org, sl return externalGroup, resp, nil } -// RemoveConnectedExternalGroup removes the connection between on external group and a team +// RemoveConnectedExternalGroup removes the connection between an external group and a team. // // GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/reference/teams#remove-the-connection-between-an-external-group-and-a-team func (s *TeamsService) RemoveConnectedExternalGroup(ctx context.Context, org, slug string) (*Response, error) { From af776fea58afe90268c6c40b98c0ea3050a83014 Mon Sep 17 00:00:00 2001 From: Hiroki Ito Date: Mon, 6 Dec 2021 14:55:43 +0900 Subject: [PATCH 3/9] Fix property types. Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/teams.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github/teams.go b/github/teams.go index 8bd0eab5a0d..7c256017b11 100644 --- a/github/teams.go +++ b/github/teams.go @@ -873,7 +873,7 @@ type ExternalGroupTeam struct { type ExternalGroup struct { GroupID *int64 `json:"group_id,omitempty"` GroupName *string `json:"group_name,omitempty"` - UpdatedAt *string `json:"updated_at,omitempty"` + UpdatedAt *Timestamp `json:"updated_at,omitempty"` Teams []*ExternalGroupTeam `json:"teams,omitempty"` Members []*ExternalGroupMember `json:"members,omitempty"` } @@ -905,7 +905,7 @@ func (s *TeamsService) GetExternalGroup(ctx context.Context, org string, groupID // ListExternalGroupsOptions specifies the optional parameters to the // TeamsService.ListExternalGroups method. type ListExternalGroupsOptions struct { - DisplayName string `url:"display_name,omitempty"` + DisplayName *string `url:"display_name,omitempty"` ListOptions } From d5f06a2044984388c8447731eba2d552eabcaa3a Mon Sep 17 00:00:00 2001 From: kabesan Date: Mon, 6 Dec 2021 15:07:06 +0900 Subject: [PATCH 4/9] Fix properties. --- github/teams.go | 12 ++++----- github/teams_test.go | 60 +++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/github/teams.go b/github/teams.go index 7c256017b11..06ac1b142b9 100644 --- a/github/teams.go +++ b/github/teams.go @@ -857,16 +857,16 @@ func (s *TeamsService) CreateOrUpdateIDPGroupConnectionsBySlug(ctx context.Conte // ExternalGroupMember represents a member of an external group. type ExternalGroupMember struct { - ID int64 `json:"member_id,omitempty"` - Login string `json:"member_login,omitempty"` - Name string `json:"member_name,omitempty"` - Email string `json:"member_email,omitempty"` + MemberID *int64 `json:"member_id,omitempty"` + MemberLogin *string `json:"member_login,omitempty"` + MemberName *string `json:"member_name,omitempty"` + MemberEmail *string `json:"member_email,omitempty"` } // ExternalGroupTeam represents a team connected to an external group. type ExternalGroupTeam struct { - ID int64 `json:"team_id,omitempty"` - Name string `json:"team_name,omitempty"` + TeamID *int64 `json:"team_id,omitempty"` + TeamName *string `json:"team_name,omitempty"` } // ExternalGroup represents an external group. diff --git a/github/teams_test.go b/github/teams_test.go index 16a92d4113d..caf46430f3f 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -14,6 +14,7 @@ import ( "net/http" "strings" "testing" + "time" "github.com/google/go-cmp/cmp" ) @@ -1789,32 +1790,33 @@ func TestTeamsService_GetExternalGroup(t *testing.T) { t.Errorf("Teams.GetExternalGroup returned error: %v", err) } + updatedAt, _ := time.Parse(time.RFC3339, "2021-01-24T11:31:04-06:00") want := &ExternalGroup{ GroupID: Int64(123), GroupName: String("Octocat admins"), - UpdatedAt: String("2021-01-24T11:31:04-06:00"), + UpdatedAt: &Timestamp{Time: updatedAt}, Teams: []*ExternalGroupTeam{ { - ID: 1, - Name: "team-test", + TeamID: Int64(1), + TeamName: String("team-test"), }, { - ID: 2, - Name: "team-test2", + TeamID: Int64(2), + TeamName: String("team-test2"), }, }, Members: []*ExternalGroupMember{ { - ID: 1, - Login: "mona-lisa_eocsaxrs", - Name: "Mona Lisa", - Email: "mona_lisa@github.com", + MemberID: Int64(1), + MemberLogin: String("mona-lisa_eocsaxrs"), + MemberName: String("Mona Lisa"), + MemberEmail: String("mona_lisa@github.com"), }, { - ID: 2, - Login: "octo-lisa_eocsaxrs", - Name: "Octo Lisa", - Email: "octo_lisa@github.com", + MemberID: Int64(2), + MemberLogin: String("octo-lisa_eocsaxrs"), + MemberName: String("Octo Lisa"), + MemberEmail: String("octo_lisa@github.com"), }, }, } @@ -1877,19 +1879,20 @@ func TestTeamsService_ListExternalGroups(t *testing.T) { ctx := context.Background() opts := &ListExternalGroupsOptions{ - DisplayName: "Octocat", + DisplayName: String("Octocat"), } list, _, err := client.Teams.ListExternalGroups(ctx, "o", opts) if err != nil { t.Errorf("Teams.ListExternalGroups returned error: %v", err) } + updatedAt, _ := time.Parse(time.RFC3339, "2021-01-24T11:31:04-06:00") want := &ExternalGroupList{ Groups: []*ExternalGroup{ { GroupID: Int64(123), GroupName: String("Octocat admins"), - UpdatedAt: String("2021-01-24T11:31:04-06:00"), + UpdatedAt: &Timestamp{Time: updatedAt}, }, }, } @@ -1979,32 +1982,33 @@ func TestTeamsService_UpdateConnectedExternalGroup(t *testing.T) { t.Errorf("Teams.UpdateConnectedExternalGroup returned error: %v", err) } + updatedAt, _ := time.Parse(time.RFC3339, "2021-01-24T11:31:04-06:00") want := &ExternalGroup{ GroupID: Int64(123), GroupName: String("Octocat admins"), - UpdatedAt: String("2021-01-24T11:31:04-06:00"), + UpdatedAt: &Timestamp{Time: updatedAt}, Teams: []*ExternalGroupTeam{ { - ID: 1, - Name: "team-test", + TeamID: Int64(1), + TeamName: String("team-test"), }, { - ID: 2, - Name: "team-test2", + TeamID: Int64(2), + TeamName: String("team-test2"), }, }, Members: []*ExternalGroupMember{ { - ID: 1, - Login: "mona-lisa_eocsaxrs", - Name: "Mona Lisa", - Email: "mona_lisa@github.com", + MemberID: Int64(1), + MemberLogin: String("mona-lisa_eocsaxrs"), + MemberName: String("Mona Lisa"), + MemberEmail: String("mona_lisa@github.com"), }, { - ID: 2, - Login: "octo-lisa_eocsaxrs", - Name: "Octo Lisa", - Email: "octo_lisa@github.com", + MemberID: Int64(2), + MemberLogin: String("octo-lisa_eocsaxrs"), + MemberName: String("Octo Lisa"), + MemberEmail: String("octo_lisa@github.com"), }, }, } From d2191d9e73963dace12078161216e10b214de778 Mon Sep 17 00:00:00 2001 From: kabesan Date: Mon, 6 Dec 2021 16:13:21 +0900 Subject: [PATCH 5/9] Add blank lines to tests. --- github/teams_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/github/teams_test.go b/github/teams_test.go index caf46430f3f..e319276f975 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -1829,6 +1829,7 @@ func TestTeamsService_GetExternalGroup(t *testing.T) { _, _, err = client.Teams.GetExternalGroup(ctx, "", -1) return err }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { got, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) if got != nil { @@ -1905,6 +1906,7 @@ func TestTeamsService_ListExternalGroups(t *testing.T) { _, _, err = client.Teams.ListExternalGroups(ctx, "", nil) return err }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { got, resp, err := client.Teams.ListExternalGroups(ctx, "o", nil) if got != nil { @@ -2021,6 +2023,7 @@ func TestTeamsService_UpdateConnectedExternalGroup(t *testing.T) { _, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, "", "", body) return err }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { got, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) if got != nil { @@ -2075,6 +2078,7 @@ func TestTeamsService_RemoveConnectedExternalGroup(t *testing.T) { _, err = client.Teams.RemoveConnectedExternalGroup(ctx, "", "") return err }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { return client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") }) From 7bf95f8afe59489b986162c44a3bbcb2ae5737ff Mon Sep 17 00:00:00 2001 From: kabesan Date: Mon, 6 Dec 2021 16:19:00 +0900 Subject: [PATCH 6/9] Run go generate github.com/google/go-github/... --- github/github-accessors.go | 80 +++++++++++++++++++++++++ github/github-accessors_test.go | 100 ++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index 7bd870167e6..00051f8ce21 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -4372,6 +4372,78 @@ func (e *Event) GetType() string { return *e.Type } +// GetGroupID returns the GroupID field if it's non-nil, zero value otherwise. +func (e *ExternalGroup) GetGroupID() int64 { + if e == nil || e.GroupID == nil { + return 0 + } + return *e.GroupID +} + +// GetGroupName returns the GroupName field if it's non-nil, zero value otherwise. +func (e *ExternalGroup) GetGroupName() string { + if e == nil || e.GroupName == nil { + return "" + } + return *e.GroupName +} + +// GetUpdatedAt returns the UpdatedAt field if it's non-nil, zero value otherwise. +func (e *ExternalGroup) GetUpdatedAt() Timestamp { + if e == nil || e.UpdatedAt == nil { + return Timestamp{} + } + return *e.UpdatedAt +} + +// GetMemberEmail returns the MemberEmail field if it's non-nil, zero value otherwise. +func (e *ExternalGroupMember) GetMemberEmail() string { + if e == nil || e.MemberEmail == nil { + return "" + } + return *e.MemberEmail +} + +// GetMemberID returns the MemberID field if it's non-nil, zero value otherwise. +func (e *ExternalGroupMember) GetMemberID() int64 { + if e == nil || e.MemberID == nil { + return 0 + } + return *e.MemberID +} + +// GetMemberLogin returns the MemberLogin field if it's non-nil, zero value otherwise. +func (e *ExternalGroupMember) GetMemberLogin() string { + if e == nil || e.MemberLogin == nil { + return "" + } + return *e.MemberLogin +} + +// GetMemberName returns the MemberName field if it's non-nil, zero value otherwise. +func (e *ExternalGroupMember) GetMemberName() string { + if e == nil || e.MemberName == nil { + return "" + } + return *e.MemberName +} + +// GetTeamID returns the TeamID field if it's non-nil, zero value otherwise. +func (e *ExternalGroupTeam) GetTeamID() int64 { + if e == nil || e.TeamID == nil { + return 0 + } + return *e.TeamID +} + +// GetTeamName returns the TeamName field if it's non-nil, zero value otherwise. +func (e *ExternalGroupTeam) GetTeamName() string { + if e == nil || e.TeamName == nil { + return "" + } + return *e.TeamName +} + // GetHRef returns the HRef field if it's non-nil, zero value otherwise. func (f *FeedLink) GetHRef() string { if f == nil || f.HRef == nil { @@ -7532,6 +7604,14 @@ func (l *ListCollaboratorOptions) GetAffiliation() string { return *l.Affiliation } +// GetDisplayName returns the DisplayName field if it's non-nil, zero value otherwise. +func (l *ListExternalGroupsOptions) GetDisplayName() string { + if l == nil || l.DisplayName == nil { + return "" + } + return *l.DisplayName +} + // GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. func (l *ListRepositories) GetTotalCount() int { if l == nil || l.TotalCount == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index bbdff0ad3f5..710e64889c7 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -5121,6 +5121,96 @@ func TestEvent_GetType(tt *testing.T) { e.GetType() } +func TestExternalGroup_GetGroupID(tt *testing.T) { + var zeroValue int64 + e := &ExternalGroup{GroupID: &zeroValue} + e.GetGroupID() + e = &ExternalGroup{} + e.GetGroupID() + e = nil + e.GetGroupID() +} + +func TestExternalGroup_GetGroupName(tt *testing.T) { + var zeroValue string + e := &ExternalGroup{GroupName: &zeroValue} + e.GetGroupName() + e = &ExternalGroup{} + e.GetGroupName() + e = nil + e.GetGroupName() +} + +func TestExternalGroup_GetUpdatedAt(tt *testing.T) { + var zeroValue Timestamp + e := &ExternalGroup{UpdatedAt: &zeroValue} + e.GetUpdatedAt() + e = &ExternalGroup{} + e.GetUpdatedAt() + e = nil + e.GetUpdatedAt() +} + +func TestExternalGroupMember_GetMemberEmail(tt *testing.T) { + var zeroValue string + e := &ExternalGroupMember{MemberEmail: &zeroValue} + e.GetMemberEmail() + e = &ExternalGroupMember{} + e.GetMemberEmail() + e = nil + e.GetMemberEmail() +} + +func TestExternalGroupMember_GetMemberID(tt *testing.T) { + var zeroValue int64 + e := &ExternalGroupMember{MemberID: &zeroValue} + e.GetMemberID() + e = &ExternalGroupMember{} + e.GetMemberID() + e = nil + e.GetMemberID() +} + +func TestExternalGroupMember_GetMemberLogin(tt *testing.T) { + var zeroValue string + e := &ExternalGroupMember{MemberLogin: &zeroValue} + e.GetMemberLogin() + e = &ExternalGroupMember{} + e.GetMemberLogin() + e = nil + e.GetMemberLogin() +} + +func TestExternalGroupMember_GetMemberName(tt *testing.T) { + var zeroValue string + e := &ExternalGroupMember{MemberName: &zeroValue} + e.GetMemberName() + e = &ExternalGroupMember{} + e.GetMemberName() + e = nil + e.GetMemberName() +} + +func TestExternalGroupTeam_GetTeamID(tt *testing.T) { + var zeroValue int64 + e := &ExternalGroupTeam{TeamID: &zeroValue} + e.GetTeamID() + e = &ExternalGroupTeam{} + e.GetTeamID() + e = nil + e.GetTeamID() +} + +func TestExternalGroupTeam_GetTeamName(tt *testing.T) { + var zeroValue string + e := &ExternalGroupTeam{TeamName: &zeroValue} + e.GetTeamName() + e = &ExternalGroupTeam{} + e.GetTeamName() + e = nil + e.GetTeamName() +} + func TestFeedLink_GetHRef(tt *testing.T) { var zeroValue string f := &FeedLink{HRef: &zeroValue} @@ -8840,6 +8930,16 @@ func TestListCollaboratorOptions_GetAffiliation(tt *testing.T) { l.GetAffiliation() } +func TestListExternalGroupsOptions_GetDisplayName(tt *testing.T) { + var zeroValue string + l := &ListExternalGroupsOptions{DisplayName: &zeroValue} + l.GetDisplayName() + l = &ListExternalGroupsOptions{} + l.GetDisplayName() + l = nil + l.GetDisplayName() +} + func TestListRepositories_GetTotalCount(tt *testing.T) { var zeroValue int l := &ListRepositories{TotalCount: &zeroValue} From 931139924caa46e0a2a6ba51883e7d40351e03e8 Mon Sep 17 00:00:00 2001 From: kabesan Date: Mon, 6 Dec 2021 23:52:12 +0900 Subject: [PATCH 7/9] Use referenceTime. --- github/teams_test.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/github/teams_test.go b/github/teams_test.go index e319276f975..4c4877f784c 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -14,7 +14,6 @@ import ( "net/http" "strings" "testing" - "time" "github.com/google/go-cmp/cmp" ) @@ -1756,7 +1755,7 @@ func TestTeamsService_GetExternalGroup(t *testing.T) { fmt.Fprint(w, `{ "group_id": 123, "group_name": "Octocat admins", - "updated_at": "2021-01-24T11:31:04-06:00", + "updated_at": "2006-01-02T15:04:05Z", "teams": [ { "team_id": 1, @@ -1790,11 +1789,10 @@ func TestTeamsService_GetExternalGroup(t *testing.T) { t.Errorf("Teams.GetExternalGroup returned error: %v", err) } - updatedAt, _ := time.Parse(time.RFC3339, "2021-01-24T11:31:04-06:00") want := &ExternalGroup{ GroupID: Int64(123), GroupName: String("Octocat admins"), - UpdatedAt: &Timestamp{Time: updatedAt}, + UpdatedAt: &Timestamp{Time: referenceTime}, Teams: []*ExternalGroupTeam{ { TeamID: Int64(1), @@ -1872,7 +1870,7 @@ func TestTeamsService_ListExternalGroups(t *testing.T) { { "group_id": 123, "group_name": "Octocat admins", - "updated_at": "2021-01-24T11:31:04-06:00" + "updated_at": "2006-01-02T15:04:05Z" } ] }`) @@ -1887,13 +1885,12 @@ func TestTeamsService_ListExternalGroups(t *testing.T) { t.Errorf("Teams.ListExternalGroups returned error: %v", err) } - updatedAt, _ := time.Parse(time.RFC3339, "2021-01-24T11:31:04-06:00") want := &ExternalGroupList{ Groups: []*ExternalGroup{ { GroupID: Int64(123), GroupName: String("Octocat admins"), - UpdatedAt: &Timestamp{Time: updatedAt}, + UpdatedAt: &Timestamp{Time: referenceTime}, }, }, } @@ -1947,7 +1944,7 @@ func TestTeamsService_UpdateConnectedExternalGroup(t *testing.T) { fmt.Fprint(w, `{ "group_id": 123, "group_name": "Octocat admins", - "updated_at": "2021-01-24T11:31:04-06:00", + "updated_at": "2006-01-02T15:04:05Z", "teams": [ { "team_id": 1, @@ -1984,11 +1981,10 @@ func TestTeamsService_UpdateConnectedExternalGroup(t *testing.T) { t.Errorf("Teams.UpdateConnectedExternalGroup returned error: %v", err) } - updatedAt, _ := time.Parse(time.RFC3339, "2021-01-24T11:31:04-06:00") want := &ExternalGroup{ GroupID: Int64(123), GroupName: String("Octocat admins"), - UpdatedAt: &Timestamp{Time: updatedAt}, + UpdatedAt: &Timestamp{Time: referenceTime}, Teams: []*ExternalGroupTeam{ { TeamID: Int64(1), From 62b5351ea783a71b465d7fc77ac75570dea68f7c Mon Sep 17 00:00:00 2001 From: kabesan Date: Mon, 6 Dec 2021 23:52:59 +0900 Subject: [PATCH 8/9] Run go fmt ./github/teams.go --- github/teams.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/teams.go b/github/teams.go index 06ac1b142b9..71cbbab6a9a 100644 --- a/github/teams.go +++ b/github/teams.go @@ -873,7 +873,7 @@ type ExternalGroupTeam struct { type ExternalGroup struct { GroupID *int64 `json:"group_id,omitempty"` GroupName *string `json:"group_name,omitempty"` - UpdatedAt *Timestamp `json:"updated_at,omitempty"` + UpdatedAt *Timestamp `json:"updated_at,omitempty"` Teams []*ExternalGroupTeam `json:"teams,omitempty"` Members []*ExternalGroupMember `json:"members,omitempty"` } From c0a7d7d5161b78184c0f330fa6e89b615dde816c Mon Sep 17 00:00:00 2001 From: kabesan Date: Wed, 8 Dec 2021 00:51:23 +0900 Subject: [PATCH 9/9] Fix func in testBadOptions --- github/teams_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/github/teams_test.go b/github/teams_test.go index 4c4877f784c..83f559beff8 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -1824,7 +1824,7 @@ func TestTeamsService_GetExternalGroup(t *testing.T) { const methodName = "GetExternalGroup" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Teams.GetExternalGroup(ctx, "", -1) + _, _, err = client.Teams.GetExternalGroup(ctx, "\n", -1) return err }) @@ -1900,12 +1900,12 @@ func TestTeamsService_ListExternalGroups(t *testing.T) { const methodName = "ListExternalGroups" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Teams.ListExternalGroups(ctx, "", nil) + _, _, err = client.Teams.ListExternalGroups(ctx, "\n", opts) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Teams.ListExternalGroups(ctx, "o", nil) + got, resp, err := client.Teams.ListExternalGroups(ctx, "o", opts) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -2016,7 +2016,7 @@ func TestTeamsService_UpdateConnectedExternalGroup(t *testing.T) { const methodName = "UpdateConnectedExternalGroup" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, "", "", body) + _, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, "\n", "\n", body) return err }) @@ -2071,7 +2071,7 @@ func TestTeamsService_RemoveConnectedExternalGroup(t *testing.T) { const methodName = "RemoveConnectedExternalGroup" testBadOptions(t, methodName, func() (err error) { - _, err = client.Teams.RemoveConnectedExternalGroup(ctx, "", "") + _, err = client.Teams.RemoveConnectedExternalGroup(ctx, "\n", "\n") return err })