feat: Implement domain logic separation with clean architecture
- Created complete domain layer with pure business logic - Implemented Issue domain models with 48 passing tests - Implemented Project domain models with 31 passing tests - Added domain services for complex business operations - Established clean separation between domain, application, and infrastructure - All 250 tests passing with no breaking changes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
20
domain/issues/__init__.py
Normal file
20
domain/issues/__init__.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""
|
||||
Issue domain module.
|
||||
|
||||
Contains domain models, services, and interfaces for issue management.
|
||||
"""
|
||||
|
||||
from .models import Issue, Label, IssueState, LabelCategories
|
||||
from .services import IssueStatusService, IssueValidationService
|
||||
from .exceptions import IssueDomainError, IssueValidationError
|
||||
|
||||
__all__ = [
|
||||
'Issue',
|
||||
'Label',
|
||||
'IssueState',
|
||||
'LabelCategories',
|
||||
'IssueStatusService',
|
||||
'IssueValidationService',
|
||||
'IssueDomainError',
|
||||
'IssueValidationError'
|
||||
]
|
||||
29
domain/issues/exceptions.py
Normal file
29
domain/issues/exceptions.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""
|
||||
Domain-specific exceptions for issue management.
|
||||
"""
|
||||
|
||||
|
||||
class IssueDomainError(Exception):
|
||||
"""Base exception for issue domain errors."""
|
||||
|
||||
def __init__(self, message: str, issue_number: int = None):
|
||||
super().__init__(message)
|
||||
self.issue_number = issue_number
|
||||
|
||||
|
||||
class IssueValidationError(IssueDomainError):
|
||||
"""Exception raised when issue validation fails."""
|
||||
|
||||
def __init__(self, message: str, field: str = None, value=None):
|
||||
super().__init__(message)
|
||||
self.field = field
|
||||
self.value = value
|
||||
|
||||
|
||||
class IssueStateError(IssueDomainError):
|
||||
"""Exception raised when invalid state transitions are attempted."""
|
||||
|
||||
def __init__(self, message: str, current_state: str, attempted_state: str):
|
||||
super().__init__(message)
|
||||
self.current_state = current_state
|
||||
self.attempted_state = attempted_state
|
||||
116
domain/issues/models.py
Normal file
116
domain/issues/models.py
Normal file
@@ -0,0 +1,116 @@
|
||||
"""
|
||||
Issue domain models.
|
||||
|
||||
Contains core business entities and value objects for issue management.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
from .exceptions import IssueStateError
|
||||
|
||||
|
||||
class IssueState(Enum):
|
||||
"""Issue state enumeration."""
|
||||
OPEN = "open"
|
||||
CLOSED = "closed"
|
||||
IN_PROGRESS = "in_progress"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Label:
|
||||
"""Value object representing an issue label."""
|
||||
name: str
|
||||
color: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
|
||||
def is_state_label(self) -> bool:
|
||||
"""Check if this is a state-related label."""
|
||||
return self.name.startswith('status:')
|
||||
|
||||
def is_priority_label(self) -> bool:
|
||||
"""Check if this is a priority-related label."""
|
||||
return self.name.startswith('priority:')
|
||||
|
||||
def is_type_label(self) -> bool:
|
||||
"""Check if this is a type-related label."""
|
||||
return self.name in ['bug', 'enhancement', 'feature', 'documentation']
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LabelCategories:
|
||||
"""Value object for categorized labels."""
|
||||
state_labels: List[str]
|
||||
priority_labels: List[str]
|
||||
type_labels: List[str]
|
||||
other_labels: List[str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class Issue:
|
||||
"""Issue aggregate root."""
|
||||
number: int
|
||||
title: str
|
||||
state: IssueState
|
||||
labels: List[Label]
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
milestone: Optional[str] = None
|
||||
assignee: Optional[str] = None
|
||||
closed_at: Optional[datetime] = None
|
||||
|
||||
def categorize_labels(self) -> LabelCategories:
|
||||
"""Categorize labels by type - pure domain logic."""
|
||||
state_labels = [label.name for label in self.labels if label.is_state_label()]
|
||||
priority_labels = [label.name for label in self.labels if label.is_priority_label()]
|
||||
type_labels = [label.name for label in self.labels if label.is_type_label()]
|
||||
other_labels = [
|
||||
label.name for label in self.labels
|
||||
if not (label.is_state_label() or label.is_priority_label() or label.is_type_label())
|
||||
]
|
||||
|
||||
return LabelCategories(
|
||||
state_labels=state_labels,
|
||||
priority_labels=priority_labels,
|
||||
type_labels=type_labels,
|
||||
other_labels=other_labels
|
||||
)
|
||||
|
||||
def close(self) -> None:
|
||||
"""Close the issue - domain business rule."""
|
||||
if self.state == IssueState.CLOSED:
|
||||
raise IssueStateError(
|
||||
"Issue is already closed",
|
||||
current_state=self.state.value,
|
||||
attempted_state=IssueState.CLOSED.value
|
||||
)
|
||||
|
||||
self.state = IssueState.CLOSED
|
||||
self.closed_at = datetime.utcnow()
|
||||
|
||||
def reopen(self) -> None:
|
||||
"""Reopen the issue - domain business rule."""
|
||||
if self.state != IssueState.CLOSED:
|
||||
raise IssueStateError(
|
||||
"Issue is not closed",
|
||||
current_state=self.state.value,
|
||||
attempted_state=IssueState.OPEN.value
|
||||
)
|
||||
|
||||
self.state = IssueState.OPEN
|
||||
self.closed_at = None
|
||||
|
||||
def add_label(self, label: Label) -> None:
|
||||
"""Add a label to the issue."""
|
||||
if label not in self.labels:
|
||||
self.labels.append(label)
|
||||
|
||||
def remove_label(self, label_name: str) -> None:
|
||||
"""Remove a label from the issue."""
|
||||
self.labels = [label for label in self.labels if label.name != label_name]
|
||||
|
||||
def has_label(self, label_name: str) -> bool:
|
||||
"""Check if issue has a specific label."""
|
||||
return any(label.name == label_name for label in self.labels)
|
||||
116
domain/issues/repositories.py
Normal file
116
domain/issues/repositories.py
Normal file
@@ -0,0 +1,116 @@
|
||||
"""
|
||||
Repository interfaces for issue domain.
|
||||
|
||||
Defines contracts for data access without infrastructure dependencies.
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import List, Optional, Dict, Any
|
||||
from .models import Issue
|
||||
|
||||
|
||||
class IssueRepository(ABC):
|
||||
"""Repository interface for issue persistence."""
|
||||
|
||||
@abstractmethod
|
||||
async def get_issue(self, issue_number: int) -> Issue:
|
||||
"""Retrieve issue by number.
|
||||
|
||||
Args:
|
||||
issue_number: The issue number to retrieve
|
||||
|
||||
Returns:
|
||||
Issue domain object
|
||||
|
||||
Raises:
|
||||
IssueNotFoundError: If issue doesn't exist
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def list_issues(self, state: Optional[str] = None, limit: Optional[int] = None) -> List[Issue]:
|
||||
"""List issues, optionally filtered by state.
|
||||
|
||||
Args:
|
||||
state: Optional state filter (open, closed)
|
||||
limit: Optional limit on number of results
|
||||
|
||||
Returns:
|
||||
List of Issue domain objects
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def save_issue(self, issue: Issue) -> None:
|
||||
"""Save issue changes.
|
||||
|
||||
Args:
|
||||
issue: Issue domain object to save
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def create_issue(self, title: str, description: str, labels: List[str]) -> Issue:
|
||||
"""Create a new issue.
|
||||
|
||||
Args:
|
||||
title: Issue title
|
||||
description: Issue description
|
||||
labels: List of label names
|
||||
|
||||
Returns:
|
||||
Created Issue domain object
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def delete_issue(self, issue_number: int) -> None:
|
||||
"""Delete an issue.
|
||||
|
||||
Args:
|
||||
issue_number: The issue number to delete
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class ProjectRepository(ABC):
|
||||
"""Repository interface for project information."""
|
||||
|
||||
@abstractmethod
|
||||
async def get_issue_project_info(self, issue_number: int) -> Dict[str, Any]:
|
||||
"""Get project information for an issue.
|
||||
|
||||
Args:
|
||||
issue_number: The issue number
|
||||
|
||||
Returns:
|
||||
Dictionary containing project context information
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def get_kanban_columns(self) -> List[str]:
|
||||
"""Get available kanban columns for the project.
|
||||
|
||||
Returns:
|
||||
List of kanban column names
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def get_project_labels(self) -> List[Dict[str, Any]]:
|
||||
"""Get available labels for the project.
|
||||
|
||||
Returns:
|
||||
List of label definitions
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def get_milestones(self) -> List[Dict[str, Any]]:
|
||||
"""Get available milestones for the project.
|
||||
|
||||
Returns:
|
||||
List of milestone information
|
||||
"""
|
||||
pass
|
||||
173
domain/issues/services.py
Normal file
173
domain/issues/services.py
Normal file
@@ -0,0 +1,173 @@
|
||||
"""
|
||||
Issue domain services.
|
||||
|
||||
Contains business logic for issue-related operations.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, List
|
||||
from .models import Issue, IssueState, LabelCategories
|
||||
from .exceptions import IssueValidationError
|
||||
|
||||
|
||||
class IssueStatusService:
|
||||
"""Domain service for issue status-related business logic."""
|
||||
|
||||
def determine_kanban_column(self, issue: Issue, project_info: Dict[str, Any]) -> str:
|
||||
"""Determine kanban column based on issue state and labels."""
|
||||
# Pure business logic - no infrastructure dependencies
|
||||
label_categories = issue.categorize_labels()
|
||||
|
||||
# Business rules for kanban column determination
|
||||
if issue.state == IssueState.CLOSED:
|
||||
return "Done"
|
||||
|
||||
# Check for explicit status labels
|
||||
for state_label in label_categories.state_labels:
|
||||
if state_label == "status:in-progress":
|
||||
return "In Progress"
|
||||
elif state_label == "status:review":
|
||||
return "Review"
|
||||
elif state_label == "status:blocked":
|
||||
return "Blocked"
|
||||
elif state_label == "status:ready":
|
||||
return "Ready"
|
||||
|
||||
# Default for open issues without explicit status
|
||||
return "Todo"
|
||||
|
||||
def extract_priority_info(self, issue: Issue) -> Dict[str, Any]:
|
||||
"""Extract priority information from issue labels."""
|
||||
label_categories = issue.categorize_labels()
|
||||
|
||||
priority_mapping = {
|
||||
"priority:low": "Low",
|
||||
"priority:medium": "Medium",
|
||||
"priority:high": "High",
|
||||
"priority:critical": "Critical"
|
||||
}
|
||||
|
||||
for priority_label in label_categories.priority_labels:
|
||||
if priority_label in priority_mapping:
|
||||
return {
|
||||
"level": priority_mapping[priority_label],
|
||||
"label": priority_label
|
||||
}
|
||||
|
||||
# Default priority
|
||||
return {"level": "Medium", "label": None}
|
||||
|
||||
def extract_state_info(self, issue: Issue) -> Dict[str, Any]:
|
||||
"""Extract state information from issue labels and state."""
|
||||
label_categories = issue.categorize_labels()
|
||||
|
||||
return {
|
||||
"state": issue.state.value,
|
||||
"state_labels": label_categories.state_labels,
|
||||
"is_closed": issue.state == IssueState.CLOSED,
|
||||
"closed_at": issue.closed_at.isoformat() if issue.closed_at else None
|
||||
}
|
||||
|
||||
def calculate_issue_age_days(self, issue: Issue) -> int:
|
||||
"""Calculate issue age in days."""
|
||||
from datetime import datetime
|
||||
return (datetime.utcnow() - issue.created_at).days
|
||||
|
||||
def is_stale_issue(self, issue: Issue, stale_threshold_days: int = 30) -> bool:
|
||||
"""Determine if issue is considered stale based on business rules."""
|
||||
if issue.state == IssueState.CLOSED:
|
||||
return False
|
||||
|
||||
age_days = self.calculate_issue_age_days(issue)
|
||||
return age_days > stale_threshold_days
|
||||
|
||||
|
||||
class IssueValidationService:
|
||||
"""Domain service for issue validation business rules."""
|
||||
|
||||
def validate_issue_creation(self, title: str, labels: List[str]) -> None:
|
||||
"""Validate issue creation according to business rules."""
|
||||
if not title or not title.strip():
|
||||
raise IssueValidationError(
|
||||
"Issue title cannot be empty",
|
||||
field="title",
|
||||
value=title
|
||||
)
|
||||
|
||||
if len(title) > 255:
|
||||
raise IssueValidationError(
|
||||
"Issue title cannot exceed 255 characters",
|
||||
field="title",
|
||||
value=title
|
||||
)
|
||||
|
||||
# Business rule: Cannot have conflicting priority labels
|
||||
priority_labels = [label for label in labels if label.startswith("priority:")]
|
||||
if len(priority_labels) > 1:
|
||||
raise IssueValidationError(
|
||||
"Issue cannot have multiple priority labels",
|
||||
field="labels",
|
||||
value=priority_labels
|
||||
)
|
||||
|
||||
# Business rule: Cannot have conflicting state labels
|
||||
state_labels = [label for label in labels if label.startswith("status:")]
|
||||
if len(state_labels) > 1:
|
||||
raise IssueValidationError(
|
||||
"Issue cannot have multiple state labels",
|
||||
field="labels",
|
||||
value=state_labels
|
||||
)
|
||||
|
||||
def validate_title_update(self, new_title: str) -> None:
|
||||
"""Validate issue title update."""
|
||||
if not new_title or not new_title.strip():
|
||||
raise IssueValidationError(
|
||||
"Issue title cannot be empty",
|
||||
field="title",
|
||||
value=new_title
|
||||
)
|
||||
|
||||
if len(new_title) > 255:
|
||||
raise IssueValidationError(
|
||||
"Issue title cannot exceed 255 characters",
|
||||
field="title",
|
||||
value=new_title
|
||||
)
|
||||
|
||||
def validate_label_addition(self, issue: Issue, new_label: str) -> None:
|
||||
"""Validate adding a label to an issue."""
|
||||
# Business rule: Cannot add duplicate labels
|
||||
if issue.has_label(new_label):
|
||||
raise IssueValidationError(
|
||||
f"Issue already has label '{new_label}'",
|
||||
field="labels",
|
||||
value=new_label
|
||||
)
|
||||
|
||||
# Business rule: Cannot add conflicting priority labels
|
||||
if new_label.startswith("priority:"):
|
||||
existing_priority_labels = [
|
||||
label.name for label in issue.labels
|
||||
if label.is_priority_label()
|
||||
]
|
||||
if existing_priority_labels:
|
||||
raise IssueValidationError(
|
||||
f"Issue already has priority label '{existing_priority_labels[0]}'. "
|
||||
f"Cannot add '{new_label}'",
|
||||
field="labels",
|
||||
value=new_label
|
||||
)
|
||||
|
||||
# Business rule: Cannot add conflicting state labels
|
||||
if new_label.startswith("status:"):
|
||||
existing_state_labels = [
|
||||
label.name for label in issue.labels
|
||||
if label.is_state_label()
|
||||
]
|
||||
if existing_state_labels:
|
||||
raise IssueValidationError(
|
||||
f"Issue already has state label '{existing_state_labels[0]}'. "
|
||||
f"Cannot add '{new_label}'",
|
||||
field="labels",
|
||||
value=new_label
|
||||
)
|
||||
Reference in New Issue
Block a user