fix(api): fallback weekly git stats to gitea
Some checks failed
Code Review / ai-code-review (push) Successful in 13s
CD Pipeline / tests (push) Successful in 2m28s
CD Pipeline / build-and-deploy (push) Successful in 5m1s
CD Pipeline / post-deploy-checks (push) Has been cancelled

This commit is contained in:
Your Name
2026-06-27 16:13:42 +08:00
parent 67f1da991d
commit 562a61990e
2 changed files with 108 additions and 3 deletions

View File

@@ -58,12 +58,33 @@ class TestWeeklyReportGitStats:
)
svc = WeeklyReportService(stats_service=object(), k3s_monitor=object())
monkeypatch.setattr(svc, "_get_gitea_commit_stats", lambda since: (0, 0, False))
commits, deploys, source_ok = svc._get_git_stats(datetime.now(_TZ_TAIPEI))
assert commits == 0
assert deploys == 0
assert source_ok is False
def test_git_log_failure_uses_gitea_fallback(self, monkeypatch):
class Result:
returncode = 128
stdout = ""
stderr = "fatal: not a git repository"
monkeypatch.setattr(
weekly_report_module.subprocess,
"run",
lambda *args, **kwargs: Result(),
)
svc = WeeklyReportService(stats_service=object(), k3s_monitor=object())
monkeypatch.setattr(svc, "_get_gitea_commit_stats", lambda since: (12, 4, True))
commits, deploys, source_ok = svc._get_git_stats(datetime.now(_TZ_TAIPEI))
assert commits == 12
assert deploys == 4
assert source_ok is True
def test_git_deploy_log_failure_marks_source_failed_after_commits(self, monkeypatch):
class CommitResult:
returncode = 0
@@ -84,12 +105,48 @@ class TestWeeklyReportGitStats:
monkeypatch.setattr(weekly_report_module.subprocess, "run", fake_run)
svc = WeeklyReportService(stats_service=object(), k3s_monitor=object())
monkeypatch.setattr(svc, "_get_gitea_commit_stats", lambda since: (0, 0, False))
commits, deploys, source_ok = svc._get_git_stats(datetime.now(_TZ_TAIPEI))
assert commits == 2
assert commits == 0
assert deploys == 0
assert source_ok is False
def test_gitea_commit_stats_counts_deploy_markers(self, monkeypatch):
payload = [
{"commit": {"message": "chore(cd): deploy abc123 [skip ci]\n"}},
{"commit": {"message": "fix(api): repair source health\n"}},
{"commit": {"message": "docs(logbook): record deploy verification [skip ci]\n"}},
]
class Response:
def __enter__(self):
return self
def __exit__(self, *_args):
return None
def read(self):
return weekly_report_module.json.dumps(payload).encode("utf-8")
def fake_urlopen(request, timeout):
assert "/api/v1/repos/wooo/awoooi/commits?" in request.full_url
assert timeout == 8
return Response()
monkeypatch.setattr(weekly_report_module, "urlopen", fake_urlopen)
monkeypatch.setattr(weekly_report_module.settings, "GITEA_API_URL", "https://gitea.example.test")
monkeypatch.setattr(weekly_report_module.settings, "GITEA_REPO_OWNER", "wooo")
monkeypatch.setattr(weekly_report_module.settings, "GITEA_REPO_NAME", "awoooi")
monkeypatch.setattr(weekly_report_module.settings, "GITEA_API_TOKEN", "")
svc = WeeklyReportService(stats_service=object(), k3s_monitor=object())
commits, deploys, source_ok = svc._get_gitea_commit_stats(datetime.now(_TZ_TAIPEI))
assert commits == 3
assert deploys == 2
assert source_ok is True
# =============================================================================
# DailyKpi 計算屬性