Automatically fix failing tests and linting errors using OpenAI, then create pull requests with the fixes.
- AI-Powered Fixes: Uses OpenAI to intelligently fix code errors
- Automatic Pull Requests: Creates PRs with fixes when tests or linting fail
- Multiple Fix Attempts: Tries multiple approaches if initial fixes don't work
- Works with Many Languages: Supports Python, JavaScript, TypeScript, and more
- Customizable: Adaptable to different testing and linting setups
- Easy Integration: Works with your existing test workflows
- The action is triggered when a test workflow fails and the commit message contains "auto-pr"
- It extracts error information from test and lint failures
- The errors and code context are sent to OpenAI to generate fixes
- Fixes are applied and verified by running tests again
- If successful, a pull request is created with the changes
- An OpenAI API key (get one at platform.openai.com)
- A GitHub repository with tests and/or linting
Add this to your repository as .github/workflows/auto-pr.yml
:
name: Auto-PR Fixer
on:
workflow_run:
workflows: ["Tests"] # Name of your test workflow
types: [completed]
branches: ["**"]
jobs:
auto-fix:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' && contains(github.event.workflow_run.head_commit.message, 'auto-pr') }}
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.workflow_run.head_branch }}
- name: Auto-PR AI Code Fixer
uses: yourusername/github-auto-pr-action@v1
with:
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
- Go to your repository's Settings > Secrets and variables > Actions
- Create a new repository secret:
- Name:
OPENAI_API_KEY
- Value: Your OpenAI API key
- Name:
- name: Auto-PR AI Code Fixer
uses: yourusername/github-auto-pr-action@v1
with:
# Required
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
# Optional - customize test and lint commands
test-command: "pytest -v"
lint-command: "pylint **/*.py --exit-zero"
# Optional - fine-tune behavior
max-attempts: 3
openai-model: "gpt-3.5-turbo"
Parameter | Required | Default | Description |
---|---|---|---|
openai-api-key |
Yes | Your OpenAI API key | |
test-command |
No | pytest |
Command to run tests |
lint-command |
No | pylint **/*.py --exit-zero |
Command to run linting |
max-attempts |
No | 3 |
Maximum number of fix attempts per file |
openai-model |
No | gpt-3.5-turbo |
OpenAI model to use |
- name: Auto-PR AI Code Fixer
uses: yourusername/github-auto-pr-action@v1
with:
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
test-command: "pytest"
lint-command: "pylint **/*.py --exit-zero"
- name: Auto-PR AI Code Fixer
uses: yourusername/github-auto-pr-action@v1
with:
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
test-command: "npm test"
lint-command: "eslint **/*.js"
- name: Auto-PR AI Code Fixer
uses: yourusername/github-auto-pr-action@v1
with:
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
test-command: "python -m pytest tests/ -v"
lint-command: "flake8 ."
max-attempts: 5
openai-model: "gpt-4"
- Fixes are limited by OpenAI model capabilities
- Complex bugs or architectural issues may not be fixable
- Requires well-structured tests that provide clear error messages
- The action needs appropriate permissions to create branches and pull requests
The action doesn't trigger:
- Ensure your test workflow is properly named in the trigger
- Verify commit messages contain "auto-pr"
- Check workflow permissions
OpenAI API errors:
- Validate your API key
- Check OpenAI service status
- Ensure you have sufficient API credits
No fixes are generated:
- Make sure error messages are clear and detailed
- Check if your tests provide enough context for the AI
- Try increasing max-attempts
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch:
git checkout -b feature-name
- Commit your changes:
git commit -m 'Add some feature'
- Push to the branch:
git push origin feature-name
- Open a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with OpenAI API
- Uses create-pull-request action
- Inspired by the need to automate routine code fixes
Created with β€οΈ by Your Name