-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.go
More file actions
440 lines (395 loc) · 12.4 KB
/
Copy pathfiles.go
File metadata and controls
440 lines (395 loc) · 12.4 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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"github.com/extism/go-pdk"
)
var (
GetFileContentsTool = ToolDescription{
Name: "gh-get-file-contents",
Description: "Get the contents of a file or a directory in a GitHub repository",
InputSchema: schema{
"type": "object",
"properties": props{
"owner": prop("string", "The owner of the repository"),
"repo": prop("string", "The repository name"),
"path": prop("string", "The path of the file"),
"branch": prop("string", "(optional string): Branch to get contents from"),
},
"required": []string{"owner", "repo", "path"},
},
}
CreateOrUpdateFileTool = ToolDescription{
Name: "gh-create-or-update-file",
Description: "Create or update a file in a GitHub repository",
InputSchema: schema{
"type": "object",
"properties": props{
"owner": prop("string", "The owner of the repository"),
"repo": prop("string", "The repository name"),
"path": prop("string", "The path of the file"),
"content": prop("string", "The content of the file"),
"message": prop("string", "The commit message"),
"branch": prop("string", "The branch name"),
"sha": prop("string", "(optional) The sha of the file, required for updates"),
},
"required": []string{"owner", "repo", "path", "content", "message", "branch"},
},
}
PushFilesTool = ToolDescription{
Name: "gh-push-files",
Description: "Push files to a GitHub repository",
InputSchema: schema{
"type": "object",
"properties": props{
"owner": prop("string", "The owner of the repository"),
"repo": prop("string", "The repository name"),
"branch": prop("string", "The branch name to push to"),
"message": prop("string", "The commit message"),
"files": SchemaProperty{
Type: "array",
Description: "Array of files to push",
Items: &schema{
"type": "object",
"properties": props{
"path": prop("string", "The path of the file"),
"content": prop("string", "The content of the file"),
},
},
},
},
},
}
FileTools = []ToolDescription{
GetFileContentsTool,
CreateOrUpdateFileTool,
PushFilesTool,
}
)
type FileCreate struct {
Content string `json:"content"`
Message string `json:"message"`
Branch string `json:"branch"`
Sha *string `json:"sha,omitempty"`
}
func fileCreateFromArgs(args map[string]interface{}) FileCreate {
file := FileCreate{}
if content, ok := args["content"].(string); ok {
b64c := base64.StdEncoding.EncodeToString([]byte(content))
file.Content = b64c
}
if message, ok := args["message"].(string); ok {
file.Message = message
}
if branch, ok := args["branch"].(string); ok {
file.Branch = branch
}
if sha, ok := args["sha"].(string); ok {
file.Sha = some(sha)
}
return file
}
func filesCreateOrUpdate(apiKey string, owner string, repo string, path string, file FileCreate) (CallToolResult, error) {
if file.Sha == nil {
uc, err := filesGetContentsInternal(apiKey, owner, repo, path, &file.Branch)
if err != nil {
pdk.Log(pdk.LogDebug, "File does not exist, creating it")
} else if !uc.isArray {
sha := uc.FileContent.Sha
file.Sha = &sha
}
}
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/contents/", path)
req := pdk.NewHTTPRequest(pdk.MethodPut, url)
req.SetHeader("Authorization", fmt.Sprint("token ", apiKey))
req.SetHeader("Accept", "application/vnd.github.v3+json")
req.SetHeader("User-Agent", "github-mcpx-servlet")
res, err := json.Marshal(file)
if err != nil {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to marshal file: ", err)),
}},
}, nil
}
req.SetBody([]byte(res))
resp := req.Send()
if resp.Status() != 201 {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to create or update file: ", resp.Status(), " ", string(resp.Body()))),
}},
}, nil
}
return CallToolResult{
Content: []Content{{
Type: ContentTypeText,
Text: some(string(resp.Body())),
}},
}, nil
}
type UnionContent struct {
isArray bool
FileContent FileContent
DirectoryContents []DirectoryContent
}
type FileContent struct {
Type string `json:"type"`
Encoding string `json:"encoding"`
Size int `json:"size"`
Name string `json:"name"`
Path string `json:"path"`
Content string `json:"content"`
Sha string `json:"sha"`
Url string `json:"url"`
GitUrl string `json:"git_url"`
HtmlUrl string `json:"html_url"`
DownloadUrl string `json:"download_url"`
}
type DirectoryContent struct {
Type string `json:"type"`
Size int `json:"size"`
Name string `json:"name"`
Path string `json:"path"`
Sha string `json:"sha"`
Url string `json:"url"`
GitUrl string `json:"git_url"`
HtmlUrl string `json:"html_url"`
DownloadUrl *string `json:"download_url"`
}
func filesGetContents(apiKey string, owner string, repo string, path string, branch *string) CallToolResult {
res, err := filesGetContentsInternal(apiKey, owner, repo, path, branch)
if err == nil {
var v []byte
if res.isArray {
v, err = json.Marshal(res.DirectoryContents)
} else {
v, err = json.Marshal(res.FileContent)
}
if err == nil {
return CallToolResult{
Content: []Content{{
Type: ContentTypeText,
Text: some(string(v)),
}},
}
}
}
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(err.Error()),
}},
}
}
func filesGetContentsInternal(apiKey string, owner string, repo string, path string, branch *string) (UnionContent, error) {
u := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/contents/", path)
params := url.Values{}
if branch != nil {
params.Add("ref", *branch)
}
u = fmt.Sprint(u, "?", params.Encode())
req := pdk.NewHTTPRequest(pdk.MethodGet, u)
req.SetHeader("Authorization", fmt.Sprint("token ", apiKey))
req.SetHeader("Accept", "application/vnd.github.v3+json")
req.SetHeader("User-Agent", "github-mcpx-servlet")
resp := req.Send()
if resp.Status() != 200 {
return UnionContent{}, fmt.Errorf("Failed to get file contents: %d %s (%s)", resp.Status(), string(resp.Body()), u)
}
// attempt to parse this as a file
uc := UnionContent{}
fc := &uc.FileContent
if err := json.Unmarshal(resp.Body(), fc); err == nil {
base64.StdEncoding.DecodeString(fc.Content)
// replace it with the decoded content
fc.Content = string(fc.Content)
return uc, nil
} else {
// if it's not a file, try to parse it as a directory
d := []DirectoryContent{}
if err := json.Unmarshal(resp.Body(), &d); err != nil {
return UnionContent{}, fmt.Errorf("Failed to unmarshal directory contents: %w", err)
}
uc.DirectoryContents = d
uc.isArray = true
return uc, nil
}
}
type FileOperation struct {
Path string `json:"path"`
Content string `json:"content"`
}
func filePushFromArgs(args map[string]interface{}) []FileOperation {
files := []FileOperation{}
if f, ok := args["files"].([]interface{}); ok {
for _, file := range f {
if file, ok := file.(map[string]interface{}); ok {
files = append(files, FileOperation{
Path: file["path"].(string),
Content: file["content"].(string),
})
}
}
}
return files
}
func filesPush(apiKey, owner, repo, branch, message string, files []FileOperation) CallToolResult {
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/git/refs/heads/", branch)
req := pdk.NewHTTPRequest(pdk.MethodGet, url)
req.SetHeader("Authorization", fmt.Sprint("token ", apiKey))
req.SetHeader("Accept", "application/vnd.github.v3+json")
req.SetHeader("User-Agent", "github-mcpx-servlet")
resp := req.Send()
if resp.Status() != 200 {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to get branch: ", resp.Status())),
}},
}
}
ref := RefSchema{}
json.Unmarshal(resp.Body(), &ref)
commitSha := ref.Object.Sha
if tree, err := createTree(apiKey, owner, repo, files, commitSha); err != nil {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to create tree: ", err)),
}},
}
} else if commit, err := createCommit(apiKey, owner, repo, message, tree.Sha, []string{commitSha}); err != nil {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to create commit: ", err)),
}},
}
} else {
return updateRef(apiKey, owner, repo, "heads/"+branch, commit.Sha)
}
}
type TreeSchema struct {
BaseTree string `json:"base_tree,omitempty"`
Tree []TreeEntry `json:"tree"`
Truncated bool `json:"truncated,omitempty"`
Url string `json:"url,omitempty"`
Sha string `json:"sha,omitempty"`
}
type TreeEntry struct {
Path string `json:"path"`
Mode string `json:"mode"`
Type string `json:"type"`
Content string `json:"content,omitempty"`
Size int `json:"size,omitempty"`
Sha string `json:"sha,omitempty"`
Url string `json:"url,omitempty"`
}
func createTree(apiKey, owner, repo string, files []FileOperation, baseTree string) (TreeSchema, error) {
tree := TreeSchema{
BaseTree: baseTree,
Tree: []TreeEntry{},
}
for _, file := range files {
tree.Tree = append(tree.Tree, TreeEntry{
Path: file.Path, Mode: "100644", Type: "blob", Content: file.Content})
}
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/git/trees")
req := pdk.NewHTTPRequest(pdk.MethodPost, url)
req.SetHeader("Authorization", fmt.Sprint("token ", apiKey))
req.SetHeader("Accept", "application/vnd.github.v3+json")
req.SetHeader("User-Agent", "github-mcpx-servlet")
req.SetHeader("Content-Type", "application/json")
res, err := json.Marshal(tree)
if err != nil {
return TreeSchema{}, fmt.Errorf("Failed to marshal tree: %w", err)
}
req.SetBody(res)
resp := req.Send()
if resp.Status() != 201 {
return TreeSchema{}, fmt.Errorf("Failed to create tree: %d %s", resp.Status(), string(resp.Body()))
}
ts := TreeSchema{}
err = json.Unmarshal(resp.Body(), &res)
return ts, err
}
type Author struct {
Name string `json:"name"`
Email string `json:"email"`
Date string `json:"date"`
}
type Commit struct {
Sha string `json:"sha"`
NodeID string `json:"node_id"`
Url string `json:"url"`
Author Author `json:"author"`
Committer Author `json:"committer"`
Message string `json:"message"`
Tree []struct {
Sha string `json:"sha"`
Url string `json:"url"`
} `json:"tree"`
Parents []struct {
Sha string `json:"sha"`
Url string `json:"url"`
} `json:"parents"`
}
func createCommit(apiKey, owner, repo, message, tree string, parents []string) (Commit, error) {
commit := map[string]interface{}{
"message": message,
"tree": tree,
"parents": parents,
}
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/git/commits")
req := pdk.NewHTTPRequest(pdk.MethodPost, url)
req.SetHeader("Authorization", fmt.Sprint("token ", apiKey))
req.SetHeader("Accept", "application/vnd.github.v3+json")
req.SetHeader("User-Agent", "github-mcpx-servlet")
req.SetHeader("Content-Type", "application/json")
res, _ := json.Marshal(commit)
req.SetBody(res)
resp := req.Send()
if resp.Status() != 201 {
return Commit{}, fmt.Errorf("Failed to create commit: %d %s", resp.Status(), string(resp.Body()))
}
cs := Commit{}
json.Unmarshal(resp.Body(), &cs)
return cs, nil
}
func updateRef(apiKey, owner, repo, ref, sha string) CallToolResult {
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/git/refs/", ref)
req := pdk.NewHTTPRequest(pdk.MethodPatch, url)
req.SetHeader("Authorization", fmt.Sprint("token ", apiKey))
req.SetHeader("Accept", "application/vnd.github.v3+json")
req.SetHeader("User-Agent", "github-mcpx-servlet")
req.SetHeader("Content-Type", "application/json")
res, _ := json.Marshal(map[string]any{"sha": sha, "force": true})
req.SetBody(res)
resp := req.Send()
if resp.Status() != 200 {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to update ref: ", resp.Status(), " ", string(resp.Body()))),
}},
}
}
return CallToolResult{
Content: []Content{{
Type: ContentTypeText,
Text: some(string(resp.Body())),
}},
}
}