fix(api): avoid slow weekly git fallback
Some checks failed
Code Review / ai-code-review (push) Successful in 15s
CD Pipeline / tests (push) Successful in 1m44s
CD Pipeline / build-and-deploy (push) Successful in 5m48s
CD Pipeline / post-deploy-checks (push) Has been cancelled

This commit is contained in:
Your Name
2026-06-27 16:23:28 +08:00
parent 4a7f804731
commit 1847d5a2e5
2 changed files with 74 additions and 30 deletions

View File

@@ -58,6 +58,7 @@ class TestWeeklyReportGitStats:
)
svc = WeeklyReportService(stats_service=object(), k3s_monitor=object())
monkeypatch.setattr(svc, "_has_local_git_worktree", lambda cwd: True)
monkeypatch.setattr(svc, "_get_gitea_commit_stats", lambda since: (0, 0, False))
commits, deploys, source_ok = svc._get_git_stats(datetime.now(_TZ_TAIPEI))
@@ -78,6 +79,7 @@ class TestWeeklyReportGitStats:
)
svc = WeeklyReportService(stats_service=object(), k3s_monitor=object())
monkeypatch.setattr(svc, "_has_local_git_worktree", lambda cwd: True)
monkeypatch.setattr(svc, "_get_gitea_commit_stats", lambda since: (12, 4, True))
commits, deploys, source_ok = svc._get_git_stats(datetime.now(_TZ_TAIPEI))
@@ -105,6 +107,7 @@ class TestWeeklyReportGitStats:
monkeypatch.setattr(weekly_report_module.subprocess, "run", fake_run)
svc = WeeklyReportService(stats_service=object(), k3s_monitor=object())
monkeypatch.setattr(svc, "_has_local_git_worktree", lambda cwd: True)
monkeypatch.setattr(svc, "_get_gitea_commit_stats", lambda since: (0, 0, False))
commits, deploys, source_ok = svc._get_git_stats(datetime.now(_TZ_TAIPEI))
@@ -112,6 +115,21 @@ class TestWeeklyReportGitStats:
assert deploys == 0
assert source_ok is False
def test_missing_local_git_worktree_uses_gitea_without_subprocess(self, monkeypatch):
def fail_run(*_args, **_kwargs):
raise AssertionError("subprocess git log should not run without local .git")
monkeypatch.setattr(weekly_report_module.subprocess, "run", fail_run)
svc = WeeklyReportService(stats_service=object(), k3s_monitor=object())
monkeypatch.setattr(svc, "_has_local_git_worktree", lambda cwd: False)
monkeypatch.setattr(svc, "_get_gitea_commit_stats", lambda since: (21, 7, True))
commits, deploys, source_ok = svc._get_git_stats(datetime.now(_TZ_TAIPEI))
assert commits == 21
assert deploys == 7
assert source_ok is True
def test_gitea_commit_stats_counts_deploy_markers(self, monkeypatch):
payload = [
{"commit": {"message": "chore(cd): deploy abc123 [skip ci]\n"}},
@@ -131,7 +149,7 @@ class TestWeeklyReportGitStats:
def fake_urlopen(request, timeout):
assert "/api/v1/repos/wooo/awoooi/commits?" in request.full_url
assert timeout == 8
assert timeout == 3
return Response()
monkeypatch.setattr(weekly_report_module, "urlopen", fake_urlopen)