|
| 1 | +name: 🤖 AI-Powered PR Review |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, labeled] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + issues: write |
| 12 | + |
| 13 | +env: |
| 14 | + PROVIDER_API_KEY: ${{ secrets.GOOGLE_API_KEY }} |
| 15 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 16 | + GH_TOKEN: ${{ github.token }} |
| 17 | + |
| 18 | +jobs: |
| 19 | + goose-ai-review: |
| 20 | + name: 🧠 Goose AI Code Review |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + # Skip if PR is from dependabot or other bots |
| 24 | + if: github.actor != 'dependabot[bot]' |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: 📥 Check out repository |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + - name: 📊 Gather PR information |
| 33 | + run: | |
| 34 | + echo "🔍 Analyzing Pull Request #$PR_NUMBER..." |
| 35 | + |
| 36 | + # Create comprehensive PR analysis |
| 37 | + { |
| 38 | + echo "# Pull Request Analysis" |
| 39 | + echo "**PR #$PR_NUMBER**: $(gh pr view "$PR_NUMBER" --json title -q '.title')" |
| 40 | + echo "**Author**: $(gh pr view "$PR_NUMBER" --json author -q '.author.login')" |
| 41 | + echo "**Base Branch**: $(gh pr view "$PR_NUMBER" --json baseRefName -q '.baseRefName')" |
| 42 | + echo "**Head Branch**: $(gh pr view "$PR_NUMBER" --json headRefName -q '.headRefName')" |
| 43 | + echo "" |
| 44 | + echo "## Files Changed" |
| 45 | + gh pr view "$PR_NUMBER" --json files \ |
| 46 | + -q '.files[] | "* **" + .path + "** (" + (.additions|tostring) + " additions, " + (.deletions|tostring) + " deletions)"' |
| 47 | + echo "" |
| 48 | + echo "## Changes Summary" |
| 49 | + echo '```diff' |
| 50 | + gh pr diff "$PR_NUMBER" |
| 51 | + echo '```' |
| 52 | + } > pr_analysis.txt |
| 53 | + |
| 54 | + # Display what we gathered |
| 55 | + echo "📋 Generated PR analysis:" |
| 56 | + head -20 pr_analysis.txt |
| 57 | +
|
| 58 | + - name: 🛠️ Install Goose CLI |
| 59 | + run: | |
| 60 | + echo "🚀 Installing Goose CLI..." |
| 61 | + mkdir -p /home/runner/.local/bin |
| 62 | + curl -fsSL https://github.com/block/goose/releases/download/stable/download_cli.sh \ |
| 63 | + | CONFIGURE=false INSTALL_PATH=/home/runner/.local/bin bash |
| 64 | + echo "/home/runner/.local/bin" >> "$GITHUB_PATH" |
| 65 | + |
| 66 | + # Verify installation |
| 67 | + /home/runner/.local/bin/goose --version |
| 68 | +
|
| 69 | + - name: ⚙️ Configure Goose |
| 70 | + run: | |
| 71 | + echo "🔧 Configuring Goose with Google Gemini..." |
| 72 | + mkdir -p ~/.config/goose |
| 73 | + cat > ~/.config/goose/config.yaml <<'EOF' |
| 74 | + GOOSE_PROVIDER: google |
| 75 | + GOOSE_MODEL: gemini-2.0-flash-exp |
| 76 | + keyring: false |
| 77 | + EOF |
| 78 | + |
| 79 | + echo "📋 Goose configuration:" |
| 80 | + cat ~/.config/goose/config.yaml |
| 81 | +
|
| 82 | + - name: 📝 Create AI review instructions |
| 83 | + run: | |
| 84 | + cat > ai_instructions.txt <<'EOF' |
| 85 | + You are an expert code reviewer analyzing a pull request for a machine learning application. |
| 86 | +
|
| 87 | + Please provide a comprehensive but concise review of the changes with the following structure: |
| 88 | +
|
| 89 | + ## 🔍 Code Review Summary |
| 90 | + |
| 91 | + Provide a brief overview of what this PR accomplishes. |
| 92 | +
|
| 93 | + ## 📁 File Analysis |
| 94 | + |
| 95 | + For each changed file, provide: |
| 96 | + `filename/path` |
| 97 | + - Brief description of changes |
| 98 | + - Any notable improvements or concerns |
| 99 | + - Suggestions if applicable |
| 100 | +
|
| 101 | + ## 🚀 Recommendations |
| 102 | + |
| 103 | + - **Positive aspects**: What's done well |
| 104 | + - **Suggestions**: Areas for improvement |
| 105 | + - **Security considerations**: Any security implications |
| 106 | + - **Performance impact**: Expected performance changes |
| 107 | +
|
| 108 | + ## 🎯 Overall Assessment |
| 109 | + |
| 110 | + Provide an overall assessment: Approve, Request Changes, or Comment with reasoning. |
| 111 | +
|
| 112 | + Focus on: |
| 113 | + - Code quality and best practices |
| 114 | + - Security implications |
| 115 | + - Performance considerations |
| 116 | + - Maintainability |
| 117 | + - Docker/containerization best practices |
| 118 | + - CI/CD pipeline improvements |
| 119 | +
|
| 120 | + Keep the review constructive, specific, and helpful. Avoid generic comments. |
| 121 | +
|
| 122 | + The changes to analyze are: |
| 123 | + EOF |
| 124 | + |
| 125 | + # Append the PR analysis |
| 126 | + cat pr_analysis.txt >> ai_instructions.txt |
| 127 | +
|
| 128 | + - name: 🤖 Run Goose AI Analysis |
| 129 | + env: |
| 130 | + GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }} |
| 131 | + run: | |
| 132 | + echo "🧠 Running AI-powered code review..." |
| 133 | + |
| 134 | + # Run Goose and capture output |
| 135 | + goose run --instructions ai_instructions.txt \ |
| 136 | + | sed -E 's/\x1B\[[0-9;]*[mK]//g' \ |
| 137 | + | grep -v "logging to /home/runner/.config/goose/sessions/" \ |
| 138 | + | grep -v "^starting session" \ |
| 139 | + | grep -v "^Closing session" \ |
| 140 | + | sed 's/[[:space:]]*$//' \ |
| 141 | + > ai_review.txt |
| 142 | + |
| 143 | + echo "✅ AI review generated successfully" |
| 144 | +
|
| 145 | + - name: 📤 Post AI review to PR |
| 146 | + run: | |
| 147 | + echo "📬 Posting AI review to PR #$PR_NUMBER..." |
| 148 | + |
| 149 | + # Add header to the review |
| 150 | + { |
| 151 | + echo "## 🤖 AI-Powered Code Review" |
| 152 | + echo "*Automated review generated by Goose + Google Gemini*" |
| 153 | + echo "" |
| 154 | + echo "---" |
| 155 | + echo "" |
| 156 | + cat ai_review.txt |
| 157 | + echo "" |
| 158 | + echo "---" |
| 159 | + echo "*This review was automatically generated. Please use human judgment for final decisions.*" |
| 160 | + } > final_review.txt |
| 161 | + |
| 162 | + # Post the review |
| 163 | + gh pr comment "$PR_NUMBER" --body-file final_review.txt |
| 164 | + |
| 165 | + echo "✅ AI review posted successfully!" |
| 166 | +
|
| 167 | + - name: 📊 Generate review summary |
| 168 | + run: | |
| 169 | + echo "## 🤖 AI Review Summary" >> $GITHUB_STEP_SUMMARY |
| 170 | + echo "| Property | Value |" >> $GITHUB_STEP_SUMMARY |
| 171 | + echo "|----------|-------|" >> $GITHUB_STEP_SUMMARY |
| 172 | + echo "| PR Number | #$PR_NUMBER |" >> $GITHUB_STEP_SUMMARY |
| 173 | + echo "| AI Model | Google Gemini 2.0 Flash |" >> $GITHUB_STEP_SUMMARY |
| 174 | + echo "| Review Tool | Goose CLI |" >> $GITHUB_STEP_SUMMARY |
| 175 | + echo "| Files Analyzed | $(gh pr view "$PR_NUMBER" --json files -q '.files | length') |" >> $GITHUB_STEP_SUMMARY |
| 176 | + echo "| Status | ✅ Review Posted |" >> $GITHUB_STEP_SUMMARY |
| 177 | +
|
| 178 | + - name: 💾 Upload review artifacts |
| 179 | + uses: actions/upload-artifact@v3 |
| 180 | + with: |
| 181 | + name: ai-review-artifacts |
| 182 | + path: | |
| 183 | + pr_analysis.txt |
| 184 | + ai_instructions.txt |
| 185 | + ai_review.txt |
| 186 | + final_review.txt |
0 commit comments