refactor: Factor out Gitea interfacing into clean facade pattern
- Create new gitea/ package with clean API facade - Establish proper separation of concerns: tddai uses gitea, not vice versa - Replace duplicate curl+subprocess patterns with unified HTTP client - Add rich domain models with properties (issue.priority, issue.status) - Maintain full backwards compatibility in tddai modules - Reduce code complexity: -373 lines, +151 lines (net -222 lines) - Improve testability and maintainability through clean interfaces Architecture: - gitea.client.GiteaClient - main facade with sub-clients - gitea.api_client - high-level API with model conversion - gitea.http_client - low-level HTTP operations - gitea.models - rich domain objects (Issue, Milestone, Label) - gitea.config - gitea-specific configuration - gitea.exceptions - clean exception hierarchy 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,150 +1,89 @@
|
||||
"""
|
||||
Project management functionality for Gitea using milestones and labels.
|
||||
Project management functionality using the Gitea facade.
|
||||
|
||||
Since Gitea project boards may not be available in all instances, this module
|
||||
provides project management using milestones (for projects) and labels (for states).
|
||||
This module now acts as an adapter to the new gitea package,
|
||||
maintaining backwards compatibility while using the cleaner API.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
from subprocess import PIPE
|
||||
from typing import Dict, Any, List, Optional
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
|
||||
from gitea import GiteaClient, GiteaConfig
|
||||
from gitea.models import ProjectState, Priority, Milestone as GiteaMilestone, Label as GiteaLabel
|
||||
from .config import get_config
|
||||
from .exceptions import IssueError
|
||||
|
||||
|
||||
class ProjectState(Enum):
|
||||
"""Standard project states using labels."""
|
||||
TODO = "status:todo"
|
||||
ACTIVE = "status:active"
|
||||
REVIEW = "status:review"
|
||||
DONE = "status:done"
|
||||
BLOCKED = "status:blocked"
|
||||
|
||||
|
||||
class Priority(Enum):
|
||||
"""Priority levels using labels."""
|
||||
LOW = "priority:low"
|
||||
MEDIUM = "priority:medium"
|
||||
HIGH = "priority:high"
|
||||
CRITICAL = "priority:critical"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Milestone:
|
||||
"""Represents a project milestone."""
|
||||
id: int
|
||||
title: str
|
||||
description: str
|
||||
state: str
|
||||
open_issues: int
|
||||
closed_issues: int
|
||||
due_on: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Label:
|
||||
"""Represents an issue label."""
|
||||
id: int
|
||||
name: str
|
||||
color: str
|
||||
description: str
|
||||
# Re-export for backwards compatibility
|
||||
Milestone = GiteaMilestone
|
||||
Label = GiteaLabel
|
||||
|
||||
|
||||
class ProjectManager:
|
||||
"""Manages project organization using milestones and labels."""
|
||||
"""Manages project organization using the Gitea facade."""
|
||||
|
||||
def __init__(self, config=None, auth_token=None):
|
||||
self.config = config or get_config()
|
||||
self.auth_token = auth_token or os.getenv('GITEA_API_TOKEN')
|
||||
|
||||
# Create Gitea client from tddai config
|
||||
gitea_config = GiteaConfig.from_tddai_config(self.config)
|
||||
if self.auth_token:
|
||||
gitea_config.auth_token = self.auth_token
|
||||
self.gitea_client = GiteaClient(gitea_config)
|
||||
|
||||
def _make_api_call(self, method: str, url: str, data: Dict[str, Any] = None) -> Dict[str, Any]:
|
||||
"""Make authenticated API call to Gitea."""
|
||||
if not self.auth_token:
|
||||
raise IssueError("Authentication token required for project operations")
|
||||
|
||||
cmd = [
|
||||
'curl', '-s', '-X', method,
|
||||
'-H', 'Content-Type: application/json',
|
||||
'-H', f'Authorization: token {self.auth_token}',
|
||||
]
|
||||
|
||||
if data:
|
||||
cmd.extend(['-d', json.dumps(data)])
|
||||
|
||||
cmd.append(url)
|
||||
|
||||
"""Make authenticated API call to Gitea (kept for backwards compatibility)."""
|
||||
# This method is kept for backwards compatibility but now delegates to the gitea client
|
||||
# For new code, use the gitea_client directly
|
||||
try:
|
||||
result = subprocess.run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True, check=True)
|
||||
|
||||
if result.returncode != 0:
|
||||
raise IssueError(f"API call failed: {result.stderr}")
|
||||
|
||||
if result.stdout.strip():
|
||||
response_data = json.loads(result.stdout)
|
||||
|
||||
# Check for API error responses
|
||||
if isinstance(response_data, dict) and 'message' in response_data and 'id' not in response_data:
|
||||
raise IssueError(f"API error: {response_data['message']}")
|
||||
|
||||
return response_data
|
||||
if method == 'GET' and 'issues' in url and url.endswith('/issues'):
|
||||
issues = self.gitea_client.issues.list()
|
||||
return [self._issue_to_dict(issue) for issue in issues]
|
||||
elif method == 'GET' and '/issues/' in url and not url.endswith('/labels'):
|
||||
issue_number = int(url.split('/issues/')[-1])
|
||||
issue = self.gitea_client.issues.get(issue_number)
|
||||
return self._issue_to_dict(issue)
|
||||
else:
|
||||
return {}
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise IssueError(f"Legacy API call not supported: {method} {url}")
|
||||
except Exception as e:
|
||||
raise IssueError(f"API call failed: {e}")
|
||||
except json.JSONDecodeError as e:
|
||||
raise IssueError(f"Failed to parse API response: {e}")
|
||||
|
||||
def _issue_to_dict(self, issue) -> Dict[str, Any]:
|
||||
"""Convert Issue object to dict for backwards compatibility."""
|
||||
return {
|
||||
'number': issue.number,
|
||||
'title': issue.title,
|
||||
'body': issue.body,
|
||||
'state': issue.state,
|
||||
'html_url': issue.html_url,
|
||||
'created_at': issue.created_at.isoformat(),
|
||||
'updated_at': issue.updated_at.isoformat(),
|
||||
'assignee': {'login': issue.assignee.login} if issue.assignee else None,
|
||||
'labels': [{'name': label.name, 'color': label.color} for label in issue.labels]
|
||||
}
|
||||
|
||||
# Milestone Management (Projects)
|
||||
|
||||
def create_milestone(self, title: str, description: str = "", due_date: str = None) -> Milestone:
|
||||
"""Create a new milestone (project)."""
|
||||
url = f"{self.config.gitea_url}/api/v1/repos/{self.config.repo_owner}/{self.config.repo_name}/milestones"
|
||||
|
||||
data = {
|
||||
'title': title,
|
||||
'description': description,
|
||||
}
|
||||
|
||||
if due_date:
|
||||
data['due_on'] = due_date
|
||||
|
||||
response = self._make_api_call('POST', url, data)
|
||||
|
||||
return Milestone(
|
||||
id=response['id'],
|
||||
title=response['title'],
|
||||
description=response.get('description', ''),
|
||||
state=response['state'],
|
||||
open_issues=response['open_issues'],
|
||||
closed_issues=response['closed_issues'],
|
||||
due_on=response.get('due_on')
|
||||
)
|
||||
try:
|
||||
return self.gitea_client.milestones.create(title, description, due_date)
|
||||
except Exception as e:
|
||||
raise IssueError(f"Failed to create milestone: {e}")
|
||||
|
||||
def list_milestones(self, state: str = "open") -> List[Milestone]:
|
||||
"""List all milestones (projects)."""
|
||||
url = f"{self.config.gitea_url}/api/v1/repos/{self.config.repo_owner}/{self.config.repo_name}/milestones"
|
||||
|
||||
params = f"?state={state}" if state else ""
|
||||
response = self._make_api_call('GET', url + params)
|
||||
|
||||
return [
|
||||
Milestone(
|
||||
id=m['id'],
|
||||
title=m['title'],
|
||||
description=m.get('description', ''),
|
||||
state=m['state'],
|
||||
open_issues=m['open_issues'],
|
||||
closed_issues=m['closed_issues'],
|
||||
due_on=m.get('due_on')
|
||||
)
|
||||
for m in response
|
||||
]
|
||||
try:
|
||||
if state == "all":
|
||||
return self.gitea_client.milestones.list()
|
||||
elif state == "open":
|
||||
return self.gitea_client.milestones.list_open()
|
||||
elif state == "closed":
|
||||
return self.gitea_client.milestones.list_closed()
|
||||
else:
|
||||
return self.gitea_client.milestones.list(state)
|
||||
except Exception as e:
|
||||
raise IssueError(f"Failed to list milestones: {e}")
|
||||
|
||||
def update_milestone(self, milestone_id: int, **kwargs) -> Milestone:
|
||||
"""Update milestone details."""
|
||||
@@ -209,36 +148,24 @@ class ProjectManager:
|
||||
|
||||
def ensure_project_labels(self) -> None:
|
||||
"""Ensure all required project management labels exist."""
|
||||
existing_labels = {label.name for label in self.list_labels()}
|
||||
try:
|
||||
self.gitea_client.labels.ensure_project_labels()
|
||||
except Exception as e:
|
||||
raise IssueError(f"Failed to ensure project labels: {e}")
|
||||
|
||||
# Standard state labels
|
||||
required_labels = [
|
||||
('status:todo', 'e6e6e6', 'Issues ready to be worked on'),
|
||||
('status:active', '0052cc', 'Issues currently being worked on'),
|
||||
('status:review', 'fbca04', 'Issues under review'),
|
||||
('status:done', '0e8a16', 'Completed issues'),
|
||||
('status:blocked', 'd93f0b', 'Issues blocked by dependencies'),
|
||||
def list_labels(self) -> List[Label]:
|
||||
"""List all repository labels."""
|
||||
try:
|
||||
return self.gitea_client.labels.list()
|
||||
except Exception as e:
|
||||
raise IssueError(f"Failed to list labels: {e}")
|
||||
|
||||
# Priority labels
|
||||
('priority:low', 'c2e0c6', 'Low priority issue'),
|
||||
('priority:medium', 'fef2c0', 'Medium priority issue'),
|
||||
('priority:high', 'f9d0c4', 'High priority issue'),
|
||||
('priority:critical', 'f4c2c2', 'Critical priority issue'),
|
||||
|
||||
# Type labels
|
||||
('type:bug', 'fc2929', 'Bug report'),
|
||||
('type:feature', '84b6eb', 'New feature request'),
|
||||
('type:enhancement', '7057ff', 'Enhancement to existing feature'),
|
||||
('type:documentation', '0075ca', 'Documentation update'),
|
||||
]
|
||||
|
||||
for name, color, description in required_labels:
|
||||
if name not in existing_labels:
|
||||
try:
|
||||
self.create_label(name, color, description)
|
||||
print(f"✅ Created label: {name}")
|
||||
except IssueError as e:
|
||||
print(f"⚠️ Failed to create label {name}: {e}")
|
||||
def create_label(self, name: str, color: str, description: str = "") -> Label:
|
||||
"""Create a new label."""
|
||||
try:
|
||||
return self.gitea_client.labels.create(name, color, description)
|
||||
except Exception as e:
|
||||
raise IssueError(f"Failed to create label: {e}")
|
||||
|
||||
# Project Management Operations
|
||||
|
||||
@@ -251,61 +178,31 @@ class ProjectManager:
|
||||
|
||||
def set_issue_state(self, issue_number: int, state: ProjectState) -> Dict[str, Any]:
|
||||
"""Set issue project state using labels."""
|
||||
# Use the dedicated labels endpoint which works more reliably
|
||||
labels_url = f"{self.config.issues_api_url}/{issue_number}/labels"
|
||||
|
||||
# First get current labels
|
||||
issue_url = f"{self.config.issues_api_url}/{issue_number}"
|
||||
issue_data = self._make_api_call('GET', issue_url)
|
||||
|
||||
current_labels = [label['name'] for label in issue_data.get('labels', [])]
|
||||
state_labels = [label for label in current_labels if label.startswith('status:')]
|
||||
|
||||
# Remove old state labels
|
||||
for old_state in state_labels:
|
||||
if old_state in current_labels:
|
||||
current_labels.remove(old_state)
|
||||
|
||||
# Add new state label
|
||||
current_labels.append(state.value)
|
||||
|
||||
# Use PUT to replace all labels on the dedicated labels endpoint
|
||||
data = {'labels': current_labels}
|
||||
return self._make_api_call('PUT', labels_url, data)
|
||||
try:
|
||||
issue = self.gitea_client.issues.set_status(issue_number, state)
|
||||
return self._issue_to_dict(issue)
|
||||
except Exception as e:
|
||||
raise IssueError(f"Failed to set issue state: {e}")
|
||||
|
||||
def set_issue_priority(self, issue_number: int, priority: Priority) -> Dict[str, Any]:
|
||||
"""Set issue priority using labels."""
|
||||
# Use the dedicated labels endpoint which works more reliably
|
||||
labels_url = f"{self.config.issues_api_url}/{issue_number}/labels"
|
||||
|
||||
# First get current labels
|
||||
issue_url = f"{self.config.issues_api_url}/{issue_number}"
|
||||
issue_data = self._make_api_call('GET', issue_url)
|
||||
|
||||
current_labels = [label['name'] for label in issue_data.get('labels', [])]
|
||||
priority_labels = [label for label in current_labels if label.startswith('priority:')]
|
||||
|
||||
# Remove old priority labels
|
||||
for old_priority in priority_labels:
|
||||
if old_priority in current_labels:
|
||||
current_labels.remove(old_priority)
|
||||
|
||||
# Add new priority label
|
||||
current_labels.append(priority.value)
|
||||
|
||||
# Use PUT to replace all labels on the dedicated labels endpoint
|
||||
data = {'labels': current_labels}
|
||||
return self._make_api_call('PUT', labels_url, data)
|
||||
try:
|
||||
issue = self.gitea_client.issues.set_priority(issue_number, priority)
|
||||
return self._issue_to_dict(issue)
|
||||
except Exception as e:
|
||||
raise IssueError(f"Failed to set issue priority: {e}")
|
||||
|
||||
def move_issue_to_done(self, issue_number: int) -> Dict[str, Any]:
|
||||
"""Move issue to done state and close it."""
|
||||
# Set state to done
|
||||
self.set_issue_state(issue_number, ProjectState.DONE)
|
||||
try:
|
||||
# Set state to done
|
||||
self.set_issue_state(issue_number, ProjectState.DONE)
|
||||
|
||||
# Close the issue
|
||||
url = f"{self.config.issues_api_url}/{issue_number}"
|
||||
data = {'state': 'closed'}
|
||||
return self._make_api_call('PATCH', url, data)
|
||||
# Close the issue
|
||||
issue = self.gitea_client.issues.close(issue_number)
|
||||
return self._issue_to_dict(issue)
|
||||
except Exception as e:
|
||||
raise IssueError(f"Failed to move issue to done: {e}")
|
||||
|
||||
def get_project_overview(self) -> Dict[str, Any]:
|
||||
"""Get overview of project status."""
|
||||
|
||||
Reference in New Issue
Block a user