Skip to content

Commit

Permalink
Add GetTeamBySlug methods in teamsService (#1171)
Browse files Browse the repository at this point in the history
Fixes #1172.
  • Loading branch information
adrienzieba authored and gmlewis committed May 30, 2019
1 parent 6264c79 commit 8354c07
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
19 changes: 19 additions & 0 deletions github/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ func (s *TeamsService) GetTeam(ctx context.Context, team int64) (*Team, *Respons
return t, resp, nil
}

// GetTeamBySlug fetches a team by slug.
//
// GitHub API docs: https://developer.github.com/v3/teams/#get-team-by-name
func (s *TeamsService) GetTeamBySlug(ctx context.Context, org, slug string) (*Team, *Response, error) {
u := fmt.Sprintf("orgs/%v/teams/%v", org, slug)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

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

return t, resp, nil
}

// NewTeam represents a team to be created or modified.
type NewTeam struct {
Name string `json:"name"` // Name of the team. (Required.)
Expand Down
49 changes: 49 additions & 0 deletions github/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,55 @@ func TestTeamsService_GetTeam_nestedTeams(t *testing.T) {
}
}

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

mux.HandleFunc("/orgs/o/teams/s", 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", "parent":null}`)
})

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

want := &Team{ID: Int64(1), Name: String("n"), Description: String("d"), URL: String("u"), Slug: String("s"), Permission: String("p"), LDAPDN: String("cn=n,ou=groups,dc=example,dc=com")}
if !reflect.DeepEqual(team, want) {
t.Errorf("Teams.GetTeamBySlug returned %+v, want %+v", team, want)
}
}

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

_, _, err := client.Teams.GetTeamBySlug(context.Background(), "%", "s")
testURLParseError(t, err)
}

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

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

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

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

0 comments on commit 8354c07

Please sign in to comment.