diff --git a/pkg/github/issues_test.go b/pkg/github/issues_test.go index 79eeb22100..98c9ed719a 100644 --- a/pkg/github/issues_test.go +++ b/pkg/github/issues_test.go @@ -1184,7 +1184,7 @@ func Test_SearchIssues(t *testing.T) { expectedResult: mockSearchResult, }, { - name: "issues search with only owner parameter (should ignore it)", + name: "issues search with only owner parameter is rejected", mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ GetSearchIssues: expectQueryParams( t, @@ -1202,11 +1202,11 @@ func Test_SearchIssues(t *testing.T) { "query": "bug", "owner": "test-owner", }, - expectError: false, - expectedResult: mockSearchResult, + expectError: true, + expectedErrMsg: "owner and repo must be provided together", }, { - name: "issues search with only repo parameter (should ignore it)", + name: "issues search with only repo parameter is rejected", mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ GetSearchIssues: expectQueryParams( t, @@ -1224,8 +1224,8 @@ func Test_SearchIssues(t *testing.T) { "query": "feature", "repo": "test-repo", }, - expectError: false, - expectedResult: mockSearchResult, + expectError: true, + expectedErrMsg: "owner and repo must be provided together", }, { name: "issues search with minimal parameters", @@ -1282,6 +1282,28 @@ func Test_SearchIssues(t *testing.T) { expectError: false, expectedResult: mockSearchResult, }, + { + name: "query with existing repo: filter and only owner param - uses query filter", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ + GetSearchIssues: expectQueryParams( + t, + map[string]string{ + "q": "is:issue repo:github/github-mcp-server critical", + "page": "1", + "per_page": "30", + "search_type": "semantic", + }, + ).andThen( + mockResponse(t, http.StatusOK, mockSearchResult), + ), + }), + requestArgs: map[string]any{ + "query": "repo:github/github-mcp-server critical", + "owner": "github", + }, + expectError: false, + expectedResult: mockSearchResult, + }, { name: "query with both is: and repo: filters already present", mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ diff --git a/pkg/github/notifications.go b/pkg/github/notifications.go index 332d2ffd3b..005b1fdd84 100644 --- a/pkg/github/notifications.go +++ b/pkg/github/notifications.go @@ -98,6 +98,9 @@ func ListNotifications(t translations.TranslationHelperFunc) inventory.ServerToo if err != nil { return utils.NewToolResultError(err.Error()), nil, nil } + if err := checkOwnerRepoPair(owner, repo); err != nil { + return utils.NewToolResultError(err.Error()), nil, nil + } paginationParams, err := OptionalPaginationParams(args) if err != nil { @@ -307,6 +310,9 @@ func MarkAllNotificationsRead(t translations.TranslationHelperFunc) inventory.Se if err != nil { return utils.NewToolResultError(err.Error()), nil, nil } + if err := checkOwnerRepoPair(owner, repo); err != nil { + return utils.NewToolResultError(err.Error()), nil, nil + } var lastReadTime time.Time if lastReadAt != "" { diff --git a/pkg/github/notifications_test.go b/pkg/github/notifications_test.go index 0fb37eebc8..4b31333137 100644 --- a/pkg/github/notifications_test.go +++ b/pkg/github/notifications_test.go @@ -95,6 +95,24 @@ func Test_ListNotifications(t *testing.T) { expectError: false, expectedResult: []*github.Notification{mockNotification}, }, + { + name: "owner without repo", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ + GetNotifications: mockResponse(t, http.StatusOK, []*github.Notification{mockNotification}), + }), + requestArgs: map[string]any{"owner": "octocat"}, + expectError: true, + expectedErrMsg: "owner and repo must be provided together", + }, + { + name: "repo without owner", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ + GetNotifications: mockResponse(t, http.StatusOK, []*github.Notification{mockNotification}), + }), + requestArgs: map[string]any{"repo": "hello-world"}, + expectError: true, + expectedErrMsg: "owner and repo must be provided together", + }, { name: "error", mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ @@ -624,6 +642,24 @@ func Test_MarkAllNotificationsRead(t *testing.T) { expectError: false, expectMarked: true, }, + { + name: "owner without repo", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ + PutNotifications: mockResponse(t, http.StatusOK, nil), + }), + requestArgs: map[string]any{"owner": "octocat"}, + expectError: true, + expectedErrMsg: "owner and repo must be provided together", + }, + { + name: "repo without owner", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ + PutNotifications: mockResponse(t, http.StatusOK, nil), + }), + requestArgs: map[string]any{"repo": "hello-world"}, + expectError: true, + expectedErrMsg: "owner and repo must be provided together", + }, { name: "API error", mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ diff --git a/pkg/github/params.go b/pkg/github/params.go index 9e1b582714..e0692c025e 100644 --- a/pkg/github/params.go +++ b/pkg/github/params.go @@ -54,6 +54,16 @@ func OptionalNullableStringParam(args map[string]any, p string) (*string, bool, return &stringValue, true, nil } +// checkOwnerRepoPair rejects an optional owner without repo, or repo without +// owner. Tools that scope to a repository only when both are set would +// otherwise drop the lone value and act on every repository. +func checkOwnerRepoPair(owner, repo string) error { + if (owner == "") != (repo == "") { + return errors.New("owner and repo must be provided together") + } + return nil +} + // isAcceptedError checks if the error is an accepted error. func isAcceptedError(err error) bool { var acceptedError *github.AcceptedError diff --git a/pkg/github/pullrequests_test.go b/pkg/github/pullrequests_test.go index 2241598eda..30660cc5e0 100644 --- a/pkg/github/pullrequests_test.go +++ b/pkg/github/pullrequests_test.go @@ -1045,7 +1045,7 @@ func Test_SearchPullRequests(t *testing.T) { expectedResult: mockSearchResult, }, { - name: "pull request search with only owner parameter (should ignore it)", + name: "pull request search with only owner parameter is rejected", mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ GetSearchIssues: expectQueryParams( t, @@ -1062,11 +1062,11 @@ func Test_SearchPullRequests(t *testing.T) { "query": "feature", "owner": "test-owner", }, - expectError: false, - expectedResult: mockSearchResult, + expectError: true, + expectedErrMsg: "owner and repo must be provided together", }, { - name: "pull request search with only repo parameter (should ignore it)", + name: "pull request search with only repo parameter is rejected", mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ GetSearchIssues: expectQueryParams( t, @@ -1083,8 +1083,8 @@ func Test_SearchPullRequests(t *testing.T) { "query": "review-required", "repo": "test-repo", }, - expectError: false, - expectedResult: mockSearchResult, + expectError: true, + expectedErrMsg: "owner and repo must be provided together", }, { name: "pull request search with minimal parameters", diff --git a/pkg/github/search_utils.go b/pkg/github/search_utils.go index 0f62e8b26e..8269f040c7 100644 --- a/pkg/github/search_utils.go +++ b/pkg/github/search_utils.go @@ -108,8 +108,13 @@ func prepareSearchArgs(input SearchIssuesInput, targetType string, mode searchMo query = fmt.Sprintf("is:%s %s", targetType, query) } - if input.Owner != "" && input.Repo != "" && !hasRepoFilter(query) { - query = fmt.Sprintf("repo:%s/%s %s", input.Owner, input.Repo, query) + if !hasRepoFilter(query) { + if err := checkOwnerRepoPair(input.Owner, input.Repo); err != nil { + return "", nil, err + } + if input.Owner != "" { + query = fmt.Sprintf("repo:%s/%s %s", input.Owner, input.Repo, query) + } } pagination := PaginationParams{Page: 1, PerPage: 30}