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(), [])