push-upgrade-pr.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env bash
  2. # 常驻 PR 分支的推送与 PR 维护脚本(两个子命令)
  3. # prepare 准备 PR 分支:从 master 新建,或合并 master 最新内容(保证后续 push 可 fast-forward)
  4. # push <latest> <current> 提交升级、推送 auto-upgrade/dsh 分支并创建/更新唯一 PR
  5. # 版本提升(upgrade-dsh.sh)与冒烟测试(smoke-test.sh)由调用方在两个子命令之间完成
  6. #
  7. # Push and maintain the standing PR branch (two subcommands)
  8. # prepare Prep the PR branch: create it from master, or merge master's latest
  9. # content (so the later push is guaranteed fast-forward)
  10. # push <latest> <current> Commit the upgrade, push auto-upgrade/dsh, and create/update the single PR
  11. # The version bump (upgrade-dsh.sh) and smoke test (smoke-test.sh) run between the two subcommands
  12. #
  13. # 测试注入:设置 GH=/path/to/mock 可替换 gh 命令(用于单测)
  14. #
  15. # Test injection: set GH=/path/to/mock to substitute the gh command (used by unit tests)
  16. set -euo pipefail
  17. BRANCH="auto-upgrade/dsh"
  18. # gh 凭据双兜底:GH_TOKEN 优先,回退 GITHUB_TOKEN(与 open-issue.sh 同模式)
  19. #
  20. # gh credential belt-and-suspenders: GH_TOKEN wins, GITHUB_TOKEN as fallback (same pattern as open-issue.sh)
  21. GH_BIN="${GH:-gh}"
  22. GH_TOKEN="${GH_TOKEN:-${GITHUB_TOKEN:-}}"
  23. export GH_TOKEN
  24. pr_body() { # <latest> <current>
  25. local latest="$1" current="$2"
  26. cat <<EOF
  27. ## 自动升级 | Auto-upgrade
  28. - 上游版本 (upstream): \`$latest\`
  29. - 仓库当前版本 (repo current): \`$current\`
  30. - 变更文件 (files): Dockerfile / docker-compose.yml / README.md / README.zh.md
  31. - 运行日志 (run log): ${GITHUB_SERVER_URL:-https://github.com}/${GITHUB_REPOSITORY:-}/actions/runs/${GITHUB_RUN_ID:-}
  32. 升级脚本已提升版本并通过构建与冒烟测试(通过后才推送本分支)。
  33. The upgrade script bumped the version and passed build & smoke tests (this branch is pushed only after they pass).
  34. 合并时请勾选 **Delete branch**,下次自动升级将重建分支。
  35. Please tick **Delete branch** when merging so the next auto-upgrade can recreate the branch.
  36. EOF
  37. }
  38. upsert_pr() { # <latest> <current>
  39. local latest="$1" current="$2"
  40. local title state
  41. title="【维护,构建】升级 dsh 版本至 $latest"
  42. # 该分支已有 OPEN PR 时编辑刷新;否则新建(无 PR / 已合并 / 已关闭)
  43. #
  44. # Edit an OPEN PR on this branch; otherwise create a new one (none / merged / closed)
  45. state="$("$GH_BIN" pr view "$BRANCH" --json state -q '.state' 2>/dev/null || true)"
  46. if [ "$state" = "OPEN" ]; then
  47. "$GH_BIN" pr edit "$BRANCH" --title "$title" --body "$(pr_body "$latest" "$current")"
  48. else
  49. "$GH_BIN" pr create --base master --head "$BRANCH" --title "$title" --body "$(pr_body "$latest" "$current")"
  50. fi
  51. }
  52. prepare_branch() {
  53. # 分支不存在 → 从 master 新建;存在 → 合并 master 最新内容:
  54. # 工作树回到 master 状态(-X theirs),并产生以远端分支 HEAD 为祖先的合并提交,
  55. # 使后续 push 必然 fast-forward(ruleset 禁 force push 与删除分支)
  56. #
  57. # No branch → create from master; exists → merge master's latest:
  58. # the worktree returns to master's state (-X theirs) and the merge commit descends from the
  59. # remote branch HEAD, so the later push is guaranteed fast-forward
  60. # (the ruleset forbids force pushes and branch deletion)
  61. git fetch origin "$BRANCH" >/dev/null 2>&1 || true
  62. if git rev-parse --verify -q "refs/remotes/origin/$BRANCH" >/dev/null; then
  63. git checkout -B "$BRANCH" "origin/$BRANCH"
  64. # -X theirs 仅能解决文本冲突;结构性冲突(如文件删除)会中止合并,由 set -e 转为失败 Issue 人工介入
  65. #
  66. # -X theirs only resolves textual conflicts; structural ones (e.g. file deletion) abort the
  67. # merge and fail the job via set -e, surfacing as a failure Issue for human intervention
  68. git merge --no-edit -X theirs master
  69. else
  70. git checkout -B "$BRANCH" master
  71. fi
  72. }
  73. push_branch() { # <latest> <current>
  74. local latest="$1" current="$2"
  75. git config user.name "github-actions[bot]"
  76. git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
  77. git add Dockerfile docker-compose.yml README.md README.zh.md
  78. git commit -m "【维护,构建】升级 dsh 版本至 $latest"
  79. git push origin "$BRANCH"
  80. upsert_pr "$latest" "$current"
  81. }
  82. main() {
  83. case "${1:-}" in
  84. prepare) prepare_branch ;;
  85. push)
  86. shift
  87. push_branch "${1:?用法:push-upgrade-pr.sh push <latest> <current>}" \
  88. "${2:?用法:push-upgrade-pr.sh push <latest> <current>}"
  89. ;;
  90. *) echo "用法:push-upgrade-pr.sh prepare|push <latest> <current>" >&2; exit 2 ;;
  91. esac
  92. }
  93. # 仅在直接执行时进入 main;被 source(单测载入函数)时不执行
  94. #
  95. # Enter main only when executed directly; a source (unit tests loading functions) skips it
  96. if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  97. main "$@"
  98. fi