listLabels lists the labels in the given repo. Pass -1 for limit to list all labels; otherwise, only that number of labels is returned for any number of pages.
(client *http.Client, repo ghrepo.Interface, opts listQueryOptions)
| 66 | // listLabels lists the labels in the given repo. Pass -1 for limit to list all labels; |
| 67 | // otherwise, only that number of labels is returned for any number of pages. |
| 68 | func listLabels(client *http.Client, repo ghrepo.Interface, opts listQueryOptions) ([]label, int, error) { |
| 69 | if len(opts.fields) == 0 { |
| 70 | opts.fields = defaultFields |
| 71 | } |
| 72 | |
| 73 | apiClient := api.NewClientFromHTTP(client) |
| 74 | fragment := fmt.Sprintf("fragment label on Label{%s}", strings.Join(opts.fields, ",")) |
| 75 | query := fragment + ` |
| 76 | query LabelList($owner: String!, $repo: String!, $limit: Int!, $endCursor: String, $query: String, $orderBy: LabelOrder) { |
| 77 | repository(owner: $owner, name: $repo) { |
| 78 | labels(first: $limit, after: $endCursor, query: $query, orderBy: $orderBy) { |
| 79 | totalCount, |
| 80 | nodes { |
| 81 | ...label |
| 82 | } |
| 83 | pageInfo { |
| 84 | hasNextPage |
| 85 | endCursor |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | }` |
| 90 | |
| 91 | variables := map[string]interface{}{ |
| 92 | "owner": repo.RepoOwner(), |
| 93 | "repo": repo.RepoName(), |
| 94 | "orderBy": opts.OrderBy(), |
| 95 | "query": opts.Query, |
| 96 | } |
| 97 | |
| 98 | var labels []label |
| 99 | var totalCount int |
| 100 | |
| 101 | loop: |
| 102 | for { |
| 103 | var response listLabelsResponseData |
| 104 | variables["limit"] = determinePageSize(opts.Limit - len(labels)) |
| 105 | err := apiClient.GraphQL(repo.RepoHost(), query, variables, &response) |
| 106 | if err != nil { |
| 107 | return nil, 0, err |
| 108 | } |
| 109 | |
| 110 | totalCount = response.Repository.Labels.TotalCount |
| 111 | |
| 112 | for _, label := range response.Repository.Labels.Nodes { |
| 113 | labels = append(labels, label) |
| 114 | if len(labels) == opts.Limit { |
| 115 | break loop |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if response.Repository.Labels.PageInfo.HasNextPage { |
| 120 | variables["endCursor"] = response.Repository.Labels.PageInfo.EndCursor |
| 121 | } else { |
| 122 | break |
| 123 | } |
| 124 | } |
| 125 |