-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add get_prs_reviewed_by tool for direct review lookup #1971
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
brettfire
wants to merge
2
commits into
github:main
Choose a base branch
from
brettfire:feat/get-prs-reviewed-by
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.
+479
−6
Open
Changes from all commits
Commits
Show all changes
2 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
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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/go-viper/mapstructure/v2" | ||
| "github.com/google/go-github/v79/github" | ||
|
|
@@ -29,8 +30,8 @@ func PullRequestRead(t translations.TranslationHelperFunc) inventory.ServerTool | |
| Properties: map[string]*jsonschema.Schema{ | ||
| "method": { | ||
| Type: "string", | ||
| Description: `Action to specify what pull request data needs to be retrieved from GitHub. | ||
| Possible options: | ||
| Description: `Action to specify what pull request data needs to be retrieved from GitHub. | ||
| Possible options: | ||
| 1. get - Get details of a specific pull request. | ||
| 2. get_diff - Get the diff of a pull request. | ||
| 3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks. | ||
|
|
@@ -1046,6 +1047,10 @@ func ListPullRequests(t translations.TranslationHelperFunc) inventory.ServerTool | |
| Description: "Sort direction", | ||
| Enum: []any{"asc", "desc"}, | ||
| }, | ||
| "author": { | ||
| Type: "string", | ||
| Description: "Filter by PR author username (client-side filter)", | ||
| }, | ||
| }, | ||
| Required: []string{"owner", "repo"}, | ||
| } | ||
|
|
@@ -1055,7 +1060,7 @@ func ListPullRequests(t translations.TranslationHelperFunc) inventory.ServerTool | |
| ToolsetMetadataPullRequests, | ||
| mcp.Tool{ | ||
| Name: "list_pull_requests", | ||
| Description: t("TOOL_LIST_PULL_REQUESTS_DESCRIPTION", "List pull requests in a GitHub repository. If the user specifies an author, then DO NOT use this tool and use the search_pull_requests tool instead."), | ||
| Description: t("TOOL_LIST_PULL_REQUESTS_DESCRIPTION", "List pull requests in a GitHub repository. If the user specifies an author, then DO NOT use this tool and use the search_pull_requests tool instead. If you receive a 422 error from search_pull_requests, then use the get_prs_reviewed_by tool (to list by reviewer), or this tool with the author parameter (for filtering by author) depending on what you need."), | ||
| Annotations: &mcp.ToolAnnotations{ | ||
| Title: t("TOOL_LIST_PULL_REQUESTS_USER_TITLE", "List pull requests"), | ||
| ReadOnlyHint: true, | ||
|
|
@@ -1092,6 +1097,10 @@ func ListPullRequests(t translations.TranslationHelperFunc) inventory.ServerTool | |
| if err != nil { | ||
| return utils.NewToolResultError(err.Error()), nil, nil | ||
| } | ||
| author, err := OptionalParam[string](args, "author") | ||
| if err != nil { | ||
| return utils.NewToolResultError(err.Error()), nil, nil | ||
| } | ||
| pagination, err := OptionalPaginationParams(args) | ||
| if err != nil { | ||
| return utils.NewToolResultError(err.Error()), nil, nil | ||
|
|
@@ -1131,6 +1140,18 @@ func ListPullRequests(t translations.TranslationHelperFunc) inventory.ServerTool | |
| return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to list pull requests", resp, bodyBytes), nil, nil | ||
| } | ||
|
|
||
| // Filter by author if specified (client-side filtering) | ||
| if author != "" { | ||
| filtered := make([]*github.PullRequest, 0) | ||
| for _, pr := range prs { | ||
| if pr != nil && pr.User != nil && pr.User.Login != nil && | ||
| strings.EqualFold(*pr.User.Login, author) { | ||
| filtered = append(filtered, pr) | ||
| } | ||
| } | ||
| prs = filtered | ||
| } | ||
|
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. Add some comments for the new function |
||
|
|
||
| // sanitize title/body on each PR | ||
| for _, pr := range prs { | ||
| if pr == nil { | ||
|
|
@@ -1153,6 +1174,154 @@ func ListPullRequests(t translations.TranslationHelperFunc) inventory.ServerTool | |
| }) | ||
| } | ||
|
|
||
| // GetPRsReviewedBy creates a tool for finding PRs reviewed by a specific user | ||
| func GetPRsReviewedBy(t translations.TranslationHelperFunc) inventory.ServerTool { | ||
| schema := &jsonschema.Schema{ | ||
| Type: "object", | ||
| Properties: map[string]*jsonschema.Schema{ | ||
| "owner": { | ||
| Type: "string", | ||
| Description: "Repository owner", | ||
| }, | ||
| "repo": { | ||
| Type: "string", | ||
| Description: "Repository name", | ||
| }, | ||
| "reviewer": { | ||
| Type: "string", | ||
| Description: "GitHub username of the reviewer", | ||
| }, | ||
| "state": { | ||
| Type: "string", | ||
| Description: "PR state filter: open, closed, or all", | ||
| Enum: []any{"open", "closed", "all"}, | ||
| }, | ||
| }, | ||
| Required: []string{"owner", "repo", "reviewer"}, | ||
| } | ||
| WithPagination(schema) | ||
|
|
||
| return NewTool( | ||
| ToolsetMetadataPullRequests, | ||
| mcp.Tool{ | ||
| Name: "get_prs_reviewed_by", | ||
| Description: t("TOOL_GET_PRS_REVIEWED_BY_DESCRIPTION", | ||
| "Find PRs reviewed by a user. Use this tool if you receive a 422 error when using the search_pull_requests tool."), | ||
| Annotations: &mcp.ToolAnnotations{ | ||
| Title: t("TOOL_GET_PRS_REVIEWED_BY_TITLE", "Get PRs reviewed by user"), | ||
| ReadOnlyHint: true, | ||
| }, | ||
| InputSchema: schema, | ||
| }, | ||
| []scopes.Scope{scopes.Repo}, | ||
| func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) { | ||
| owner, err := RequiredParam[string](args, "owner") | ||
| if err != nil { | ||
| return utils.NewToolResultError(err.Error()), nil, nil | ||
| } | ||
| repo, err := RequiredParam[string](args, "repo") | ||
| if err != nil { | ||
| return utils.NewToolResultError(err.Error()), nil, nil | ||
| } | ||
| reviewer, err := RequiredParam[string](args, "reviewer") | ||
| if err != nil { | ||
| return utils.NewToolResultError(err.Error()), nil, nil | ||
| } | ||
| state, err := OptionalParam[string](args, "state") | ||
| if err != nil { | ||
| return utils.NewToolResultError(err.Error()), nil, nil | ||
| } | ||
| if state == "" { | ||
| state = "all" | ||
| } | ||
|
|
||
| opts := &github.PullRequestListOptions{ | ||
| State: state, | ||
| ListOptions: github.ListOptions{ | ||
| PerPage: 100, | ||
| }, | ||
| } | ||
|
|
||
| pagination, err := OptionalPaginationParams(args) | ||
| if err != nil { | ||
| return utils.NewToolResultError(err.Error()), nil, nil | ||
| } | ||
| if pagination.Page > 0 { | ||
| opts.ListOptions.Page = pagination.Page | ||
| } | ||
| if pagination.PerPage > 0 { | ||
| opts.ListOptions.PerPage = pagination.PerPage | ||
| } | ||
|
|
||
| client, err := deps.GetClient(ctx) | ||
| if err != nil { | ||
| return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil | ||
| } | ||
|
|
||
| // List all PRs | ||
| prs, resp, err := client.PullRequests.List(ctx, owner, repo, opts) | ||
| if err != nil { | ||
| return ghErrors.NewGitHubAPIErrorResponse(ctx, | ||
| "failed to list pull requests", | ||
| resp, | ||
| err, | ||
| ), nil, nil | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
|
|
||
| // Filter PRs by reviewer | ||
| var reviewedPRs []*github.PullRequest | ||
| for _, pr := range prs { | ||
| if pr.Number == nil { | ||
| continue | ||
| } | ||
| reviews, _, err := client.PullRequests.ListReviews(ctx, owner, repo, *pr.Number, nil) | ||
| if err != nil { | ||
| continue // Skip PRs we can't get reviews for | ||
| } | ||
| for _, review := range reviews { | ||
| if review.User != nil && review.User.Login != nil && | ||
| strings.EqualFold(*review.User.Login, reviewer) { | ||
| reviewedPRs = append(reviewedPRs, pr) | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Sanitize the results | ||
| sanitized := make([]map[string]any, 0, len(reviewedPRs)) | ||
| for _, pr := range reviewedPRs { | ||
| if pr.Title != nil { | ||
| pr.Title = github.Ptr(sanitize.Sanitize(*pr.Title)) | ||
| } | ||
| if pr.Body != nil { | ||
| pr.Body = github.Ptr(sanitize.Sanitize(*pr.Body)) | ||
| } | ||
| sanitized = append(sanitized, map[string]any{ | ||
| "number": pr.GetNumber(), | ||
| "title": pr.GetTitle(), | ||
| "state": pr.GetState(), | ||
| "html_url": pr.GetHTMLURL(), | ||
| "user": pr.GetUser().GetLogin(), | ||
| "draft": pr.GetDraft(), | ||
| }) | ||
| } | ||
|
|
||
| result := map[string]any{ | ||
| "pull_requests": sanitized, | ||
| "total_count": len(sanitized), | ||
| } | ||
|
|
||
| r, err := json.Marshal(result) | ||
| if err != nil { | ||
| return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil | ||
| } | ||
|
|
||
| return utils.NewToolResultText(string(r)), nil, nil | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| // MergePullRequest creates a tool to merge a pull request. | ||
| func MergePullRequest(t translations.TranslationHelperFunc) inventory.ServerTool { | ||
| schema := &jsonschema.Schema{ | ||
|
|
@@ -1310,7 +1479,7 @@ func SearchPullRequests(t translations.TranslationHelperFunc) inventory.ServerTo | |
| ToolsetMetadataPullRequests, | ||
| mcp.Tool{ | ||
| Name: "search_pull_requests", | ||
| Description: t("TOOL_SEARCH_PULL_REQUESTS_DESCRIPTION", "Search for pull requests in GitHub repositories using issues search syntax already scoped to is:pr"), | ||
| Description: t("TOOL_SEARCH_PULL_REQUESTS_DESCRIPTION", "Search for pull requests in GitHub repositories using issues search syntax already scoped to is:pr. If you receive a 422 error, then use the get_prs_reviewed_by tool instead."), | ||
| Annotations: &mcp.ToolAnnotations{ | ||
| Title: t("TOOL_SEARCH_PULL_REQUESTS_USER_TITLE", "Search pull requests"), | ||
| ReadOnlyHint: true, | ||
|
|
||
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.
The description for
TOOL_LIST_PULL_REQUESTS_DESCRIPTIONis getting a bit long and complex. Consider rephrasing for clarity or splitting it into multiple sentences.