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

Capture parent team information in github connector #1828

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 50 additions & 6 deletions connector/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func (c *githubConnector) userOrgTeams(ctx context.Context, client *http.Client)
for {
// https://developer.github.com/v3/orgs/teams/#list-user-teams
var (
teams []team
teams []teamWithParent
err error
)
if apiURL, err = get(ctx, client, apiURL, &teams); err != nil {
Expand All @@ -460,6 +460,20 @@ func (c *githubConnector) userOrgTeams(ctx context.Context, client *http.Client)
groups[t.Org.Login] = append(groups[t.Org.Login], c.teamGroupClaims(t)...)
}

for _, t := range teams {
keys := make(map[string]bool)
uniqueGroups := []string{}

for _, group := range groups[t.Org.Login] {
if _, exists := keys[group]; !exists {
keys[group] = true
uniqueGroups = append(uniqueGroups, group)
}

groups[t.Org.Login] = uniqueGroups
}
}

if apiURL == "" {
break
}
Expand Down Expand Up @@ -656,6 +670,11 @@ type team struct {
Slug string `json:"slug"`
}

type teamWithParent struct {
team
Parent *team `json:"parent"`
}

type org struct {
Login string `json:"login"`
}
Expand All @@ -669,7 +688,7 @@ func (c *githubConnector) teamsForOrg(ctx context.Context, client *http.Client,
for {
// https://developer.github.com/v3/orgs/teams/#list-user-teams
var (
teams []team
teams []teamWithParent
err error
)
if apiURL, err = get(ctx, client, apiURL, &teams); err != nil {
Expand All @@ -682,6 +701,18 @@ func (c *githubConnector) teamsForOrg(ctx context.Context, client *http.Client,
}
}

keys := make(map[string]bool)
uniqueGroups := []string{}

for _, group := range groups {
if _, exists := keys[group]; !exists {
keys[group] = true
uniqueGroups = append(uniqueGroups, group)
}
}

groups = uniqueGroups

if apiURL == "" {
break
}
Expand All @@ -693,13 +724,26 @@ func (c *githubConnector) teamsForOrg(ctx context.Context, client *http.Client,
// teamGroupClaims returns team slug if 'teamNameField' option is set to
// 'slug', returns the slug *and* name if set to 'both', otherwise returns team
// name.
func (c *githubConnector) teamGroupClaims(t team) []string {
func (c *githubConnector) teamGroupClaims(t teamWithParent) []string {
var groups []string

switch c.teamNameField {
case "both":
return []string{t.Name, t.Slug}
if t.Parent != nil {
groups = append(groups, t.Parent.Name, t.Parent.Slug)
}
groups = append(groups, t.Name, t.Slug)
case "slug":
return []string{t.Slug}
if t.Parent != nil {
groups = append(groups, t.Parent.Slug)
}
groups = append(groups, t.Slug)
default:
return []string{t.Name}
if t.Parent != nil {
groups = append(groups, t.Parent.Name)
}
groups = append(groups, t.Name)
}

return groups
}
23 changes: 12 additions & 11 deletions connector/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ func TestUserGroups(t *testing.T) {
},
"/user/orgs?since=2": {data: []org{{Login: "org-3"}}},
"/user/teams": {
data: []team{
{Name: "team-1", Org: org{Login: "org-1"}},
{Name: "team-2", Org: org{Login: "org-1"}},
data: []teamWithParent{
{team: team{Name: "team-1", Org: org{Login: "org-1"}}, Parent: &team{Name: "team-0", Org: org{Login: "org-1"}}},
{team: team{Name: "team-2", Org: org{Login: "org-1"}}, Parent: &team{Name: "team-0", Org: org{Login: "org-1"}}},
},
nextLink: "/user/teams?since=2",
lastLink: "/user/teams?since=2",
},
"/user/teams?since=2": {
data: []team{
{Name: "team-3", Org: org{Login: "org-1"}},
{Name: "team-4", Org: org{Login: "org-2"}},
data: []teamWithParent{
{team: team{Name: "team-3", Org: org{Login: "org-1"}}, Parent: nil},
{team: team{Name: "team-4", Org: org{Login: "org-2"}}, Parent: nil},
},
nextLink: "/user/teams?since=2",
lastLink: "/user/teams?since=2",
Expand All @@ -54,6 +54,7 @@ func TestUserGroups(t *testing.T) {
expectNil(t, err)
expectEquals(t, groups, []string{
"org-1",
"org-1:team-0",
"org-1:team-1",
"org-1:team-2",
"org-1:team-3",
Expand All @@ -66,7 +67,7 @@ func TestUserGroups(t *testing.T) {
func TestUserGroupsWithoutOrgs(t *testing.T) {
s := newTestServer(map[string]testResponse{
"/user/orgs": {data: []org{}},
"/user/teams": {data: []team{}},
"/user/teams": {data: []teamWithParent{}},
})
defer s.Close()

Expand All @@ -83,8 +84,8 @@ func TestUserGroupsWithTeamNameFieldConfig(t *testing.T) {
data: []org{{Login: "org-1"}},
},
"/user/teams": {
data: []team{
{Name: "Team 1", Slug: "team-1", Org: org{Login: "org-1"}},
data: []teamWithParent{
{team: team{Name: "Team 1", Slug: "team-1", Org: org{Login: "org-1"}}, Parent: nil},
},
},
})
Expand All @@ -106,8 +107,8 @@ func TestUserGroupsWithTeamNameAndSlugFieldConfig(t *testing.T) {
data: []org{{Login: "org-1"}},
},
"/user/teams": {
data: []team{
{Name: "Team 1", Slug: "team-1", Org: org{Login: "org-1"}},
data: []teamWithParent{
{team: team{Name: "Team 1", Slug: "team-1", Org: org{Login: "org-1"}}, Parent: nil},
},
},
})
Expand Down