|
| 1 | +name: Codebase Agent |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + pull_request_review_comment: |
| 7 | + types: [created] |
| 8 | + issues: |
| 9 | + types: [opened, labeled] |
| 10 | + pull_request: |
| 11 | + types: [opened, labeled, ready_for_review] |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + pull-requests: write |
| 16 | + issues: write |
| 17 | + id-token: write # Required for GCP Workload Identity |
| 18 | + |
| 19 | +jobs: |
| 20 | + codebase-agent: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + # Only run if: |
| 23 | + # 1. Comment contains @cba mention, OR |
| 24 | + # 2. Issue/PR has 'cba-review' label, OR |
| 25 | + # 3. Issue/PR has 'cba-help' label |
| 26 | + if: | |
| 27 | + (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@cba')) || |
| 28 | + (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@cba')) || |
| 29 | + contains(github.event.issue.labels.*.name, 'cba-review') || |
| 30 | + contains(github.event.pull_request.labels.*.name, 'cba-review') || |
| 31 | + contains(github.event.issue.labels.*.name, 'cba-help') || |
| 32 | + contains(github.event.pull_request.labels.*.name, 'cba-help') |
| 33 | +
|
| 34 | + steps: |
| 35 | + - name: Checkout repository |
| 36 | + uses: actions/checkout@v4 |
| 37 | + with: |
| 38 | + fetch-depth: 0 # Full history for better context |
| 39 | + |
| 40 | + - name: Authenticate to Google Cloud |
| 41 | + id: auth |
| 42 | + uses: google-github-actions/auth@v2 |
| 43 | + with: |
| 44 | + workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }} |
| 45 | + service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }} |
| 46 | + |
| 47 | + - name: Set up Cloud SDK |
| 48 | + uses: google-github-actions/setup-gcloud@v2 |
| 49 | + |
| 50 | + - name: Extract @cba command |
| 51 | + id: parse_command |
| 52 | + run: | |
| 53 | + # Parse the @cba mention and extract the command |
| 54 | + COMMENT_BODY="${{ github.event.comment.body }}" |
| 55 | +
|
| 56 | + # Extract text after @cba mention |
| 57 | + if [[ "$COMMENT_BODY" =~ @cba[[:space:]]+(.*) ]]; then |
| 58 | + COMMAND="${BASH_REMATCH[1]}" |
| 59 | + echo "command=$COMMAND" >> $GITHUB_OUTPUT |
| 60 | + echo "has_mention=true" >> $GITHUB_OUTPUT |
| 61 | + else |
| 62 | + # No @cba mention, triggered by label |
| 63 | + echo "has_mention=false" >> $GITHUB_OUTPUT |
| 64 | +
|
| 65 | + # Determine action based on label |
| 66 | + if [[ "${{ contains(github.event.issue.labels.*.name, 'cba-review') || contains(github.event.pull_request.labels.*.name, 'cba-review') }}" == "true" ]]; then |
| 67 | + echo "command=review this PR" >> $GITHUB_OUTPUT |
| 68 | + elif [[ "${{ contains(github.event.issue.labels.*.name, 'cba-help') || contains(github.event.pull_request.labels.*.name, 'cba-help') }}" == "true" ]]; then |
| 69 | + echo "command=help with this issue" >> $GITHUB_OUTPUT |
| 70 | + fi |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: Set up Python |
| 74 | + uses: actions/setup-python@v5 |
| 75 | + with: |
| 76 | + python-version: '3.11' |
| 77 | + |
| 78 | + - name: Install dependencies |
| 79 | + run: | |
| 80 | + pip install google-cloud-aiplatform anthropic requests |
| 81 | +
|
| 82 | + - name: Run Codebase Agent |
| 83 | + id: run_agent |
| 84 | + env: |
| 85 | + COMMAND: ${{ steps.parse_command.outputs.command }} |
| 86 | + HAS_MENTION: ${{ steps.parse_command.outputs.has_mention }} |
| 87 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 88 | + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} |
| 89 | + GCP_PROJECT_ID: ambient-prototypes |
| 90 | + GCP_REGION: us-central1 |
| 91 | + run: | |
| 92 | + python3 << 'PYTHON_SCRIPT' |
| 93 | + import os |
| 94 | + import json |
| 95 | + import requests |
| 96 | + from anthropic import Anthropic |
| 97 | +
|
| 98 | + # Get environment variables |
| 99 | + command = os.environ.get('COMMAND', 'help') |
| 100 | + github_token = os.environ['GITHUB_TOKEN'] |
| 101 | + anthropic_api_key = os.environ['ANTHROPIC_API_KEY'] |
| 102 | +
|
| 103 | + # GitHub context |
| 104 | + github_context = json.loads('''${{ toJson(github) }}''') |
| 105 | + event_name = github_context['event_name'] |
| 106 | +
|
| 107 | + print(f"🤖 Codebase Agent activated!") |
| 108 | + print(f"Command: {command}") |
| 109 | + print(f"Event: {event_name}") |
| 110 | +
|
| 111 | + # Prepare context for Claude |
| 112 | + repo_name = github_context['repository'] |
| 113 | +
|
| 114 | + if event_name in ['issue_comment', 'pull_request_review_comment']: |
| 115 | + issue_number = github_context['event']['issue']['number'] |
| 116 | + issue_url = github_context['event']['issue']['html_url'] |
| 117 | + context_type = "comment" |
| 118 | + elif 'pull_request' in github_context['event']: |
| 119 | + issue_number = github_context['event']['pull_request']['number'] |
| 120 | + issue_url = github_context['event']['pull_request']['html_url'] |
| 121 | + context_type = "pull_request" |
| 122 | + elif 'issue' in github_context['event']: |
| 123 | + issue_number = github_context['event']['issue']['number'] |
| 124 | + issue_url = github_context['event']['issue']['html_url'] |
| 125 | + context_type = "issue" |
| 126 | +
|
| 127 | + # Call Claude API (using Anthropic directly for now) |
| 128 | + # In production, this would call Vertex AI using the GCP service account |
| 129 | + client = Anthropic(api_key=anthropic_api_key) |
| 130 | +
|
| 131 | + prompt = f"""You are the Codebase Agent (CBA) for the {repo_name} repository. |
| 132 | +
|
| 133 | +You've been invoked with the command: {command} |
| 134 | + |
| 135 | +Context: |
| 136 | +- Type: {context_type} |
| 137 | +- URL: {issue_url} |
| 138 | +- Issue/PR: #{issue_number} |
| 139 | + |
| 140 | +Please provide a helpful, concise response that addresses the user's request. |
| 141 | +If this is a code review, focus on: |
| 142 | +1. Code quality and best practices |
| 143 | +2. Potential bugs or edge cases |
| 144 | +3. Documentation and testing suggestions |
| 145 | +4. Security considerations |
| 146 | + |
| 147 | +If this is a help request, provide: |
| 148 | +1. Clear explanation of the issue |
| 149 | +2. Actionable next steps |
| 150 | +3. Relevant documentation or examples""" |
| 151 | + |
| 152 | + message = client.messages.create( |
| 153 | + model="claude-3-5-sonnet-20241022", |
| 154 | + max_tokens=2000, |
| 155 | + messages=[{"role": "user", "content": prompt}] |
| 156 | + ) |
| 157 | + |
| 158 | + response_text = message.content[0].text |
| 159 | + |
| 160 | + # Save response to file for next step |
| 161 | + with open('cba_response.txt', 'w') as f: |
| 162 | + f.write(response_text) |
| 163 | + |
| 164 | + print("✅ Codebase Agent response generated") |
| 165 | + PYTHON_SCRIPT |
| 166 | + |
| 167 | + - name: Post response to GitHub |
| 168 | + env: |
| 169 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 170 | + run: | |
| 171 | + # Read the response from previous step |
| 172 | + RESPONSE=$(cat cba_response.txt) |
| 173 | +
|
| 174 | + # Determine where to post the comment |
| 175 | + if [[ "${{ github.event_name }}" == "issue_comment" ]] || [[ "${{ github.event_name }}" == "pull_request_review_comment" ]]; then |
| 176 | + # Reply to the comment |
| 177 | + ISSUE_NUMBER="${{ github.event.issue.number }}" |
| 178 | + elif [[ "${{ github.event_name }}" == "pull_request" ]]; then |
| 179 | + ISSUE_NUMBER="${{ github.event.pull_request.number }}" |
| 180 | + elif [[ "${{ github.event_name }}" == "issues" ]]; then |
| 181 | + ISSUE_NUMBER="${{ github.event.issue.number }}" |
| 182 | + fi |
| 183 | +
|
| 184 | + # Post comment using GitHub API |
| 185 | + curl -X POST \ |
| 186 | + -H "Authorization: token $GITHUB_TOKEN" \ |
| 187 | + -H "Accept: application/vnd.github.v3+json" \ |
| 188 | + "https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER/comments" \ |
| 189 | + -d @- << EOF |
| 190 | + { |
| 191 | + "body": "## 🤖 Codebase Agent Response\n\n$RESPONSE\n\n---\n*Triggered by: ${{ github.event_name }} | Powered by Vertex AI*" |
| 192 | + } |
| 193 | + EOF |
| 194 | +
|
| 195 | + echo "✅ Response posted to GitHub" |
| 196 | +
|
| 197 | + - name: Add reaction to triggering comment |
| 198 | + if: github.event_name == 'issue_comment' || github.event_name == 'pull_request_review_comment' |
| 199 | + env: |
| 200 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 201 | + run: | |
| 202 | + # Add 👀 reaction to show we're processing |
| 203 | + curl -X POST \ |
| 204 | + -H "Authorization: token $GITHUB_TOKEN" \ |
| 205 | + -H "Accept: application/vnd.github.v3+json" \ |
| 206 | + "https://api.github.com/repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions" \ |
| 207 | + -d '{"content":"rocket"}' |
0 commit comments