Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions .github/scripts/dependabot_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions .github/scripts/test_dependabot_merge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import json
import os
import subprocess
import tempfile
import unittest
from pathlib import Path
Expand Down Expand Up @@ -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]
Expand All @@ -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]

Expand All @@ -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(), [])
Expand Down