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.
The codecov agent trigger consists of two main components:
-
Coverage Report Processor (
process_coverage_report.py)- Parses Codecov XML reports
- Evaluates coverage against thresholds
- Triggers Codegen agent when needed
-
Agent Prompt Generator (
generate_codecov_agent_prompt.py)- Creates contextual prompts for the Codegen agent
- Provides necessary information for test generation
- Includes setup and test execution instructions
The workflow operates in several steps:
-
Coverage Analysis
coverage_data = parse_coverage_xml(xml_file) if coverage_data["coverage_percentage"] < threshold: # Trigger agent to fix coverage
- Parses XML coverage reports
- Extracts key metrics (line coverage, branch coverage)
- Compares against defined threshold (77%)
-
Agent Triggering
from codegen import Agent new_agent = Agent(token=token, org_id=ORG_ID) second_task = new_agent.run(generate_codecov_agent_prompt(pr_number, repo))
- Creates new Codegen agent instance
- Generates contextual prompt
- Initiates automated fix process
-
GitHub Integration
- name: Codecov Agent Trigger env: PR_NUMBER: ${{ github.event.number }} REPO: ${{ github.repository }} TOKEN: ${{ secrets.CODEGEN_AGENT_TOKEN }} run: | python process_coverage_report.py coverage.xml $PR_NUMBER $REPO $TOKEN
- Runs as part of CI/CD pipeline
- Passes PR context to processor
- Manages authentication securely
-
Install dependencies:
pip install codegen
-
Configure GitHub secrets:
CODECOV_TOKEN: Your Codecov API tokenCODEGEN_AGENT_TOKEN: Your Codegen agent token- Get your codegen token here
-
Add to your GitHub Actions workflow:
- name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4.5.0 with: token: ${{ secrets.CODECOV_TOKEN }} file: coverage.xml - name: Codecov Agent Trigger run: python process_coverage_report.py coverage.xml $PR_NUMBER $REPO $TOKEN
The script is typically run automatically as part of your CI/CD pipeline, but you can also run it manually:
python process_coverage_report.py <coverage_xml_file> <pr_number> <repo> <token>Arguments:
coverage_xml_file: Path to the coverage XML reportpr_number: GitHub PR numberrepo: GitHub repository nametoken: Codegen agent token
When coverage is below threshold:
WARNING: Coverage 75.50% is below threshold of 77%
Agent will be notified.
Agent has been notified. URL: https://codegen.com/tasks/123
When coverage meets threshold:
Coverage is above threshold.
The script tracks several key metrics:
- Line coverage
- Branch coverage
- Overall coverage percentage
- Lines covered vs. total lines
- Branches covered vs. total branches
The default coverage threshold is set to 77%, but you can modify this in process_coverage_report.py:
threshold = 77 # Modify this value to change the coverage thresholdFeel free to submit issues and enhancement requests! We welcome contributions to improve the codecov agent trigger functionality.