From fa05ff512302088842ce7ce90041e22237901890 Mon Sep 17 00:00:00 2001 From: Lucain Date: Thu, 10 Nov 2022 17:18:23 +0100 Subject: [PATCH] Fix cannot create pr on foreign repo (#1183) --- src/huggingface_hub/_commit_api.py | 2 ++ src/huggingface_hub/hf_api.py | 1 + tests/test_hf_api.py | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/huggingface_hub/_commit_api.py b/src/huggingface_hub/_commit_api.py index f86c4f3f2f..6b466ce47e 100644 --- a/src/huggingface_hub/_commit_api.py +++ b/src/huggingface_hub/_commit_api.py @@ -400,6 +400,7 @@ def fetch_upload_modes( token: Optional[str], revision: str, endpoint: Optional[str] = None, + create_pr: bool = False, ) -> Dict[str, UploadMode]: """ Requests the Hub "preupload" endpoint to determine wether each input file @@ -450,6 +451,7 @@ def fetch_upload_modes( f"{endpoint}/api/{repo_type}s/{repo_id}/preupload/{revision}", json=payload, headers=headers, + params={"create_pr": "1"} if create_pr else None, ) hf_raise_for_status(resp) preupload_info = _validate_preupload_info(resp.json()) diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index f3541968c5..c6d2643a5e 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -1887,6 +1887,7 @@ def create_commit( token=token or self.token, revision=revision, endpoint=self.endpoint, + create_pr=create_pr, ) except RepositoryNotFoundError as e: e.append_to_message(_CREATE_COMMIT_NO_REPO_ERROR_MESSAGE) diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index 8069f2ae9a..023c9d3a77 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -808,6 +808,24 @@ def test_create_commit_create_pr_against_branch(self): # Cleanup self._api.delete_repo(repo_id=repo_id) + @retry_endpoint + def test_create_commit_create_pr_on_foreign_repo(self): + # Repo on which we don't have right + # We must be able to create a PR on it + self._api.create_commit( + operations=[ + CommitOperationAdd( + path_in_repo="regular.txt", path_or_fileobj=b"File content" + ), + CommitOperationAdd( + path_in_repo="lfs.pkl", path_or_fileobj=b"File content" + ), + ], + commit_message="PR on foreign repo", + repo_id="datasets_server_org/repo_for_huggingface_hub_ci_with_prs", + create_pr=True, + ) + @retry_endpoint def test_create_commit(self): for private in (False, True):