"""Comment operations for JIRA."""
import logging
from typing import Any, Dict, List
from jira.exceptions import JIRAError
from mcp_jira.client import JiraClient
logger = logging.getLogger(__name__)
class CommentOperations:
"""Handles JIRA comment operations."""
def __init__(self, client: JiraClient):
"""Initialize comment operations.
Args:
client: JiraClient instance
"""
self.client = client
def add_comment(
self,
issue_key: str,
body: str,
visibility: Any = None,
) -> Dict[str, Any]:
"""Add a comment to an issue.
Args:
issue_key: Issue key
body: Comment text (plain text or Atlassian Document Format)
visibility: Comment visibility settings (optional)
Returns:
Dictionary with comment information
Raises:
JIRAError: If comment creation fails
"""
try:
jira = self.client.jira
logger.info(f"Adding comment to issue {issue_key}")
comment = jira.add_comment(
issue_key,
body,
visibility=visibility,
)
return {
"id": comment.id,
"issue_key": issue_key,
"author": comment.author.displayName if hasattr(comment, "author") else None,
"body": comment.body,
"created": comment.created,
}
except JIRAError as e:
logger.error(f"Failed to add comment to {issue_key}: {e}")
raise
def get_comments(
self,
issue_key: str,
) -> List[Dict[str, Any]]:
"""Get all comments for an issue.
Args:
issue_key: Issue key
Returns:
List of comments
Raises:
JIRAError: If retrieval fails
"""
try:
jira = self.client.jira
logger.info(f"Getting comments for issue {issue_key}")
comments = jira.comments(issue_key)
return [
{
"id": comment.id,
"author": comment.author.displayName if hasattr(comment, "author") else None,
"body": comment.body,
"created": comment.created,
"updated": comment.updated if hasattr(comment, "updated") else None,
}
for comment in comments
]
except JIRAError as e:
logger.error(f"Failed to get comments for {issue_key}: {e}")
raise
def update_comment(
self,
issue_key: str,
comment_id: str,
body: str,
) -> Dict[str, Any]:
"""Update an existing comment.
Args:
issue_key: Issue key
comment_id: Comment ID
body: New comment text
Returns:
Dictionary with update result
Raises:
JIRAError: If update fails
"""
try:
jira = self.client.jira
logger.info(f"Updating comment {comment_id} on issue {issue_key}")
comment = jira.comment(issue_key, comment_id)
comment.update(body=body)
return {
"id": comment_id,
"issue_key": issue_key,
"updated": True,
"body": body,
}
except JIRAError as e:
logger.error(f"Failed to update comment {comment_id}: {e}")
raise
def delete_comment(
self,
issue_key: str,
comment_id: str,
) -> Dict[str, Any]:
"""Delete a comment.
Args:
issue_key: Issue key
comment_id: Comment ID
Returns:
Dictionary with deletion result
Raises:
JIRAError: If deletion fails
"""
try:
jira = self.client.jira
logger.info(f"Deleting comment {comment_id} from issue {issue_key}")
comment = jira.comment(issue_key, comment_id)
comment.delete()
return {
"id": comment_id,
"issue_key": issue_key,
"deleted": True,
}
except JIRAError as e:
logger.error(f"Failed to delete comment {comment_id}: {e}")
raise