name: Delete CodeRabbit PR Comments on: issue_comment: types: [created] pull_request_review: types: [submitted] jobs: delete-coderabbit-comments: if: > (github.event_name == 'issue_comment' && github.event.issue.pull_request && (github.event.comment.user.login == 'coderabbitai' || github.event.comment.user.login == 'coderabbitai[bot]')) || (github.event_name == 'pull_request_review' && (github.event.review.user.login == 'coderabbitai' || github.event.review.user.login == 'coderabbitai[bot]')) runs-on: ubuntu-latest steps: # Fork PRs trigger `pull_request_review` without exposing repo secrets, # so the App token can't be minted. Skip the job cleanly in that case # instead of letting it fail loudly. - name: Check secrets availability id: check-secrets env: APP_ID: ${{ secrets.CODERABBIT_CLEANUP_APP_ID }} run: | if [ -z "$APP_ID" ]; then echo "App secrets not available (likely a fork PR event); skipping." echo "available=false" >> "$GITHUB_OUTPUT" else echo "available=true" >> "$GITHUB_OUTPUT" fi - name: Generate GitHub App token if: steps.check-secrets.outputs.available == 'true' id: app-token uses: actions/create-github-app-token@v3 with: app-id: ${{ secrets.CODERABBIT_CLEANUP_APP_ID }} private-key: ${{ secrets.CODERABBIT_CLEANUP_APP_PRIVATE_KEY }} - name: Clean up CodeRabbit comments if: steps.check-secrets.outputs.available == 'true' env: GH_TOKEN: ${{ steps.app-token.outputs.token }} REPO: ${{ github.repository }} PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }} run: | # Clear the body of all CodeRabbit reviews on this PR # (We can't delete reviews, but clearing the body removes the spam # while keeping inline code comments visible) REVIEW_IDS=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}/reviews" \ --jq '[.[] | select(.user.login == "coderabbitai[bot]" and .body != "") | .id] | .[]') for REVIEW_ID in $REVIEW_IDS; do gh api -X PUT "repos/${REPO}/pulls/${PR_NUMBER}/reviews/${REVIEW_ID}" \ -f body="." done # Delete all CodeRabbit issue comments on this PR COMMENT_URLS=$(gh api "repos/${REPO}/issues/${PR_NUMBER}/comments?per_page=100" \ --jq '[.[] | select(.user.login == "coderabbitai[bot]") | .url] | .[]') for URL in $COMMENT_URLS; do gh api -X DELETE "$URL" done