|
| 1 | +package describers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/opengovern/og-describer-github/discovery/pkg/models" |
| 6 | + model "github.com/opengovern/og-describer-github/discovery/provider" |
| 7 | + "github.com/shurcooL/githubv4" |
| 8 | + steampipemodels "github.com/turbot/steampipe-plugin-github/github/models" |
| 9 | + "strconv" |
| 10 | +) |
| 11 | + |
| 12 | +func GetTeamList(ctx context.Context, githubClient model.GitHubClient, organizationName string, stream *models.StreamSender) ([]models.Resource, error) { |
| 13 | + client := githubClient.GraphQLClient |
| 14 | + var query struct { |
| 15 | + RateLimit steampipemodels.RateLimit |
| 16 | + Viewer struct { |
| 17 | + Organization struct { |
| 18 | + Login string |
| 19 | + Teams struct { |
| 20 | + PageInfo steampipemodels.PageInfo |
| 21 | + Nodes []steampipemodels.TeamWithCounts |
| 22 | + } `graphql:"teams(first: $pageSize, after: $cursor)"` |
| 23 | + } `graphql:"organization(login: $orgName)"` |
| 24 | + } |
| 25 | + } |
| 26 | + variables := map[string]interface{}{ |
| 27 | + "orgName": githubv4.String(organizationName), |
| 28 | + "pageSize": githubv4.Int(pageSize), |
| 29 | + "cursor": (*githubv4.String)(nil), |
| 30 | + } |
| 31 | + appendTeamColumnIncludes(&variables, teamCols()) |
| 32 | + var values []models.Resource |
| 33 | + var teams []steampipemodels.TeamWithCounts |
| 34 | + err := client.Query(ctx, &query, variables) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + teams = append(teams, query.Viewer.Organization.Teams.Nodes...) |
| 39 | + if query.Viewer.Organization.Teams.PageInfo.HasNextPage { |
| 40 | + ts, err := getAdditionalTeams(ctx, client, query.Viewer.Organization.Login, query.Viewer.Organization.Teams.PageInfo.EndCursor) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + teams = append(teams, ts...) |
| 45 | + } |
| 46 | + |
| 47 | + for _, team := range teams { |
| 48 | + value := models.Resource{ |
| 49 | + ID: strconv.Itoa(team.Id), |
| 50 | + Name: team.Name, |
| 51 | + Description: model.TeamDescription{ |
| 52 | + Organization: team.Organization.Name, |
| 53 | + Slug: team.Slug, |
| 54 | + Name: team.Name, |
| 55 | + ID: team.Id, |
| 56 | + NodeID: team.NodeId, |
| 57 | + Description: team.Description, |
| 58 | + CreatedAt: team.CreatedAt, |
| 59 | + UpdatedAt: team.UpdatedAt, |
| 60 | + CombinedSlug: team.CombinedSlug, |
| 61 | + ParentTeam: struct { |
| 62 | + Id int |
| 63 | + NodeId string |
| 64 | + Name string |
| 65 | + Slug string |
| 66 | + }{Id: team.ParentTeam.Id, NodeId: team.ParentTeam.NodeId, Name: team.ParentTeam.Name, Slug: team.ParentTeam.Slug}, |
| 67 | + Privacy: team.Privacy, |
| 68 | + AncestorsTotalCount: team.Ancestors.TotalCount, |
| 69 | + ChildTeamsTotalCount: team.ChildTeams.TotalCount, |
| 70 | + DiscussionsTotalCount: team.Discussions.TotalCount, |
| 71 | + InvitationsTotalCount: team.Invitations.TotalCount, |
| 72 | + MembersTotalCount: team.Members.TotalCount, |
| 73 | + ProjectsV2TotalCount: team.ProjectsV2.TotalCount, |
| 74 | + RepositoriesTotalCount: team.Repositories.TotalCount, |
| 75 | + URL: team.Url, |
| 76 | + DiscussionsURL: team.DiscussionsUrl, |
| 77 | + MembersURL: team.MembersUrl, |
| 78 | + NewTeamURL: team.NewTeamUrl, |
| 79 | + RepositoriesURL: team.RepositoriesUrl, |
| 80 | + TeamsURL: team.TeamsUrl, |
| 81 | + Subscription: team.Subscription, |
| 82 | + OrganizationID: team.Organization.Id, |
| 83 | + }, |
| 84 | + } |
| 85 | + if stream != nil { |
| 86 | + if err := (*stream)(value); err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + } else { |
| 90 | + values = append(values, value) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return values, nil |
| 95 | +} |
0 commit comments