|
| 1 | +name: Understand-First Commenter Service |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, closed] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + pr_number: |
| 9 | + description: "PR number to analyze" |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + |
| 13 | +jobs: |
| 14 | + commenter-service: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + contents: read |
| 18 | + pull-requests: write |
| 19 | + checks: write |
| 20 | + actions: read |
| 21 | + statuses: write |
| 22 | + issues: write |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout code |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - name: Set up Python |
| 31 | + uses: actions/setup-python@v4 |
| 32 | + with: |
| 33 | + python-version: "3.11" |
| 34 | + |
| 35 | + - name: Install dependencies |
| 36 | + run: | |
| 37 | + python -m pip install --upgrade pip |
| 38 | + pip install typer rich aiohttp asyncpg pydantic requests |
| 39 | +
|
| 40 | + - name: Run Understand-First Commenter |
| 41 | + env: |
| 42 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + PR_NUMBER: ${{ github.event.number || github.event.inputs.pr_number }} |
| 44 | + run: | |
| 45 | + python -c " |
| 46 | + import os |
| 47 | + import json |
| 48 | + import requests |
| 49 | + from datetime import datetime |
| 50 | +
|
| 51 | + # GitHub API setup |
| 52 | + token = os.environ['GITHUB_TOKEN'] |
| 53 | + pr_number = os.environ['PR_NUMBER'] |
| 54 | + repo = os.environ['GITHUB_REPOSITORY'] |
| 55 | +
|
| 56 | + headers = { |
| 57 | + 'Authorization': f'token {token}', |
| 58 | + 'Accept': 'application/vnd.github.v3+json' |
| 59 | + } |
| 60 | +
|
| 61 | + # Get PR details |
| 62 | + pr_url = f'https://api.github.com/repos/{repo}/pulls/{pr_number}' |
| 63 | + pr_response = requests.get(pr_url, headers=headers) |
| 64 | + pr_data = pr_response.json() |
| 65 | +
|
| 66 | + base_sha = pr_data['base']['sha'] |
| 67 | + head_sha = pr_data['head']['sha'] |
| 68 | +
|
| 69 | + print(f'Analyzing PR #{pr_number}: {pr_data[\"title\"]}') |
| 70 | + print(f'Base: {base_sha[:8]} -> Head: {head_sha[:8]}') |
| 71 | +
|
| 72 | + # Run analysis on both commits |
| 73 | + os.system(f'u analyze --output base-analysis.json --format json --commit {base_sha}') |
| 74 | + os.system(f'u analyze --output head-analysis.json --format json --commit {head_sha}') |
| 75 | +
|
| 76 | + # Generate delta analysis |
| 77 | + os.system('u diff base-analysis.json head-analysis.json --output pr-delta.json --format json --policy-check --verbose') |
| 78 | +
|
| 79 | + # Generate comment content |
| 80 | + os.system('u diff base-analysis.json head-analysis.json --output pr-comment.md --format markdown --policy-check') |
| 81 | +
|
| 82 | + # Generate mini-map |
| 83 | + os.system('u diff base-analysis.json head-analysis.json --output pr-minimap.svg --format svg') |
| 84 | +
|
| 85 | + # Read results |
| 86 | + with open('pr-delta.json', 'r') as f: |
| 87 | + delta_data = json.load(f) |
| 88 | +
|
| 89 | + with open('pr-comment.md', 'r') as f: |
| 90 | + comment_content = f.read() |
| 91 | +
|
| 92 | + # Read mini-map |
| 93 | + with open('pr-minimap.svg', 'r') as f: |
| 94 | + minimap_svg = f.read() |
| 95 | +
|
| 96 | + # Create enhanced comment |
| 97 | + violations = delta_data.get('violations', []) |
| 98 | + policy_passed = len(violations) == 0 |
| 99 | +
|
| 100 | + # Convert SVG to data URL |
| 101 | + import base64 |
| 102 | + minimap_base64 = base64.b64encode(minimap_svg.encode()).decode() |
| 103 | + minimap_data_url = f'data:image/svg+xml;base64,{minimap_base64}' |
| 104 | +
|
| 105 | + enhanced_comment = f''' |
| 106 | + ## 🔍 Understand-First PR Analysis |
| 107 | +
|
| 108 | + {comment_content} |
| 109 | +
|
| 110 | + ### 📊 Mini-Map Visualization |
| 111 | + <img src=\"{minimap_data_url}\" alt=\"Code Map Delta\" width=\"100%\" /> |
| 112 | +
|
| 113 | + ### ✅ Policy Compliance Checklist |
| 114 | + {('✅ **All policies passed!** No violations detected.' if policy_passed else |
| 115 | + '\\n'.join([f'❌ **{v[\"rule\"]}**: {v[\"description\"]}' for v in violations]))} |
| 116 | +
|
| 117 | + ### 📈 Key Metrics |
| 118 | + - **Functions Added**: {len(delta_data.get('functions_added', []))} |
| 119 | + - **Functions Removed**: {len(delta_data.get('functions_removed', []))} |
| 120 | + - **Functions Modified**: {len(delta_data.get('functions_modified', []))} |
| 121 | + - **Complexity Change**: {delta_data.get('complexity_change', 0):+d} |
| 122 | + - **Side Effects**: {len(delta_data.get('side_effects_added', []))} added, {len(delta_data.get('side_effects_removed', []))} removed |
| 123 | +
|
| 124 | + ### 🎯 Action Items |
| 125 | + {('✅ Ready to merge!' if policy_passed else |
| 126 | + '\\n'.join([f'- [ ] Fix: {v[\"description\"]} in `{v[\"function_name\"]}`' for v in violations]))} |
| 127 | +
|
| 128 | + --- |
| 129 | + *Generated by [Understand-First](https://github.com/your-org/understand-first) • [View full analysis]({pr_data[\"html_url\"]}/checks)* |
| 130 | + ''' |
| 131 | +
|
| 132 | + # Post or update comment |
| 133 | + comments_url = f'https://api.github.com/repos/{repo}/issues/{pr_number}/comments' |
| 134 | + comments_response = requests.get(comments_url, headers=headers) |
| 135 | + comments = comments_response.json() |
| 136 | +
|
| 137 | + existing_comment = next((c for c in comments if 'Understand-First PR Analysis' in c['body']), None) |
| 138 | +
|
| 139 | + if existing_comment: |
| 140 | + # Update existing comment |
| 141 | + update_url = f'https://api.github.com/repos/{repo}/issues/comments/{existing_comment[\"id\"]}' |
| 142 | + requests.patch(update_url, headers=headers, json={'body': enhanced_comment}) |
| 143 | + print('Updated existing comment') |
| 144 | + else: |
| 145 | + # Create new comment |
| 146 | + requests.post(comments_url, headers=headers, json={'body': enhanced_comment}) |
| 147 | + print('Created new comment') |
| 148 | +
|
| 149 | + # Create Check Run |
| 150 | + check_run_data = { |
| 151 | + 'name': 'Understand-First Analysis', |
| 152 | + 'head_sha': head_sha, |
| 153 | + 'status': 'completed', |
| 154 | + 'conclusion': 'success' if policy_passed else 'failure', |
| 155 | + 'output': { |
| 156 | + 'title': '✅ Understand-First Policy Check Passed' if policy_passed else '❌ Understand-First Policy Check Failed', |
| 157 | + 'summary': 'All code quality policies passed. The PR is safe to merge.' if policy_passed else f'{len(violations)} policy violations detected. Please review before merging.', |
| 158 | + 'text': '\\n\\n'.join([f'**{v[\"rule\"]}**: {v[\"description\"]}\\n- Function: {v[\"function_name\"]}\\n- Severity: {v[\"severity\"]}\\n- File: {v[\"file_path\"]}:{v[\"line_number\"]}' for v in violations]) if violations else 'No violations found.' |
| 159 | + }, |
| 160 | + 'actions': [ |
| 161 | + { |
| 162 | + 'label': 'View Full Report', |
| 163 | + 'description': 'Open detailed analysis report', |
| 164 | + 'identifier': 'view-report' |
| 165 | + }, |
| 166 | + { |
| 167 | + 'label': 'Download Artifacts', |
| 168 | + 'description': 'Download analysis artifacts', |
| 169 | + 'identifier': 'download-artifacts' |
| 170 | + } |
| 171 | + ] |
| 172 | + } |
| 173 | +
|
| 174 | + checks_url = f'https://api.github.com/repos/{repo}/check-runs' |
| 175 | + requests.post(checks_url, headers=headers, json=check_run_data) |
| 176 | + print('Created Check Run') |
| 177 | +
|
| 178 | + # Set commit status |
| 179 | + status_data = { |
| 180 | + 'state': 'success' if policy_passed else 'failure', |
| 181 | + 'description': 'Understand-First: All policies passed' if policy_passed else 'Understand-First: Policy violations detected', |
| 182 | + 'context': 'understand-first/policy-check' |
| 183 | + } |
| 184 | +
|
| 185 | + status_url = f'https://api.github.com/repos/{repo}/statuses/{head_sha}' |
| 186 | + requests.post(status_url, headers=headers, json=status_data) |
| 187 | + print('Set commit status') |
| 188 | +
|
| 189 | + print(f'Analysis complete. Policy check: {\"PASSED\" if policy_passed else \"FAILED\"}') |
| 190 | + " |
| 191 | +
|
| 192 | + - name: Upload analysis artifacts |
| 193 | + uses: actions/upload-artifact@v3 |
| 194 | + with: |
| 195 | + name: understand-first-pr-analysis-${{ github.event.number || github.event.inputs.pr_number }} |
| 196 | + path: | |
| 197 | + base-analysis.json |
| 198 | + head-analysis.json |
| 199 | + pr-delta.json |
| 200 | + pr-comment.md |
| 201 | + pr-minimap.svg |
0 commit comments