Skip to content

Fix duplicate filter addition in search_issues tool #828

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gokhanarkan
Copy link
Member

@gokhanarkan gokhanarkan commented Aug 6, 2025

Closes: #817

Problem

The search_issues tool was incorrectly adding duplicate repo: and is: filters to user queries, causing GitHub API validation errors when users provided queries that already contained these filters. This issue also affected users with complex queries containing multiple OR operators, as the duplicate filters contributed to exceeding GitHub's 5 AND/OR/NOT operator limit.

Example of the bug:

  • Input query: "repo:github/github-mcp-server is:issue is:open (label:critical OR label:urgent)"
  • Before fix: "is:issue repo:github/github-mcp-server is:issue is:open (label:critical OR label:urgent)"
  • After fix: "repo:github/github-mcp-server is:issue is:open (label:critical OR label:urgent)"

Solution

Implemented intelligent filter detection to prevent duplicate filters with a clean, idiomatic approach:

Helper Functions

  • hasSpecificFilter(query, filterType, filterValue): Detects exact filter:value combinations (e.g., is:issue)
  • hasRepoFilter(query): Simple check for existing repo: filters
  • hasFilter(query, filterType): General filter type detection

Updated Logic

  • is: filters: Only add is:searchType if the exact combination doesn't already exist
  • repo: filters: Only add from owner/repo parameters if query doesn't already have a repo filter
  • Simple approach: No complex conflict resolution - just prevent duplicates

The core fix is in searchHandler():

// Only add is:searchType if exact filter doesn't exist
if !hasSpecificFilter(query, "is", searchType) {
    query = fmt.Sprintf("is:%s %s", searchType, query)
}

// Only add repo filter if none exists
if owner != "" && repo != "" && !hasRepoFilter(query) {
    query = fmt.Sprintf("repo:%s/%s %s", owner, repo, query)
}

Team Discussion Context

This implementation follows the approach agreed upon in our team discussion:

  • Consensus: Implement smart deduplication as a short-term fix
  • Scope: Fix our API's double-encoding behavior, not validate GitHub's search syntax
  • Simplicity: Keep logic simple - just prevent duplicate filter addition
  • Tool integrity: Maintain semantic separation between search_issues and search_pull_requests
  • User experience: Prevent frustrating API validation errors from duplicate filters

Benefits

Prevents API errors: No more "More than five AND/OR/NOT operators were used" errors
Maintains user intent: Existing filters in queries are preserved
Backward compatible: Existing functionality preserved
Clean & maintainable: Simple, focused functions following Go idioms
Applies to both tools: Fixes both search_issues and search_pull_requests (both use searchHandler)

Testing

Added comprehensive test coverage:

  • 13 test cases for hasFilter() function
  • 10 test cases for hasRepoFilter() function
  • 12 test cases for hasSpecificFilter() function
  • 4 integration test cases for duplicate filter scenarios
  • Updated existing tests to match corrected behavior

All tests pass with 100% coverage of new logic.

Alternatives Considered

  1. Complex conflict resolution: Parse and resolve conflicts between query filters and parameters

    • Discarded: Added unnecessary complexity; conflicts are rare and simple prevention is sufficient
  2. GitHub API validation: Let the API handle duplicate filters

    • Discarded: API returns validation errors, poor user experience
  3. Query parsing library: Use a dedicated GitHub search query parser

    • Discarded: Added significant dependency for a focused problem
  4. String replacement: Remove existing filters before adding new ones

    • Discarded: Too fragile, could break legitimate queries with filter-like text

The fix ensures that users can confidently use complex GitHub search queries without encountering unexpected API validation errors, while maintaining the convenience of automatic filter addition for simple queries. The implementation prioritizes simplicity and maintainability over handling edge cases that rarely occur in practice.

gokhanarkan and others added 3 commits August 6, 2025 19:58
…ions

- Added multiple test cases for searching issues with various query filters in `issues_test.go`.
- Introduced utility functions in `search_utils.go` to check for specific filters and extract repository information from queries.
- Created comprehensive tests in `search_utils_test.go` to validate the new filtering logic and ensure accurate query parsing.
- Renamed `extractRepoFilter` to `hasRepoFilter` to simplify the function's purpose.
- Updated test cases in `search_utils_test.go` to reflect the new function name and logic.
- Adjusted tests to focus on the presence of the `repo:` filter rather than extracting owner and repo details.
@shreyabaid007
Copy link

shreyabaid007 commented Aug 9, 2025

Thanks @gokhanarkan for taking up my issue #817. This is a good approach to fixing the duplicate filter issue.

I know this is still in draft. However, I've identified that the exact same pattern as the original bug also exists in the userOrOrgHandler function and needs to be addressed as well.

DUPLICATE FILTER ISSUE IN search.go

searchQuery := "type:" + accountType + " " + query
  • Impact: If user query already contains type:user or type:org, creates duplicates and results in HTTP 422 Validation Failed

  • Total Affected Tools: search_users, search_pull_requests, search_users and search_orgs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: search_issues tool creates duplicate filters causing 422 validation errors
2 participants