Skip to content

Commit c06f33c

Browse files
committed
update getlabel description
1 parent dd5373d commit c06f33c

File tree

1 file changed

+85
-24
lines changed

1 file changed

+85
-24
lines changed

pkg/github/issues.go

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,12 +1871,12 @@ func CreateLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFu
18711871
}
18721872
}
18731873

1874-
// Get label
1874+
// GetLabel handles both listing all labels and getting a specific label
18751875
func GetLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (mcp.Tool, server.ToolHandlerFunc) {
18761876
return mcp.NewTool("get_label",
1877-
mcp.WithDescription(t("TOOL_GET_LABEL_DESCRIPTION", "Get a label in a GitHub repository.")),
1877+
mcp.WithDescription(t("TOOL_GET_LABEL_DESCRIPTION", "Get a label from a specific repository. If no label name is provided, lists all labels in the repository.")),
18781878
mcp.WithToolAnnotation(mcp.ToolAnnotation{
1879-
Title: t("TOOL_GET_LABEL_TITLE", "Get label"),
1879+
Title: t("TOOL_GET_LABEL_TITLE", "Get/List labels"),
18801880
ReadOnlyHint: ToBoolPtr(true),
18811881
}),
18821882
mcp.WithString("owner",
@@ -1888,8 +1888,7 @@ func GetLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc)
18881888
mcp.Description("Repository name"),
18891889
),
18901890
mcp.WithString("name",
1891-
mcp.Required(),
1892-
mcp.Description("Name of the label to retrieve"),
1891+
mcp.Description("Name of the label to retrieve. If not provided, lists all labels in the repository."),
18931892
),
18941893
),
18951894
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
@@ -1901,7 +1900,7 @@ func GetLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc)
19011900
if err != nil {
19021901
return mcp.NewToolResultError(err.Error()), nil
19031902
}
1904-
name, err := RequiredParam[string](request, "name")
1903+
name, err := OptionalParam[string](request, "name")
19051904
if err != nil {
19061905
return mcp.NewToolResultError(err.Error()), nil
19071906
}
@@ -1911,42 +1910,104 @@ func GetLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc)
19111910
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
19121911
}
19131912

1913+
// If name is provided, get specific label
1914+
if name != "" {
1915+
var query struct {
1916+
Repository struct {
1917+
Label struct {
1918+
ID githubv4.ID
1919+
Name githubv4.String
1920+
Color githubv4.String
1921+
Description githubv4.String
1922+
} `graphql:"label(name: $name)"`
1923+
} `graphql:"repository(owner: $owner, name: $repo)"`
1924+
}
1925+
1926+
vars := map[string]any{
1927+
"owner": githubv4.String(owner),
1928+
"repo": githubv4.String(repo),
1929+
"name": githubv4.String(name),
1930+
}
1931+
1932+
if err := client.Query(ctx, &query, vars); err != nil {
1933+
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find label", err), nil
1934+
}
1935+
1936+
// If label wasn't found, return a helpful error
1937+
if query.Repository.Label.Name == "" {
1938+
return mcp.NewToolResultError(fmt.Sprintf("label '%s' not found in %s/%s", name, owner, repo)), nil
1939+
}
1940+
1941+
label := map[string]any{
1942+
"id": fmt.Sprintf("%v", query.Repository.Label.ID),
1943+
"name": string(query.Repository.Label.Name),
1944+
"color": string(query.Repository.Label.Color),
1945+
"description": string(query.Repository.Label.Description),
1946+
}
1947+
1948+
out, err := json.Marshal(label)
1949+
if err != nil {
1950+
return nil, fmt.Errorf("failed to marshal label: %w", err)
1951+
}
1952+
1953+
return mcp.NewToolResultText(string(out)), nil
1954+
}
1955+
1956+
// If no name provided, list all labels
19141957
var query struct {
19151958
Repository struct {
1916-
Label struct {
1917-
ID githubv4.ID
1918-
Name githubv4.String
1919-
Color githubv4.String
1920-
Description githubv4.String
1921-
} `graphql:"label(name: $name)"`
1959+
Labels struct {
1960+
Nodes []struct {
1961+
ID githubv4.ID
1962+
Name githubv4.String
1963+
Color githubv4.String
1964+
Description githubv4.String
1965+
}
1966+
PageInfo struct {
1967+
HasNextPage githubv4.Boolean
1968+
HasPreviousPage githubv4.Boolean
1969+
StartCursor githubv4.String
1970+
EndCursor githubv4.String
1971+
}
1972+
TotalCount githubv4.Int
1973+
} `graphql:"labels(first: 100)"`
19221974
} `graphql:"repository(owner: $owner, name: $repo)"`
19231975
}
19241976

19251977
vars := map[string]any{
19261978
"owner": githubv4.String(owner),
19271979
"repo": githubv4.String(repo),
1928-
"name": githubv4.String(name),
19291980
}
19301981

19311982
if err := client.Query(ctx, &query, vars); err != nil {
1932-
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find label", err), nil
1983+
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to list labels", err), nil
19331984
}
19341985

1935-
// If label wasn't found, return a helpful error
1936-
if query.Repository.Label.Name == "" {
1937-
return mcp.NewToolResultError(fmt.Sprintf("label '%s' not found in %s/%s", name, owner, repo)), nil
1986+
var labels []map[string]any
1987+
for _, label := range query.Repository.Labels.Nodes {
1988+
labels = append(labels, map[string]any{
1989+
"id": fmt.Sprintf("%v", label.ID),
1990+
"name": string(label.Name),
1991+
"color": string(label.Color),
1992+
"description": string(label.Description),
1993+
})
19381994
}
19391995

1940-
label := map[string]any{
1941-
"id": fmt.Sprintf("%v", query.Repository.Label.ID),
1942-
"name": string(query.Repository.Label.Name),
1943-
"color": string(query.Repository.Label.Color),
1944-
"description": string(query.Repository.Label.Description),
1996+
response := map[string]any{
1997+
"labels": labels,
1998+
"count": len(labels),
1999+
"pageInfo": map[string]any{
2000+
"hasNextPage": bool(query.Repository.Labels.PageInfo.HasNextPage),
2001+
"hasPreviousPage": bool(query.Repository.Labels.PageInfo.HasPreviousPage),
2002+
"startCursor": string(query.Repository.Labels.PageInfo.StartCursor),
2003+
"endCursor": string(query.Repository.Labels.PageInfo.EndCursor),
2004+
},
2005+
"totalCount": int(query.Repository.Labels.TotalCount),
19452006
}
19462007

1947-
out, err := json.Marshal(label)
2008+
out, err := json.Marshal(response)
19482009
if err != nil {
1949-
return nil, fmt.Errorf("failed to marshal label: %w", err)
2010+
return nil, fmt.Errorf("failed to marshal labels: %w", err)
19502011
}
19512012

19522013
return mcp.NewToolResultText(string(out)), nil

0 commit comments

Comments
 (0)