cli/pkg/cmd/label/http.go
Kynan Ware b8ee5c5eba Add godoc comments to exported symbols in remaining packages
Add documentation comments to exported symbols across all remaining
smaller packages including cmd/gen-docs, context, internal/browser,
internal/gh, internal/ghcmd, internal/ghinstance, internal/ghrepo,
internal/keyring, internal/run, internal/safepaths, internal/tableprinter,
internal/text, internal/update, pkg/cmd/accessibility, pkg/cmd/actions,
pkg/cmd/alias, pkg/cmd/api, pkg/cmd/browse, pkg/cmd/cache,
pkg/cmd/completion, pkg/cmd/copilot, pkg/cmd/factory, pkg/cmd/gpg-key,
pkg/cmd/label, pkg/cmd/licenses, pkg/cmd/org, pkg/cmd/preview,
pkg/cmd/ssh-key, pkg/cmd/version, pkg/extensions, pkg/jsoncolor,
pkg/markdown, pkg/option, pkg/set, pkg/ssh, pkg/surveyext, test,
internal/authflow, and utils.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-04 16:14:20 -07:00

136 lines
2.9 KiB
Go

package label
import (
"fmt"
"net/http"
"strings"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
)
const (
maxPageSize = 100
defaultOrder = "asc"
defaultSort = "created"
)
var defaultFields = []string{
"color",
"description",
"name",
}
type listLabelsResponseData struct {
Repository struct {
Labels struct {
TotalCount int
Nodes []label
PageInfo struct {
HasNextPage bool
EndCursor string
}
}
}
}
type listQueryOptions struct {
Limit int
Query string
Order string
Sort string
fields []string
}
// OrderBy returns the GraphQL ordering clause.
func (opts listQueryOptions) OrderBy() map[string]string {
// Set on copy to keep original intact.
if opts.Order == "" {
opts.Order = defaultOrder
}
if opts.Sort == "" {
opts.Sort = defaultSort
}
field := strings.ToUpper(opts.Sort)
if opts.Sort == "created" {
field = "CREATED_AT"
}
return map[string]string{
"direction": strings.ToUpper(opts.Order),
"field": field,
}
}
// 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.
func listLabels(client *http.Client, repo ghrepo.Interface, opts listQueryOptions) ([]label, int, error) {
if len(opts.fields) == 0 {
opts.fields = defaultFields
}
apiClient := api.NewClientFromHTTP(client)
fragment := fmt.Sprintf("fragment label on Label{%s}", strings.Join(opts.fields, ","))
query := fragment + `
query LabelList($owner: String!, $repo: String!, $limit: Int!, $endCursor: String, $query: String, $orderBy: LabelOrder) {
repository(owner: $owner, name: $repo) {
labels(first: $limit, after: $endCursor, query: $query, orderBy: $orderBy) {
totalCount,
nodes {
...label
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`
variables := map[string]interface{}{
"owner": repo.RepoOwner(),
"repo": repo.RepoName(),
"orderBy": opts.OrderBy(),
"query": opts.Query,
}
var labels []label
var totalCount int
loop:
for {
var response listLabelsResponseData
variables["limit"] = determinePageSize(opts.Limit - len(labels))
err := apiClient.GraphQL(repo.RepoHost(), query, variables, &response)
if err != nil {
return nil, 0, err
}
totalCount = response.Repository.Labels.TotalCount
for _, label := range response.Repository.Labels.Nodes {
labels = append(labels, label)
if len(labels) == opts.Limit {
break loop
}
}
if response.Repository.Labels.PageInfo.HasNextPage {
variables["endCursor"] = response.Repository.Labels.PageInfo.EndCursor
} else {
break
}
}
return labels, totalCount, nil
}
func determinePageSize(numRequestedItems int) int {
// If numRequestedItems is -1 then retrieve maxPageSize
if numRequestedItems < 0 || numRequestedItems > maxPageSize {
return maxPageSize
}
return numRequestedItems
}