|
9 | 9 | "strings" |
10 | 10 | "time" |
11 | 11 |
|
| 12 | + ghErrors "github.com/github/github-mcp-server/pkg/errors" |
12 | 13 | "github.com/github/github-mcp-server/pkg/translations" |
13 | 14 | "github.com/go-viper/mapstructure/v2" |
14 | 15 | "github.com/google/go-github/v73/github" |
@@ -175,7 +176,7 @@ func AddSubIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (t |
175 | 176 | ), |
176 | 177 | mcp.WithNumber("sub_issue_id", |
177 | 178 | mcp.Required(), |
178 | | - mcp.Description("The ID of the sub-issue to add"), |
| 179 | + mcp.Description("The ID of the sub-issue to add. Note: This is NOT the same as the issue number."), |
179 | 180 | ), |
180 | 181 | mcp.WithBoolean("replace_parent", |
181 | 182 | mcp.Description("When true, replaces the sub-issue's current parent issue"), |
@@ -208,56 +209,31 @@ func AddSubIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (t |
208 | 209 | return nil, fmt.Errorf("failed to get GitHub client: %w", err) |
209 | 210 | } |
210 | 211 |
|
211 | | - // Create the request body |
212 | | - requestBody := map[string]interface{}{ |
213 | | - "sub_issue_id": subIssueID, |
214 | | - } |
215 | | - if replaceParent { |
216 | | - requestBody["replace_parent"] = replaceParent |
217 | | - } |
218 | | - |
219 | | - // Since the go-github library might not have sub-issues support yet, |
220 | | - // we'll make a direct HTTP request using the client's HTTP client |
221 | | - reqBodyBytes, err := json.Marshal(requestBody) |
222 | | - if err != nil { |
223 | | - return nil, fmt.Errorf("failed to marshal request body: %w", err) |
| 212 | + subIssueRequest := github.SubIssueRequest{ |
| 213 | + SubIssueID: int64(subIssueID), |
| 214 | + ReplaceParent: ToBoolPtr(replaceParent), |
224 | 215 | } |
225 | 216 |
|
226 | | - url := fmt.Sprintf("%srepos/%s/%s/issues/%d/sub_issues", |
227 | | - client.BaseURL.String(), owner, repo, issueNumber) |
228 | | - req, err := http.NewRequestWithContext(ctx, "POST", url, strings.NewReader(string(reqBodyBytes))) |
| 217 | + subIssue, resp, err := client.SubIssue.Add(ctx, owner, repo, int64(issueNumber), subIssueRequest) |
229 | 218 | if err != nil { |
230 | | - return nil, fmt.Errorf("failed to create request: %w", err) |
| 219 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 220 | + "failed to add sub-issue", |
| 221 | + resp, |
| 222 | + err, |
| 223 | + ), nil |
231 | 224 | } |
232 | 225 |
|
233 | | - req.Header.Set("Accept", "application/vnd.github+json") |
234 | | - req.Header.Set("Content-Type", "application/json") |
235 | | - req.Header.Set("X-GitHub-Api-Version", "2022-11-28") |
236 | | - |
237 | | - // Use the same authentication as the GitHub client |
238 | | - httpClient := client.Client() |
239 | | - resp, err := httpClient.Do(req) |
240 | | - if err != nil { |
241 | | - return nil, fmt.Errorf("failed to add sub-issue: %w", err) |
242 | | - } |
243 | | - defer func() { _ = resp.Body.Close() }() |
244 | | - |
245 | | - body, err := io.ReadAll(resp.Body) |
246 | | - if err != nil { |
247 | | - return nil, fmt.Errorf("failed to read response body: %w", err) |
248 | | - } |
| 226 | + defer func() { _ = resp.Body.Close() }() |
249 | 227 |
|
250 | 228 | if resp.StatusCode != http.StatusCreated { |
| 229 | + body, err := io.ReadAll(resp.Body) |
| 230 | + if err != nil { |
| 231 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 232 | + } |
251 | 233 | return mcp.NewToolResultError(fmt.Sprintf("failed to add sub-issue: %s", string(body))), nil |
252 | 234 | } |
253 | 235 |
|
254 | | - // Parse and re-marshal to ensure consistent formatting |
255 | | - var result interface{} |
256 | | - if err := json.Unmarshal(body, &result); err != nil { |
257 | | - return nil, fmt.Errorf("failed to unmarshal response: %w", err) |
258 | | - } |
259 | | - |
260 | | - r, err := json.Marshal(result) |
| 236 | + r, err := json.Marshal(subIssue) |
261 | 237 | if err != nil { |
262 | 238 | return nil, fmt.Errorf("failed to marshal response: %w", err) |
263 | 239 | } |
|
0 commit comments