feat: move all remaining inline logic from routers to services (Tier 2)
This commit is contained in:
@@ -3,12 +3,17 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.config import settings
|
||||
from app.domain.errors import EntityNotFoundError
|
||||
from app.domain.exceptions import InvalidOperationError
|
||||
from app.models.jira_link import JiraLink
|
||||
from app.models.campaign import Campaign
|
||||
from app.models.jira_link import JiraLink, JiraLinkEntityType, JiraSyncDirection
|
||||
from app.models.technique import Technique
|
||||
from app.models.test import Test
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -103,3 +108,128 @@ def _build_sync_comment(data: dict) -> str:
|
||||
lines.append(f"*{key}:* {value}")
|
||||
lines.append(f"\n_Synced at {datetime.utcnow().isoformat()}_")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
# ── Link CRUD ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def create_link(
|
||||
db: Session,
|
||||
*,
|
||||
entity_type: JiraLinkEntityType,
|
||||
entity_id: UUID,
|
||||
jira_issue_key: str,
|
||||
sync_direction: JiraSyncDirection,
|
||||
created_by: UUID,
|
||||
) -> JiraLink:
|
||||
"""Create a Jira link and optionally pull initial data from Jira."""
|
||||
link = JiraLink(
|
||||
entity_type=entity_type,
|
||||
entity_id=entity_id,
|
||||
jira_issue_key=jira_issue_key,
|
||||
sync_direction=sync_direction,
|
||||
created_by=created_by,
|
||||
)
|
||||
db.add(link)
|
||||
db.flush()
|
||||
|
||||
if settings.JIRA_ENABLED:
|
||||
try:
|
||||
sync_jira_to_aegis(db, link)
|
||||
except Exception as e:
|
||||
logger.warning("Initial Jira sync failed for %s: %s", jira_issue_key, e)
|
||||
|
||||
return link
|
||||
|
||||
|
||||
def list_links(
|
||||
db: Session,
|
||||
*,
|
||||
entity_type: Optional[JiraLinkEntityType] = None,
|
||||
entity_id: Optional[UUID] = None,
|
||||
) -> list[JiraLink]:
|
||||
"""List Jira links with optional filters."""
|
||||
query = db.query(JiraLink)
|
||||
if entity_type:
|
||||
query = query.filter(JiraLink.entity_type == entity_type)
|
||||
if entity_id:
|
||||
query = query.filter(JiraLink.entity_id == entity_id)
|
||||
return query.order_by(JiraLink.created_at.desc()).all()
|
||||
|
||||
|
||||
def get_link_or_raise(db: Session, link_id: UUID) -> JiraLink:
|
||||
"""Get a Jira link by ID or raise EntityNotFoundError."""
|
||||
link = db.query(JiraLink).filter(JiraLink.id == link_id).first()
|
||||
if not link:
|
||||
raise EntityNotFoundError("JiraLink", str(link_id))
|
||||
return link
|
||||
|
||||
|
||||
def delete_link(db: Session, link_id: UUID) -> JiraLink:
|
||||
"""Delete a Jira link. Returns the deleted link (for audit)."""
|
||||
link = get_link_or_raise(db, link_id)
|
||||
db.delete(link)
|
||||
return link
|
||||
|
||||
|
||||
def build_issue_data(db: Session, entity_type: JiraLinkEntityType, entity_id: UUID) -> tuple[str, str]:
|
||||
"""Build Jira issue summary and description from an Aegis entity."""
|
||||
if entity_type == JiraLinkEntityType.test:
|
||||
entity = db.query(Test).filter(Test.id == entity_id).first()
|
||||
if not entity:
|
||||
raise EntityNotFoundError("Test", str(entity_id))
|
||||
return (
|
||||
f"[Aegis Test] {entity.name}",
|
||||
f"Test: {entity.name}\n"
|
||||
f"State: {entity.state.value if entity.state else 'draft'}\n"
|
||||
f"Description: {entity.description or 'N/A'}",
|
||||
)
|
||||
elif entity_type == JiraLinkEntityType.campaign:
|
||||
entity = db.query(Campaign).filter(Campaign.id == entity_id).first()
|
||||
if not entity:
|
||||
raise EntityNotFoundError("Campaign", str(entity_id))
|
||||
return (
|
||||
f"[Aegis Campaign] {entity.name}",
|
||||
f"Campaign: {entity.name}\n"
|
||||
f"Type: {entity.type}\nStatus: {entity.status}\n"
|
||||
f"Description: {entity.description or 'N/A'}",
|
||||
)
|
||||
elif entity_type == JiraLinkEntityType.technique:
|
||||
entity = db.query(Technique).filter(Technique.id == entity_id).first()
|
||||
if not entity:
|
||||
raise EntityNotFoundError("Technique", str(entity_id))
|
||||
return (
|
||||
f"[Aegis Technique] {entity.mitre_id} - {entity.name}",
|
||||
f"MITRE ID: {entity.mitre_id}\nName: {entity.name}\n"
|
||||
f"Tactic: {entity.tactic or 'N/A'}\n"
|
||||
f"Description: {entity.description or 'N/A'}",
|
||||
)
|
||||
else:
|
||||
return f"[Aegis] Entity {entity_id}", f"Entity type: {entity_type.value}"
|
||||
|
||||
|
||||
def create_issue_and_link(
|
||||
db: Session,
|
||||
*,
|
||||
entity_type: JiraLinkEntityType,
|
||||
entity_id: UUID,
|
||||
created_by: UUID,
|
||||
) -> dict:
|
||||
"""Create a Jira issue from an Aegis entity and link them."""
|
||||
summary, description = build_issue_data(db, entity_type, entity_id)
|
||||
result = create_jira_issue(
|
||||
project_key=settings.JIRA_DEFAULT_PROJECT,
|
||||
summary=summary,
|
||||
description=description,
|
||||
labels=["aegis", entity_type.value],
|
||||
)
|
||||
link = JiraLink(
|
||||
entity_type=entity_type,
|
||||
entity_id=entity_id,
|
||||
jira_issue_key=result["issue_key"],
|
||||
jira_issue_id=result["issue_id"],
|
||||
jira_project_key=settings.JIRA_DEFAULT_PROJECT,
|
||||
created_by=created_by,
|
||||
)
|
||||
db.add(link)
|
||||
return {"issue_key": result["issue_key"], "link_id": str(link.id)}
|
||||
|
||||
Reference in New Issue
Block a user