Spaces:
Running
Running
update PR logic
Browse files- backend_api.py +7 -2
- backend_deploy.py +25 -16
- frontend/src/app/page.tsx +1 -0
- frontend/src/types/index.ts +1 -0
backend_api.py
CHANGED
|
@@ -197,6 +197,7 @@ class CodeGenerationRequest(BaseModel):
|
|
| 197 |
history: List[List[str]] = []
|
| 198 |
agent_mode: bool = False
|
| 199 |
existing_repo_id: Optional[str] = None # For auto-deploy to update existing space
|
|
|
|
| 200 |
|
| 201 |
|
| 202 |
class DeploymentRequest(BaseModel):
|
|
@@ -848,9 +849,13 @@ async def generate_code(
|
|
| 848 |
})
|
| 849 |
yield f"data: {completion_data}\n\n"
|
| 850 |
|
| 851 |
-
# Auto-deploy after code generation (if authenticated)
|
| 852 |
auth = get_auth_from_header(authorization)
|
| 853 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 854 |
try:
|
| 855 |
# Send deploying status
|
| 856 |
deploying_data = json.dumps({
|
|
|
|
| 197 |
history: List[List[str]] = []
|
| 198 |
agent_mode: bool = False
|
| 199 |
existing_repo_id: Optional[str] = None # For auto-deploy to update existing space
|
| 200 |
+
skip_auto_deploy: bool = False # Skip auto-deploy (for PR creation)
|
| 201 |
|
| 202 |
|
| 203 |
class DeploymentRequest(BaseModel):
|
|
|
|
| 849 |
})
|
| 850 |
yield f"data: {completion_data}\n\n"
|
| 851 |
|
| 852 |
+
# Auto-deploy after code generation (if authenticated and not skipped)
|
| 853 |
auth = get_auth_from_header(authorization)
|
| 854 |
+
|
| 855 |
+
if request.skip_auto_deploy:
|
| 856 |
+
print(f"[Auto-Deploy] Skipped - PR creation will be handled by frontend")
|
| 857 |
+
|
| 858 |
+
if auth.is_authenticated() and not (auth.token and auth.token.startswith("dev_token_")) and not request.skip_auto_deploy:
|
| 859 |
try:
|
| 860 |
# Send deploying status
|
| 861 |
deploying_data = json.dumps({
|
backend_deploy.py
CHANGED
|
@@ -1399,33 +1399,42 @@ Generated by [AnyCoder](https://huggingface.co/spaces/akhaliq/anycoder)"""
|
|
| 1399 |
file_path.parent.mkdir(parents=True, exist_ok=True)
|
| 1400 |
file_path.write_text(content, encoding='utf-8')
|
| 1401 |
|
| 1402 |
-
#
|
|
|
|
|
|
|
| 1403 |
try:
|
| 1404 |
print(f"[PR] Creating pull request on {repo_id}")
|
| 1405 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1406 |
commit_info = api.upload_folder(
|
| 1407 |
folder_path=str(temp_path),
|
| 1408 |
repo_id=repo_id,
|
| 1409 |
repo_type="space",
|
| 1410 |
-
commit_message=pr_title,
|
| 1411 |
-
commit_description=
|
| 1412 |
-
|
|
|
|
| 1413 |
)
|
| 1414 |
|
| 1415 |
-
|
| 1416 |
-
pr_url = commit_info.pr_url if hasattr(commit_info, 'pr_url') else None
|
| 1417 |
-
if not pr_url:
|
| 1418 |
-
# Fallback: construct PR URL from pr_num if available
|
| 1419 |
-
pr_num = commit_info.pr_num if hasattr(commit_info, 'pr_num') else None
|
| 1420 |
-
if pr_num:
|
| 1421 |
-
pr_url = f"https://huggingface.co/spaces/{repo_id}/discussions/{pr_num}"
|
| 1422 |
-
else:
|
| 1423 |
-
pr_url = f"https://huggingface.co/spaces/{repo_id}/discussions"
|
| 1424 |
-
|
| 1425 |
-
success_msg = f"β
Pull Request created! View at: {pr_url}"
|
| 1426 |
print(f"[PR] {success_msg}")
|
| 1427 |
|
| 1428 |
-
return True, success_msg,
|
| 1429 |
|
| 1430 |
except Exception as e:
|
| 1431 |
print(f"[PR] Error creating pull request: {e}")
|
|
|
|
| 1399 |
file_path.parent.mkdir(parents=True, exist_ok=True)
|
| 1400 |
file_path.write_text(content, encoding='utf-8')
|
| 1401 |
|
| 1402 |
+
# Create PR using the correct API flow:
|
| 1403 |
+
# 1. Create discussion with pull_request=True
|
| 1404 |
+
# 2. Add commits to the PR branch
|
| 1405 |
try:
|
| 1406 |
print(f"[PR] Creating pull request on {repo_id}")
|
| 1407 |
|
| 1408 |
+
# Step 1: Create the PR (draft mode)
|
| 1409 |
+
discussion = api.create_discussion(
|
| 1410 |
+
repo_id=repo_id,
|
| 1411 |
+
title=pr_title,
|
| 1412 |
+
description=pr_description,
|
| 1413 |
+
repo_type="space",
|
| 1414 |
+
pull_request=True, # This makes it a PR instead of discussion
|
| 1415 |
+
token=token
|
| 1416 |
+
)
|
| 1417 |
+
|
| 1418 |
+
print(f"[PR] Created PR #{discussion.num}: {discussion.url}")
|
| 1419 |
+
|
| 1420 |
+
# Step 2: Upload files to the PR branch
|
| 1421 |
+
# The PR branch is automatically created and has a specific name
|
| 1422 |
+
pr_branch = f"refs/pr/{discussion.num}"
|
| 1423 |
+
|
| 1424 |
commit_info = api.upload_folder(
|
| 1425 |
folder_path=str(temp_path),
|
| 1426 |
repo_id=repo_id,
|
| 1427 |
repo_type="space",
|
| 1428 |
+
commit_message=f"{pr_title} (files)",
|
| 1429 |
+
commit_description="Updated files for redesign",
|
| 1430 |
+
revision=pr_branch, # Commit to the PR branch
|
| 1431 |
+
token=token
|
| 1432 |
)
|
| 1433 |
|
| 1434 |
+
success_msg = f"β
Pull Request created! View at: {discussion.url}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1435 |
print(f"[PR] {success_msg}")
|
| 1436 |
|
| 1437 |
+
return True, success_msg, discussion.url
|
| 1438 |
|
| 1439 |
except Exception as e:
|
| 1440 |
print(f"[PR] Error creating pull request: {e}")
|
frontend/src/app/page.tsx
CHANGED
|
@@ -301,6 +301,7 @@ export default function Home() {
|
|
| 301 |
history: messages.map((m) => [m.role, m.content]),
|
| 302 |
agent_mode: false,
|
| 303 |
existing_repo_id: effectiveRepoId, // Pass duplicated/imported space ID for auto-deploy
|
|
|
|
| 304 |
};
|
| 305 |
|
| 306 |
const assistantMessage: Message = {
|
|
|
|
| 301 |
history: messages.map((m) => [m.role, m.content]),
|
| 302 |
agent_mode: false,
|
| 303 |
existing_repo_id: effectiveRepoId, // Pass duplicated/imported space ID for auto-deploy
|
| 304 |
+
skip_auto_deploy: !!pendingPR, // Skip auto-deploy if PR is pending
|
| 305 |
};
|
| 306 |
|
| 307 |
const assistantMessage: Message = {
|
frontend/src/types/index.ts
CHANGED
|
@@ -20,6 +20,7 @@ export interface CodeGenerationRequest {
|
|
| 20 |
history: string[][];
|
| 21 |
agent_mode: boolean;
|
| 22 |
existing_repo_id?: string; // For auto-deploy to update existing space
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
export interface CodeGenerationResponse {
|
|
|
|
| 20 |
history: string[][];
|
| 21 |
agent_mode: boolean;
|
| 22 |
existing_repo_id?: string; // For auto-deploy to update existing space
|
| 23 |
+
skip_auto_deploy?: boolean; // Skip auto-deploy (for PR creation)
|
| 24 |
}
|
| 25 |
|
| 26 |
export interface CodeGenerationResponse {
|