From e31085a262994c493543e713350ef41bc7ece750 Mon Sep 17 00:00:00 2001 From: Aaditya Agarwal Date: Sun, 5 Jan 2025 01:37:30 +0530 Subject: [PATCH] Added Merge Conflict Check (#3138) * added merge conflict check * changed checkout repo * added max tries feature * added needs condition * added pr target synchronize to trigger workflow --- .github/workflows/pull-request.yml | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index f387698bce..c42b0d19f6 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -15,6 +15,14 @@ on: pull_request: branches: - '**' + types: + - opened + - reopened + - synchronize + - ready_for_review + pull_request_target: + types: + - synchronize env: CODECOV_UNIQUE_NAME: CODECOV_UNIQUE_NAME-${{ github.run_id }}-${{ github.run_number }} @@ -449,3 +457,49 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.pull_request.number }} GITHUB_REPOSITORY: ${{ github.repository }} + + Merge-Conflict-Check: + name: Check for Merge Conflicts + runs-on: ubuntu-latest + if: github.actor != 'dependabot[bot]' + needs: [Code-Quality-Checks] + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Check Mergeable Status via API + run: | + PR_NUMBER=${{ github.event.pull_request.number }} + max_retries=3 + retry_delay=5 + + for ((i=1; i<=max_retries; i++)); do + echo "Attempt $i of $max_retries" + + response=$(curl -s -f -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER") + + if [ $? -ne 0 ]; then + echo "Failed to call GitHub API" + if [ $i -eq $max_retries ]; then + exit 1 + fi + sleep $retry_delay + continue + fi + + mergeable=$(echo "$response" | jq -r '.mergeable') + if [ "$mergeable" == "true" ]; then + echo "No conflicts detected." + exit 0 + elif [ "$mergeable" == "false" ]; then + echo "Merge conflicts detected." + exit 1 + else + echo "Mergeable status unknown." + if [ $i -eq $max_retries ]; then + exit 1 + fi + sleep $retry_delay + fi + done \ No newline at end of file