akhaliq HF Staff commited on
Commit
2f7e3a4
·
1 Parent(s): 9ffda57
Files changed (1) hide show
  1. backend_deploy.py +34 -24
backend_deploy.py CHANGED
@@ -1399,41 +1399,51 @@ 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
- # Create PR using the correct API flow:
1403
- # 1. Create PR with create_pull_request()
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) using the wrapper function
1409
- discussion = api.create_pull_request(
1410
- repo_id=repo_id,
1411
- title=pr_title,
1412
- description=pr_description,
1413
- repo_type="space",
1414
- token=token
1415
- )
1416
 
1417
- print(f"[PR] Created PR #{discussion.num}: {discussion.url}")
 
 
 
 
 
 
 
 
1418
 
1419
- # Step 2: Upload files to the PR branch
1420
- # The PR branch is automatically created and has a specific name
1421
- pr_branch = f"refs/pr/{discussion.num}"
1422
 
1423
- commit_info = api.upload_folder(
1424
- folder_path=str(temp_path),
1425
  repo_id=repo_id,
1426
  repo_type="space",
1427
- commit_message=f"{pr_title} (files)",
1428
- commit_description="Updated files for redesign",
1429
- revision=pr_branch, # Commit to the PR branch
 
1430
  token=token
1431
  )
1432
 
1433
- success_msg = f"✅ Pull Request created! View at: {discussion.url}"
1434
- print(f"[PR] {success_msg}")
 
 
 
 
 
 
 
 
 
1435
 
1436
- return True, success_msg, discussion.url
1437
 
1438
  except Exception as e:
1439
  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 with files using create_commit (recommended approach)
1403
+ # This creates the PR and uploads files in one API call
 
1404
  try:
1405
+ print(f"[PR] Creating pull request with files on {repo_id}")
1406
 
1407
+ # Prepare operations for all files
1408
+ from huggingface_hub import CommitOperationAdd
1409
+ operations = []
 
 
 
 
 
1410
 
1411
+ for file_path in temp_path.rglob('*'):
1412
+ if file_path.is_file():
1413
+ rel_path = file_path.relative_to(temp_path)
1414
+ operations.append(
1415
+ CommitOperationAdd(
1416
+ path_in_repo=str(rel_path),
1417
+ path_or_fileobj=str(file_path)
1418
+ )
1419
+ )
1420
 
1421
+ print(f"[PR] Prepared {len(operations)} file operations")
 
 
1422
 
1423
+ # Create commit with PR
1424
+ commit_info = api.create_commit(
1425
  repo_id=repo_id,
1426
  repo_type="space",
1427
+ operations=operations,
1428
+ commit_message=pr_title,
1429
+ commit_description=pr_description,
1430
+ create_pr=True, # This creates a PR with the changes
1431
  token=token
1432
  )
1433
 
1434
+ # Extract PR URL
1435
+ pr_url = commit_info.pr_url if hasattr(commit_info, 'pr_url') else None
1436
+ pr_num = commit_info.pr_num if hasattr(commit_info, 'pr_num') else None
1437
+
1438
+ if not pr_url and pr_num:
1439
+ pr_url = f"https://huggingface.co/spaces/{repo_id}/discussions/{pr_num}"
1440
+ elif not pr_url:
1441
+ pr_url = f"https://huggingface.co/spaces/{repo_id}/discussions"
1442
+
1443
+ print(f"[PR] Created PR: {pr_url}")
1444
+ success_msg = f"✅ Pull Request created! View at: {pr_url}"
1445
 
1446
+ return True, success_msg, pr_url
1447
 
1448
  except Exception as e:
1449
  print(f"[PR] Error creating pull request: {e}")