-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissues.go
More file actions
374 lines (336 loc) · 11.1 KB
/
Copy pathissues.go
File metadata and controls
374 lines (336 loc) · 11.1 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
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/extism/go-pdk"
)
var (
ListIssuesTool = ToolDescription{
Name: "gh-list-issues",
Description: "List issues from a GitHub repository",
InputSchema: schema{
"type": "object",
"properties": props{
"owner": prop("string", "The owner of the repository"),
"repo": prop("string", "The repository name"),
"filter": prop("string", "Filter by assigned, created, mentioned, subscribed, repos, all"),
"state": prop("string", "The state of the issues (open, closed, all)"),
"labels": prop("string", "A list of comma separated label names (e.g. bug,ui,@high)"),
"sort": prop("string", "Sort field (created, updated, comments)"),
"direction": prop("string", "Sort direction (asc or desc)"),
"since": prop("string", "ISO 8601 timestamp (YYYY-MM-DDTHH:MM:SSZ)"),
"collab": prop("boolean", "Filter by issues that are collaborated on"),
"orgs": prop("boolean", "Filter by organization issues"),
"owned": prop("boolean", "Filter by owned issues"),
"pulls": prop("boolean", "Include pull requests in results"),
"per_page": prop("integer", "Number of results per page (max 100)"),
"page": prop("integer", "Page number for pagination"),
},
"required": []string{"owner", "repo"},
},
}
CreateIssueTool = ToolDescription{
Name: "gh-create-issue",
Description: "Create an issue on a GitHub repository",
InputSchema: schema{
"type": "object",
"properties": props{
"owner": prop("string", "The owner of the repository"),
"repo": prop("string", "The repository name"),
"title": prop("string", "The title of the issue"),
"body": prop("string", "The body of the issue"),
"state": prop("string", "The state of the issue"),
"assignees": arrprop("array", "The assignees of the issue", "string"),
"milestone": prop("integer", "The milestone of the issue"),
},
"required": []string{"owner", "repo", "title", "body"},
},
}
GetIssueTool = ToolDescription{
Name: "gh-get-issue",
Description: "Get an issue from a GitHub repository",
InputSchema: schema{
"type": "object",
"properties": props{
"owner": prop("string", "The owner of the repository"),
"repo": prop("string", "The repository name"),
"issue": prop("integer", "The issue number"),
},
"required": []string{"owner", "repo", "issue"},
},
}
AddIssueCommentTool = ToolDescription{
Name: "gh-add-issue-comment",
Description: "Add a comment to an issue in a GitHub repository",
InputSchema: schema{
"type": "object",
"properties": props{
"owner": prop("string", "The owner of the repository"),
"repo": prop("string", "The repository name"),
"issue": prop("integer", "The issue number"),
"body": prop("string", "The body of the issue"),
},
"required": []string{"owner", "repo", "issue", "body"},
},
}
UpdateIssueTool = ToolDescription{
Name: "gh-update-issue",
Description: "Update an issue in a GitHub repository",
InputSchema: schema{
"type": "object",
"properties": props{
"owner": prop("string", "The owner of the repository"),
"repo": prop("string", "The repository name"),
"issue": prop("integer", "The issue number"),
"title": prop("string", "The title of the issue"),
"body": prop("string", "The body of the issue"),
"state": prop("string", "The state of the issue"),
"assignees": arrprop("array", "The assignees of the issue", "string"),
"milestone": prop("integer", "The milestone of the issue"),
},
"required": []string{"owner", "repo", "issue"},
},
}
IssueTools = []ToolDescription{
ListIssuesTool,
CreateIssueTool,
GetIssueTool,
UpdateIssueTool,
AddIssueCommentTool,
}
)
type Issue struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Assignees []string `json:"assignees,omitempty"`
Milestone int `json:"milestone,omitempty"`
Labels []string `json:"labels,omitempty"`
}
func issueList(apiKey string, owner, repo string, args map[string]interface{}) (CallToolResult, error) {
baseURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/issues", owner, repo)
params := make([]string, 0)
// String parameters
stringParams := map[string]string{
"filter": "assigned", // Default value
"state": "open", // Default value
"labels": "",
"sort": "created", // Default value
"direction": "desc", // Default value
"since": "",
}
for key := range stringParams {
if value, ok := args[key].(string); ok && value != "" {
params = append(params, fmt.Sprintf("%s=%s", key, value))
} else if stringParams[key] != "" {
// Add default value if one exists
params = append(params, fmt.Sprintf("%s=%s", key, stringParams[key]))
}
}
// Boolean parameters
boolParams := []string{"collab", "orgs", "owned", "pulls"}
for _, param := range boolParams {
if value, ok := args[param].(bool); ok {
params = append(params, fmt.Sprintf("%s=%t", param, value))
}
}
// Pagination parameters
perPage := 30 // Default value
if value, ok := args["per_page"].(float64); ok {
if value > 100 {
perPage = 100 // Max value
} else if value > 0 {
perPage = int(value)
}
}
params = append(params, fmt.Sprintf("per_page=%d", perPage))
page := 1 // Default value
if value, ok := args["page"].(float64); ok && value > 0 {
page = int(value)
}
params = append(params, fmt.Sprintf("page=%d", page))
// Build final URL
url := baseURL
if len(params) > 0 {
url = fmt.Sprintf("%s?%s", baseURL, strings.Join(params, "&"))
}
pdk.Log(pdk.LogDebug, fmt.Sprint("Listing issues: ", url))
// Make request
req := pdk.NewHTTPRequest(pdk.MethodGet, url)
req.SetHeader("Authorization", fmt.Sprint("token ", apiKey))
req.SetHeader("Accept", "application/vnd.github+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.Sprintf("Failed to list issues: %d %s", resp.Status(), string(resp.Body()))),
}},
}, nil
}
return CallToolResult{
Content: []Content{{
Type: ContentTypeText,
Text: some(string(resp.Body())),
}},
}, nil
}
func issueFromArgs(args map[string]interface{}) Issue {
data := Issue{}
if title, ok := args["title"].(string); ok {
data.Title = title
}
if body, ok := args["body"].(string); ok {
data.Body = body
}
if assignees, ok := args["assignees"].([]interface{}); ok {
for _, a := range assignees {
data.Assignees = append(data.Assignees, a.(string))
}
}
if milestone, ok := args["milestone"].(float64); ok {
data.Milestone = int(milestone)
}
if labels, ok := args["labels"].([]interface{}); ok {
for _, l := range labels {
data.Labels = append(data.Labels, l.(string))
}
}
return data
}
func issueCreate(apiKey string, owner, repo string, data Issue) (CallToolResult, error) {
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/issues")
pdk.Log(pdk.LogDebug, fmt.Sprint("Adding comment: ", url))
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(data)
if err != nil {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to create issue: ", 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 issue: ", resp.Status(), " ", string(resp.Body()))),
}},
}, nil
}
return CallToolResult{
Content: []Content{{
Type: ContentTypeText,
Text: some(string(resp.Body())),
}},
}, nil
}
func issueGet(apiKey string, owner, repo string, issue int) (CallToolResult, error) {
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/issues/", issue)
pdk.Log(pdk.LogDebug, fmt.Sprint("Getting issue: ", url))
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 issue: ", resp.Status())),
}},
}, nil
}
return CallToolResult{
Content: []Content{{
Type: ContentTypeText,
Text: some(string(resp.Body())),
}},
}, nil
}
func issueUpdate(apiKey string, owner, repo string, issue int, data Issue) (CallToolResult, error) {
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/issues/", issue)
pdk.Log(pdk.LogDebug, fmt.Sprint("Getting issue: ", url))
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, err := json.Marshal(data)
if err != nil {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to update issue: ", err)),
}},
}, nil
}
req.SetBody([]byte(res))
resp := req.Send()
if resp.Status() != 200 {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to update issue: ", resp.Status())),
}},
}, nil
}
return CallToolResult{
Content: []Content{{
Type: ContentTypeText,
Text: some(string(resp.Body())),
}},
}, nil
}
func issueAddComment(apiKey string, owner, repo string, issue int, comment string) (CallToolResult, error) {
url := fmt.Sprint("https://api.github.com/repos/", owner, "/", repo, "/issues/", issue, "/comments")
pdk.Log(pdk.LogDebug, fmt.Sprint("Adding comment: ", url))
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(map[string]string{
"body": comment,
})
if err != nil {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some(fmt.Sprint("Failed to create issue: ", 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 add comment: ", resp.Status())),
}},
}, nil
}
return CallToolResult{
Content: []Content{{
Type: ContentTypeText,
Text: some(string(resp.Body())),
}},
}, nil
}