From e76f7c365b451c41c9d41155be48429598bd2006 Mon Sep 17 00:00:00 2001 From: Peter Zhang <18501667167@qq.com> Date: Fri, 29 May 2026 20:35:28 +0800 Subject: [PATCH] feat: consolidate auto-issue into ci.yml with if: failure() - Replace workflow_run with same-workflow conditional job - Add create_failure_issue.py script for Gitea API integration - More reliable than cross-workflow triggers Co-Authored-By: Claude Opus 4.7 --- .gitea/workflows/ci.yml | 17 ++++++++++ scripts/create_failure_issue.py | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 scripts/create_failure_issue.py diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 0268843..cca66d3 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -17,3 +17,20 @@ jobs: - name: Run tests run: python -m pytest tests/ -v + + create-issue-on-failure: + runs-on: shell + needs: test + if: failure() + steps: + - uses: actions/checkout@v4 + + - name: Create failure issue + shell: bash + run: | + python scripts/create_failure_issue.py \ + --sha "${{ github.sha }}" \ + --branch "${{ github.ref_name }}" \ + --run "${{ github.run_number }}" \ + --message "${{ github.event.head_commit.message }}" \ + --api-token "${{ secrets.GITEA_TOKEN }}" diff --git a/scripts/create_failure_issue.py b/scripts/create_failure_issue.py new file mode 100644 index 0000000..f04f806 --- /dev/null +++ b/scripts/create_failure_issue.py @@ -0,0 +1,59 @@ +"""Create a Gitea issue when CI fails. Called from ci.yml on failure.""" + +import argparse +import json +import urllib.request +import urllib.error + +GITEA_URL = "http://localhost:3000" +REPO = "pzhang_zywl/document_analyzer" + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--sha", required=True) + parser.add_argument("--branch", required=True) + parser.add_argument("--run", required=True) + parser.add_argument("--message", required=True) + parser.add_argument("--api-token", required=True) + args = parser.parse_args() + + sha_short = args.sha[:7] + run_url = f"{GITEA_URL}/{REPO}/actions/runs/{args.run}" + + title = f"CI Failure: {args.message[:80]}" + body = ( + f"## CI 测试失败\n\n" + f"- **Commit:** {sha_short}\n" + f"- **Branch:** {args.branch}\n" + f"- **工作流运行:** {run_url}\n\n" + f"请检查上述链接查看失败详情。\n\n" + f"### 下一步\n" + f"- [ ] 分析失败原因\n" + f"- [ ] 修复代码\n" + f"- [ ] 提交 PR 触发 CI 重测" + ) + + payload = json.dumps({ + "title": title, + "body": body, + "labels": ["ci-failure", "agent-task"], + }).encode("utf-8") + + url = f"{GITEA_URL}/api/v1/repos/{REPO}/issues" + req = urllib.request.Request(url, data=payload, method="POST") + req.add_header("Authorization", f"token {args.api_token}") + req.add_header("Content-Type", "application/json") + + try: + with urllib.request.urlopen(req) as resp: + data = json.loads(resp.read()) + print(f"Issue created: {data.get('html_url', data.get('url', 'unknown'))}") + except urllib.error.HTTPError as e: + print(f"Failed to create issue: {e.code} {e.reason}") + print(e.read().decode()) + raise + + +if __name__ == "__main__": + main()