Skip to content

Commit e6df078

Browse files
committed
Extract common code
1 parent 413e0d3 commit e6df078

File tree

5 files changed

+76
-104
lines changed

5 files changed

+76
-104
lines changed

pkg/github/__toolsnaps__/search_issues.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"title": "Search issues",
44
"readOnlyHint": true
55
},
6-
"description": "Search for issues in GitHub repositories.",
6+
"description": "Search for issues in GitHub repositories using is:issue search scope parameter.",
77
"inputSchema": {
88
"properties": {
99
"order": {

pkg/github/__toolsnaps__/search_pull_requests.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"title": "Search pull requests",
44
"readOnlyHint": true
55
},
6-
"description": "Search for pull requests in GitHub repositories.",
6+
"description": "Search for pull requests in GitHub repositories using is:pr search scope parameter.",
77
"inputSchema": {
88
"properties": {
99
"order": {

pkg/github/issues.go

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func AddIssueComment(getClient GetClientFn, t translations.TranslationHelperFunc
156156
// SearchIssues creates a tool to search for issues.
157157
func SearchIssues(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
158158
return mcp.NewTool("search_issues",
159-
mcp.WithDescription(t("TOOL_SEARCH_ISSUES_DESCRIPTION", "Search for issues in GitHub repositories using is:issue search parameter.")),
159+
mcp.WithDescription(t("TOOL_SEARCH_ISSUES_DESCRIPTION", "Search for issues in GitHub repositories using is:issue search scope parameter.")),
160160
mcp.WithToolAnnotation(mcp.ToolAnnotation{
161161
Title: t("TOOL_SEARCH_ISSUES_USER_TITLE", "Search issues"),
162162
ReadOnlyHint: ToBoolPtr(true),
@@ -188,56 +188,7 @@ func SearchIssues(getClient GetClientFn, t translations.TranslationHelperFunc) (
188188
WithPagination(),
189189
),
190190
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
191-
query, err := RequiredParam[string](request, "q")
192-
if err != nil {
193-
return mcp.NewToolResultError(err.Error()), nil
194-
}
195-
sort, err := OptionalParam[string](request, "sort")
196-
if err != nil {
197-
return mcp.NewToolResultError(err.Error()), nil
198-
}
199-
order, err := OptionalParam[string](request, "order")
200-
if err != nil {
201-
return mcp.NewToolResultError(err.Error()), nil
202-
}
203-
pagination, err := OptionalPaginationParams(request)
204-
if err != nil {
205-
return mcp.NewToolResultError(err.Error()), nil
206-
}
207-
208-
opts := &github.SearchOptions{
209-
Sort: sort,
210-
Order: order,
211-
ListOptions: github.ListOptions{
212-
PerPage: pagination.perPage,
213-
Page: pagination.page,
214-
},
215-
}
216-
217-
client, err := getClient(ctx)
218-
if err != nil {
219-
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
220-
}
221-
result, resp, err := client.Search.Issues(ctx, query, opts)
222-
if err != nil {
223-
return nil, fmt.Errorf("failed to search issues: %w", err)
224-
}
225-
defer func() { _ = resp.Body.Close() }()
226-
227-
if resp.StatusCode != http.StatusOK {
228-
body, err := io.ReadAll(resp.Body)
229-
if err != nil {
230-
return nil, fmt.Errorf("failed to read response body: %w", err)
231-
}
232-
return mcp.NewToolResultError(fmt.Sprintf("failed to search issues: %s", string(body))), nil
233-
}
234-
235-
r, err := json.Marshal(result)
236-
if err != nil {
237-
return nil, fmt.Errorf("failed to marshal response: %w", err)
238-
}
239-
240-
return mcp.NewToolResultText(string(r)), nil
191+
return searchHandler(ctx, getClient, request, "failed to search issues")
241192
}
242193
}
243194

pkg/github/pullrequests.go

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func MergePullRequest(getClient GetClientFn, t translations.TranslationHelperFun
536536
// SearchPullRequests creates a tool to search for pull requests.
537537
func SearchPullRequests(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
538538
return mcp.NewTool("search_pull_requests",
539-
mcp.WithDescription(t("TOOL_SEARCH_PULL_REQUESTS_DESCRIPTION", "Search for pull requests in GitHub repositories using is:pr search parameter.")),
539+
mcp.WithDescription(t("TOOL_SEARCH_PULL_REQUESTS_DESCRIPTION", "Search for pull requests in GitHub repositories using is:pr search scope parameter.")),
540540
mcp.WithToolAnnotation(mcp.ToolAnnotation{
541541
Title: t("TOOL_SEARCH_PULL_REQUESTS_USER_TITLE", "Search pull requests"),
542542
ReadOnlyHint: ToBoolPtr(true),
@@ -568,56 +568,7 @@ func SearchPullRequests(getClient GetClientFn, t translations.TranslationHelperF
568568
WithPagination(),
569569
),
570570
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
571-
query, err := RequiredParam[string](request, "q")
572-
if err != nil {
573-
return mcp.NewToolResultError(err.Error()), nil
574-
}
575-
sort, err := OptionalParam[string](request, "sort")
576-
if err != nil {
577-
return mcp.NewToolResultError(err.Error()), nil
578-
}
579-
order, err := OptionalParam[string](request, "order")
580-
if err != nil {
581-
return mcp.NewToolResultError(err.Error()), nil
582-
}
583-
pagination, err := OptionalPaginationParams(request)
584-
if err != nil {
585-
return mcp.NewToolResultError(err.Error()), nil
586-
}
587-
588-
opts := &github.SearchOptions{
589-
Sort: sort,
590-
Order: order,
591-
ListOptions: github.ListOptions{
592-
PerPage: pagination.perPage,
593-
Page: pagination.page,
594-
},
595-
}
596-
597-
client, err := getClient(ctx)
598-
if err != nil {
599-
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
600-
}
601-
result, resp, err := client.Search.Issues(ctx, query, opts)
602-
if err != nil {
603-
return nil, fmt.Errorf("failed to search pull requests: %w", err)
604-
}
605-
defer func() { _ = resp.Body.Close() }()
606-
607-
if resp.StatusCode != http.StatusOK {
608-
body, err := io.ReadAll(resp.Body)
609-
if err != nil {
610-
return nil, fmt.Errorf("failed to read response body: %w", err)
611-
}
612-
return mcp.NewToolResultError(fmt.Sprintf("failed to search pull requests: %s", string(body))), nil
613-
}
614-
615-
r, err := json.Marshal(result)
616-
if err != nil {
617-
return nil, fmt.Errorf("failed to marshal response: %w", err)
618-
}
619-
620-
return mcp.NewToolResultText(string(r)), nil
571+
return searchHandler(ctx, getClient, request, "failed to search pull requests")
621572
}
622573
}
623574

pkg/github/search_utils.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
10+
"github.com/google/go-github/v72/github"
11+
"github.com/mark3labs/mcp-go/mcp"
12+
)
13+
14+
func searchHandler(
15+
ctx context.Context,
16+
getClient GetClientFn,
17+
request mcp.CallToolRequest,
18+
errorPrefix string,
19+
) (*mcp.CallToolResult, error) {
20+
query, err := RequiredParam[string](request, "q")
21+
if err != nil {
22+
return mcp.NewToolResultError(err.Error()), nil
23+
}
24+
sort, err := OptionalParam[string](request, "sort")
25+
if err != nil {
26+
return mcp.NewToolResultError(err.Error()), nil
27+
}
28+
order, err := OptionalParam[string](request, "order")
29+
if err != nil {
30+
return mcp.NewToolResultError(err.Error()), nil
31+
}
32+
pagination, err := OptionalPaginationParams(request)
33+
if err != nil {
34+
return mcp.NewToolResultError(err.Error()), nil
35+
}
36+
37+
opts := &github.SearchOptions{
38+
Sort: sort,
39+
Order: order,
40+
ListOptions: github.ListOptions{
41+
Page: pagination.page,
42+
PerPage: pagination.perPage,
43+
},
44+
}
45+
46+
client, err := getClient(ctx)
47+
if err != nil {
48+
return nil, fmt.Errorf("%s: failed to get GitHub client: %w", errorPrefix, err)
49+
}
50+
result, resp, err := client.Search.Issues(ctx, query, opts)
51+
if err != nil {
52+
return nil, fmt.Errorf("%s: %w", errorPrefix, err)
53+
}
54+
defer func() { _ = resp.Body.Close() }()
55+
56+
if resp.StatusCode != http.StatusOK {
57+
body, err := io.ReadAll(resp.Body)
58+
if err != nil {
59+
return nil, fmt.Errorf("%s: failed to read response body: %w", errorPrefix, err)
60+
}
61+
return mcp.NewToolResultError(fmt.Sprintf("%s: %s", errorPrefix, string(body))), nil
62+
}
63+
64+
r, err := json.Marshal(result)
65+
if err != nil {
66+
return nil, fmt.Errorf("%s: failed to marshal response: %w", errorPrefix, err)
67+
}
68+
69+
return mcp.NewToolResultText(string(r)), nil
70+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy