internal/directory: use both id and name for group (#1086)

Fixes #1085
This commit is contained in:
Cuong Manh Le 2020-07-17 00:15:11 +07:00 committed by GitHub
parent 96424dac0f
commit ee1f9093ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 59 additions and 49 deletions

View file

@ -11,6 +11,7 @@ import (
"net/http"
"net/url"
"sort"
"strconv"
"github.com/rs/zerolog"
"github.com/tomnomnom/linkheader"
@ -103,14 +104,14 @@ func (p *Provider) UserGroups(ctx context.Context) ([]*directory.User, error) {
return nil, err
}
for _, teamSlug := range teamSlugs {
for teamSlug, teamID := range teamSlugs {
userLogins, err := p.listTeamMembers(ctx, orgSlug, teamSlug)
if err != nil {
return nil, err
}
for _, userLogin := range userLogins {
userLoginToGroups[userLogin] = append(userLoginToGroups[userLogin], teamSlug)
userLoginToGroups[userLogin] = append(userLoginToGroups[userLogin], teamSlug, strconv.Itoa(teamID))
}
}
}
@ -154,13 +155,15 @@ func (p *Provider) listOrgs(ctx context.Context) (orgSlugs []string, err error)
return orgSlugs, nil
}
func (p *Provider) listTeams(ctx context.Context, orgSlug string) (teamSlugs []string, err error) {
func (p *Provider) listTeams(ctx context.Context, orgSlug string) (map[string]int, error) {
nextURL := p.cfg.url.ResolveReference(&url.URL{
Path: fmt.Sprintf("/orgs/%s/teams", orgSlug),
}).String()
teamSlugs := make(map[string]int)
for nextURL != "" {
var results []struct {
ID int `json:"id"`
Slug string `json:"slug"`
}
hdrs, err := p.api(ctx, "GET", nextURL, nil, &results)
@ -169,7 +172,7 @@ func (p *Provider) listTeams(ctx context.Context, orgSlug string) (teamSlugs []s
}
for _, result := range results {
teamSlugs = append(teamSlugs, result.Slug)
teamSlugs[result.Slug] = result.ID
}
nextURL = getNextLink(hdrs)

View file

@ -38,12 +38,12 @@ func newMockAPI(t *testing.T, srv *httptest.Server) http.Handler {
r.Get("/orgs/{org_id}/teams", func(w http.ResponseWriter, r *http.Request) {
teams := map[string][]M{
"org1": {
{"slug": "team1"},
{"slug": "team2"},
{"slug": "team1", "id": 1},
{"slug": "team2", "id": 2},
},
"org2": {
{"slug": "team3"},
{"slug": "team4"},
{"slug": "team3", "id": 3},
{"slug": "team4", "id": 4},
},
}
orgID := chi.URLParam(r, "org_id")
@ -96,10 +96,10 @@ func Test(t *testing.T) {
users, err := p.UserGroups(context.Background())
assert.NoError(t, err)
testutil.AssertProtoJSONEqual(t, `[
{ "id": "github/user1", "groups": ["team1", "team2", "team3"] },
{ "id": "github/user2", "groups": ["team1", "team3"] },
{ "id": "github/user3", "groups": ["team3"] },
{ "id": "github/user4", "groups": ["team4"] }
{ "id": "github/user1", "groups": ["1", "2", "3", "team1", "team2", "team3"] },
{ "id": "github/user2", "groups": ["1", "3", "team1", "team3"] },
{ "id": "github/user3", "groups": ["3", "team3"] },
{ "id": "github/user4", "groups": ["4", "team4"] }
]`, users)
}