Skip to content

deepakkjfrog/create-pr

Repository files navigation

GitHub Pull Request Creation API

A Flask server that provides a REST API to create pull requests in GitHub repositories with multiple file changes.

Features

  • 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

Setup

1. Install Dependencies

pip install -r requirements.txt

2. Environment Variables

Set 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"

3. GitHub Token Setup

  1. Go to GitHub Settings → Developer settings → Personal access tokens
  2. Generate a new token with the following permissions:
    • repo (Full control of private repositories)
    • workflow (if you need to trigger workflows)

Running the Server

python app.py

The server will start on http://localhost:5000

API Endpoints

Health Check

  • GET /health
  • Returns server status

Create Pull Request

  • POST /create-pr
  • Creates a new pull request with file changes

Request Body

{
  "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"
  }
}

Response

{
  "success": true,
  "pr_url": "https://github.com/owner/repo/pull/123",
  "pr_number": 123,
  "branch_name": "auto-fix-20241201123456",
  "commit_sha": "abc123..."
}

Create Pull Request with Diff

  • POST /create-pr-with-diff
  • Creates a new pull request by applying diff patches to existing files

Request Body

{
  "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\""
  }
}

Response

{
  "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
}

Example Usage

Using curl

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"
    }
  }'

Using curl with Diff API

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"
    }
  }'

Using Python requests

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())

Using Python requests with Diff API

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())

Error Handling

The API returns appropriate HTTP status codes:

  • 200 - Success (health check)
  • 201 - Pull request created successfully
  • 400 - Bad request (missing fields, invalid data)
  • 404 - Endpoint not found
  • 500 - Internal server error

Error responses include a descriptive message:

{
  "error": "Missing required field: pr_title"
}

Branch Naming

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.

Security Notes

  • 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

Troubleshooting

Common Issues

  1. Authentication Error: Ensure your GitHub token is valid and has the required permissions
  2. Repository Not Found: Verify REPO_OWNER and REPO_NAME are correct
  3. Branch Not Found: Ensure the BASE_BRANCH exists in your repository
  4. Permission Denied: Check that your token has write access to the repository

Logs

The server provides detailed logging. Check the console output for debugging information.

About

create-pr

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages