get_review_changes
Retrieve file differences in GitLab merge requests to identify changes made during code review. Supports integration with the Model Context Protocol (MCP) server for efficient pipeline and review management.
Instructions
GitLab MRで修正したファイルの差分を取得
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Input Schema (JSON Schema)
{
"properties": {},
"title": "get_review_changesArguments",
"type": "object"
}
Implementation Reference
- main.py:63-77 (handler)The main handler function for the 'get_review_changes' tool, decorated with @mcp.tool() for registration. It retrieves the current MR ID, fetches the changes using get_mr_changes, and formats a review prompt.@mcp.tool() def get_review_changes() -> str: """GitLab MRで修正したファイルの差分を取得""" mr_id = get_current_mr_id() changes = get_mr_changes(mr_id=mr_id) if changes: return f""" 以下の変更について @Codebase を考慮してレビューし、コードの問題点や改善点を出してください。 {changes} """ else: return "変更内容を取得できません。"
- src/utils/gitlab_utils.py:327-370 (helper)Helper function that fetches the MR details, extracts the base_sha, and computes the local git diff from base to current using get_diff_from_base.def get_mr_changes(mr_id: int) -> str: """ 指定したMR IDに関連するMRのベースコミット(base_sha)から現在のローカルの状態までの差分を取得します。 リモートの差分ではなく、ローカルの最新状態との差分を取得します。 Args: mr_id (int): Merge Request ID Returns: str: MRのベースコミットから現在のローカル状態までの変更内容(差分) Raises: ValueError: 変更内容の取得に失敗した場合 """ try: project = get_gitlab_project() # MRを取得 try: mr: MergeRequest = project.mergerequests.get(mr_id) except gitlab.exceptions.GitlabGetError: return f"MR ID #{mr_id} が見つかりません。" # MRのdiff_refsからbase_shaを取得 if not hasattr(mr, "diff_refs") or not mr.diff_refs: return f"MR #{mr.iid} の差分情報が取得できません。" base_sha: Optional[str] = mr.diff_refs.get("base_sha") if not base_sha: return f"MR #{mr.iid} のベースコミットが特定できません。" # ローカルリポジトリでbase_shaからの差分を取得 try: diff_output = get_diff_from_base(base_sha) if not diff_output or diff_output == "変更されたファイルはありません。": return f"MR #{mr.iid} (ベースコミット: {base_sha}) からの変更はありません。" return diff_output except Exception as e: return f"ローカルリポジトリからの差分取得に失敗しました: {str(e)}" except Exception as e: raise ValueError(f"MRの変更内容の取得に失敗しました: {str(e)}")
- src/utils/git_utils.py:109-173 (helper)Helper function that computes and formats the git diff from the base SHA to the current local repository state, including staged and unstaged changes.def get_diff_from_base(base_sha: str) -> str: """ 指定されたベースSHAから現在の状態までの差分を取得します。 ローカルリポジトリの変更を含みます。 Args: base_sha (str): 比較の基点となるコミットのSHA Returns: str: ベースSHAから現在までの差分 Raises: ValueError: 差分の取得に失敗した場合 """ try: repo_path = get_git_repo_path() repo = git.Repo(repo_path) # 変更されたファイルのリストを取得 change_details = [] # リポジトリの現在の状態(インデックスと作業ディレクトリ)をベースSHAと比較 diff_index = repo.git.diff(base_sha, name_status=True).strip() if not diff_index: return "変更されたファイルはありません。" # 変更されたファイルごとに詳細な差分を取得 for line in diff_index.split("\n"): if not line: continue parts = line.split("\t") if len(parts) < 2: continue change_type_code = parts[0] file_path = parts[1] # 変更の種類を判定 change_type = "変更" if change_type_code.startswith("A"): change_type = "新規追加" elif change_type_code.startswith("D"): change_type = "削除" elif change_type_code.startswith("R"): change_type = "名前変更" # ファイルの差分を取得 try: file_diff = repo.git.diff(base_sha, file_path, unified=3) change_details.append( f"# ファイル: {file_path} ({change_type})\n```diff\n{file_diff}\n```" ) except Exception as e: # 特定のファイルの差分取得に失敗した場合はエラーメッセージを追加 change_details.append( f"# ファイル: {file_path} ({change_type})\n```\n差分の取得に失敗しました: {str(e)}\n```" ) return "\n\n".join(change_details) except Exception as e: raise ValueError(f"差分の取得に失敗しました: {str(e)}")
- main.py:30-44 (helper)Helper function to get the current MR ID from the current branch using get_merge_request.# カレントのブランチのMRIDを取得 def get_current_mr_id() -> int: """ 現在のブランチのMRIDを取得します。 Returns: int: 現在のブランチのMRID """ branch_name = get_current_branch() mr = get_merge_request(branch_name) if mr: return mr.iid else: return "現在のブランチに関連するMerge Requestが見つかりません。"