-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_diff_usage.py
More file actions
191 lines (163 loc) Β· 6.15 KB
/
example_diff_usage.py
File metadata and controls
191 lines (163 loc) Β· 6.15 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
"""
Example usage of the GitHub Pull Request Creation API with Diff
This demonstrates the new /create-pr-with-diff endpoint.
"""
import requests
import json
def test_create_pr_with_diff():
"""
Example of creating a pull request with diff content
"""
# Example unified diff format
readme_diff = """--- README.md
+++ README.md
@@ -1 +1,3 @@
# self-healed
+Deepak kumar
+Deepak kUmar
"""
# Example diff for a Python file
python_file_diff = """--- src/example.py
+++ src/example.py
@@ -1,3 +1,5 @@
def hello_world():
return "Hello World"
+
+def new_function():
+ return "This is a new function added via diff"
"""
# The exact format for the diff API
data = {
"pr_title": "Update files with diff patches",
"body": "This PR applies diff patches to update existing files with new content.",
"files_and_content": {
"README.md": readme_diff,
"src/example.py": python_file_diff
}
}
print("π Creating Pull Request with Diff...")
print(f"Title: {data['pr_title']}")
print(f"Body: {data['body']}")
print(f"Files to modify with diffs: {list(data['files_and_content'].keys())}")
print("-" * 50)
try:
response = requests.post(
"http://localhost:5000/create-pr-with-diff",
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 with Diff 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']}")
print(f"Files Processed: {result['files_processed']}")
else:
print(f"\nβ Failed to create pull request with diff: {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 test_create_pr_with_new_file_diff():
"""
Example of creating a pull request with diff for a new file
"""
# Diff for creating a new file (empty original content)
new_file_diff = """--- new_file.txt
+++ new_file.txt
@@ -0,0 +1,3 @@
+This is a new file created via diff
+It contains multiple lines
+And demonstrates diff functionality
"""
data = {
"pr_title": "Add new file via diff",
"body": "This PR creates a new file using diff format.",
"files_and_content": {
"new_file.txt": new_file_diff
}
}
print("\n" + "=" * 60)
print("π Creating Pull Request with New File Diff...")
print(f"Title: {data['pr_title']}")
print(f"Files to create: {list(data['files_and_content'].keys())}")
print("-" * 50)
try:
response = requests.post(
"http://localhost:5000/create-pr-with-diff",
json=data,
headers={"Content-Type": "application/json"}
)
print(f"Status Code: {response.status_code}")
if response.status_code == 201:
result = response.json()
print("\nβ
New 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']}")
print(f"Files Processed: {result['files_processed']}")
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 test_invalid_diff_request():
"""
Test with invalid diff request
"""
print("\n" + "=" * 60)
print("π§ͺ Testing Invalid Diff Request...")
# Invalid diff format
data = {
"pr_title": "Test Invalid Diff",
"body": "This should fail",
"files_and_content": {
"README.md": "This is not a valid diff format"
}
}
try:
response = requests.post(
"http://localhost:5000/create-pr-with-diff",
json=data,
headers={"Content-Type": "application/json"}
)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.json()}")
if response.status_code == 400 or response.status_code == 500:
print("β
Invalid diff properly rejected!")
else:
print("β Expected error for invalid diff")
except requests.exceptions.ConnectionError:
print("β Error: Could not connect to server.")
print("Make sure the server is running: python app.py")
if __name__ == "__main__":
print("GitHub Pull Request Creation API with Diff - Example Usage")
print("=" * 70)
# 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
test_create_pr_with_diff()
test_create_pr_with_new_file_diff()
test_invalid_diff_request()
print("\n" + "=" * 70)
print("π Diff API examples completed!")
print("Check your GitHub repository for the created pull requests.")
print("\nNote: Make sure the files referenced in the diffs exist in your repository,")
print("or the API will treat them as new files to be created.")