-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: Add mark_pr_ready_for_review tool #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
efouts
wants to merge
3
commits into
github:main
Choose a base branch
from
efouts:feat/mark-pr-ready-for-review
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1607,3 +1607,127 @@ func newGQLIntPtr(i *int32) *githubv4.Int { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
gi := githubv4.Int(*i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return &gi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// SetPRStatus creates a tool to set pull request status between draft and ready-for-review states. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// This uses the GraphQL API because the REST API does not support changing PR draft status. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func SetPRStatus(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (mcp.Tool, server.ToolHandlerFunc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewTool("set_pr_status", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.WithDescription(t("TOOL_SET_PR_STATUS_DESCRIPTION", "Set pull request status between draft and ready-for-review states. Use this to change a pull request from draft to ready-for-review or vice versa.")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Title: t("TOOL_SET_PR_STATUS_USER_TITLE", "Set pull request status"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ReadOnlyHint: toBoolPtr(false), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.WithString("owner", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Required(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Description("Repository owner"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.WithString("repo", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Required(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Description("Repository name"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.WithNumber("pullNumber", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Required(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Description("Pull request number"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.WithString("status", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Required(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Description("Target status for the pull request"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mcp.Enum("draft", "ready_for_review"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var params struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Owner string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Repo string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PullNumber int32 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Status string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err := mapstructure.Decode(request.Params.Arguments, ¶ms); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultError(err.Error()), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Validate status parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if params.Status != "draft" && params.Status != "ready_for_review" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultError("status must be either 'draft' or 'ready_for_review'"), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1639
to
+1653
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider using the existing parameter helpers (e.g.,
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Get the GraphQL client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client, err := getGQLClient(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultError(fmt.Sprintf("failed to get GitHub GraphQL client: %v", err)), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// First, we need to get the GraphQL ID of the pull request and its current status | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var getPullRequestQuery struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Repository struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PullRequest struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ID githubv4.ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IsDraft githubv4.Boolean | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} `graphql:"pullRequest(number: $prNum)"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} `graphql:"repository(owner: $owner, name: $repo)"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vars := map[string]any{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"owner": githubv4.String(params.Owner), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"repo": githubv4.String(params.Repo), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"prNum": githubv4.Int(params.PullNumber), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err := client.Query(ctx, &getPullRequestQuery, vars); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultError(fmt.Sprintf("failed to get pull request: %v", err)), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
currentIsDraft := bool(getPullRequestQuery.Repository.PullRequest.IsDraft) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
targetIsDraft := params.Status == "draft" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Check if the PR is already in the target state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if currentIsDraft == targetIsDraft { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if targetIsDraft { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultText("Pull request is already in draft state"), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultText("Pull request is already marked as ready for review"), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Perform the appropriate mutation based on target status | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if targetIsDraft { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Convert to draft | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var convertToDraftMutation struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ConvertPullRequestToDraft struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PullRequest struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ID githubv4.ID // Required by GraphQL schema, but not used in response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} `graphql:"convertPullRequestToDraft(input: $input)"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
input := githubv4.ConvertPullRequestToDraftInput{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PullRequestID: getPullRequestQuery.Repository.PullRequest.ID, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err := client.Mutate(ctx, &convertToDraftMutation, input, nil); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultError(fmt.Sprintf("failed to convert pull request to draft: %v", err)), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultText("Pull request successfully converted to draft"), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Mark as ready for review | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var markReadyForReviewMutation struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MarkPullRequestReadyForReview struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PullRequest struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ID githubv4.ID // Required by GraphQL schema, but not used in response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} `graphql:"markPullRequestReadyForReview(input: $input)"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
input := githubv4.MarkPullRequestReadyForReviewInput{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PullRequestID: getPullRequestQuery.Repository.PullRequest.ID, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err := client.Mutate(ctx, &markReadyForReviewMutation, input, nil); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultError(fmt.Sprintf("failed to mark pull request as ready for review: %v", err)), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mcp.NewToolResultText("Pull request successfully marked as ready for review"), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] You're validating against raw string literals; consider defining constants or an enum type for the allowed statuses to avoid typos and improve maintainability.
Copilot uses AI. Check for mistakes.