feat: Implement automatic git repository configuration detection for Gitea
Some checks failed
Test Suite / unit-tests (3.11) (push) Has been cancelled
Test Suite / unit-tests (3.12) (push) Has been cancelled
Test Suite / integration-tests (push) Has been cancelled
Test Suite / e2e-tests (push) Has been cancelled
Test Suite / performance-tests (push) Has been cancelled
Test Suite / code-quality (push) Has been cancelled
Test Suite / security-scan (push) Has been cancelled
Test Suite / test-summary (push) Has been cancelled

- Add GiteaConfig.from_git_repository() method for auto-detection
- Support HTTP(S) and SSH git remote URL formats
- Parse gitea_url, repo_owner, repo_name from git remote origin
- Only requires GITEA_API_TOKEN environment variable
- Update GiteaClient to use auto-detection as primary method
- Maintain backward compatibility with environment variables
- Fix issue creation API to use label IDs instead of names
- Add comprehensive error handling and validation
- Successfully tested with issues #33 and #34

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-28 23:27:13 +02:00
parent ad355f970c
commit ad25b2a7d7
4 changed files with 221 additions and 6 deletions

View File

@@ -3,9 +3,11 @@ Gitea-specific configuration management.
"""
import os
import subprocess
from pathlib import Path
from dataclasses import dataclass
from typing import Optional
from urllib.parse import urlparse
from .exceptions import GiteaConfigError
@@ -82,6 +84,66 @@ class GiteaConfig:
return config
@classmethod
def from_git_repository(cls) -> "GiteaConfig":
"""Create config by auto-detecting from current git repository.
Only requires GITEA_API_TOKEN environment variable.
All other settings are detected from git remote origin.
"""
try:
# Get git remote origin URL
result = subprocess.run(
['git', 'remote', 'get-url', 'origin'],
capture_output=True,
text=True,
check=True
)
origin_url = result.stdout.strip()
# Parse different URL formats
if origin_url.startswith('http://') or origin_url.startswith('https://'):
# HTTP(S) format: https://gitea.example.com/owner/repo.git
parsed = urlparse(origin_url)
gitea_url = f"{parsed.scheme}://{parsed.netloc}"
path_parts = parsed.path.strip('/').split('/')
if len(path_parts) >= 2:
repo_owner = path_parts[0]
repo_name = path_parts[1].replace('.git', '')
else:
raise GiteaConfigError(f"Cannot parse repository path from URL: {origin_url}")
elif '@' in origin_url:
# SSH format: git@gitea.example.com:owner/repo.git
if ':' in origin_url:
host_part, path_part = origin_url.split(':', 1)
host = host_part.split('@')[-1]
gitea_url = f"https://{host}" # Assume HTTPS for API
path_parts = path_part.strip('/').split('/')
if len(path_parts) >= 2:
repo_owner = path_parts[0]
repo_name = path_parts[1].replace('.git', '')
else:
raise GiteaConfigError(f"Cannot parse repository path from SSH URL: {origin_url}")
else:
raise GiteaConfigError(f"Cannot parse SSH URL format: {origin_url}")
else:
raise GiteaConfigError(f"Unsupported git remote URL format: {origin_url}")
# Get auth token from environment
auth_token = os.getenv('GITEA_API_TOKEN')
return cls(
gitea_url=gitea_url,
repo_owner=repo_owner,
repo_name=repo_name,
auth_token=auth_token
)
except subprocess.CalledProcessError as e:
raise GiteaConfigError(f"Failed to get git remote origin: {e}")
except Exception as e:
raise GiteaConfigError(f"Failed to auto-detect git repository config: {e}")
@classmethod
def from_tddai_config(cls, tddai_config) -> "GiteaConfig":
"""Create GiteaConfig from legacy TddaiConfig for backwards compatibility."""
@@ -110,4 +172,4 @@ class GiteaConfig:
def requires_auth(self, operation: str = "read") -> bool:
"""Check if operation requires authentication."""
write_operations = {"create", "update", "delete", "write"}
return operation in write_operations and not self.auth_token
return operation in write_operations and not self.auth_token