-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
128 lines (109 loc) Β· 5.68 KB
/
example_usage.py
File metadata and controls
128 lines (109 loc) Β· 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
"""
Example usage of the GitHub Pull Request Creation API
This demonstrates the exact format you specified in your request.
"""
import requests
import json
def create_pull_request_example():
"""
Example of creating a pull request with the exact format you specified:
{
"pr_title": "title",
"body": "Pr body",
"files_and_content": {"/src/readme.md": "readme content"}
}
"""
# The exact format you specified
data = {
"pr_title": "Update README with new documentation",
"body": "This PR adds comprehensive documentation for the new API endpoints and includes usage examples for developers.",
"files_and_content": {
"/src/readme.md": "# API Documentation\n\n## Overview\n\nThis API allows you to create pull requests programmatically.\n\n## Usage\n\n```bash\ncurl -X POST http://localhost:5000/create-pr \\\n -H \"Content-Type: application/json\" \\\n -d '{\n \"pr_title\": \"Your Title\",\n \"body\": \"Your description\",\n \"files_and_content\": {\n \"/path/to/file\": \"content\"\n }\n }'\n```\n\n## Features\n\n- Create PRs with multiple file changes\n- Automatic branch creation\n- Comprehensive error handling\n- RESTful API design"
}
}
print("π Creating Pull Request...")
print(f"Title: {data['pr_title']}")
print(f"Body: {data['body']}")
print(f"Files to modify: {list(data['files_and_content'].keys())}")
print("-" * 50)
try:
response = requests.post(
"http://localhost:5000/create-pr",
json=data,
headers={"Content-Type": "application/json"}
)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
if response.status_code == 201:
result = response.json()
print("\nβ
Pull Request Created Successfully!")
print(f"PR URL: {result['pr_url']}")
print(f"PR Number: {result['pr_number']}")
print(f"Branch Name: {result['branch_name']}")
print(f"Commit SHA: {result['commit_sha']}")
else:
print(f"\nβ Failed to create pull request: {response.json().get('error', 'Unknown error')}")
except requests.exceptions.ConnectionError:
print("β Error: Could not connect to server.")
print("Make sure the server is running: python app.py")
except Exception as e:
print(f"β Error: {str(e)}")
def create_multiple_files_example():
"""
Example of creating a pull request with multiple files
"""
data = {
"pr_title": "Add new feature with multiple files",
"body": "This PR implements a new feature across multiple files including the main implementation, tests, and documentation.",
"files_and_content": {
"/src/new_feature.py": "def new_feature():\n \"\"\"\n A new feature that does something amazing.\n \"\"\"\n return \"Feature implemented!\"\n\n\ndef helper_function():\n return \"Helper function for the new feature\"\n",
"/tests/test_new_feature.py": "import pytest\nfrom src.new_feature import new_feature, helper_function\n\n\ndef test_new_feature():\n assert new_feature() == \"Feature implemented!\"\n\n\ndef test_helper_function():\n assert helper_function() == \"Helper function for the new feature\"\n",
"/docs/new_feature.md": "# New Feature Documentation\n\n## Overview\n\nThis document describes the new feature that was implemented.\n\n## Usage\n\n```python\nfrom src.new_feature import new_feature\n\nresult = new_feature()\nprint(result) # Output: Feature implemented!\n```\n\n## Testing\n\nRun the tests with:\n```bash\npytest tests/test_new_feature.py\n```"
}
}
print("\n" + "=" * 60)
print("π Creating Pull Request with Multiple Files...")
print(f"Title: {data['pr_title']}")
print(f"Files to modify: {list(data['files_and_content'].keys())}")
print("-" * 50)
try:
response = requests.post(
"http://localhost:5000/create-pr",
json=data,
headers={"Content-Type": "application/json"}
)
print(f"Status Code: {response.status_code}")
if response.status_code == 201:
result = response.json()
print("\nβ
Multi-file Pull Request Created Successfully!")
print(f"PR URL: {result['pr_url']}")
print(f"PR Number: {result['pr_number']}")
print(f"Branch Name: {result['branch_name']}")
else:
print(f"\nβ Failed to create pull request: {response.json().get('error', 'Unknown error')}")
except requests.exceptions.ConnectionError:
print("β Error: Could not connect to server.")
print("Make sure the server is running: python app.py")
except Exception as e:
print(f"β Error: {str(e)}")
if __name__ == "__main__":
print("GitHub Pull Request Creation API - Example Usage")
print("=" * 60)
# Check if server is running
try:
health_response = requests.get("http://localhost:5000/health")
if health_response.status_code == 200:
print("β
Server is running!")
else:
print("β Server is not responding properly")
exit(1)
except requests.exceptions.ConnectionError:
print("β Server is not running. Please start it with: python app.py")
exit(1)
# Run examples
create_pull_request_example()
create_multiple_files_example()
print("\n" + "=" * 60)
print("π Examples completed!")
print("Check your GitHub repository for the created pull requests.")