Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions pkg/github/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions pkg/github/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 != "" {
Expand Down
36 changes: 36 additions & 0 deletions pkg/github/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
10 changes: 10 additions & 0 deletions pkg/github/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/github/pullrequests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions pkg/github/search_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quoted case is real, but it comes from hasFilter and predates this PR. On main, prepareSearchArgs with owner: "octocat", repo: "hello" and query: label:"repo:octocat/project" bug already returns is:issue label:"repo:octocat/project" bug without the repo:octocat/hello scope. The regex (^|\s|\W)repo:\S+ treats the " as a boundary. This PR doesn't change which queries count as having a repo filter; it only stops dropping a lone owner or repo when there isn't one.

Making hasFilter skip quoted text would also change is: and type: detection, so I'd rather keep it out of this PR and send it separately.

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}
Expand Down