-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathrepository_resource_completions.go
More file actions
337 lines (303 loc) · 9.22 KB
/
repository_resource_completions.go
File metadata and controls
337 lines (303 loc) · 9.22 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
package github
import (
"context"
"errors"
"fmt"
"strings"
"github.com/google/go-github/v82/github"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
// CompleteHandler defines function signature for completion handlers
type CompleteHandler func(ctx context.Context, client *github.Client, resolved map[string]string, argValue string) ([]string, error)
// RepositoryResourceArgumentResolvers is a map of argument names to their completion handlers
var RepositoryResourceArgumentResolvers = map[string]CompleteHandler{
"owner": completeOwner,
"repo": completeRepo,
"branch": completeBranch,
"sha": completeSHA,
"tag": completeTag,
"prNumber": completePRNumber,
"path": completePath,
}
// RepositoryResourceCompletionHandler returns a CompletionHandlerFunc for repository resource completions.
func RepositoryResourceCompletionHandler(getClient GetClientFn) func(ctx context.Context, req *mcp.CompleteRequest) (*mcp.CompleteResult, error) {
return func(ctx context.Context, req *mcp.CompleteRequest) (*mcp.CompleteResult, error) {
if req.Params.Ref.Type != "ref/resource" {
return nil, nil // Not a resource completion
}
argName := req.Params.Argument.Name
argValue := req.Params.Argument.Value
var resolved map[string]string
if req.Params.Context != nil && req.Params.Context.Arguments != nil {
resolved = req.Params.Context.Arguments
} else {
resolved = map[string]string{}
}
client, err := getClient(ctx)
if err != nil {
return nil, err
}
// Argument resolver functions
resolvers := RepositoryResourceArgumentResolvers
resolver, ok := resolvers[argName]
if !ok {
return nil, errors.New("no resolver for argument: " + argName)
}
values, err := resolver(ctx, client, resolved, argValue)
if err != nil {
return nil, err
}
if len(values) > 100 {
values = values[:100]
}
return &mcp.CompleteResult{
Completion: mcp.CompletionResultDetails{
Values: values,
Total: len(values),
HasMore: false,
},
}, nil
}
}
// --- Per-argument resolver functions ---
func completeOwner(ctx context.Context, client *github.Client, _ map[string]string, argValue string) ([]string, error) {
var values []string
user, _, err := client.Users.Get(ctx, "")
if err == nil && user.GetLogin() != "" {
values = append(values, user.GetLogin())
}
orgs, _, err := client.Organizations.List(ctx, "", &github.ListOptions{PerPage: 100})
if err != nil {
return nil, err
}
for _, org := range orgs {
values = append(values, org.GetLogin())
}
// filter values based on argValue and replace values slice
if argValue != "" {
var filteredValues []string
for _, value := range values {
if strings.Contains(value, argValue) {
filteredValues = append(filteredValues, value)
}
}
values = filteredValues
}
if len(values) > 100 {
values = values[:100]
return values, nil // Limit to 100 results
}
// Else also do a client.Search.Users()
if argValue == "" {
return values, nil // No need to search if no argValue
}
users, _, err := client.Search.Users(ctx, argValue, &github.SearchOptions{ListOptions: github.ListOptions{PerPage: 100 - len(values)}})
if err != nil || users == nil {
return nil, err
}
for _, user := range users.Users {
values = append(values, user.GetLogin())
}
if len(values) > 100 {
values = values[:100]
}
return values, nil
}
func completeRepo(ctx context.Context, client *github.Client, resolved map[string]string, argValue string) ([]string, error) {
var values []string
owner := resolved["owner"]
if owner == "" {
return values, errors.New("owner not specified")
}
query := fmt.Sprintf("org:%s", owner)
if argValue != "" {
query = fmt.Sprintf("%s %s", query, argValue)
}
repos, _, err := client.Search.Repositories(ctx, query, &github.SearchOptions{ListOptions: github.ListOptions{PerPage: 100}})
if err != nil || repos == nil {
return values, errors.New("failed to get repositories")
}
// filter repos based on argValue
for _, repo := range repos.Repositories {
name := repo.GetName()
if argValue == "" || strings.HasPrefix(name, argValue) {
values = append(values, name)
}
}
return values, nil
}
func completeBranch(ctx context.Context, client *github.Client, resolved map[string]string, argValue string) ([]string, error) {
var values []string
owner := resolved["owner"]
repo := resolved["repo"]
if owner == "" || repo == "" {
return values, errors.New("owner or repo not specified")
}
branches, _, _ := client.Repositories.ListBranches(ctx, owner, repo, nil)
for _, branch := range branches {
if argValue == "" || strings.HasPrefix(branch.GetName(), argValue) {
values = append(values, branch.GetName())
}
}
if len(values) > 100 {
values = values[:100]
}
return values, nil
}
func completeSHA(ctx context.Context, client *github.Client, resolved map[string]string, argValue string) ([]string, error) {
var values []string
owner := resolved["owner"]
repo := resolved["repo"]
if owner == "" || repo == "" {
return values, errors.New("owner or repo not specified")
}
commits, _, _ := client.Repositories.ListCommits(ctx, owner, repo, nil)
for _, commit := range commits {
sha := commit.GetSHA()
if argValue == "" || strings.HasPrefix(sha, argValue) {
values = append(values, sha)
}
}
if len(values) > 100 {
values = values[:100]
}
return values, nil
}
func completeTag(ctx context.Context, client *github.Client, resolved map[string]string, argValue string) ([]string, error) {
owner := resolved["owner"]
repo := resolved["repo"]
if owner == "" || repo == "" {
return nil, errors.New("owner or repo not specified")
}
tags, _, _ := client.Repositories.ListTags(ctx, owner, repo, nil)
var values []string
for _, tag := range tags {
if argValue == "" || strings.Contains(tag.GetName(), argValue) {
values = append(values, tag.GetName())
}
}
if len(values) > 100 {
values = values[:100]
}
return values, nil
}
func completePRNumber(ctx context.Context, client *github.Client, resolved map[string]string, argValue string) ([]string, error) {
var values []string
owner := resolved["owner"]
repo := resolved["repo"]
if owner == "" || repo == "" {
return values, errors.New("owner or repo not specified")
}
prs, _, err := client.Search.Issues(ctx, fmt.Sprintf("repo:%s/%s is:open is:pr", owner, repo), &github.SearchOptions{ListOptions: github.ListOptions{PerPage: 100}})
if err != nil {
return values, err
}
for _, pr := range prs.Issues {
num := fmt.Sprintf("%d", pr.GetNumber())
if argValue == "" || strings.HasPrefix(num, argValue) {
values = append(values, num)
}
}
if len(values) > 100 {
values = values[:100]
}
return values, nil
}
func completePath(ctx context.Context, client *github.Client, resolved map[string]string, argValue string) ([]string, error) {
owner := resolved["owner"]
repo := resolved["repo"]
if owner == "" || repo == "" {
return nil, errors.New("owner or repo not specified")
}
refVal := resolved["branch"]
if refVal == "" {
refVal = resolved["sha"]
}
if refVal == "" {
refVal = resolved["tag"]
}
if refVal == "" {
refVal = "HEAD"
}
// Determine the prefix to complete (directory path or file path)
prefix := argValue
if prefix != "" && !strings.HasSuffix(prefix, "/") {
lastSlash := strings.LastIndex(prefix, "/")
if lastSlash >= 0 {
prefix = prefix[:lastSlash+1]
} else {
prefix = ""
}
}
// Get the tree for the ref (recursive)
tree, _, err := client.Git.GetTree(ctx, owner, repo, refVal, true)
if err != nil || tree == nil {
return nil, errors.New("failed to get file tree")
}
// Collect immediate children of the prefix (files and directories, no duplicates)
dirs := map[string]struct{}{}
files := map[string]struct{}{}
prefixLen := len(prefix)
for _, entry := range tree.Entries {
if !strings.HasPrefix(entry.GetPath(), prefix) {
continue
}
rel := entry.GetPath()[prefixLen:]
if rel == "" {
continue
}
// Only immediate children
slashIdx := strings.Index(rel, "/")
if slashIdx >= 0 {
// Directory: only add the directory name (with trailing slash), prefixed with full path
dirName := prefix + rel[:slashIdx+1]
dirs[dirName] = struct{}{}
} else if entry.GetType() == "blob" {
// File: add as-is, prefixed with full path
fileName := prefix + rel
files[fileName] = struct{}{}
}
}
// Optionally filter by argValue (if user is typing after last slash)
var filter string
if argValue != "" {
if lastSlash := strings.LastIndex(argValue, "/"); lastSlash >= 0 {
filter = argValue[lastSlash+1:]
} else {
filter = argValue
}
}
var values []string
// Add directories first, then files, both filtered
for dir := range dirs {
// Only filter on the last segment after the last slash
if filter == "" {
values = append(values, dir)
} else {
last := dir
if idx := strings.LastIndex(strings.TrimRight(dir, "/"), "/"); idx >= 0 {
last = dir[idx+1:]
}
if strings.HasPrefix(last, filter) {
values = append(values, dir)
}
}
}
for file := range files {
if filter == "" {
values = append(values, file)
} else {
last := file
if idx := strings.LastIndex(file, "/"); idx >= 0 {
last = file[idx+1:]
}
if strings.HasPrefix(last, filter) {
values = append(values, file)
}
}
}
if len(values) > 100 {
values = values[:100]
}
return values, nil
}