diff options
| author | Dawid Rycerz <dawid@rycerz.xyz> | 2025-05-28 15:58:05 +0200 |
|---|---|---|
| committer | Dawid Rycerz <dawid@rycerz.xyz> | 2025-05-28 15:58:05 +0200 |
| commit | 126ce542dca21e62b38049f67c7f4c951284a2c2 (patch) | |
| tree | 467d91d26bb7a29bb0ddc4ab3149e8c2d99e77f6 /servers/gitlab_glab/src/mcp_server_gitlab_glab/server.py | |
| parent | 54b521f19e00bea52304f7379ca59de0c2e20962 (diff) | |
feat(gitlab_glab): add diff content filtering to get_mr_diff
- Add filter_diff_content method to filter out unwanted file extensions
- Default filtering excludes .lock and .log files to reduce noise
- Smart lock file detection handles package-lock.json, yarn.lock, etc.
- Customizable filtering via filter_extensions parameter
- Filtering can be disabled by passing empty list
- Add comprehensive tests for all filtering scenarios
- Update README.md with filtering documentation
- Maintain backward compatibility with existing API
Diffstat (limited to 'servers/gitlab_glab/src/mcp_server_gitlab_glab/server.py')
| -rw-r--r-- | servers/gitlab_glab/src/mcp_server_gitlab_glab/server.py | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/servers/gitlab_glab/src/mcp_server_gitlab_glab/server.py b/servers/gitlab_glab/src/mcp_server_gitlab_glab/server.py index b661255..f239246 100644 --- a/servers/gitlab_glab/src/mcp_server_gitlab_glab/server.py +++ b/servers/gitlab_glab/src/mcp_server_gitlab_glab/server.py @@ -310,6 +310,63 @@ class GitLabServer: logger.error(f"Failed to extract issue URL from output: {result}") return {"error": "Failed to extract issue URL from command output"} + def filter_diff_content( + self, + diff_content: str, + exclude_extensions: list[str], + ) -> str: + """Filter out files with specified extensions from diff content. + + Args: + diff_content: The original diff content. + exclude_extensions: List of file extensions to exclude + (e.g., [".lock", ".log"]). + + Returns: + Filtered diff content with excluded files removed. + """ + if not exclude_extensions: + return diff_content + + lines = diff_content.split('\n') + filtered_lines = [] + skip_block = False + + for line in lines: + if line.startswith("diff --git a/"): + parts = line.split() + if len(parts) >= 4: + file_a = parts[2] + file_b = parts[3] + # Check if either file should be excluded + should_exclude = False + for ext in exclude_extensions: + # Handle both exact extension matches and lock file patterns + if ext == ".lock": + # Special handling for lock files + # (package-lock.json, yarn.lock, etc.) + if (file_a.endswith('.lock') or + file_b.endswith('.lock') or + 'lock.' in file_a or 'lock.' in file_b or + file_a.endswith('-lock.json') or + file_b.endswith('-lock.json')): + should_exclude = True + break + else: + # Standard extension matching + if file_a.endswith(ext) or file_b.endswith(ext): + should_exclude = True + break + + skip_block = should_exclude + else: + skip_block = False + + if not skip_block: + filtered_lines.append(line) + + return '\n'.join(filtered_lines) + def get_mr_diff( self, working_directory: str, @@ -318,6 +375,7 @@ class GitLabServer: raw: bool = False, repo: str | None = None, max_size_kb: int = 100, + filter_extensions: list[str] | None = None, ) -> dict[str, Any]: """Get the diff for a merge request. @@ -329,11 +387,17 @@ class GitLabServer: repo: Select another repository (OWNER/REPO or GROUP/NAMESPACE/REPO format). max_size_kb: Maximum size in KB before saving to temporary file (default: 100). + filter_extensions: List of file extensions to exclude from diff + (default: [".lock", ".log"]). Returns: A dictionary containing the diff content or a path to temporary file if too large. """ + # Set default filter extensions if not provided + if filter_extensions is None: + filter_extensions = [".lock", ".log"] + # Build command arguments args = ["mr", "diff"] @@ -359,8 +423,10 @@ class GitLabServer: if not success: return result + # Apply filtering to remove unwanted file extensions + diff_content = self.filter_diff_content(result, filter_extensions) + # Check if the diff is too large - diff_content = result diff_size_kb = len(diff_content.encode('utf-8')) / 1024 if diff_size_kb > max_size_kb: @@ -664,6 +730,7 @@ def create_server(host: str = "127.0.0.1", port: int = 8080) -> FastMCP: raw: bool = False, repo: str | None = None, max_size_kb: int = 100, + filter_extensions: list[str] | None = None, ) -> dict[str, Any]: """Get the diff for a merge request. @@ -675,6 +742,8 @@ def create_server(host: str = "127.0.0.1", port: int = 8080) -> FastMCP: repo: Select another repository (OWNER/REPO or GROUP/NAMESPACE/REPO format). max_size_kb: Maximum size in KB before saving to temporary file (default: 100). + filter_extensions: List of file extensions to exclude from diff + (default: [".lock", ".log"]). Returns: A dictionary containing the diff content or a path to temporary file @@ -687,6 +756,7 @@ def create_server(host: str = "127.0.0.1", port: int = 8080) -> FastMCP: raw=raw, repo=repo, max_size_kb=max_size_kb, + filter_extensions=filter_extensions, ) @mcp.tool() |
