Skip to content

Add auto merge

Add auto merge #24

Workflow file for this run

name: Auto Approve and Merge
permissions:
checks: write
contents: write
pull-requests: write
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches: [main, auto-approve-merge] # Only trigger for PRs targeting this branch
workflow_dispatch: # manual trigger for testing
inputs:
pr_number:
description: 'PR number to test auto-merge on'
required: false
type: string
jobs:
debug-context:
runs-on: ubuntu-latest
# Always run this job to see what's happening
steps:
- name: Debug PR Context
run: |
echo "=== WORKFLOW TRIGGER DEBUG ==="
echo "Event Name: ${{ github.event_name }}"
echo "Workflow Run ID: ${{ github.run_id }}"
echo "Workflow Run Number: ${{ github.run_number }}"
echo "Repository: ${{ github.repository }}"
echo "Ref: ${{ github.ref }}"
echo "Actor: ${{ github.actor }}"
echo ""
echo "=== PR DETAILS ==="
echo "PR Title: '${{ github.event.pull_request.title }}'"
echo "PR Number: ${{ github.event.pull_request.number }}"
echo "PR State: ${{ github.event.pull_request.state }}"
echo "Is Draft: ${{ github.event.pull_request.draft }}"
echo "Base Branch: ${{ github.event.pull_request.base.ref }}"
echo "Head Branch: ${{ github.event.pull_request.head.ref }}"
echo "PR Author: ${{ github.event.pull_request.user.login }}"
echo "PR Author Type: ${{ github.event.pull_request.user.type }}"
echo ""
echo "=== CONDITION CHECKS ==="
echo "Title contains 'Auto-merge workflow': ${{ contains(github.event.pull_request.title, 'Auto-merge workflow') }}"
echo "Author is github-actions: ${{ github.event.pull_request.user.login == 'app/github-actions' }}"
echo "Both conditions met: ${{ github.event.pull_request.user.login == 'app/github-actions' && contains(github.event.pull_request.title, 'Auto-merge workflow') }}"
echo ""
echo "=== TOKEN DEBUG ==="
echo "AUTO_MERGE_TOKEN exists: ${{ secrets.AUTO_MERGE_TOKEN != '' }}"
echo "GITHUB_TOKEN exists: ${{ secrets.GITHUB_TOKEN != '' }}"
auto-approve-merge:
runs-on: ubuntu-latest
# Only auto-merge PRs created by github-actions[bot] with the test title
if: |
github.event.pull_request.user.login == 'app/github-actions' &&
contains(github.event.pull_request.title, 'Auto-merge workflow')
steps:
- name: Check conditions
run: |
echo "🔍 Checking if should auto-merge..."
echo "PR Title: '${{ github.event.pull_request.title }}'"
echo "PR Author: '${{ github.event.pull_request.user.login }}'"
echo "Title condition: ${{ contains(github.event.pull_request.title, 'Auto-merge workflow') }}"
echo "Author condition: ${{ github.event.pull_request.user.login == 'app/github-actions' }}"
echo "Both conditions: ${{ github.event.pull_request.user.login == 'app/github-actions' && contains(github.event.pull_request.title, 'Auto-merge workflow') }}"
if [[ "${{ github.event.pull_request.user.login }}" == "app/github-actions" && "${{ github.event.pull_request.title }}" == *"Auto-merge workflow"* ]]; then
echo "✅ Both conditions met - proceeding with auto-merge"
else
echo "❌ Conditions not met - would skip"
echo " - Author is github-actions: ${{ github.event.pull_request.user.login == 'app/github-actions' }}"
echo " - Title contains 'Auto-merge workflow': ${{ contains(github.event.pull_request.title, 'Auto-merge workflow') }}"
exit 0 # Don't fail, just skip
fi
- name: Check PR status
run: |
echo "=== PR STATUS CHECK ==="
gh pr view ${{ github.event.pull_request.number }} --json state,mergeable,mergeStateStatus,reviewDecision
echo ""
echo "=== PR REVIEWS ==="
gh pr view ${{ github.event.pull_request.number }} --json reviews
echo ""
echo "=== PR CHECKS ==="
gh pr checks ${{ github.event.pull_request.number }}
env:
GH_TOKEN: ${{ secrets.AUTO_MERGE_TOKEN }}
- name: Auto approve and merge
if: |
github.event.pull_request.user.login == 'app/github-actions' &&
contains(github.event.pull_request.title, 'Auto-merge workflow')
run: |
echo "=== STARTING AUTO-MERGE PROCESS ==="
echo "PR Number: ${{ github.event.pull_request.number }}"
echo "Token available: ${{ secrets.AUTO_MERGE_TOKEN != '' }}"
echo "Step 1: Approving PR..."
gh pr review ${{ github.event.pull_request.number }} --approve --body "Auto-approved by workflow"
echo "✅ Approval completed"
echo "Step 2: Enabling auto-merge..."
gh pr merge ${{ github.event.pull_request.number }} --auto --merge --delete-branch
echo "✅ Auto-merge enabled"
echo "Step 3: Final PR status..."
gh pr view ${{ github.event.pull_request.number }} --json state,mergeable,mergeStateStatus
env:
GH_TOKEN: ${{ secrets.AUTO_MERGE_TOKEN }}