Skip to content

Implement search_code handler with tests #95

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

Closed
Tiberriver256 opened this issue Apr 1, 2025 · 0 comments
Closed

Implement search_code handler with tests #95

Tiberriver256 opened this issue Apr 1, 2025 · 0 comments

Comments

@Tiberriver256
Copy link
Owner

Tiberriver256 commented Apr 1, 2025

Implement search_code Handler with Tests

The search_code handler will enable querying for code across Azure DevOps repositories using the Azure DevOps Search REST API. This feature will allow users to search for specific code snippets, files, or patterns across all accessible repositories.

Implementation Details

The Azure DevOps Search API is separate from the core Azure DevOps API and requires a different endpoint structure:

  • Base URL: https://almsearch.dev.azure.com/{organization}/{project}/_apis/search/codesearchresults
  • Method: POST
  • Authentication: Same as other Azure DevOps API endpoints
  • API Version: 7.1

Note: The azure-devops-node-api library does not directly support the Search API, so we'll need to implement custom HTTP requests.

Important Addition: Even though the Search API doesn't directly return full code content, our implementation should fetch and return the actual code for each search result to avoid requiring additional API calls. This will require making additional GitAPI calls to fetch file content after receiving search results.

Subtasks

  1. Schema Definition

    • Create SearchCodeSchema in a new file under src/features/search/schemas.ts
    • Include parameters:
      • searchText (required): The text to search for
      • projectId (required): The project to search in
      • filters (optional): Repository, path, branch, and code element filters
      • top (optional): Number of results to return (default: 100)
      • skip (optional): Number of results to skip (pagination)
      • includeSnippet (optional): Whether to include code snippets in results (default: true)
      • includeContent (optional): Whether to include full file content in results (default: true)
  2. Types Definition

    • Create types for the request and response objects
    • Define interfaces for code search results, matches, and filters
    • Add content property to result objects to hold full file content
    • Place in src/features/search/types.ts
  3. Feature Implementation

    • Create feature directory structure: src/features/search/search-code/
    • Implement feature.ts with the main searchCode function
    • Handle proper error mapping and API response transformation
    • Use Axios or similar for direct API calls to the Search API
    • For each search result, make an additional call to the Git API to fetch full file content
    • Ensure proper authentication and parameter validation
  4. Integration with Server

    • Add the new tool to server.ts
    • Add schema to ListToolsRequestSchema response
    • Add handler in the request handler switch statement
  5. Unit Tests

    • Create feature.spec.unit.ts with mock API responses
    • Test various combinations of parameters
    • Test file content retrieval functionality
    • Ensure error cases are properly handled
  6. Integration Tests

    • Create feature.spec.int.ts to test against a real Azure DevOps instance
    • Test basic search functionality
    • Test filtering and pagination
    • Test file content retrieval
    • Skip if integration credentials aren't available
  7. Documentation

    • Document the new tool in docs/tools/search.md
    • Include examples of common search queries
    • Document all parameters and response format
    • Highlight the file content retrieval feature

API Sample Request

{
  "searchText": "function searchCode",
  "$skip": 0,
  "$top": 10,
  "filters": {
    "Project": ["MyProject"],
    "Repository": ["MyRepo"],
    "Path": ["/src"],
    "Branch": ["main"],
    "CodeElement": ["function", "class"]
  },
  "includeFacets": true,
  "includeSnippet": true,
  "includeContent": true
}

API Sample Response Structure

{
  "count": 1,
  "results": [
    {
      "fileName": "example.ts",
      "path": "/src/example.ts",
      "content": "import { WebApi } from 'azure-devops-node-api';\n\n/**\n * Search code in repositories\n */\nexport async function searchCode(connection, options) {\n  // Implementation details\n  return results;\n}\n",
      "matches": {
        "content": [
          {
            "charOffset": 120,
            "length": 20
          }
        ]
      },
      "collection": {
        "name": "DefaultCollection"
      },
      "project": {
        "name": "MyProject",
        "id": "project-guid"
      },
      "repository": {
        "name": "MyRepo",
        "id": "repo-guid",
        "type": "git"
      },
      "versions": [
        {
          "branchName": "main",
          "changeId": "commit-hash"
        }
      ],
      "contentId": "content-hash"
    }
  ]
}

Notes

  • The Search API uses a different base URL (almsearch.dev.azure.com) than the regular Azure DevOps API
  • Due to the lack of direct support in azure-devops-node-api, we'll need to implement custom HTTP requests
  • Pagination works differently from some other Azure DevOps APIs, using $skip and $top parameters in the request body instead of continuation tokens
  • Our implementation will enhance the API response by adding full file content, which requires additional API calls
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

No branches or pull requests

1 participant