get_review_comments
Retrieve unresolved review comments from GitLab merge requests to facilitate issue resolution and enhance collaboration during code reviews.
Instructions
GitLab MRの未解決の指摘事項(コメント)を取得
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Input Schema (JSON Schema)
{
"properties": {},
"title": "get_review_commentsArguments",
"type": "object"
}
Implementation Reference
- main.py:79-93 (handler)The main handler function for the 'get_review_comments' tool. It is registered via @mcp.tool() decorator. Retrieves the MR ID using get_current_mr_id(), fetches comments using get_mr_comments(), and formats the response.@mcp.tool() def get_review_comments() -> str: """GitLab MRの未解決の指摘事項(コメント)を取得""" mr_id = get_current_mr_id() mr_comments = get_mr_comments(mr_id=mr_id) if mr_comments: return f""" 以下の指摘事項に対応してください。対応後は今後へのアドバイスを出力してください。 {mr_comments} """ else: return f"MR #{mr_id} への未解決の指摘事項はありません。"
- src/utils/gitlab_utils.py:218-263 (helper)Core helper function that fetches unresolved, file-associated comments from GitLab MR discussions. Processes each discussion using process_discussion and formats them.def get_mr_comments(mr_id: int) -> str: """ 指定したMR IDに関連するMRの指摘事項(コメント)を取得します。 解決済み(resolved)のコメントは除外されます。 ファイルに紐づいているコメントのみが取得されます。 Args: mr_id (int): Merge Request ID Returns: str: MRへの指摘事項(AIが理解しやすい形式に整形) 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のディスカッション(スレッド)を取得 discussions = mr.discussions.list() if not discussions: return f"MR #{mr.iid} へのコメントはありません。" comments: List[str] = [] # 各ディスカッションを処理 for discussion in discussions: # 個々のディスカッション内のノートを処理 discussion_comments = process_discussion(discussion) if discussion_comments: comments.extend(discussion_comments) if not comments: return f"MR #{mr.iid} への未解決の指摘事項はありません。" return "\n---\n".join(comments) except Exception as e: raise ValueError(f"MRへの指摘事項の取得に失敗しました: {str(e)}")
- src/utils/gitlab_utils.py:266-325 (helper)Helper function to process individual discussion threads, extracting only unresolved comments associated with specific file positions.def process_discussion(discussion: Dict[str, Any]) -> List[str]: """ ディスカッション(スレッド)内のノートを処理し、未解決のコメントを抽出します。 ファイルに紐づいているコメントのみを抽出します。 Args: discussion: GitLabディスカッションオブジェクト Returns: List[str]: 未解決のコメント文字列のリスト """ discussion_comments = [] has_unresolved_notes = False # ディスカッションにはノートの配列が含まれています notes = ( discussion.attributes.get("notes", []) if hasattr(discussion, "attributes") else discussion.get("notes", []) ) for note in notes: # システムノートは除外 if note.get("system", False): continue # 解決可能で、かつ解決済みのノートは除外 if note.get("resolvable", False) and note.get("resolved", False): continue # コードとの関連付けがあるか確認 position = note.get("position", {}) file_path = position.get("new_path", "") if position else "" # ファイルに紐づいていないコメントは除外 if not file_path: continue # ここに到達したノートは未解決かつファイルに紐づいている has_unresolved_notes = True author = note.get("author", {}).get("name", "不明なユーザー") body = note.get("body", "") line = position.get("new_line", "") if position else "" location = ( f" (ファイル: {file_path}, 行: {line})" if file_path and line else f" (ファイル: {file_path})" ) discussion_comments.append( f"# 対象: {location}\n- コメント者: {author}\n- コメント:\n```\n{body}\n```" ) # ディスカッションに未解決のノートがある場合のみ、そのコメントを返す if has_unresolved_notes: return discussion_comments return []
- main.py:31-44 (helper)Helper function to get the current MR ID from the current branch name using get_merge_request.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が見つかりません。"
- main.py:79-79 (registration)The @mcp.tool() decorator registers the get_review_comments function as an MCP tool.@mcp.tool()