jira-ai-fixer/api/services/jira.py

111 lines
3.1 KiB
Python

"""
JIRA Service - Client for JIRA Server API.
"""
from typing import Optional, Dict, Any, List
import httpx
import logging
logger = logging.getLogger(__name__)
class JiraClient:
"""JIRA Server REST API client."""
def __init__(self, base_url: str, token: str):
self.base_url = base_url.rstrip("/")
self.headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
async def get_issue(self, issue_key: str) -> Dict[str, Any]:
"""Fetch issue details."""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/rest/api/2/issue/{issue_key}",
headers=self.headers,
)
response.raise_for_status()
return response.json()
async def add_comment(self, issue_key: str, body: str) -> Dict[str, Any]:
"""Add a comment to an issue."""
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/rest/api/2/issue/{issue_key}/comment",
headers=self.headers,
json={"body": body},
)
response.raise_for_status()
return response.json()
async def search_issues(
self,
jql: str,
start_at: int = 0,
max_results: int = 50,
fields: Optional[List[str]] = None,
) -> Dict[str, Any]:
"""Search issues using JQL."""
params = {
"jql": jql,
"startAt": start_at,
"maxResults": max_results,
}
if fields:
params["fields"] = ",".join(fields)
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/rest/api/2/search",
headers=self.headers,
params=params,
)
response.raise_for_status()
return response.json()
async def get_projects(self) -> List[Dict[str, Any]]:
"""List all accessible projects."""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/rest/api/2/project",
headers=self.headers,
)
response.raise_for_status()
return response.json()
def format_analysis_comment(
self,
root_cause: str,
affected_files: List[str],
proposed_fix: str,
confidence: float,
pr_url: Optional[str] = None,
) -> str:
"""Format AI analysis as a JIRA comment."""
files_list = "\n".join([f"* {f}" for f in affected_files])
comment = f"""
h2. 📋 Análise Automática
h3. 🔍 Causa Raiz Identificada
{root_cause}
h3. 📁 Arquivos Afetados
{files_list}
h3. 💡 Correção Proposta
{{code:cobol}}
{proposed_fix}
{{code}}
h3. 📊 Confiança: {confidence:.0%}
"""
if pr_url:
comment += f"\nh3. 🔗 Pull Request\n[Ver PR|{pr_url}]"
comment += "\n\n_Gerado automaticamente por JIRA AI Fixer_"
return comment