Skip to content

fix(github-api): 增加请求超时和重试 - #292

Merged
Terry-Mao merged 2 commits into
mainfrom
fix/github-api-retry-291
Oct 3, 2026
Merged

Terry-Mao merged 2 commits into
mainfrom
fix/github-api-retry-291

Conversation

@Terry-Mao

Copy link
Copy Markdown
Owner

摘要

  • 为 post_pr_review.py 的 GitHub API 请求增加 30 秒网络超时。
  • 对 DNS、连接和超时错误最多重试 3 次,并使用 1 秒、2 秒指数退避;HTTP 错误保持立即失败。
  • 增加成功重试和最终失败场景的测试,确保 review artifact 可由 workflow 后续步骤保留。

验证

  • PYTHONDONTWRITEBYTECODE=1 .venv/bin/python -m unittest discover -s .github/aicodingflow-tests -p 'test_post_pr_review.py'
  • PYTHONDONTWRITEBYTECODE=1 .venv/bin/python -m unittest discover -s .github/aicodingflow-tests(507 项)
  • git diff --check

Closes #291

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本次改动为 GitHub API 请求增加 30 秒超时和最多 3 次指数退避重试,方向合理,但重试未区分请求的幂等性:github_api_response 会被 request_json 用于 POST 创建 PR review(/pulls/{n}/reviews)以及 request_reviewer 请求 reviewer(/requested_reviewers)。当请求已到达 GitHub、但在读取响应阶段超时时,重试会重复创建 review 或重复请求 reviewer;重复请求 reviewer 还会因 422 触发 SystemExit,把已成功的调用判定为失败。详见行内评论。

补充(无法定位到具体 changed line 的缺失项):PR 描述声明“HTTP 错误保持立即失败”,但新增测试只覆盖了网络错误重试与重试耗尽,未覆盖 HTTPError 立即失败(不被重试)的路径,建议补一条断言固化该行为,避免后续改动误改 except 顺序。

review_discussion_context.json 为空,无历史评论需要去重。

Comment thread .github/scripts/post_pr_review.py

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查结论:本次改动为 github_api_response 增加了 30 秒网络超时,并按 HTTP 方法限制重试:仅对 GET/HEAD/OPTIONS/PUT/DELETE 等幂等请求最多重试 3 次(退避 1s、2s),POST 立即失败;实现与 PR 描述一致。新增测试覆盖了成功重试、达到重试上限、POST 不重试三种场景,本地运行 test_post_pr_review.py 全部通过。

关于既有未解决讨论线程(.github/scripts/post_pr_review.py 第 84 行,comment 4173791633):该线程担忧重试非幂等的 POST 会重复创建 review 或重复请求 reviewer。当前 diff 已通过 GITHUB_API_RETRYABLE_METHODS 允许列表把 POST 的 max_attempts 固定为 1,并新增 test_github_api_response_does_not_retry_post_requests 验证,原问题已解决,因此不再重复提交同一 inline 评论。

非阻塞建议:PR 描述称 HTTP 错误仍立即失败,但新增测试未覆盖 urllib.error.HTTPError 不会被重试这一路径;后续可补一个 HTTPError 测试以防回归。

@Terry-Mao
Terry-Mao merged commit 7703e16 into main Oct 3, 2026
3 checks passed
@Terry-Mao
Terry-Mao deleted the fix/github-api-retry-291 branch October 3, 2026 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

增强 GitHub Action 中 GitHub API 请求健壮性

1 participant