|
| 1 | +name: Require Clean Merges |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - devnet-ready |
| 7 | + - devnet |
| 8 | + - testnet |
| 9 | + |
| 10 | +jobs: |
| 11 | + assert-clean-merges: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout Repository |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 # Ensures we get all branches for merging |
| 18 | + |
| 19 | + - name: Determine Target Branch and Set Merge List |
| 20 | + id: set-merge-branches |
| 21 | + run: | |
| 22 | + TARGET_BRANCH="${{ github.event.pull_request.base.ref }}" |
| 23 | + PR_BRANCH="${{ github.event.pull_request.head.ref }}" |
| 24 | + echo "PR_BRANCH=$PR_BRANCH" >> $GITHUB_ENV |
| 25 | +
|
| 26 | + if [[ "$TARGET_BRANCH" == "devnet-ready" ]]; then |
| 27 | + echo "MERGE_BRANCHES=devnet testnet main" >> $GITHUB_ENV |
| 28 | + elif [[ "$TARGET_BRANCH" == "devnet" ]]; then |
| 29 | + echo "MERGE_BRANCHES=testnet main" >> $GITHUB_ENV |
| 30 | + elif [[ "$TARGET_BRANCH" == "testnet" ]]; then |
| 31 | + echo "MERGE_BRANCHES=main" >> $GITHUB_ENV |
| 32 | + elif [[ "$TARGET_BRANCH" == "main" ]]; then |
| 33 | + echo "MERGE_BRANCHES=" >> $GITHUB_ENV # No need to merge anything into main |
| 34 | + else |
| 35 | + echo "MERGE_BRANCHES=devnet-ready devnet testnet main" >> $GITHUB_ENV |
| 36 | + fi |
| 37 | +
|
| 38 | + - name: Check Merge Cleanliness |
| 39 | + run: | |
| 40 | + TARGET_BRANCH="${{ github.event.pull_request.base.ref }}" |
| 41 | + PR_BRANCH="${{ github.event.pull_request.head.ref }}" |
| 42 | + echo "Fetching all branches..." |
| 43 | + git fetch --all --prune |
| 44 | +
|
| 45 | + echo "Checking out PR branch: $PR_BRANCH" |
| 46 | + git checkout $PR_BRANCH |
| 47 | + git reset --hard origin/$PR_BRANCH |
| 48 | +
|
| 49 | + for branch in $MERGE_BRANCHES; do |
| 50 | + echo "Checking merge from $branch into $PR_BRANCH..." |
| 51 | + |
| 52 | + # Ensure PR branch is up to date |
| 53 | + git reset --hard origin/$PR_BRANCH |
| 54 | +
|
| 55 | + # Merge without committing to check for conflicts |
| 56 | + if git merge --no-commit --no-ff origin/$branch; then |
| 57 | + echo "✅ Merge from $branch into $PR_BRANCH is clean." |
| 58 | + else |
| 59 | + echo "❌ Merge conflict detected when merging $branch into $PR_BRANCH" |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | + |
| 63 | + # Abort merge if one was started, suppressing errors if no merge happened |
| 64 | + git merge --abort 2>/dev/null || true |
| 65 | + done |
0 commit comments