-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathlabels.go
More file actions
443 lines (393 loc) · 13.5 KB
/
labels.go
File metadata and controls
443 lines (393 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package github
import (
"context"
"encoding/json"
"fmt"
"strings"
ghErrors "github.com/github/github-mcp-server/pkg/errors"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/github/github-mcp-server/pkg/scopes"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/github/github-mcp-server/pkg/utils"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/shurcooL/githubv4"
)
// GetLabel retrieves a specific label by name from a GitHub repository
func GetLabel(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataIssues,
mcp.Tool{
Name: "get_label",
Description: t("TOOL_GET_LABEL_DESCRIPTION", "Get a specific label from a repository."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_GET_LABEL_TITLE", "Get a specific label from a repository."),
ReadOnlyHint: true,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {
Type: "string",
Description: "Repository owner (username or organization name)",
},
"repo": {
Type: "string",
Description: "Repository name",
},
"name": {
Type: "string",
Description: "Label name.",
},
},
Required: []string{"owner", "repo", "name"},
},
},
[]scopes.Scope{scopes.Repo},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
name, err := RequiredParam[string](args, "name")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
var query struct {
Repository struct {
Label struct {
ID githubv4.ID
Name githubv4.String
Color githubv4.String
Description githubv4.String
} `graphql:"label(name: $name)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}
vars := map[string]any{
"owner": githubv4.String(owner),
"repo": githubv4.String(repo),
"name": githubv4.String(name),
}
client, err := deps.GetGQLClient(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to get GitHub client: %w", err)
}
if err := client.Query(ctx, &query, vars); err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find label", err), nil, nil
}
if query.Repository.Label.Name == "" {
return utils.NewToolResultError(fmt.Sprintf("label '%s' not found in %s/%s", name, owner, repo)), nil, nil
}
label := map[string]any{
"id": fmt.Sprintf("%v", query.Repository.Label.ID),
"name": string(query.Repository.Label.Name),
"color": string(query.Repository.Label.Color),
"description": string(query.Repository.Label.Description),
}
out, err := json.Marshal(label)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal label: %w", err)
}
return utils.NewToolResultText(string(out)), nil, nil
},
)
}
// GetLabelForLabelsToolset returns the same GetLabel tool but registered in the labels toolset.
// This provides conformance with the original behavior where get_label was in both toolsets.
func GetLabelForLabelsToolset(t translations.TranslationHelperFunc) inventory.ServerTool {
tool := GetLabel(t)
tool.Toolset = ToolsetLabels
return tool
}
// ListLabels lists labels from a repository
func ListLabels(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetLabels,
mcp.Tool{
Name: "list_label",
Description: t("TOOL_LIST_LABEL_DESCRIPTION", "List labels from a repository"),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_LIST_LABEL_DESCRIPTION", "List labels from a repository."),
ReadOnlyHint: true,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {
Type: "string",
Description: "Repository owner (username or organization name) - required for all operations",
},
"repo": {
Type: "string",
Description: "Repository name - required for all operations",
},
},
Required: []string{"owner", "repo"},
},
},
[]scopes.Scope{scopes.Repo},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
client, err := deps.GetGQLClient(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to get GitHub client: %w", err)
}
var query struct {
Repository struct {
Labels struct {
Nodes []struct {
ID githubv4.ID
Name githubv4.String
Color githubv4.String
Description githubv4.String
}
TotalCount githubv4.Int
} `graphql:"labels(first: 100)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}
vars := map[string]any{
"owner": githubv4.String(owner),
"repo": githubv4.String(repo),
}
if err := client.Query(ctx, &query, vars); err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to list labels", err), nil, nil
}
labels := make([]map[string]any, len(query.Repository.Labels.Nodes))
for i, labelNode := range query.Repository.Labels.Nodes {
labels[i] = map[string]any{
"id": fmt.Sprintf("%v", labelNode.ID),
"name": string(labelNode.Name),
"color": string(labelNode.Color),
"description": string(labelNode.Description),
}
}
response := map[string]any{
"labels": labels,
"totalCount": int(query.Repository.Labels.TotalCount),
}
out, err := json.Marshal(response)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal labels: %w", err)
}
return utils.NewToolResultText(string(out)), nil, nil
},
)
}
// LabelWrite handles create, update, and delete operations for GitHub labels
func LabelWrite(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetLabels,
mcp.Tool{
Name: "label_write",
Description: t("TOOL_LABEL_WRITE_DESCRIPTION", "Perform write operations on repository labels. To set labels on issues, use the 'update_issue' tool."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_LABEL_WRITE_TITLE", "Write operations on repository labels."),
ReadOnlyHint: false,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"method": {
Type: "string",
Description: "Operation to perform: 'create', 'update', or 'delete'",
Enum: []any{"create", "update", "delete"},
},
"owner": {
Type: "string",
Description: "Repository owner (username or organization name)",
},
"repo": {
Type: "string",
Description: "Repository name",
},
"name": {
Type: "string",
Description: "Label name - required for all operations",
},
"new_name": {
Type: "string",
Description: "New name for the label (used only with 'update' method to rename)",
},
"color": {
Type: "string",
Description: "Label color as 6-character hex code without '#' prefix (e.g., 'f29513'). Required for 'create', optional for 'update'.",
},
"description": {
Type: "string",
Description: "Label description text. Optional for 'create' and 'update'.",
},
},
Required: []string{"method", "owner", "repo", "name"},
},
},
[]scopes.Scope{scopes.Repo},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
// Get and validate required parameters
method, err := RequiredParam[string](args, "method")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
method = strings.ToLower(method)
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
name, err := RequiredParam[string](args, "name")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
// Get optional parameters
newName, _ := OptionalParam[string](args, "new_name")
color, _ := OptionalParam[string](args, "color")
description, _ := OptionalParam[string](args, "description")
client, err := deps.GetGQLClient(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to get GitHub client: %w", err)
}
switch method {
case "create":
// Validate required params for create
if color == "" {
return utils.NewToolResultError("color is required for create"), nil, nil
}
// Get repository ID
repoID, err := getRepositoryID(ctx, client, owner, repo)
if err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find repository", err), nil, nil
}
input := githubv4.CreateLabelInput{
RepositoryID: repoID,
Name: githubv4.String(name),
Color: githubv4.String(color),
}
if description != "" {
d := githubv4.String(description)
input.Description = &d
}
var mutation struct {
CreateLabel struct {
Label struct {
Name githubv4.String
ID githubv4.ID
}
} `graphql:"createLabel(input: $input)"`
}
if err := client.Mutate(ctx, &mutation, input, nil); err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to create label", err), nil, nil
}
return utils.NewToolResultText(fmt.Sprintf("label '%s' created successfully", mutation.CreateLabel.Label.Name)), nil, nil
case "update":
// Validate required params for update
if newName == "" && color == "" && description == "" {
return utils.NewToolResultError("at least one of new_name, color, or description must be provided for update"), nil, nil
}
// Get the label ID
labelID, err := getLabelID(ctx, client, owner, repo, name)
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
input := githubv4.UpdateLabelInput{
ID: labelID,
}
if newName != "" {
n := githubv4.String(newName)
input.Name = &n
}
if color != "" {
c := githubv4.String(color)
input.Color = &c
}
if description != "" {
d := githubv4.String(description)
input.Description = &d
}
var mutation struct {
UpdateLabel struct {
Label struct {
Name githubv4.String
ID githubv4.ID
}
} `graphql:"updateLabel(input: $input)"`
}
if err := client.Mutate(ctx, &mutation, input, nil); err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to update label", err), nil, nil
}
return utils.NewToolResultText(fmt.Sprintf("label '%s' updated successfully", mutation.UpdateLabel.Label.Name)), nil, nil
case "delete":
// Get the label ID
labelID, err := getLabelID(ctx, client, owner, repo, name)
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
input := githubv4.DeleteLabelInput{
ID: labelID,
}
var mutation struct {
DeleteLabel struct {
ClientMutationID githubv4.String
} `graphql:"deleteLabel(input: $input)"`
}
if err := client.Mutate(ctx, &mutation, input, nil); err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to delete label", err), nil, nil
}
return utils.NewToolResultText(fmt.Sprintf("label '%s' deleted successfully", name)), nil, nil
default:
return utils.NewToolResultError(fmt.Sprintf("unknown method: %s. Supported methods are: create, update, delete", method)), nil, nil
}
},
)
}
// Helper function to get repository ID
func getRepositoryID(ctx context.Context, client *githubv4.Client, owner, repo string) (githubv4.ID, error) {
var repoQuery struct {
Repository struct {
ID githubv4.ID
} `graphql:"repository(owner: $owner, name: $repo)"`
}
vars := map[string]any{
"owner": githubv4.String(owner),
"repo": githubv4.String(repo),
}
if err := client.Query(ctx, &repoQuery, vars); err != nil {
return "", err
}
return repoQuery.Repository.ID, nil
}
// Helper function to get label by name
func getLabelID(ctx context.Context, client *githubv4.Client, owner, repo, labelName string) (githubv4.ID, error) {
var query struct {
Repository struct {
Label struct {
ID githubv4.ID
Name githubv4.String
} `graphql:"label(name: $name)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}
vars := map[string]any{
"owner": githubv4.String(owner),
"repo": githubv4.String(repo),
"name": githubv4.String(labelName),
}
if err := client.Query(ctx, &query, vars); err != nil {
return "", err
}
if query.Repository.Label.Name == "" {
return "", fmt.Errorf("label '%s' not found in %s/%s", labelName, owner, repo)
}
return query.Repository.Label.ID, nil
}