# fast-forward for PR # # 维护者或写权限用户回复含 /fast-forward 的评论后, 调用平台 API 以变基 (rebase) # 方式合并 PR, 保留原始提交与签名. 合并失败时评论说明原因. # (纯 shell + 平台 API, 不依赖第三方 action) # # 要求: 平台启用 Gitea Actions 并注册 ubuntu runner. name: fast-forward for PR on: issue_comment: types: [created, edited] pull_request_review_comment: types: [created, edited] jobs: fast-forward: if: > contains(github.event.comment.body, '/fast-forward') && (github.event.issue.pull_request || github.event.pull_request) runs-on: ubuntu-latest steps: - name: Install jq run: | command -v jq && exit 0 sudo apt-get install -y --no-install-recommends jq \ || (sudo apt-get update && sudo apt-get install -y --no-install-recommends jq) - name: Check permission and merge env: TOKEN: ${{ secrets.GITHUB_TOKEN }} API: ${{ gitea.server_url || 'https://git.xhmcl.com' }}/api/v1 REPO: ${{ github.repository }} PR_NUM: ${{ github.event.issue.number || github.event.pull_request.number }} COMMENT_USER: ${{ github.event.comment.user.login }} run: | set -euo pipefail OWNER="${REPO%%/*}" NAME="${REPO#*/}" api() { curl -fsS -H "Authorization: token ${TOKEN}" -H "Content-Type: application/json" "$@"; } comment() { local msg="$1" BODY_JSON=$(jq -n --arg b "$msg" '{"body": $b}') api -X POST -d "$BODY_JSON" "$API/repos/$OWNER/$NAME/issues/$PR_NUM/comments" || true } # 权限检查 (write 及以上) perm=$(api "$API/repos/$OWNER/$NAME/collaborators/$COMMENT_USER/permission" 2>/dev/null | jq -r '.permission // "none"' || echo none) case "$perm" in admin|write|owner) ;; *) comment "**无法合并 (Merge rejected).** @$COMMENT_USER 没有该仓库的写权限, \`/fast-forward\` 仅限维护者使用. (You need write access to use \`/fast-forward\`.)" exit 1 ;; esac # 变基合并 (Gitea 的 rebase 合并会保留原始提交与 GPG 签名) HTTP_CODE=$(curl -sS -o "$RUNNER_TEMP/merge.json" -w '%{http_code}' \ -X POST -H "Authorization: token ${TOKEN}" -H "Content-Type: application/json" \ -d '{"Do":"rebase"}' "$API/repos/$OWNER/$NAME/pulls/$PR_NUM/merge") if [ "$HTTP_CODE" = "200" ]; then MERGED=$(jq -r '.merged // false' "$RUNNER_TEMP/merge.json") STATE=$(jq -r '.state // ""' "$RUNNER_TEMP/merge.json") if [ "$MERGED" = "true" ] || [ "$STATE" = "closed" ]; then comment "**合并完成 (Merged)!** 已以变基方式合并, 原始提交与签名得到保留. (Merged with rebase; original commits and signatures preserved.) 🎉" exit 0 fi comment "**合并结果 (Merge result):** state=$STATE merged=$MERGED\n$(jq -r '.message // ""' "$RUNNER_TEMP/merge.json")" exit 1 fi # 非 200: 输出错误原因 ERR_MSG=$(jq -r '.message // .error // "unknown error"' "$RUNNER_TEMP/merge.json" 2>/dev/null || echo "HTTP $HTTP_CODE") comment "**合并失败 (Merge failed, HTTP $HTTP_CODE):**\n\n\`\`\`\n$ERR_MSG\n\`\`\`\n\n常见原因: 存在合并冲突、需要先变基到最新 main, 或检查未通过. 请先在本地 rebase 后再推送. (Common causes: conflicts, branch needs rebasing onto latest main, or checks failed.)" exit 1