( ctx context.Context, getClient GetClientFn, args map[string]any, searchType string, errorPrefix string, options ...searchOption, )
| 113 | } |
| 114 | |
| 115 | func searchHandler( |
| 116 | ctx context.Context, |
| 117 | getClient GetClientFn, |
| 118 | args map[string]any, |
| 119 | searchType string, |
| 120 | errorPrefix string, |
| 121 | options ...searchOption, |
| 122 | ) (*mcp.CallToolResult, error) { |
| 123 | cfg := searchConfig{} |
| 124 | for _, opt := range options { |
| 125 | opt(&cfg) |
| 126 | } |
| 127 | query, opts, err := prepareSearchArgs(args, searchType) |
| 128 | if err != nil { |
| 129 | return utils.NewToolResultError(err.Error()), nil |
| 130 | } |
| 131 | |
| 132 | client, err := getClient(ctx) |
| 133 | if err != nil { |
| 134 | return utils.NewToolResultErrorFromErr(errorPrefix+": failed to get GitHub client", err), nil |
| 135 | } |
| 136 | result, resp, err := client.Search.Issues(ctx, query, opts) |
| 137 | if err != nil { |
| 138 | return utils.NewToolResultErrorFromErr(errorPrefix, err), nil |
| 139 | } |
| 140 | defer func() { _ = resp.Body.Close() }() |
| 141 | |
| 142 | if resp.StatusCode != http.StatusOK { |
| 143 | body, err := io.ReadAll(resp.Body) |
| 144 | if err != nil { |
| 145 | return utils.NewToolResultErrorFromErr(errorPrefix+": failed to read response body", err), nil |
| 146 | } |
| 147 | return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, errorPrefix, resp, body), nil |
| 148 | } |
| 149 | |
| 150 | r, err := json.Marshal(result) |
| 151 | if err != nil { |
| 152 | return utils.NewToolResultErrorFromErr(errorPrefix+": failed to marshal response", err), nil |
| 153 | } |
| 154 | |
| 155 | callResult := utils.NewToolResultText(string(r)) |
| 156 | if cfg.postProcess != nil { |
| 157 | cfg.postProcess(ctx, result, callResult) |
| 158 | } |
| 159 | return callResult, nil |
| 160 | } |
no test coverage detected