A Flask server that provides a REST API to create pull requests in GitHub repositories with multiple file changes.
- Create pull requests with custom titles and descriptions
- Update or create multiple files in a single pull request
- Automatic branch creation with timestamp-based naming
- Comprehensive error handling and validation
- Health check endpoint
pip install -r requirements.txtSet the following environment variables:
export GITHUB_TOKEN="your_github_personal_access_token"
export REPO_OWNER="your_github_username"
export REPO_NAME="your_repository_name"
export BASE_BRANCH="main" # Optional, defaults to "main"- Go to GitHub Settings → Developer settings → Personal access tokens
- Generate a new token with the following permissions:
repo(Full control of private repositories)workflow(if you need to trigger workflows)
python app.pyThe server will start on http://localhost:5000
- GET
/health - Returns server status
- POST
/create-pr - Creates a new pull request with file changes
{
"pr_title": "Your PR Title",
"body": "Your PR description",
"files_and_content": {
"/path/to/file1.md": "Content for file 1",
"/path/to/file2.py": "Content for file 2"
}
}{
"success": true,
"pr_url": "https://github.com/owner/repo/pull/123",
"pr_number": 123,
"branch_name": "auto-fix-20241201123456",
"commit_sha": "abc123..."
}- POST
/create-pr-with-diff - Creates a new pull request by applying diff patches to existing files
{
"pr_title": "Your PR Title",
"body": "Your PR description",
"files_and_content": {
"/path/to/file1.md": "--- file1.md\n+++ file1.md\n@@ -1 +1,3 @@\n # Title\n+New line 1\n+New line 2",
"/path/to/file2.py": "--- file2.py\n+++ file2.py\n@@ -1,3 +1,5 @@\n def hello():\n return \"Hello\"\n+\n+def world():\n+ return \"World\""
}
}{
"success": true,
"pr_url": "https://github.com/owner/repo/pull/123",
"pr_number": 123,
"branch_name": "auto-fix-20241201123456",
"commit_sha": "abc123...",
"files_processed": 2
}curl -X POST http://localhost:5000/create-pr \
-H "Content-Type: application/json" \
-d '{
"pr_title": "Update README with new features",
"body": "This PR adds documentation for the new API endpoints",
"files_and_content": {
"/README.md": "# My Project\n\nThis project now includes a new API for creating pull requests.",
"/docs/api.md": "# API Documentation\n\n## Create PR Endpoint\n\nPOST /create-pr"
}
}'curl -X POST http://localhost:5000/create-pr-with-diff \
-H "Content-Type: application/json" \
-d '{
"pr_title": "Update files with diff patches",
"body": "This PR applies diff patches to existing files",
"files_and_content": {
"/README.md": "--- README.md\n+++ README.md\n@@ -1 +1,3 @@\n # Project\n+New feature added\n+Updated documentation"
}
}'import requests
import json
url = "http://localhost:5000/create-pr"
data = {
"pr_title": "Add new feature",
"body": "This PR implements a new feature",
"files_and_content": {
"/src/feature.py": "def new_feature():\n return 'Hello World'",
"/tests/test_feature.py": "def test_new_feature():\n assert new_feature() == 'Hello World'"
}
}
response = requests.post(url, json=data)
print(response.json())import requests
import json
url = "http://localhost:5000/create-pr-with-diff"
data = {
"pr_title": "Update files with diff",
"body": "This PR applies diff patches to existing files",
"files_and_content": {
"/README.md": "--- README.md\n+++ README.md\n@@ -1 +1,3 @@\n # Project\n+New feature added\n+Updated documentation",
"/src/main.py": "--- src/main.py\n+++ src/main.py\n@@ -1,3 +1,5 @@\n def main():\n print('Hello')\n+\n+def new_function():\n+ return 'New feature'"
}
}
response = requests.post(url, json=data)
print(response.json())The API returns appropriate HTTP status codes:
200- Success (health check)201- Pull request created successfully400- Bad request (missing fields, invalid data)404- Endpoint not found500- Internal server error
Error responses include a descriptive message:
{
"error": "Missing required field: pr_title"
}New branches are automatically created with the format:
auto-fix-YYYYMMDDHHMMSS
If a branch with the same name already exists, a -v2 suffix is added.
- Never commit your GitHub token to version control
- Use environment variables for sensitive configuration
- Consider using GitHub Apps for production use cases
- The server runs on all interfaces (
0.0.0.0) by default - adjust for production
- Authentication Error: Ensure your GitHub token is valid and has the required permissions
- Repository Not Found: Verify
REPO_OWNERandREPO_NAMEare correct - Branch Not Found: Ensure the
BASE_BRANCHexists in your repository - Permission Denied: Check that your token has write access to the repository
The server provides detailed logging. Check the console output for debugging information.