refactor: 事件路由 #65
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 Merge Dev to Main | |
| on: | |
| pull_request: | |
| types: [opened, synchronize] # No longer need 'closed' trigger | |
| branches: | |
| - main | |
| paths-ignore: | |
| - '**.md' | |
| - 'docs/**' | |
| jobs: | |
| # Job to enable Auto-Merge for the PR | |
| auto-merge: | |
| runs-on: ubuntu-latest | |
| # Only run for open PRs from dev to main | |
| if: github.head_ref == 'dev' && github.base_ref == 'main' | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| checks: read | |
| statuses: read | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| fetch-depth: 0 # Required for git history | |
| - name: Configure Git | |
| run: | | |
| git config --local user.email "[email protected]" | |
| git config --local user.name "GitHub Action" | |
| - name: Get PR number | |
| id: get-pr | |
| run: | | |
| echo "pr_number=${{ github.event.number }}" >> $GITHUB_OUTPUT | |
| - name: Check for conflicts | |
| id: check-conflicts | |
| run: | | |
| # Ensure local branches are up-to-date | |
| git fetch origin main | |
| git fetch origin dev | |
| # Use merge-base to find the common ancestor and check for conflicts | |
| if git merge-tree $(git merge-base origin/main origin/dev) origin/main origin/dev | grep -q '<<<<<<<'; then | |
| echo "has_conflicts=true" >> $GITHUB_OUTPUT | |
| echo "❌ Merge conflicts detected" | |
| else | |
| echo "has_conflicts=false" >> $GITHUB_OUTPUT | |
| echo "✅ No merge conflicts" | |
| fi | |
| - name: Add conflict comment | |
| if: steps.check-conflicts.outputs.has_conflicts == 'true' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| gh pr comment ${{ steps.get-pr.outputs.pr_number }} \ | |
| --body "❌ **Merge Conflict Detection** | |
| This PR has merge conflicts and cannot be automatically merged. Please resolve conflicts manually and push to the dev branch. | |
| Steps to resolve conflicts: | |
| 1. \`git checkout dev\` | |
| 2. \`git merge main\` | |
| 3. Resolve conflicts | |
| 4. \`git commit\` | |
| 5. \`git push origin dev\`" | |
| exit 1 # Exit to prevent further steps | |
| - name: Enable auto-merge | |
| if: steps.check-conflicts.outputs.has_conflicts == 'false' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| # Enable auto-merge using a merge commit | |
| gh pr merge ${{ steps.get-pr.outputs.pr_number }} \ | |
| --auto \ | |
| --merge \ | |
| --delete-branch=false | |
| # Add success comment | |
| gh pr comment ${{ steps.get-pr.outputs.pr_number }} \ | |
| --body "✅ **Auto-merge Enabled** | |
| This PR has no merge conflicts and auto-merge has been enabled. It will be automatically merged to the main branch once all checks pass. | |
| Merge method: Merge commit | |
| Branch retention: dev branch will be preserved" | |
| - name: Summary | |
| run: | | |
| echo "### 🤖 Auto-merge Summary" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "- 📝 Processing PR #${{ steps.get-pr.outputs.pr_number }} (dev → main)" >> $GITHUB_STEP_SUMMARY | |
| if [ "${{ steps.check-conflicts.outputs.has_conflicts }}" == "true" ]; then | |
| echo "- ❌ Merge conflicts detected, manual resolution required" >> $GITHUB_STEP_SUMMARY | |
| else | |
| echo "- ✅ No merge conflicts, auto-merge enabled" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**PR Link**: https://github.com/${{ github.repository }}/pull/${{ steps.get-pr.outputs.pr_number }}" >> $GITHUB_STEP_SUMMARY |