From ae4722ff4efffb1175e4d810e7fd1b491315f50d Mon Sep 17 00:00:00 2001 From: Iulia B Date: Mon, 27 Jul 2026 11:18:31 +0300 Subject: [PATCH] Order list_label results by issue count (descending) Sends orderBy: {field: ISSUE_COUNT, direction: DESC} on the GraphQL labels query so the most-used labels (by issue count) are returned first. ISSUE_COUNT is accepted by the GitHub GraphQL API but is not part of the public schema docs or the githubv4 client library's LabelOrderField constants, so it is defined locally. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- README.md | 2 +- pkg/github/__toolsnaps__/list_label.snap | 2 +- pkg/github/labels.go | 12 ++++++++++-- pkg/github/labels_test.go | 8 +++++--- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1a06c0697d..55bca6e9df 100644 --- a/README.md +++ b/README.md @@ -1017,7 +1017,7 @@ The following sets of tools are available: - `owner`: Repository owner (username or organization name) (string, required) - `repo`: Repository name (string, required) -- **list_label** - List labels from a repository +- **list_label** - List labels from a repository, ordered by issue count (descending) so the most-used labels are returned first - **Required OAuth Scopes**: `repo` - `owner`: Repository owner (username or organization name) - required for all operations (string, required) - `repo`: Repository name - required for all operations (string, required) diff --git a/pkg/github/__toolsnaps__/list_label.snap b/pkg/github/__toolsnaps__/list_label.snap index 9aaf90f3b2..37e45b15fb 100644 --- a/pkg/github/__toolsnaps__/list_label.snap +++ b/pkg/github/__toolsnaps__/list_label.snap @@ -4,7 +4,7 @@ "readOnlyHint": true, "title": "List labels from a repository" }, - "description": "List labels from a repository", + "description": "List labels from a repository, ordered by issue count (descending) so the most-used labels are returned first", "inputSchema": { "properties": { "owner": { diff --git a/pkg/github/labels.go b/pkg/github/labels.go index 29ae3d5323..d6c7da5a1b 100644 --- a/pkg/github/labels.go +++ b/pkg/github/labels.go @@ -17,6 +17,11 @@ import ( "github.com/shurcooL/githubv4" ) +// labelOrderFieldIssueCount orders labels by the number of issues they are assigned to. +// It is not part of the githubv4.LabelOrderField constants shipped with the client library +// (or GitHub's public GraphQL schema docs), but the API accepts it, so we define it locally. +const labelOrderFieldIssueCount githubv4.LabelOrderField = "ISSUE_COUNT" + // GetLabel retrieves a specific label by name from a GitHub repository func GetLabel(t translations.TranslationHelperFunc) inventory.ServerTool { return NewTool( @@ -129,7 +134,7 @@ func ListLabels(t translations.TranslationHelperFunc) inventory.ServerTool { ToolsetLabels, mcp.Tool{ Name: "list_label", - Description: t("TOOL_LIST_LABEL_DESCRIPTION", "List labels from a repository"), + Description: t("TOOL_LIST_LABEL_DESCRIPTION", "List labels from a repository, ordered by issue count (descending) so the most-used labels are returned first"), Annotations: &mcp.ToolAnnotations{ Title: t("TOOL_LIST_LABEL_DESCRIPTION", "List labels from a repository"), ReadOnlyHint: true, @@ -176,13 +181,16 @@ func ListLabels(t translations.TranslationHelperFunc) inventory.ServerTool { Description githubv4.String } TotalCount githubv4.Int - } `graphql:"labels(first: 100)"` + } `graphql:"labels(first: 100, orderBy: {field: $orderByField, direction: $orderByDirection})"` } `graphql:"repository(owner: $owner, name: $repo)"` } vars := map[string]any{ "owner": githubv4.String(owner), "repo": githubv4.String(repo), + // Order labels by issue count (descending) so the most-used labels are returned first. + "orderByField": labelOrderFieldIssueCount, + "orderByDirection": githubv4.OrderDirectionDesc, } if err := client.Query(ctx, &query, vars); err != nil { diff --git a/pkg/github/labels_test.go b/pkg/github/labels_test.go index c3434b240b..b030c9cab7 100644 --- a/pkg/github/labels_test.go +++ b/pkg/github/labels_test.go @@ -175,12 +175,14 @@ func TestListLabels(t *testing.T) { Description githubv4.String } TotalCount githubv4.Int - } `graphql:"labels(first: 100)"` + } `graphql:"labels(first: 100, orderBy: {field: $orderByField, direction: $orderByDirection})"` } `graphql:"repository(owner: $owner, name: $repo)"` }{}, map[string]any{ - "owner": githubv4.String("owner"), - "repo": githubv4.String("repo"), + "owner": githubv4.String("owner"), + "repo": githubv4.String("repo"), + "orderByField": labelOrderFieldIssueCount, + "orderByDirection": githubv4.OrderDirectionDesc, }, githubv4mock.DataResponse(map[string]any{ "repository": map[string]any{