feat: move all remaining inline logic from routers to services (Tier 2)
This commit is contained in:
@@ -7,14 +7,9 @@ from uuid import UUID
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.config import settings
|
||||
from app.database import get_db
|
||||
from app.dependencies.auth import get_current_user, require_role
|
||||
from app.domain.exceptions import EntityNotFoundError
|
||||
from app.models.jira_link import JiraLink, JiraLinkEntityType
|
||||
from app.models.test import Test
|
||||
from app.models.technique import Technique
|
||||
from app.models.campaign import Campaign
|
||||
from app.models.jira_link import JiraLinkEntityType
|
||||
from app.models.user import User
|
||||
from app.schemas.jira_schema import (
|
||||
JiraIssueResult,
|
||||
@@ -45,23 +40,14 @@ def create_link(
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Associate an Aegis entity with a Jira issue."""
|
||||
link = JiraLink(
|
||||
link = jira_service.create_link(
|
||||
db,
|
||||
entity_type=body.entity_type,
|
||||
entity_id=body.entity_id,
|
||||
jira_issue_key=body.jira_issue_key,
|
||||
sync_direction=body.sync_direction,
|
||||
created_by=user.id,
|
||||
)
|
||||
db.add(link)
|
||||
db.flush()
|
||||
|
||||
# Pull initial data from Jira if enabled
|
||||
if settings.JIRA_ENABLED:
|
||||
try:
|
||||
jira_service.sync_jira_to_aegis(db, link)
|
||||
except Exception as e:
|
||||
logger.warning("Initial Jira sync failed for %s: %s", body.jira_issue_key, e)
|
||||
|
||||
db.commit()
|
||||
db.refresh(link)
|
||||
|
||||
@@ -88,12 +74,11 @@ def list_links(
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""List Jira links, optionally filtered by entity."""
|
||||
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()
|
||||
return jira_service.list_links(
|
||||
db,
|
||||
entity_type=entity_type,
|
||||
entity_id=entity_id,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/links/{link_id}/sync")
|
||||
@@ -103,9 +88,7 @@ def sync_link(
|
||||
user: User = Depends(require_role("admin")),
|
||||
):
|
||||
"""Force bidirectional sync for a specific Jira link."""
|
||||
link = db.query(JiraLink).filter(JiraLink.id == link_id).first()
|
||||
if not link:
|
||||
raise EntityNotFoundError("JiraLink", str(link_id))
|
||||
link = jira_service.get_link_or_raise(db, link_id)
|
||||
jira_service.sync_jira_to_aegis(db, link)
|
||||
db.commit()
|
||||
return {"message": "Sync completed", "jira_status": link.jira_status}
|
||||
@@ -118,10 +101,7 @@ def delete_link(
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Remove a Jira link."""
|
||||
link = db.query(JiraLink).filter(JiraLink.id == link_id).first()
|
||||
if not link:
|
||||
raise EntityNotFoundError("JiraLink", str(link_id))
|
||||
db.delete(link)
|
||||
link = jira_service.delete_link(db, link_id)
|
||||
db.commit()
|
||||
audit_service.log_action(
|
||||
db,
|
||||
@@ -141,61 +121,11 @@ def create_issue_from_entity(
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Auto-create a Jira issue from an Aegis entity and link them."""
|
||||
summary, description = _build_issue_data(db, entity_type, entity_id)
|
||||
result = jira_service.create_jira_issue(
|
||||
project_key=settings.JIRA_DEFAULT_PROJECT,
|
||||
summary=summary,
|
||||
description=description,
|
||||
labels=["aegis", entity_type.value],
|
||||
)
|
||||
link = JiraLink(
|
||||
result = jira_service.create_issue_and_link(
|
||||
db,
|
||||
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=user.id,
|
||||
)
|
||||
db.add(link)
|
||||
db.commit()
|
||||
return {"issue_key": result["issue_key"], "link_id": str(link.id)}
|
||||
|
||||
|
||||
def _build_issue_data(
|
||||
db: Session,
|
||||
entity_type: JiraLinkEntityType,
|
||||
entity_id: UUID,
|
||||
) -> tuple[str, str]:
|
||||
"""Build Jira issue summary + 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}"
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user