Skip to content

Commit fa6453d

Browse files
committed
adding list labels
1 parent 78cb97c commit fa6453d

File tree

1 file changed

+84
-37
lines changed

1 file changed

+84
-37
lines changed

pkg/github/issues.go

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,7 @@ func DeleteLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFu
21602160
// CRUDLabel consolidates Create/Get/Update/Delete label operations into a single tool.
21612161
func CRUDLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (mcp.Tool, server.ToolHandlerFunc) {
21622162
return mcp.NewTool("crud_label",
2163-
mcp.WithDescription(t("TOOL_CRUD_LABEL_DESCRIPTION", "Create, read, update, or delete a label in a GitHub repository. Used in context of labels in relation to GitHub resources, they are organizational tags used to categorize and filter issues and pull requests. The use of parameters depends on the specific method selected.")),
2163+
mcp.WithDescription(t("TOOL_CRUD_LABEL_DESCRIPTION", "Create, read, update, or delete a label in a GitHub repository. Used in context of labels in relation to GitHub resources, they are organizational tags used to categorize and filter issues and pull requests. For 'get' method: if name is provided, retrieves a specific label; if name is omitted, lists all labels in the repository. The use of parameters depends on the specific method selected.")),
21642164
mcp.WithToolAnnotation(mcp.ToolAnnotation{
21652165
Title: t("TOOL_CRUD_LABEL_TITLE", "CRUD label"),
21662166
ReadOnlyHint: ToBoolPtr(false),
@@ -2177,7 +2177,7 @@ func CRUDLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc
21772177
mcp.Description("Repository name"),
21782178
),
21792179
mcp.WithString("name",
2180-
mcp.Description("Label name (for get/update/delete or to create with this name)"),
2180+
mcp.Description("Label name (for get/update/delete or to create with this name). For 'get' method: optional - if provided, gets specific label; if omitted, lists all labels."),
21812181
),
21822182
mcp.WithString("new_name",
21832183
mcp.Description("New name for the label (update only)"),
@@ -2276,48 +2276,94 @@ func CRUDLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc
22762276
if repo == "" {
22772277
return mcp.NewToolResultError("repo is required for get"), nil
22782278
}
2279-
if name == "" {
2280-
return mcp.NewToolResultError("name is required for get"), nil
2281-
}
22822279

2283-
var query struct {
2284-
Repository struct {
2285-
Label struct {
2286-
ID githubv4.ID
2287-
Name githubv4.String
2288-
Color githubv4.String
2289-
Description githubv4.String
2290-
} `graphql:"label(name: $name)"`
2291-
} `graphql:"repository(owner: $owner, name: $repo)"`
2292-
}
2280+
if name != "" {
2281+
// Get specific label
2282+
var query struct {
2283+
Repository struct {
2284+
Label struct {
2285+
ID githubv4.ID
2286+
Name githubv4.String
2287+
Color githubv4.String
2288+
Description githubv4.String
2289+
} `graphql:"label(name: $name)"`
2290+
} `graphql:"repository(owner: $owner, name: $repo)"`
2291+
}
22932292

2294-
vars := map[string]any{
2295-
"owner": githubv4.String(owner),
2296-
"repo": githubv4.String(repo),
2297-
"name": githubv4.String(name),
2298-
}
2293+
vars := map[string]any{
2294+
"owner": githubv4.String(owner),
2295+
"repo": githubv4.String(repo),
2296+
"name": githubv4.String(name),
2297+
}
22992298

2300-
if err := client.Query(ctx, &query, vars); err != nil {
2301-
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find label", err), nil
2302-
}
2299+
if err := client.Query(ctx, &query, vars); err != nil {
2300+
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find label", err), nil
2301+
}
23032302

2304-
if query.Repository.Label.Name == "" {
2305-
return mcp.NewToolResultError(fmt.Sprintf("label '%s' not found in %s/%s", name, owner, repo)), nil
2306-
}
2303+
if query.Repository.Label.Name == "" {
2304+
return mcp.NewToolResultError(fmt.Sprintf("label '%s' not found in %s/%s", name, owner, repo)), nil
2305+
}
23072306

2308-
label := map[string]interface{}{
2309-
"id": fmt.Sprintf("%v", query.Repository.Label.ID),
2310-
"name": string(query.Repository.Label.Name),
2311-
"color": string(query.Repository.Label.Color),
2312-
"description": string(query.Repository.Label.Description),
2313-
}
2307+
label := map[string]interface{}{
2308+
"id": fmt.Sprintf("%v", query.Repository.Label.ID),
2309+
"name": string(query.Repository.Label.Name),
2310+
"color": string(query.Repository.Label.Color),
2311+
"description": string(query.Repository.Label.Description),
2312+
}
23142313

2315-
out, err := json.Marshal(label)
2316-
if err != nil {
2317-
return nil, fmt.Errorf("failed to marshal label: %w", err)
2318-
}
2314+
out, err := json.Marshal(label)
2315+
if err != nil {
2316+
return nil, fmt.Errorf("failed to marshal label: %w", err)
2317+
}
2318+
2319+
return mcp.NewToolResultText(string(out)), nil
2320+
} else {
2321+
// List all labels
2322+
var query struct {
2323+
Repository struct {
2324+
Labels struct {
2325+
Nodes []struct {
2326+
ID githubv4.ID
2327+
Name githubv4.String
2328+
Color githubv4.String
2329+
Description githubv4.String
2330+
}
2331+
TotalCount githubv4.Int
2332+
} `graphql:"labels(first: 100)"`
2333+
} `graphql:"repository(owner: $owner, name: $repo)"`
2334+
}
2335+
2336+
vars := map[string]any{
2337+
"owner": githubv4.String(owner),
2338+
"repo": githubv4.String(repo),
2339+
}
2340+
2341+
if err := client.Query(ctx, &query, vars); err != nil {
2342+
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to list labels", err), nil
2343+
}
2344+
2345+
labels := make([]map[string]interface{}, len(query.Repository.Labels.Nodes))
2346+
for i, labelNode := range query.Repository.Labels.Nodes {
2347+
labels[i] = map[string]interface{}{
2348+
"id": fmt.Sprintf("%v", labelNode.ID),
2349+
"name": string(labelNode.Name),
2350+
"color": string(labelNode.Color),
2351+
"description": string(labelNode.Description),
2352+
}
2353+
}
23192354

2320-
return mcp.NewToolResultText(string(out)), nil
2355+
response := map[string]interface{}{
2356+
"labels": labels,
2357+
"totalCount": int(query.Repository.Labels.TotalCount),
2358+
}
2359+
2360+
out, err := json.Marshal(response)
2361+
if err != nil {
2362+
return nil, fmt.Errorf("failed to marshal labels: %w", err)
2363+
}
2364+
2365+
return mcp.NewToolResultText(string(out)), nil
2366+
}
23212367

23222368
case "update":
23232369
// Validate required params for update
@@ -2445,3 +2491,4 @@ func CRUDLabel(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc
24452491
return mcp.NewToolResultError("method did not return a result"), nil
24462492
}
24472493
}
2494+

0 commit comments

Comments
 (0)