get_version() now returns version.name instead of str(version), which returned the numeric ID from redminelib.resources.Version. Falls back to str() when .name is absent (#14). Add issue.id as tertiary sort key in build_grouped_report so tasks within the same project+version group always appear in the same order regardless of API response ordering (#33). Remove redundant sort from client.fetch_issues_with_spent_time — the sort already runs in report_builder.build_grouped_report, so doing it twice wastes CPU. Also remove the now-unused get_version import (#32). Closes #14, closes #32, closes #33
65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
from typing import Any, Dict, List, Optional, Tuple
|
||
|
||
from redminelib import Redmine
|
||
from redminelib.resources import Issue
|
||
|
||
from .config import Config
|
||
|
||
|
||
def _get_redmine_auth_kwargs() -> Dict[str, Any]:
|
||
"""Return Redmine auth kwargs. API key has priority over legacy password auth."""
|
||
api_key = Config.get_redmine_api_key()
|
||
if api_key:
|
||
return {"key": api_key}
|
||
return {
|
||
"username": Config.get_redmine_user(),
|
||
"password": Config.get_redmine_password(),
|
||
}
|
||
|
||
|
||
def fetch_issues_with_spent_time(
|
||
from_date: str, to_date: str
|
||
) -> Optional[List[Tuple[Issue, float]]]:
|
||
"""
|
||
Fetch unique issues linked to time entries of the current user in given date range,
|
||
along with total spent hours per issue.
|
||
Returns list of (issue, total_hours) tuples.
|
||
"""
|
||
|
||
redmine = Redmine(
|
||
Config.get_redmine_url(),
|
||
**_get_redmine_auth_kwargs(),
|
||
requests={"verify": Config.get_redmine_verify()},
|
||
)
|
||
|
||
current_user = redmine.user.get("current")
|
||
time_entries = redmine.time_entry.filter(
|
||
user_id=current_user.id, from_date=from_date, to_date=to_date
|
||
)
|
||
|
||
# Агрегируем часы по issue.id
|
||
spent_time: Dict[int, float] = {}
|
||
issue_ids = set()
|
||
for entry in time_entries:
|
||
if hasattr(entry, "issue") and entry.issue and hasattr(entry, "hours"):
|
||
iid = entry.issue.id
|
||
issue_ids.add(iid)
|
||
spent_time[iid] = spent_time.get(iid, 0.0) + float(entry.hours)
|
||
|
||
if not issue_ids:
|
||
return None
|
||
|
||
# Загружаем полные объекты задач
|
||
issue_list_str = ",".join(str(i) for i in issue_ids)
|
||
issues = redmine.issue.filter(issue_id=issue_list_str, status_id="*", sort="project:asc")
|
||
|
||
# Сопоставляем задачи с суммарным временем.
|
||
# Сортировка выполняется в report_builder.build_grouped_report,
|
||
# здесь оставляем порядок API как есть.
|
||
result = []
|
||
for issue in issues:
|
||
total_hours = spent_time.get(issue.id, 0.0)
|
||
result.append((issue, total_hours))
|
||
|
||
return result
|