Skip to content

Commit e7ac4b0

Browse files
committed
use go github pck to add sub-issues
1 parent ef56393 commit e7ac4b0

1 file changed

Lines changed: 17 additions & 41 deletions

File tree

pkg/github/issues.go

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
ghErrors "github.com/github/github-mcp-server/pkg/errors"
1213
"github.com/github/github-mcp-server/pkg/translations"
1314
"github.com/go-viper/mapstructure/v2"
1415
"github.com/google/go-github/v73/github"
@@ -175,7 +176,7 @@ func AddSubIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (t
175176
),
176177
mcp.WithNumber("sub_issue_id",
177178
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."),
179180
),
180181
mcp.WithBoolean("replace_parent",
181182
mcp.Description("When true, replaces the sub-issue's current parent issue"),
@@ -208,56 +209,31 @@ func AddSubIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (t
208209
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
209210
}
210211

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),
224215
}
225216

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)
229218
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
231224
}
232225

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() }()
249227

250228
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+
}
251233
return mcp.NewToolResultError(fmt.Sprintf("failed to add sub-issue: %s", string(body))), nil
252234
}
253235

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)
261237
if err != nil {
262238
return nil, fmt.Errorf("failed to marshal response: %w", err)
263239
}

0 commit comments

Comments
 (0)