@@ -407,30 +407,43 @@ func (g *GithubClient) MarkdownPullLink(pull models.PullRequest) (string, error)
407
407
}
408
408
409
409
// GetTeamNamesForUser returns the names of the teams or groups that the user belongs to (in the organization the repository belongs to).
410
- // https://developer .github.com/v3/teams/members/#get-team-membership
410
+ // https://docs .github.com/en/graphql/reference/objects#organization
411
411
func (g * GithubClient ) GetTeamNamesForUser (repo models.Repo , user models.User ) ([]string , error ) {
412
+ orgName := repo .Owner
413
+ variables := map [string ]interface {}{
414
+ "orgName" : githubv4 .String (orgName ),
415
+ "userLogins" : []githubv4.String {githubv4 .String (user .Username )},
416
+ "teamCursor" : (* githubv4 .String )(nil ),
417
+ }
418
+ var q struct {
419
+ Organization struct {
420
+ Teams struct {
421
+ Edges []struct {
422
+ Node struct {
423
+ Name string
424
+ }
425
+ }
426
+ PageInfo struct {
427
+ EndCursor githubv4.String
428
+ HasNextPage bool
429
+ }
430
+ } `graphql:"teams(first:100, after: $teamCursor, userLogins: $userLogins)"`
431
+ } `graphql:"organization(login: $orgName)"`
432
+ }
412
433
var teamNames []string
413
- opts := & github.ListOptions {}
414
- org := repo .Owner
434
+ ctx := context .Background ()
415
435
for {
416
- teams , resp , err := g .client . Teams . ListTeams ( g . ctx , org , opts )
436
+ err := g .v4MutateClient . Query ( ctx , & q , variables )
417
437
if err != nil {
418
- return nil , errors . Wrap ( err , "retrieving GitHub teams" )
438
+ return nil , err
419
439
}
420
- for _ , t := range teams {
421
- membership , _ , err := g .client .Teams .GetTeamMembershipBySlug (g .ctx , org , * t .Slug , user .Username )
422
- if err != nil {
423
- g .logger .Err ("Failed to get team membership from GitHub: %s" , err )
424
- } else if membership != nil {
425
- if * membership .State == "active" && (* membership .Role == "member" || * membership .Role == "maintainer" ) {
426
- teamNames = append (teamNames , t .GetName ())
427
- }
428
- }
440
+ for _ , edge := range q .Organization .Teams .Edges {
441
+ teamNames = append (teamNames , edge .Node .Name )
429
442
}
430
- if resp . NextPage == 0 {
443
+ if ! q . Organization . Teams . PageInfo . HasNextPage {
431
444
break
432
445
}
433
- opts . Page = resp . NextPage
446
+ variables [ "teamCursor" ] = githubv4 . NewString ( q . Organization . Teams . PageInfo . EndCursor )
434
447
}
435
448
return teamNames , nil
436
449
}
0 commit comments