(httpClient *http.Client, query string, variables map[string]interface{}, limit int, host string)
| 47 | } |
| 48 | |
| 49 | func listRulesets(httpClient *http.Client, query string, variables map[string]interface{}, limit int, host string) (*RulesetList, error) { |
| 50 | pageLimit := min(limit, 100) |
| 51 | |
| 52 | res := RulesetList{ |
| 53 | Rulesets: []RulesetGraphQL{}, |
| 54 | } |
| 55 | client := api.NewClientFromHTTP(httpClient) |
| 56 | |
| 57 | for { |
| 58 | variables["limit"] = pageLimit |
| 59 | var data RulesetResponse |
| 60 | err := client.GraphQL(host, query, variables, &data) |
| 61 | if err != nil { |
| 62 | if strings.Contains(err.Error(), "requires one of the following scopes: ['admin:org']") { |
| 63 | return nil, errors.New("the 'admin:org' scope is required to view organization rulesets, try running 'gh auth refresh -s admin:org'") |
| 64 | } |
| 65 | |
| 66 | return nil, err |
| 67 | } |
| 68 | |
| 69 | res.TotalCount = data.Level.Rulesets.TotalCount |
| 70 | res.Rulesets = append(res.Rulesets, data.Level.Rulesets.Nodes...) |
| 71 | |
| 72 | if len(res.Rulesets) >= limit { |
| 73 | break |
| 74 | } |
| 75 | |
| 76 | if data.Level.Rulesets.PageInfo.HasNextPage { |
| 77 | variables["endCursor"] = data.Level.Rulesets.PageInfo.EndCursor |
| 78 | pageLimit = min(pageLimit, limit-len(res.Rulesets)) |
| 79 | } else { |
| 80 | break |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return &res, nil |
| 85 | } |
| 86 | |
| 87 | func min(a, b int) int { |
| 88 | if a < b { |
no test coverage detected