From 040a46ae7e5b7d0891ccd189b1a1428a62b87078 Mon Sep 17 00:00:00 2001 From: Shteryana Shopova Date: Sat, 20 Jul 2019 23:07:01 +0300 Subject: [PATCH] Add team "all",i.e everyone in the organization --- access/fetch-keys/main.go | 120 ++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 45 deletions(-) diff --git a/access/fetch-keys/main.go b/access/fetch-keys/main.go index dd9b62e..b4a6d33 100644 --- a/access/fetch-keys/main.go +++ b/access/fetch-keys/main.go @@ -27,7 +27,7 @@ func main() { keysDir := parser.String("d", "directory", &argparse.Options{Required: false, Help: "Path where to store the key files", Default: "./"}) quiet := parser.Flag("q", "quiet", &argparse.Options{Required: false, Help: "Skip output to stdout", Default: false}) ghOrganization := parser.String("o", "org", &argparse.Options{Required: false, Help: "Github Organization name", Default: "OpenFest"}) - ghTeam := parser.String("t", "team", &argparse.Options{Required: false, Help: "Github Team name", Default: "NOC"}) + ghTeam := parser.String("t", "team", &argparse.Options{Required: false, Help: "Github Team name, 'all' for all members of the organization", Default: "NOC"}) // Parse input err := parser.Parse(os.Args) @@ -107,64 +107,94 @@ func main() { func fetchUsers(client *github.Client, org *string, team *string) (teamMembers []*string) { var targetTeam *github.Team - for nextPage := 0; ; { - // list all teams for the specified org - opt := &github.ListOptions{nextPage, 50} - teams, rsp, err := client.Teams.ListTeams(context.Background(), *org, opt) + if team == nil || *team == "all" { + for nextPage := 0; ; { + // list all members for the given organization's team + opt := &github.ListMembersOptions{ + PublicOnly: false, + ListOptions: github.ListOptions{nextPage, 50}, + } - if err != nil { - fmt.Println("client.ListTeams error: ", err) - os.Exit(-1) - } + users, rsp, err := client.Organizations.ListMembers(context.Background(), *org, opt) - if rsp == nil { - fmt.Println("client.ListTeams returned empty response: ", err) - } + if err != nil { + fmt.Println("client.Organizations.ListMembers ", err) + os.Exit(-1) + } - for _, ghTeam := range teams { - if *ghTeam.Name == *team { - targetTeam = ghTeam + if rsp == nil { + fmt.Println("client.Organizations.ListMembers: ", err) + } + + for _, user := range users { + teamMembers = append(teamMembers, user.Login) + } + + if rsp.NextPage == 0 || nextPage == rsp.NextPage { break } + nextPage = rsp.NextPage + } + } else { + for nextPage := 0; ; { + // list all teams for the specified org + opt := &github.ListOptions{nextPage, 50} + teams, rsp, err := client.Teams.ListTeams(context.Background(), *org, opt) + + if err != nil { + fmt.Println("client.ListTeams error: ", err) + os.Exit(-1) + } + + if rsp == nil { + fmt.Println("client.ListTeams returned empty response: ", err) + } + + for _, ghTeam := range teams { + if *ghTeam.Name == *team { + targetTeam = ghTeam + break + } + } + + if rsp.NextPage == 0 || nextPage == rsp.NextPage { + break + } + nextPage = rsp.NextPage } - if rsp.NextPage == 0 || nextPage == rsp.NextPage { - break - } - nextPage = rsp.NextPage - } - - if targetTeam == nil { - fmt.Println(*team , " team not found in ", *org) - os.Exit(2) - } - - for nextPage := 0; ; { - // list all members for the given organization's team - opt := &github.TeamListTeamMembersOptions{ - Role: "all", - ListOptions: github.ListOptions{nextPage, 50}, + if targetTeam == nil { + fmt.Println(*team , " team not found in ", *org) + os.Exit(2) } - users, rsp, err := client.Teams.ListTeamMembers(context.Background(), *targetTeam.ID, opt) + for nextPage := 0; ; { + // list all members for the given organization's team + opt := &github.TeamListTeamMembersOptions{ + Role: "all", + ListOptions: github.ListOptions{nextPage, 50}, + } - if err != nil { - fmt.Println("client.Teams.ListTeamMembers ", err) - os.Exit(-1) - } + users, rsp, err := client.Teams.ListTeamMembers(context.Background(), *targetTeam.ID, opt) - if rsp == nil { - fmt.Println("client.Teams.ListTeamMembers: ", err) - } + if err != nil { + fmt.Println("client.Teams.ListTeamMembers ", err) + os.Exit(-1) + } - for _, user := range users { - teamMembers = append(teamMembers, user.Login) - } + if rsp == nil { + fmt.Println("client.Teams.ListTeamMembers: ", err) + } - if rsp.NextPage == 0 || nextPage == rsp.NextPage { - break + for _, user := range users { + teamMembers = append(teamMembers, user.Login) + } + + if rsp.NextPage == 0 || nextPage == rsp.NextPage { + break + } + nextPage = rsp.NextPage } - nextPage = rsp.NextPage } return teamMembers