Skip to content

Commit 5ea322b

Browse files
committed
refactor: remove redundant draft update tests and streamline UpdatePullRequest logic
1 parent d89e522 commit 5ea322b

File tree

3 files changed

+4
-463
lines changed

3 files changed

+4
-463
lines changed

pkg/github/discussions_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ var (
8787
"url": "https://github.com/owner/.github/discussions/4",
8888
"category": map[string]any{"name": "General"},
8989
},
90-
9190
}
9291

9392
// Ordered mock responses

pkg/github/pullrequests.go

Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,17 @@ func UpdatePullRequest(getClient GetClientFn, getGQLClient GetGQLClientFn, t tra
262262
return mcp.NewToolResultError(err.Error()), nil
263263
}
264264

265+
// Check if draft parameter is provided
265266
draftProvided := request.GetArguments()["draft"] != nil
266267
var draftValue bool
267268
if draftProvided {
268269
draftValue, err = OptionalParam[bool](request, "draft")
269270
if err != nil {
270-
return nil, err
271+
return mcp.NewToolResultError(err.Error()), nil
271272
}
272273
}
273274

275+
// Build the update struct only with provided fields
274276
update := &github.PullRequest{}
275277
restUpdateNeeded := false
276278

@@ -320,13 +322,7 @@ func UpdatePullRequest(getClient GetClientFn, getGQLClient GetGQLClientFn, t tra
320322
return mcp.NewToolResultError("No update parameters provided."), nil
321323
}
322324

323-
// Handle REST API updates
324-
}
325-
326-
if !restUpdateNeeded && !draftProvided {
327-
return mcp.NewToolResultError("No update parameters provided."), nil
328-
}
329-
325+
// Handle REST API updates (title, body, state, base, maintainer_can_modify)
330326
if restUpdateNeeded {
331327
client, err := getClient(ctx)
332328
if err != nil {
@@ -468,90 +464,6 @@ func UpdatePullRequest(getClient GetClientFn, getGQLClient GetGQLClientFn, t tra
468464
}
469465
}()
470466

471-
r, err := json.Marshal(finalPR)
472-
if err != nil {
473-
}
474-
475-
if draftProvided {
476-
gqlClient, err := getGQLClient(ctx)
477-
if err != nil {
478-
return nil, fmt.Errorf("failed to get GitHub GraphQL client: %w", err)
479-
}
480-
481-
var prQuery struct {
482-
Repository struct {
483-
PullRequest struct {
484-
ID githubv4.ID
485-
IsDraft githubv4.Boolean
486-
} `graphql:"pullRequest(number: $prNum)"`
487-
} `graphql:"repository(owner: $owner, name: $repo)"`
488-
}
489-
490-
err = gqlClient.Query(ctx, &prQuery, map[string]interface{}{
491-
"owner": githubv4.String(owner),
492-
"repo": githubv4.String(repo),
493-
"prNum": githubv4.Int(pullNumber), // #nosec G115 - pull request numbers are always small positive integers
494-
})
495-
if err != nil {
496-
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find pull request", err), nil
497-
}
498-
499-
currentIsDraft := bool(prQuery.Repository.PullRequest.IsDraft)
500-
501-
if currentIsDraft != draftValue {
502-
if draftValue {
503-
// Convert to draft
504-
var mutation struct {
505-
ConvertPullRequestToDraft struct {
506-
PullRequest struct {
507-
ID githubv4.ID
508-
IsDraft githubv4.Boolean
509-
}
510-
} `graphql:"convertPullRequestToDraft(input: $input)"`
511-
}
512-
513-
err = gqlClient.Mutate(ctx, &mutation, githubv4.ConvertPullRequestToDraftInput{
514-
PullRequestID: prQuery.Repository.PullRequest.ID,
515-
}, nil)
516-
if err != nil {
517-
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to convert pull request to draft", err), nil
518-
}
519-
} else {
520-
// Mark as ready for review
521-
var mutation struct {
522-
MarkPullRequestReadyForReview struct {
523-
PullRequest struct {
524-
ID githubv4.ID
525-
IsDraft githubv4.Boolean
526-
}
527-
} `graphql:"markPullRequestReadyForReview(input: $input)"`
528-
}
529-
530-
err = gqlClient.Mutate(ctx, &mutation, githubv4.MarkPullRequestReadyForReviewInput{
531-
PullRequestID: prQuery.Repository.PullRequest.ID,
532-
}, nil)
533-
if err != nil {
534-
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to mark pull request ready for review", err), nil
535-
}
536-
}
537-
}
538-
}
539-
540-
client, err := getClient(ctx)
541-
if err != nil {
542-
return nil, err
543-
}
544-
545-
finalPR, resp, err := client.PullRequests.Get(ctx, owner, repo, pullNumber)
546-
if err != nil {
547-
return ghErrors.NewGitHubAPIErrorResponse(ctx, "Failed to get pull request", resp, err), nil
548-
}
549-
defer func() {
550-
if resp != nil && resp.Body != nil {
551-
_ = resp.Body.Close()
552-
}
553-
}()
554-
555467
r, err := json.Marshal(finalPR)
556468
if err != nil {
557469
return mcp.NewToolResultError(fmt.Sprintf("Failed to marshal response: %v", err)), nil

0 commit comments

Comments
 (0)