From 1ef2d56c93e5cde73ad56eafa7a40087151c3a40 Mon Sep 17 00:00:00 2001 From: MANDEEP N H <146331633+mandeepnh5@users.noreply.github.com> Date: Wed, 1 Jan 2025 01:32:13 +0530 Subject: [PATCH] Added Code Rabbit Approval Check to pull-request.yml Workflow (#3008) * Update coderabbit approval * Update .github/workflows/pull-request.yml Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Fix some issues * Update code for testing in pr * Fix issues related to if-fi * Update pull-request.yml * Update .github/workflows/pull-request.yml Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * intentionally failing coderabbit * Fix some issues * Update .github/workflows/pull-request.yml Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update pull-request.yml * Update pull-request.yml * Update pull-request.yml * Fix issues and intentional coderabbit fail * Intentional code rabbit fail * Update coderabbit check in scripts folder * Update .github/workflows/scripts/validate-coderabbit.yml Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/workflows/scripts/validate-coderabbit.yml Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update needs for coderabbit * Update scripts folder issue * Fix issue in yml file * Update .github/workflows/scripts/validate-coderabbit.sh Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update coderabbit suggestions * Fix some issues * Fix some path issues * Fix some script issues * Update .github/workflows/pull-request.yml Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Fix some issues * Update .github/workflows/scripts/validate-coderabbit.sh Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/workflows/scripts/validate-coderabbit.sh Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Fix github pagination * Update .github/workflows/scripts/validate-coderabbit.sh Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/workflows/scripts/validate-coderabbit.sh Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/workflows/scripts/validate-coderabbit.sh Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Fix github pagination * Fix some issues * Removed debug statements --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .github/workflows/pull-request.yml | 17 +++++++++++ .../workflows/scripts/validate-coderabbit.sh | 29 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 .github/workflows/scripts/validate-coderabbit.sh diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 75560024ae..a209d514e0 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -468,3 +468,20 @@ jobs: echo "Error: Pull request target branch must be 'develop-postgres'. Please refer PR_GUIDELINES.md" echo "Error: Close this PR and try again." exit 1 + + Validate-Coderabbit: + name: Validate CodeRabbit Approval + runs-on: ubuntu-latest + if: github.actor != 'dependabot[bot]' + needs: [Code-Quality-Checks, Test-Application, Start-App-Without-Docker, Docker-Start-Check] + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + - name: Validate CodeRabbit.ai Approval + run: | + chmod +x $GITHUB_WORKSPACE/.github/workflows/scripts/validate-coderabbit.sh + $GITHUB_WORKSPACE/.github/workflows/scripts/validate-coderabbit.sh + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ github.event.pull_request.number }} + GITHUB_REPOSITORY: ${{ github.repository }} \ No newline at end of file diff --git a/.github/workflows/scripts/validate-coderabbit.sh b/.github/workflows/scripts/validate-coderabbit.sh new file mode 100644 index 0000000000..bba002c24b --- /dev/null +++ b/.github/workflows/scripts/validate-coderabbit.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +echo "Step 1: Fetching all PR reviews..." + +response=$(curl -s -f -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/reviews?per_page=1000") || { + echo "Error: Failed to fetch reviews from GitHub API" + exit 1 +} + +latest_reviews=$(echo "$response" | jq -c '[.[]] | group_by(.user.login) | map(max_by(.submitted_at))') || { + echo "Error: Failed to process reviews JSON" + exit 1 +} + +if [ "$latest_reviews" = "null" ] || [ -z "$latest_reviews" ]; then + echo "Error: Invalid reviews data" + exit 1 +fi + +echo "Step 2: Checking approval status of 'coderabbitai[bot]'..." +approval_state=$(echo "$latest_reviews" | jq -r '[.[] | select(.user.login == "coderabbitai[bot]" and .state == "APPROVED")] | length') + +if [[ "$approval_state" =~ ^[0-9]+$ ]] && [[ $approval_state -gt 0 ]]; then + echo "Success: PR approved by CodeRabbit.ai." +else + echo "Error: PR is not approved by CodeRabbit.ai." + exit 1 +fi \ No newline at end of file