|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from datetime import datetime |
| 3 | +from dataclasses import dataclass |
| 4 | +import logging |
| 5 | + |
| 6 | +# Настройка логирования |
| 7 | +logging.basicConfig( |
| 8 | + level=logging.INFO, |
| 9 | + format="%(asctime)s - %(levelname)s - %(message)s" |
| 10 | +) |
| 11 | + |
| 12 | +# Модельные классы |
| 13 | +@dataclass |
| 14 | +class Repository: |
| 15 | + _id: str |
| 16 | + name: str |
| 17 | + url: str |
| 18 | + |
| 19 | +@dataclass |
| 20 | +class Contributor: |
| 21 | + username: str |
| 22 | + email: str |
| 23 | + |
| 24 | +@dataclass |
| 25 | +class Commit: |
| 26 | + _id: str |
| 27 | + message: str |
| 28 | + author: Contributor |
| 29 | + date: datetime |
| 30 | + |
| 31 | +@dataclass |
| 32 | +class Issue: |
| 33 | + _id: str |
| 34 | + title: str |
| 35 | + author: Contributor |
| 36 | + state: str |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class PullRequest: |
| 40 | + _id: str |
| 41 | + title: str |
| 42 | + author: Contributor |
| 43 | + state: str |
| 44 | + |
| 45 | +@dataclass |
| 46 | +class WikiPage: |
| 47 | + title: str |
| 48 | + content: str |
| 49 | + |
| 50 | +@dataclass |
| 51 | +class Branch: |
| 52 | + name: str |
| 53 | + last_commit: Commit | None |
| 54 | + |
| 55 | +# Интерфейс API |
| 56 | +class IRepositoryAPI(ABC): |
| 57 | + @abstractmethod |
| 58 | + def get_repository(self, id: str) -> Repository | None: |
| 59 | + """Получить репозиторий по его идентификатору.""" |
| 60 | + pass |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def get_commits(self, repo: Repository) -> list[Commit]: |
| 64 | + """Получить список коммитов для репозитория.""" |
| 65 | + pass |
| 66 | + |
| 67 | + @abstractmethod |
| 68 | + def get_contributors(self, repo: Repository) -> list[Contributor]: |
| 69 | + """Получить список контрибьюторов для репозитория.""" |
| 70 | + pass |
| 71 | + |
| 72 | + @abstractmethod |
| 73 | + def get_issues(self, repo: Repository) -> list[Issue]: |
| 74 | + """Получить список issues для репозитория.""" |
| 75 | + pass |
| 76 | + |
| 77 | + @abstractmethod |
| 78 | + def get_pull_requests(self, repo: Repository) -> list[PullRequest]: |
| 79 | + """Получить список pull requests для репозитория.""" |
| 80 | + pass |
| 81 | + |
| 82 | + @abstractmethod |
| 83 | + def get_branches(self, repo: Repository) -> list[Branch]: |
| 84 | + """Получить список веток для репозитория.""" |
| 85 | + pass |
| 86 | + |
| 87 | + @abstractmethod |
| 88 | + def get_wiki_pages(self, repo: Repository) -> list[WikiPage]: |
| 89 | + """Получить список wiki-страниц для репозитория.""" |
| 90 | + pass |
| 91 | + |
| 92 | + |
| 93 | +# Фабрика для создания API |
| 94 | +class RepositoryFactory: |
| 95 | + @staticmethod |
| 96 | + def create_api(source: str, client) -> IRepositoryAPI: |
| 97 | + if client is None: |
| 98 | + raise ValueError("Client cannot be None") |
| 99 | + if source == 'github': |
| 100 | + return GitHubRepoAPI(client) |
| 101 | + elif source == 'forgejo': |
| 102 | + return ForgejoRepoAPI(client) |
| 103 | + else: |
| 104 | + raise ValueError(f"Unsupported source: {source}") |
| 105 | + |
| 106 | +# Сервис для расчёта метрик |
| 107 | +class CommitmentCalculator: |
| 108 | + def __init__(self, api: IRepositoryAPI): |
| 109 | + self.api = api |
| 110 | + |
| 111 | + def calculate(self, repo: Repository) -> dict[str, int]: |
| 112 | + if not repo: |
| 113 | + return {} |
| 114 | + try: |
| 115 | + commits = self.api.get_commits(repo) |
| 116 | + if not commits: |
| 117 | + return {} |
| 118 | + result = {} |
| 119 | + for commit in commits: |
| 120 | + author = commit.author.username |
| 121 | + result[author] = result.get(author, 0) + 1 |
| 122 | + return result |
| 123 | + except Exception as e: |
| 124 | + logging.error(f"Failed to calculate commitment for repo {repo.name}: {e}") |
| 125 | + return {} |
0 commit comments