|
| 1 | +# Auto PR Description Generator |
| 2 | + |
| 3 | +A reusable GitHub Action that automatically generates pull request descriptions using AI (Google Gemini) based on the git diff of changes. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🤖 **AI-Powered**: Uses Google Gemini to analyze code changes and generate meaningful descriptions |
| 8 | +- 🎯 **Smart Formatting**: Generates structured descriptions with Description, Changes, and Verification sections |
| 9 | +- 🖼️ **Image Preservation**: Maintains existing images at the top of PR descriptions |
| 10 | +- 🎫 **JIRA Integration**: Automatically extracts JIRA ticket IDs and adds ticket links |
| 11 | +- 🚫 **Smart Filtering**: Automatically ignores large files like package-lock.json to prevent token size issues |
| 12 | +- ⚡ **Fast & Lightweight**: Minimal dependencies and quick execution |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Basic Usage |
| 17 | + |
| 18 | +```yaml |
| 19 | +- name: Generate PR Description |
| 20 | + uses: ./.github/actions/auto-pr-description |
| 21 | + with: |
| 22 | + gemini-api-key: ${{ secrets.GEMINI_API_KEY }} |
| 23 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + pr-number: ${{ github.event.pull_request.number }} |
| 25 | +``` |
| 26 | +
|
| 27 | +### Complete Workflow Example |
| 28 | +
|
| 29 | +```yaml |
| 30 | +name: Auto PR Description |
| 31 | +on: |
| 32 | + pull_request: |
| 33 | + types: [labeled] |
| 34 | + |
| 35 | +jobs: |
| 36 | + update-pr-description: |
| 37 | + name: Update PR Description |
| 38 | + runs-on: ubuntu-latest |
| 39 | + if: | |
| 40 | + github.event_name == 'pull_request' && |
| 41 | + github.base_ref == 'main' && |
| 42 | + (github.event.pull_request.draft == false || github.event.action == 'labeled') && |
| 43 | + (contains(github.event.pull_request.labels.*.name, 'auto-pr-description') || |
| 44 | + contains(github.event.pull_request.labels.*.name, 'test')) |
| 45 | + permissions: |
| 46 | + pull-requests: write |
| 47 | + contents: read |
| 48 | + steps: |
| 49 | + - uses: actions/checkout@v4 |
| 50 | + |
| 51 | + - name: Generate PR Description |
| 52 | + uses: ./.github/actions/auto-pr-description |
| 53 | + with: |
| 54 | + gemini-api-key: ${{ secrets.GEMINI_API_KEY }} |
| 55 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + pr-number: ${{ github.event.pull_request.number }} |
| 57 | + jira-ticket-url-prefix: 'https://yourcompany.atlassian.net/browse/' |
| 58 | +``` |
| 59 | +
|
| 60 | +## Inputs |
| 61 | +
|
| 62 | +| Input | Description | Required | Default | |
| 63 | +|-------|-------------|----------|---------| |
| 64 | +| `gemini-api-key` | The API key for the Gemini API | ✅ | - | |
| 65 | +| `github-token` | GitHub token for PR operations | ✅ | - | |
| 66 | +| `pr-number` | Pull request number | ✅ | - | |
| 67 | +| `jira-ticket-url-prefix` | JIRA ticket URL prefix | ❌ | `https://virdocs.atlassian.net/browse/` | |
| 68 | +| `ignore-files` | Comma-separated list of files to ignore in diff | ❌ | `package-lock.json,yarn.lock,pnpm-lock.yaml,composer.lock,Gemfile.lock,poetry.lock,Pipfile.lock` | |
| 69 | + |
| 70 | +## Outputs |
| 71 | + |
| 72 | +| Output | Description | |
| 73 | +|--------|-------------| |
| 74 | +| `description` | The generated PR description | |
| 75 | +| `updated` | Whether the PR description was updated | |
| 76 | + |
| 77 | +## Generated Description Format |
| 78 | + |
| 79 | +The action generates PR descriptions in this structured format: |
| 80 | + |
| 81 | +```markdown |
| 82 | +## Description |
| 83 | +A concise summary of what the changes accomplish. |
| 84 | +
|
| 85 | +## Changes |
| 86 | +- [ ] Specific change or feature added |
| 87 | +- [ ] Another modification made |
| 88 | +- [ ] Bug fix or improvement |
| 89 | +
|
| 90 | +## Verification |
| 91 | +- [ ] Test that should be performed |
| 92 | +- [ ] Verification step to confirm functionality |
| 93 | +- [ ] Additional checks recommended |
| 94 | +
|
| 95 | +## Ticket |
| 96 | +https://yourcompany.atlassian.net/browse/TICKET-123 |
| 97 | +``` |
| 98 | + |
| 99 | +## JIRA Integration |
| 100 | + |
| 101 | +The action automatically detects JIRA ticket IDs from: |
| 102 | +1. **PR Title**: Extracts patterns like `CORE-1234`, `PAR-567`, etc. |
| 103 | +2. **Branch Name**: Falls back to branch name if not found in title |
| 104 | + |
| 105 | +Example branch names that work: |
| 106 | +- `CORE-1234-feature-description` |
| 107 | +- `PAR-567-bug-fix` |
| 108 | +- `feature/CORE-1234-new-feature` |
| 109 | + |
| 110 | +## Prerequisites |
| 111 | + |
| 112 | +### Required Secrets |
| 113 | + |
| 114 | +1. **GEMINI_API_KEY**: Get your API key from [Google AI Studio](https://makersuite.google.com/app/apikey) |
| 115 | +2. **GITHUB_TOKEN**: Automatically provided by GitHub Actions |
| 116 | + |
| 117 | +### Required Permissions |
| 118 | + |
| 119 | +The workflow must have these permissions: |
| 120 | +```yaml |
| 121 | +permissions: |
| 122 | + pull-requests: write |
| 123 | + contents: read |
| 124 | +``` |
| 125 | + |
| 126 | +## Trigger Patterns |
| 127 | + |
| 128 | +### Label-Based Triggering |
| 129 | +Add these labels to trigger the action: |
| 130 | +- `auto-pr-description`: Specific label for PR description generation |
| 131 | +- `test`: Dual-purpose label that can trigger both testing and description generation |
| 132 | + |
| 133 | +### Draft Mode Handling |
| 134 | +- **Draft PRs**: Action doesn't run automatically to save CI resources |
| 135 | +- **Label Override**: Adding trigger labels to draft PRs will run the action |
| 136 | +- **Ready for Review**: Converting draft to ready automatically triggers the action |
| 137 | + |
| 138 | +## Error Handling |
| 139 | + |
| 140 | +The action handles various error scenarios: |
| 141 | +- Missing or invalid Gemini API key |
| 142 | +- API rate limits and timeouts |
| 143 | +- Large diffs that exceed API limits |
| 144 | +- Network connectivity issues |
| 145 | +- Invalid PR numbers |
| 146 | + |
| 147 | +## Customization |
| 148 | + |
| 149 | +### Custom JIRA URL |
| 150 | +```yaml |
| 151 | +- uses: ./.github/actions/auto-pr-description |
| 152 | + with: |
| 153 | + jira-ticket-url-prefix: 'https://mycompany.atlassian.net/browse/' |
| 154 | + # ... other inputs |
| 155 | +``` |
| 156 | + |
| 157 | +### Custom File Filtering |
| 158 | +By default, the action ignores common lock files that can be very large and don't provide meaningful context for PR descriptions. You can customize which files to ignore: |
| 159 | + |
| 160 | +```yaml |
| 161 | +- uses: ./.github/actions/auto-pr-description |
| 162 | + with: |
| 163 | + ignore-files: 'package-lock.json,yarn.lock,dist/bundle.js,build/' |
| 164 | + # ... other inputs |
| 165 | +``` |
| 166 | + |
| 167 | +**Default ignored files:** |
| 168 | +- `package-lock.json` (npm) |
| 169 | +- `yarn.lock` (Yarn) |
| 170 | +- `pnpm-lock.yaml` (pnpm) |
| 171 | +- `composer.lock` (PHP Composer) |
| 172 | +- `Gemfile.lock` (Ruby Bundler) |
| 173 | +- `poetry.lock` (Python Poetry) |
| 174 | +- `Pipfile.lock` (Python Pipenv) |
| 175 | + |
| 176 | +**Why filter files?** |
| 177 | +Large files like lock files can cause the AI API to hit token limits, resulting in failed PR description generation. By filtering these files, the action focuses on meaningful code changes while staying within API limits. |
| 178 | + |
| 179 | +### Using Outputs |
| 180 | +```yaml |
| 181 | +- name: Generate PR Description |
| 182 | + id: pr-desc |
| 183 | + uses: ./.github/actions/auto-pr-description |
| 184 | + with: |
| 185 | + # ... inputs |
| 186 | +
|
| 187 | +- name: Use generated description |
| 188 | + run: | |
| 189 | + echo "Generated description: ${{ steps.pr-desc.outputs.description }}" |
| 190 | + echo "Was updated: ${{ steps.pr-desc.outputs.updated }}" |
| 191 | +``` |
| 192 | + |
| 193 | +## Troubleshooting |
| 194 | + |
| 195 | +### Common Issues |
| 196 | + |
| 197 | +1. **Missing API Key**: Ensure `GEMINI_API_KEY` is set in repository secrets |
| 198 | +2. **Permission Denied**: Check that workflow has `pull-requests: write` permission |
| 199 | +3. **Large Diffs**: Very large changes might exceed API limits - use `ignore-files` to filter out large files or consider smaller PRs |
| 200 | +4. **Rate Limits**: Gemini API has rate limits - add delays between calls if needed |
| 201 | +5. **Invalid PR Number**: Ensure the PR number is valid and accessible |
| 202 | + |
| 203 | +### Debug Mode |
| 204 | + |
| 205 | +Enable debug logging by setting: |
| 206 | +```yaml |
| 207 | +env: |
| 208 | + ACTIONS_STEP_DEBUG: true |
| 209 | +``` |
| 210 | + |
| 211 | +## License |
| 212 | + |
| 213 | +MIT License - see LICENSE file for details. |
0 commit comments