fix(api): cap weekly gitea stats readback
All checks were successful
Code Review / ai-code-review (push) Successful in 14s
CD Pipeline / tests (push) Successful in 1m51s
CD Pipeline / build-and-deploy (push) Successful in 3m46s
CD Pipeline / post-deploy-checks (push) Successful in 1m45s

This commit is contained in:
Your Name
2026-06-27 16:31:42 +08:00
parent 88304b2538
commit b93daa581a
2 changed files with 52 additions and 2 deletions

View File

@@ -162,12 +162,14 @@ class WeeklyReportService:
deploys = 0
page = 1
limit = 50
max_pages = 4
try:
for api_url in self._gitea_api_candidates():
commits = deploys = 0
page = 1
total_commits: int | None = None
try:
while page <= 20:
while page <= max_pages:
query = urlencode({
"sha": "main",
"since": since_utc,
@@ -177,6 +179,9 @@ class WeeklyReportService:
url = f"{api_url}/api/v1/repos/{owner}/{repo}/commits?{query}"
request = Request(url, headers=headers)
with urlopen(request, timeout=3) as response:
total_header = response.headers.get("X-Total-Count") or response.headers.get("X-Total")
if total_header and total_header.isdigit():
total_commits = int(total_header)
payload = json.loads(response.read().decode("utf-8"))
if not isinstance(payload, list):
logger.warning("gitea_git_stats_unexpected_payload", payload_type=type(payload).__name__)
@@ -189,8 +194,16 @@ class WeeklyReportService:
if "deploy" in subject or subject.startswith("chore(cd):"):
deploys += 1
if len(payload) < limit:
return commits, deploys, True
return total_commits or commits, deploys, True
page += 1
if total_commits is not None and total_commits > 0:
logger.info(
"gitea_git_stats_sampled",
total_commits=total_commits,
sampled_pages=max_pages,
sampled_deploys=deploys,
)
return total_commits, deploys, True
except Exception as candidate_exc:
logger.warning(
"gitea_git_stats_candidate_failed",