push-upgrade-pr.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 的 merge 与 push 的 commit 都会创建提交,而 CI runner 无全局身份
  53. # (本地开发环境自带身份,CI 上 merge 曾因 empty ident 失败)
  54. #
  55. # Committer identity: prepare's merge and push's commit both create commits, and the CI runner has
  56. # no global identity (a CI run failed the merge with "empty ident" before this was added)
  57. ensure_identity() {
  58. git config user.name "github-actions[bot]"
  59. git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
  60. }
  61. prepare_branch() {
  62. # 始终基于 master 重建本地分支(工作树回到 master 最新内容),再以 --no-ff -s ours 合并远端分支历史:
  63. # 新提交以远端分支 HEAD 为祖先,push 必然 fast-forward;合并结果完全取 master 内容,
  64. # 保证 bump 永远基于 master 而非旧分支残留(ruleset 禁 force push 与删除分支)。
  65. # --no-ff 不可省:远端分支是 master 后代时(上次推送未合并的重试场景),普通 merge 会 fast-forward
  66. # 使工作树变成旧分支内容,bump 将无事可做
  67. #
  68. # Rebuild the local branch on master (worktree returns to master's latest), then merge the remote
  69. # branch history with --no-ff -s ours: the new commit descends from the remote branch HEAD, so the
  70. # push is guaranteed fast-forward; the merge result is exactly master's content, so the bump
  71. # always starts from master, never from stale branch leftovers
  72. # (the ruleset forbids force pushes and branch deletion).
  73. # --no-ff is essential: when the remote branch descends from master (a retry after an unmerged push),
  74. # a plain merge would fast-forward the worktree back to the stale branch content and leave nothing to bump
  75. # 显式 refspec 抓取远端分支:不能依赖 remote.origin.fetch 的默认映射(浅克隆时它可能只映射
  76. # master,裸分支名 fetch 仅更新 FETCH_HEAD,导致下方 refs/remotes 判断误判「分支不存在」)
  77. #
  78. # Fetch the remote branch with an explicit refspec: never rely on remote.origin.fetch's default
  79. # mapping (a shallow clone may map only master; a bare branch-name fetch then updates only
  80. # FETCH_HEAD, making the refs/remotes check below falsely see "branch missing")
  81. git fetch origin "refs/heads/$BRANCH:refs/remotes/origin/$BRANCH" >/dev/null 2>&1 || true
  82. ensure_identity
  83. git checkout -B "$BRANCH" master
  84. if git rev-parse --verify -q "refs/remotes/origin/$BRANCH" >/dev/null; then
  85. # -s ours 策略:合并结果树完全取本地(master)内容,仅将远端分支历史并入祖先链,
  86. # 保证新提交可 fast-forward 推送。与 -X ours(仅解决文本冲突)不同,-s ours 彻底无视
  87. # 分支内容且永不因冲突中止——旧分支上的内容一律以 master 为准,丢弃也无妨
  88. # (分支只含自动升级内容,最终以 master + 新 bump 为准)
  89. #
  90. # -s ours strategy: the merge result tree is exactly the local (master) content; the remote
  91. # branch history is only grafted into the ancestry so the new commit can be pushed fast-forward.
  92. # Unlike -X ours (which only resolves textual conflicts), -s ours ignores the branch content
  93. # entirely and never aborts on conflicts — anything stale on the branch is safely discarded
  94. # (the branch only carries auto-upgrade content; master plus the new bump is authoritative)
  95. git merge --no-ff --no-edit -s ours "origin/$BRANCH"
  96. fi
  97. }
  98. push_branch() { # <latest> <current>
  99. local latest="$1" current="$2"
  100. ensure_identity
  101. git add Dockerfile docker-compose.yml README.md README.zh.md
  102. git commit -m "【维护,构建】升级 dsh 版本至 $latest"
  103. git push origin "$BRANCH"
  104. upsert_pr "$latest" "$current"
  105. }
  106. main() {
  107. case "${1:-}" in
  108. prepare) prepare_branch ;;
  109. push)
  110. shift
  111. push_branch "${1:?用法:push-upgrade-pr.sh push <latest> <current>}" \
  112. "${2:?用法:push-upgrade-pr.sh push <latest> <current>}"
  113. ;;
  114. *) echo "用法:push-upgrade-pr.sh prepare|push <latest> <current>" >&2; exit 2 ;;
  115. esac
  116. }
  117. # 仅在直接执行时进入 main;被 source(单测载入函数)时不执行
  118. #
  119. # Enter main only when executed directly; a source (unit tests loading functions) skips it
  120. if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  121. main "$@"
  122. fi