From ce3cd53a11081782f907ab6a3346f81357a20326 Mon Sep 17 00:00:00 2001
From: Alexander Matyushentsev <Alexander_Matyushentsev@intuit.com>
Date: Thu, 15 Nov 2018 09:23:57 -0800
Subject: [PATCH] Bug fix: take into account 'teamNameField' settings while
 fetching all user groups

---
 connector/github/github.go      | 23 ++++++++++++++---------
 connector/github/github_test.go | 22 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/connector/github/github.go b/connector/github/github.go
index d28cb096..5e9a302b 100644
--- a/connector/github/github.go
+++ b/connector/github/github.go
@@ -445,7 +445,7 @@ func (c *githubConnector) userOrgTeams(ctx context.Context, client *http.Client)
 		}
 
 		for _, t := range teams {
-			groups[t.Org.Login] = append(groups[t.Org.Login], t.Name)
+			groups[t.Org.Login] = append(groups[t.Org.Login], c.teamGroupClaim(t))
 		}
 
 		if apiURL == "" {
@@ -680,14 +680,9 @@ func (c *githubConnector) teamsForOrg(ctx context.Context, client *http.Client,
 			return nil, fmt.Errorf("github: get teams: %v", err)
 		}
 
-		for _, team := range teams {
-			if team.Org.Login == orgName {
-				switch c.teamNameField {
-				case "name", "":
-					groups = append(groups, team.Name)
-				case "slug":
-					groups = append(groups, team.Slug)
-				}
+		for _, t := range teams {
+			if t.Org.Login == orgName {
+				groups = append(groups, c.teamGroupClaim(t))
 			}
 		}
 
@@ -698,3 +693,13 @@ func (c *githubConnector) teamsForOrg(ctx context.Context, client *http.Client,
 
 	return groups, nil
 }
+
+// teamGroupClaim returns team slag if 'teamNameField; option is set to 'slug' otherwise returns team name.
+func (c *githubConnector) teamGroupClaim(t team) string {
+	switch c.teamNameField {
+	case "slug":
+		return t.Slug
+	default:
+		return t.Name
+	}
+}
diff --git a/connector/github/github_test.go b/connector/github/github_test.go
index 44519a6e..e871c607 100644
--- a/connector/github/github_test.go
+++ b/connector/github/github_test.go
@@ -77,6 +77,28 @@ func TestUserGroupsWithoutOrgs(t *testing.T) {
 
 }
 
+func TestUserGroupsWithTeamNameFieldConfig(t *testing.T) {
+	s := newTestServer(map[string]testResponse{
+		"/user/orgs": {
+			data: []org{{Login: "org-1"}},
+		},
+		"/user/teams": {
+			data: []team{
+				{Name: "Team 1", Slug: "team-1", Org: org{Login: "org-1"}},
+			},
+		},
+	})
+	defer s.Close()
+
+	c := githubConnector{apiURL: s.URL, teamNameField: "slug"}
+	groups, err := c.userGroups(context.Background(), newClient())
+
+	expectNil(t, err)
+	expectEquals(t, groups, []string{
+		"org-1:team-1",
+	})
+}
+
 func TestUsernameIncludedInFederatedIdentity(t *testing.T) {
 
 	s := newTestServer(map[string]testResponse{
-- 
GitLab