|
| 1 | +# Codecov Agent Trigger |
| 2 | + |
| 3 | +[](https://docs.codegen.com) |
| 4 | + |
| 5 | +This example demonstrates how to automatically trigger a Codegen agent to fix code coverage issues when a pull request's coverage falls below a specified threshold. The script integrates with GitHub Actions and Codecov to maintain high code quality standards. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The codecov agent trigger consists of two main components: |
| 10 | + |
| 11 | +1. **Coverage Report Processor** (`process_coverage_report.py`) |
| 12 | + - Parses Codecov XML reports |
| 13 | + - Evaluates coverage against thresholds |
| 14 | + - Triggers Codegen agent when needed |
| 15 | + |
| 16 | +2. **Agent Prompt Generator** (`generate_codecov_agent_prompt.py`) |
| 17 | + - Creates contextual prompts for the Codegen agent |
| 18 | + - Provides necessary information for test generation |
| 19 | + - Includes setup and test execution instructions |
| 20 | + |
| 21 | +## How It Works |
| 22 | + |
| 23 | +The workflow operates in several steps: |
| 24 | + |
| 25 | +1. **Coverage Analysis** |
| 26 | + ```python |
| 27 | + coverage_data = parse_coverage_xml(xml_file) |
| 28 | + if coverage_data["coverage_percentage"] < threshold: |
| 29 | + # Trigger agent to fix coverage |
| 30 | + ``` |
| 31 | + - Parses XML coverage reports |
| 32 | + - Extracts key metrics (line coverage, branch coverage) |
| 33 | + - Compares against defined threshold (77%) |
| 34 | + |
| 35 | +2. **Agent Triggering** |
| 36 | + ```python |
| 37 | + from codegen import Agent |
| 38 | + |
| 39 | + new_agent = Agent(token=token, org_id=ORG_ID) |
| 40 | + second_task = new_agent.run(generate_codecov_agent_prompt(pr_number, repo)) |
| 41 | + ``` |
| 42 | + - Creates new Codegen agent instance |
| 43 | + - Generates contextual prompt |
| 44 | + - Initiates automated fix process |
| 45 | + |
| 46 | +3. **GitHub Integration** |
| 47 | + ```yaml |
| 48 | + - name: Codecov Agent Trigger |
| 49 | + env: |
| 50 | + PR_NUMBER: ${{ github.event.number }} |
| 51 | + REPO: ${{ github.repository }} |
| 52 | + TOKEN: ${{ secrets.CODEGEN_AGENT_TOKEN }} |
| 53 | + run: | |
| 54 | + python process_coverage_report.py coverage.xml $PR_NUMBER $REPO $TOKEN |
| 55 | + ``` |
| 56 | + - Runs as part of CI/CD pipeline |
| 57 | + - Passes PR context to processor |
| 58 | + - Manages authentication securely |
| 59 | +
|
| 60 | +## Setup |
| 61 | +
|
| 62 | +1. Install dependencies: |
| 63 | + ```bash |
| 64 | + pip install codegen |
| 65 | + ``` |
| 66 | + |
| 67 | +2. Configure GitHub secrets: |
| 68 | + - `CODECOV_TOKEN`: Your Codecov API token |
| 69 | + - `CODEGEN_AGENT_TOKEN`: Your Codegen agent token |
| 70 | + - Get your codegen token [here](https://www.codegen.sh/token) |
| 71 | + |
| 72 | +3. Add to your GitHub Actions workflow: |
| 73 | + ```yaml |
| 74 | + - name: Upload coverage reports to Codecov |
| 75 | + uses: codecov/codecov-action@v4.5.0 |
| 76 | + with: |
| 77 | + token: ${{ secrets.CODECOV_TOKEN }} |
| 78 | + file: coverage.xml |
| 79 | + |
| 80 | + - name: Codecov Agent Trigger |
| 81 | + run: python process_coverage_report.py coverage.xml $PR_NUMBER $REPO $TOKEN |
| 82 | + ``` |
| 83 | +
|
| 84 | +## Usage |
| 85 | +
|
| 86 | +The script is typically run automatically as part of your CI/CD pipeline, but you can also run it manually: |
| 87 | +
|
| 88 | +```bash |
| 89 | +python process_coverage_report.py <coverage_xml_file> <pr_number> <repo> <token> |
| 90 | +``` |
| 91 | + |
| 92 | +Arguments: |
| 93 | +- `coverage_xml_file`: Path to the coverage XML report |
| 94 | +- `pr_number`: GitHub PR number |
| 95 | +- `repo`: GitHub repository name |
| 96 | +- `token`: Codegen agent token |
| 97 | + |
| 98 | +## Output |
| 99 | + |
| 100 | +When coverage is below threshold: |
| 101 | +``` |
| 102 | +WARNING: Coverage 75.50% is below threshold of 77% |
| 103 | +Agent will be notified. |
| 104 | +Agent has been notified. URL: https://codegen.com/tasks/123 |
| 105 | +``` |
| 106 | + |
| 107 | +When coverage meets threshold: |
| 108 | +``` |
| 109 | +Coverage is above threshold. |
| 110 | +``` |
| 111 | + |
| 112 | +## Coverage Metrics |
| 113 | + |
| 114 | +The script tracks several key metrics: |
| 115 | +- Line coverage |
| 116 | +- Branch coverage |
| 117 | +- Overall coverage percentage |
| 118 | +- Lines covered vs. total lines |
| 119 | +- Branches covered vs. total branches |
| 120 | + |
| 121 | +## Configuration |
| 122 | + |
| 123 | +The default coverage threshold is set to 77%, but you can modify this in `process_coverage_report.py`: |
| 124 | + |
| 125 | +```python |
| 126 | +threshold = 77 # Modify this value to change the coverage threshold |
| 127 | +``` |
| 128 | + |
| 129 | +## Learn More |
| 130 | + |
| 131 | +- [Codecov Documentation](https://docs.codecov.com) |
| 132 | +- [Codegen Documentation](https://docs.codegen.com) |
| 133 | +- [GitHub Actions Documentation](https://docs.github.com/en/actions) |
| 134 | + |
| 135 | +## Contributing |
| 136 | + |
| 137 | +Feel free to submit issues and enhancement requests! We welcome contributions to improve the codecov agent trigger functionality. |
0 commit comments