[投稿] 水果忍者 — by 今晚打老虎🐯 #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto-rebase submission PRs | |
| # 每当有 PR 合并到 main 时触发 | |
| on: | |
| pull_request: | |
| types: [closed] | |
| branches: [main] | |
| concurrency: | |
| group: rebase-submissions | |
| cancel-in-progress: false | |
| jobs: | |
| rebase-open-prs: | |
| # 只在 PR 被合并(不是关闭)时运行 | |
| if: github.event.pull_request.merged == true | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| fetch-depth: 0 | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Rebase all open submission PRs | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { execSync } = require('child_process'); | |
| execSync('git config user.name "github-actions[bot]"'); | |
| execSync('git config user.email "github-actions[bot]@users.noreply.github.com"'); | |
| // 获取所有 open 的 PR | |
| const { data: openPRs } = await github.rest.pulls.list({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| state: 'open', | |
| base: 'main', | |
| sort: 'created', | |
| direction: 'asc' | |
| }); | |
| if (openPRs.length === 0) { | |
| core.info('No open PRs to rebase.'); | |
| return; | |
| } | |
| core.info(`Found ${openPRs.length} open PR(s) to rebase.`); | |
| let succeeded = 0; | |
| let failed = 0; | |
| for (const pr of openPRs) { | |
| const branch = pr.head.ref; | |
| core.info(`\nRebasing PR #${pr.number}: ${pr.title} (branch: ${branch})`); | |
| try { | |
| // 获取远程分支 | |
| execSync(`git fetch origin ${branch}:${branch}`, { stdio: 'pipe' }); | |
| // 切到该分支并 rebase 到最新 main | |
| execSync(`git checkout ${branch}`, { stdio: 'pipe' }); | |
| execSync('git rebase origin/main', { stdio: 'pipe' }); | |
| // force push 更新 PR | |
| execSync(`git push origin ${branch} --force-with-lease`, { stdio: 'pipe' }); | |
| core.info(`✅ PR #${pr.number} rebased successfully.`); | |
| succeeded++; | |
| // 切回 main 准备下一个 | |
| execSync('git checkout main', { stdio: 'pipe' }); | |
| } catch (e) { | |
| core.warning(`❌ PR #${pr.number} rebase failed, aborting.`); | |
| failed++; | |
| try { | |
| execSync('git rebase --abort', { stdio: 'pipe' }); | |
| } catch (_) {} | |
| execSync('git checkout main', { stdio: 'pipe' }); | |
| // 在 PR 上留言提示冲突 | |
| await github.rest.pulls.createReview({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: pr.number, | |
| event: 'COMMENT', | |
| body: '⚠️ 自动 rebase 失败,此 PR 存在无法自动解决的冲突,需要手动处理。' | |
| }); | |
| } | |
| } | |
| core.info(`\nDone. Succeeded: ${succeeded}, Failed: ${failed}`); |