diff --git a/.github/workflows/promote-dev-to-main.yml b/.github/workflows/promote-dev-to-main.yml new file mode 100644 index 00000000..dbb805ef --- /dev/null +++ b/.github/workflows/promote-dev-to-main.yml @@ -0,0 +1,270 @@ +name: Promote Dev to Main + +# Opens (or refreshes) a Pull Request that promotes the `dev` branch into +# `main`, authored by the SnowLuma Bot GitHub App. +# +# Triggers +# 1. push to `dev` whose head commit message contains `[merge]` (anywhere, +# case-insensitive) OR starts with `chore(release):` (case-insensitive). +# 2. push of any `v*` tag (release tag, regardless of which branch it points +# to). The PR is still opened from `dev` head -> `main` head. +# 3. manual `workflow_dispatch` from the Actions tab. +# +# Required secrets (re-uses the App that already opens nnphook native PRs): +# - SNOWLUMA_BOT_APP_ID +# - SNOWLUMA_BOT_PRIVATE_KEY +# +# The GitHub App must be installed on SnowLuma/SnowLuma with: +# - Contents: Read+Write +# - Pull requests: Read+Write +# +# To make `gh pr merge --auto` actually merge under `main` branch protection, +# allow the App to bypass required PR rules (Settings -> Rules / Branches -> +# Edit rule -> "Allow specified actors to bypass" / "Restrict pushes" -> add +# the App). Otherwise the PR will sit in auto-merge state until a human +# approves it. + +on: + push: + branches: [dev] + tags: ['v*'] + workflow_dispatch: + inputs: + auto_merge: + description: Enable auto-merge on the resulting PR + type: choice + options: ['true', 'false'] + default: 'true' + merge_method: + description: Merge strategy when auto-merging + type: choice + options: ['merge', 'squash', 'rebase'] + default: 'merge' + +permissions: + contents: read + +concurrency: + group: promote-dev-to-main + cancel-in-progress: false + +jobs: + decide: + runs-on: ubuntu-latest + outputs: + run: ${{ steps.check.outputs.run }} + auto_merge: ${{ steps.check.outputs.auto_merge }} + merge_method: ${{ steps.check.outputs.merge_method }} + reason: ${{ steps.check.outputs.reason }} + steps: + - name: Decide whether to promote + id: check + env: + EVENT_NAME: ${{ github.event_name }} + REF: ${{ github.ref }} + REF_TYPE: ${{ github.ref_type }} + HEAD_COMMIT_MSG: ${{ github.event.head_commit.message }} + INPUT_AUTO_MERGE: ${{ inputs.auto_merge }} + INPUT_MERGE_METHOD: ${{ inputs.merge_method }} + run: | + set -euo pipefail + run='false' + auto_merge='true' + merge_method='merge' + reason='' + + case "${EVENT_NAME}" in + workflow_dispatch) + run='true' + auto_merge="${INPUT_AUTO_MERGE:-true}" + merge_method="${INPUT_MERGE_METHOD:-merge}" + reason='Manual workflow_dispatch' + ;; + push) + if [ "${REF_TYPE}" = 'tag' ]; then + run='true' + merge_method='merge' + reason="Tag push: ${REF}" + elif [ "${REF}" = 'refs/heads/dev' ]; then + msg="${HEAD_COMMIT_MSG:-}" + if printf '%s' "${msg}" | grep -qiE '\[merge\]|^chore\(release\):'; then + run='true' + merge_method='merge' + reason='Commit message marker on dev ([merge] or chore(release):)' + else + reason='Commit on dev without [merge] / chore(release): marker' + fi + else + reason="Push event on unhandled ref: ${REF}" + fi + ;; + *) + reason="Unhandled event: ${EVENT_NAME}" + ;; + esac + + { + echo "run=${run}" + echo "auto_merge=${auto_merge}" + echo "merge_method=${merge_method}" + echo "reason=${reason}" + } >> "${GITHUB_OUTPUT}" + + { + echo "## Promote decision" + echo + echo "| Key | Value |" + echo "| --- | --- |" + echo "| Event | \`${EVENT_NAME}\` |" + echo "| Ref | \`${REF}\` |" + echo "| Run | \`${run}\` |" + echo "| Auto-merge | \`${auto_merge}\` |" + echo "| Merge method | \`${merge_method}\` |" + echo "| Reason | ${reason} |" + } >> "${GITHUB_STEP_SUMMARY}" + + promote: + needs: decide + if: needs.decide.outputs.run == 'true' + runs-on: ubuntu-latest + steps: + - name: Validate GitHub App credentials + env: + APP_ID: ${{ secrets.SNOWLUMA_BOT_APP_ID }} + PRIVATE_KEY: ${{ secrets.SNOWLUMA_BOT_PRIVATE_KEY }} + run: | + if [ -z "${APP_ID}" ] || [ -z "${PRIVATE_KEY}" ]; then + echo 'Missing SNOWLUMA_BOT_APP_ID and/or SNOWLUMA_BOT_PRIVATE_KEY secrets.' >&2 + echo 'Configure the same GitHub App used by the nnphook native PR flow.' >&2 + exit 1 + fi + + - name: Mint GitHub App installation token + id: app-token + uses: actions/create-github-app-token@v2 + with: + app-id: ${{ secrets.SNOWLUMA_BOT_APP_ID }} + private-key: ${{ secrets.SNOWLUMA_BOT_PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + repositories: ${{ github.event.repository.name }} + + - name: Resolve bot identity + id: bot + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + APP_SLUG: ${{ steps.app-token.outputs.app-slug }} + run: | + set -euo pipefail + bot_login="${APP_SLUG}[bot]" + bot_id="$(gh api "/users/${bot_login}" --jq .id)" + bot_email="${bot_id}+${bot_login}@users.noreply.github.com" + { + echo "login=${bot_login}" + echo "id=${bot_id}" + echo "email=${bot_email}" + } >> "${GITHUB_OUTPUT}" + echo "Resolved bot: ${bot_login} <${bot_email}>" + + - name: Checkout main + uses: actions/checkout@v6 + with: + ref: main + fetch-depth: 0 + token: ${{ steps.app-token.outputs.token }} + + - name: Compare dev to main + id: diff + run: | + set -euo pipefail + git fetch origin dev:dev + ahead="$(git rev-list --count main..dev)" + behind="$(git rev-list --count dev..main)" + dev_sha="$(git rev-parse dev)" + dev_short="$(git rev-parse --short dev)" + { + echo "ahead=${ahead}" + echo "behind=${behind}" + echo "dev_sha=${dev_sha}" + echo "dev_short=${dev_short}" + } >> "${GITHUB_OUTPUT}" + if [ "${ahead}" = '0' ]; then + echo "skip=true" >> "${GITHUB_OUTPUT}" + echo "::notice::dev has no commits ahead of main; nothing to promote." + else + echo "skip=false" >> "${GITHUB_OUTPUT}" + echo "dev is ${ahead} ahead, ${behind} behind main." + fi + + - name: Ensure automation label exists + if: steps.diff.outputs.skip != 'true' + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + run: | + gh label create automation --color 0E8A16 --description 'Automated bot PRs' 2>/dev/null || true + + - name: Create or update Pull Request + id: pr + if: steps.diff.outputs.skip != 'true' + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + DEV_SHA: ${{ steps.diff.outputs.dev_sha }} + DEV_SHORT: ${{ steps.diff.outputs.dev_short }} + AHEAD: ${{ steps.diff.outputs.ahead }} + BEHIND: ${{ steps.diff.outputs.behind }} + REASON: ${{ needs.decide.outputs.reason }} + REF: ${{ github.ref }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + set -euo pipefail + title="chore: promote dev → main (${DEV_SHORT})" + + { + echo "Automated promotion of \`dev\` into \`main\`." + echo + echo "| Key | Value |" + echo "| --- | --- |" + echo "| Source | \`dev\` @ \`${DEV_SHA}\` |" + echo "| Trigger ref | \`${REF}\` |" + echo "| Trigger reason | ${REASON} |" + echo "| Workflow run | ${RUN_URL} |" + echo "| Ahead / Behind | ${AHEAD} / ${BEHIND} |" + echo + echo "Created by SnowLuma Bot." + } > pr-body.md + + existing="$(gh pr list --base main --head dev --state open --json number --jq '.[0].number' 2>/dev/null || true)" + if [ -n "${existing}" ]; then + gh pr edit "${existing}" --title "${title}" --body-file pr-body.md + pr_number="${existing}" + echo "Updated existing PR #${pr_number}" + else + url="$(gh pr create --base main --head dev --title "${title}" --body-file pr-body.md --label automation)" + pr_number="${url##*/}" + echo "Opened new PR #${pr_number} (${url})" + fi + + echo "number=${pr_number}" >> "${GITHUB_OUTPUT}" + { + echo "## Promote PR" + echo + echo "- Number: #${pr_number}" + echo "- Source: \`dev@${DEV_SHORT}\`" + echo "- Ahead/Behind: ${AHEAD}/${BEHIND}" + } >> "${GITHUB_STEP_SUMMARY}" + + - name: Enable auto-merge + if: steps.diff.outputs.skip != 'true' && needs.decide.outputs.auto_merge == 'true' + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + PR_NUMBER: ${{ steps.pr.outputs.number }} + MERGE_METHOD: ${{ needs.decide.outputs.merge_method }} + run: | + set -euo pipefail + method_flag='--merge' + case "${MERGE_METHOD}" in + squash) method_flag='--squash' ;; + rebase) method_flag='--rebase' ;; + *) method_flag='--merge' ;; + esac + gh pr merge "${PR_NUMBER}" --auto ${method_flag} + echo "::notice::Auto-merge enabled on PR #${PR_NUMBER} with strategy: ${MERGE_METHOD}" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..ed039cf9 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,96 @@ +# 贡献指南 + +## 分支模型 + +SnowLuma 使用 **`main` + `dev`** 双分支模型: + +- **`main`** — 受保护的稳定分支,仅接收来自 `dev` 的合并 PR。**禁止直接 push**。 +- **`dev`** — 日常开发分支。所有功能、修复、文档变更都先提交到 `dev`。 +- **`native/auto-update-*`** — 由 `SnowLuma Bot` 自动创建的原生产物更新分支(来自 `SnowLumaNative`,不需要手动维护)。 + +## 把 `dev` 合并到 `main` + +新增工作流 `.github/workflows/promote-dev-to-main.yml` 会以 `SnowLuma Bot` 身份开 / 更新一个 `dev → main` 的 PR,并可选地自动合并。三种触发方式: + +### 1. 提交信息标记(推荐日常使用) + +向 `dev` push 一个提交,提交信息满足以下任一条件即触发: + +- 任意位置包含 `[merge]`(不区分大小写) +- 以 `chore(release):` 开头(不区分大小写,符合 conventional commits 的发版习惯) + +示例: + +```text +fix: correct OneBot mention encoding [merge] +``` + +```text +chore(release): v1.7.0 +``` + +提交被推到 `dev` 后,工作流会启用 PR 的 auto-merge(默认 `merge` 策略),分支保护满足后由机器人自动合并。 + +### 2. 推送 `v*` Tag + +任何符合 `v*` 的 tag(如 `v1.7.0`、`v1.7.0-rc.1`)被推送时也会触发本工作流。tag 通常打在 `dev` HEAD 上,工作流随后开 PR 把 `dev` 合入 `main`。 + +> 注意:`release.yml` 同样监听 `v*` tag,并基于 tag 指向的 commit 构建发布产物。两者并行执行,互不冲突。 + +### 3. 手动触发 + +打开仓库 Actions 页面 → `Promote Dev to Main` → `Run workflow`。可选输入: + +- `auto_merge`:是否启用 auto-merge(默认 `true`)。 +- `merge_method`:`merge` / `squash` / `rebase`(默认 `merge`)。 + +工作流会按照当前 `dev` 与 `main` 的差异开 / 更新 PR。 + +## 启用 `main` 分支保护(必做) + +仓库管理员需要在 GitHub 上为 `main` 配置一条规则集(Repository Rules)或经典分支保护,至少满足: + +1. **Settings → Rules → Rulesets**(推荐)或 **Settings → Branches → Branch protection rules**。 +2. 选择 `main` 作为目标分支。 +3. 勾选: + - **Restrict deletions** — 禁止删除 `main`。 + - **Require a pull request before merging** — 所有合并必须走 PR。 + - **Block force pushes** — 禁止强推。 + - (可选)**Require status checks to pass before merging** — 关联 `Dev Build` / `typecheck` 等检查。 +4. 在 **Bypass list** 中加入 `SnowLuma Bot` GitHub App。这样 `gh pr merge --auto` 才能在没有人工 review 的情况下完成合并。 +5. 不要把任何用户加入 push 白名单,确保「禁止向 `main` 直接提交」的约束生效。 + +## 必需的 Secrets + +`promote-dev-to-main.yml` 复用 `SnowLumaNative/build-native.yml` 同一个 GitHub App。请确认 SnowLuma 仓库(或 organization)级别已经配置: + +- `SNOWLUMA_BOT_APP_ID` — 数字类型的 App ID。 +- `SNOWLUMA_BOT_PRIVATE_KEY` — 该 App 的 PEM 私钥。 + +App 必须在本仓库已安装,且授予 **Contents: Read+Write**、**Pull requests: Read+Write**。 + +## 本地工作流速查 + +```bash +# 切到 dev 开发 +git checkout dev +git pull + +# 写代码 / 跑测试 +pnpm install +pnpm typecheck +pnpm -s --filter @snowluma/core test + +# 普通提交(不会触发 promote) +git commit -m "fix: something" +git push + +# 准备发布 / 合入 main +git commit -m "chore(release): v1.7.0" +# 或者 +git commit -m "fix: hotfix [merge]" + +git push # → 触发 promote-dev-to-main 工作流 +# 或者 +git tag v1.7.0 && git push origin v1.7.0 # → 同时触发 release.yml + promote-dev-to-main +```