From e823c4c389e55b8061ebce9512f7ae1ffbe262c9 Mon Sep 17 00:00:00 2001 From: laincat <1129621+laincat@users.noreply.github.com> Date: Wed, 4 Nov 2026 17:09:03 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E4=BE=9D=E8=B5=96=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E9=81=87=E5=88=B0=E5=A4=B4=E6=8F=90=E4=BA=A4=E7=AB=9E=E6=80=81?= =?UTF-8?q?=E6=97=B6=E8=B7=B3=E8=BF=87=EF=BC=8C=E4=B8=8D=E5=86=8D=E6=9F=93?= =?UTF-8?q?=E7=BA=A2=E6=95=B4=E8=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2026-11-02 的「Dependabot validated merge」在 PR #14 上失败: CalledProcessError: Command '['gh', 'pr', 'merge', '14', '--repo', laincat/AwesomePyKit', '--squash', '--delete-branch', '--match-head-commit', '97638aa...']' returned non-zero exit status 1 脚本先按 sha 校验检查项,再执行带 --match-head-commit 的合并;这中间 Dependabot 又推了新提交(01:16:55 → d00fb666),头提交不再匹配,gh 拒绝 合并,check=True 直接把整轮门禁判红。下一次调度(01:19:27)已经正常合掉 同一个 PR,说明只是竞态。 改动: - gh pr merge 包在 try/except 里:失败时打印 "PR #N: merge skipped: ..." 并 继续下一个 PR,让下一次调度重新校验; - 合并成功后触发 post-merge / release 的 workflow_dispatch 仍然 check=True, 那一步失败是真问题,不能吞; - 补一条回归测试:合并调用抛 CalledProcessError 时不抛异常、也不触发后续 dispatch。 验证:python -m unittest test_dependabot_merge(含新增用例)通过; ruff check 通过。 --- .github/scripts/dependabot_merge.py | 16 +++++++++++++--- .github/scripts/test_dependabot_merge.py | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.github/scripts/dependabot_merge.py b/.github/scripts/dependabot_merge.py index cbc724d..e94ee15 100644 --- a/.github/scripts/dependabot_merge.py +++ b/.github/scripts/dependabot_merge.py @@ -90,9 +90,19 @@ def main(): if status["total_count"] and status["state"] != "success": print(f"PR #{number}: commit status incomplete or failed") continue - subprocess.run(["gh", "pr", "merge", str(number), "--repo", repo, - "--squash", "--delete-branch", - "--match-head-commit", sha], check=True) + try: + subprocess.run(["gh", "pr", "merge", str(number), "--repo", repo, + "--squash", "--delete-branch", + "--match-head-commit", sha], check=True, + capture_output=True, text=True) + except subprocess.CalledProcessError as error: + # 头提交可能在“检查完成”和“执行合并”之间又变了(Dependabot 重新 + # 推送),也可能已被别的流程合掉:这一轮跳过,等下一次调度重新 + # 校验,不要让整轮门禁变红。 + detail = (error.stderr or error.stdout or "").strip().splitlines() + print(f"PR #{number}: merge skipped: " + f"{detail[-1] if detail else error.returncode}") + continue print(f"Merged PR #{number} after successful validation of {sha}") # GITHUB_TOKEN merges do not trigger push workflows. Dispatch the build # explicitly so the upgrade reaches the published output. diff --git a/.github/scripts/test_dependabot_merge.py b/.github/scripts/test_dependabot_merge.py index 19f1969..f47d018 100644 --- a/.github/scripts/test_dependabot_merge.py +++ b/.github/scripts/test_dependabot_merge.py @@ -1,6 +1,7 @@ import copy import json import os +import subprocess import tempfile import unittest from pathlib import Path @@ -101,7 +102,7 @@ def setUp(self): "check_runs": [{"status": "completed", "conclusion": "success"}], } - def run_gate(self): + def run_gate(self, command_side_effect=None): def fake_api(endpoint): if endpoint.endswith("/pulls?per_page=100"): return [self.pr] @@ -122,7 +123,8 @@ def fake_api(endpoint): "GITHUB_EVENT_PATH": str(path), "GITHUB_REPOSITORY": self.repo, "POST_MERGE_WORKFLOW": "build.yml", }), patch("dependabot_merge.api", side_effect=fake_api), \ - patch("dependabot_merge.subprocess.run") as command: + patch("dependabot_merge.subprocess.run", + side_effect=command_side_effect) as command: main() return [call.args[0] for call in command.call_args_list] @@ -135,6 +137,16 @@ def test_validated_head_merges_and_dispatches(self): self.assertEqual(commands[1][1:4], ["workflow", "run", "build.yml"]) self.assertEqual(commands[2][1:4], ["workflow", "run", "release.yml"]) + def test_merge_race_is_skipped_not_fatal(self): + # 检查通过后 Dependabot 又推了新提交,gh pr merge 会因头提交不匹配失败。 + def fail_merge(*args, **kwargs): + raise subprocess.CalledProcessError( + 1, args[0], "", "X Pull request was modified") + + commands = self.run_gate(fail_merge) + self.assertEqual(len(commands), 1) + self.assertIn("pr", commands[0]) + def test_stale_head_never_merges(self): self.pr["head"]["sha"] = "new-untested" self.assertEqual(self.run_gate(), [])