update
Browse files1398 unmodified lines139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391398 unmodified linesfile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')# Create PR using the correct API flow:# 1. Create PR with create_pull_request()# 2. Add commits to the PR branchtry:print(f"[PR] Creating pull request on {repo_id}")# Step 1: Create the PR (draft mode) using the wrapper functiondiscussion = api.create_pull_request(repo_id=repo_id,title=pr_title,description=pr_description,repo_type="space",token=token)print(f"[PR] Created PR #{discussion.num}: {discussion.url}")# Step 2: Upload files to the PR branch# The PR branch is automatically created and has a specific namepr_branch = f"refs/pr/{discussion.num}"commit_info = api.upload_folder(folder_path=str(temp_path),repo_id=repo_id,repo_type="space",commit_message=f"{pr_title} (files)",commit_description="Updated files for redesign",revision=pr_branch, # Commit to the PR branchtoken=token)success_msg = f"✅ Pull Request created! View at: {discussion.url}"print(f"[PR] {success_msg}")return True, success_msg, discussion.urlexcept Exception as e:print(f"[PR] Error creating pull request: {e}")1398 unmodified lines1399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491398 unmodified linesfile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')# Create PR with files using create_commit (recommended approach)# This creates the PR and uploads files in one API calltry:print(f"[PR] Creating pull request with files on {repo_id}")# Prepare operations for all filesfrom huggingface_hub import CommitOperationAddoperations = []for file_path in temp_path.rglob('*'):if file_path.is_file():rel_path = file_path.relative_to(temp_path)operations.append(CommitOperationAdd(path_in_repo=str(rel_path),path_or_fileobj=str(file_path)))print(f"[PR] Prepared {len(operations)} file operations")# Create commit with PRcommit_info = api.create_commit(repo_id=repo_id,repo_type="space",operations=operations,commit_message=pr_title,commit_description=pr_description,create_pr=True, # This creates a PR with the changestoken=token)# Extract PR URLpr_url = commit_info.pr_url if hasattr(commit_info, 'pr_url') else Nonepr_num = commit_info.pr_num if hasattr(commit_info, 'pr_num') else Noneif not pr_url and pr_num:pr_url = f"https://huggingface.co/spaces/{repo_id}/discussions/{pr_num}"elif not pr_url:pr_url = f"https://huggingface.co/spaces/{repo_id}/discussions"print(f"[PR] Created PR: {pr_url}")success_msg = f"✅ Pull Request created! View at: {pr_url}"return True, success_msg, pr_urlexcept Exception as e:print(f"[PR] Error creating pull request: {e}")
backend_deploy.py+34-24