diff --git a/github/AuthenticatedUser.py b/github/AuthenticatedUser.py index 43018a5c..6660a8dc 100644 --- a/github/AuthenticatedUser.py +++ b/github/AuthenticatedUser.py @@ -495,18 +495,24 @@ class AuthenticatedUser(github.GithubObject.CompletableGithubObject): self._requester, headers, data, completed=True ) - def create_fork(self, repo): + @staticmethod + def create_fork( + repo, + name=github.GithubObject.NotSet, + default_branch_only=github.GithubObject.NotSet, + ): """ :calls: `POST /repos/{owner}/{repo}/forks `_ :param repo: :class:`github.Repository.Repository` + :param name: string + :param default_branch_only: bool :rtype: :class:`github.Repository.Repository` """ assert isinstance(repo, github.Repository.Repository), repo - headers, data = self._requester.requestJsonAndCheck( - "POST", f"/repos/{repo.owner.login}/{repo.name}/forks" - ) - return github.Repository.Repository( - self._requester, headers, data, completed=True + return repo.create_fork( + organization=github.GithubObject.NotSet, + name=name, + default_branch_only=default_branch_only, ) def create_repo_from_template( diff --git a/github/AuthenticatedUser.pyi b/github/AuthenticatedUser.pyi index fa730b2f..b753a465 100644 --- a/github/AuthenticatedUser.pyi +++ b/github/AuthenticatedUser.pyi @@ -55,7 +55,12 @@ class AuthenticatedUser(CompletableGithubObject): client_secret: Union[str, _NotSetType] = ..., onetime_password: Union[str, None] = ..., ) -> Authorization: ... - def create_fork(self, repo: Repository) -> Repository: ... + @staticmethod + def create_fork( + repo: Repository, + name: Union[str, _NotSetType] = ..., + default_branch_only: Union[str, _NotSetType] = ..., + ) -> Repository: ... def create_gist( self, public: bool, diff --git a/github/Organization.py b/github/Organization.py index a63cc8e3..f1f64ea3 100644 --- a/github/Organization.py +++ b/github/Organization.py @@ -384,23 +384,24 @@ class Organization(github.GithubObject.CompletableGithubObject): "PUT", f"{self.url}/public_members/{public_member._identity}" ) - def create_fork(self, repo): + def create_fork( + self, + repo, + name=github.GithubObject.NotSet, + default_branch_only=github.GithubObject.NotSet, + ): """ :calls: `POST /repos/{owner}/{repo}/forks `_ :param repo: :class:`github.Repository.Repository` + :param name: string + :param default_branch_only: bool :rtype: :class:`github.Repository.Repository` """ assert isinstance(repo, github.Repository.Repository), repo - url_parameters = { - "org": self.login, - } - headers, data = self._requester.requestJsonAndCheck( - "POST", - f"/repos/{repo.owner.login}/{repo.name}/forks", - parameters=url_parameters, - ) - return github.Repository.Repository( - self._requester, headers, data, completed=True + return repo.create_fork( + self, + name=name, + default_branch_only=default_branch_only, ) def create_repo_from_template( diff --git a/github/Organization.pyi b/github/Organization.pyi index 7dfbbb1e..396da5b3 100644 --- a/github/Organization.pyi +++ b/github/Organization.pyi @@ -35,7 +35,12 @@ class Organization(CompletableGithubObject): @property def company(self) -> Optional[str]: ... def convert_to_outside_collaborator(self, member: NamedUser) -> None: ... - def create_fork(self, repo: Repository) -> Repository: ... + def create_fork( + self, + repo: Repository, + name: Union[str, _NotSetType] = ..., + default_branch_only: Union[str, _NotSetType] = ..., + ) -> Repository: ... def create_hook( self, name: str, diff --git a/github/Repository.py b/github/Repository.py index 41ace3c7..26a6b3c6 100644 --- a/github/Repository.py +++ b/github/Repository.py @@ -2419,10 +2419,17 @@ class Repository(github.GithubObject.CompletableGithubObject): Repository, self._requester, f"{self.url}/forks", None ) - def create_fork(self, organization=github.GithubObject.NotSet): + def create_fork( + self, + organization=github.GithubObject.NotSet, + name=github.GithubObject.NotSet, + default_branch_only=github.GithubObject.NotSet, + ): """ :calls: `POST /repos/{owner}/{repo}/forks `_ :param organization: :class:`github.Organization.Organization` or string + :param name: string + :param default_branch_only: bool :rtype: :class:`github.Repository.Repository` """ post_parameters = {} @@ -2432,6 +2439,14 @@ class Repository(github.GithubObject.CompletableGithubObject): post_parameters["organization"] = organization else: assert organization is github.GithubObject.NotSet, organization + assert name is github.GithubObject.NotSet or isinstance(name, str), name + assert default_branch_only is github.GithubObject.NotSet or isinstance( + default_branch_only, bool + ), default_branch_only + if name is not github.GithubObject.NotSet: + post_parameters["name"] = name + if default_branch_only is not github.GithubObject.NotSet: + post_parameters["default_branch_only"] = default_branch_only headers, data = self._requester.requestJsonAndCheck( "POST", f"{self.url}/forks", diff --git a/github/Repository.pyi b/github/Repository.pyi index ac32735b..240c8f04 100644 --- a/github/Repository.pyi +++ b/github/Repository.pyi @@ -365,7 +365,10 @@ class Repository(CompletableGithubObject): def get_events(self) -> PaginatedList[Event]: ... def get_forks(self) -> PaginatedList[Repository]: ... def create_fork( - self, organization: Union[Organization, str, _NotSetType] = ... + self, + organization: Union[Organization, str, _NotSetType] = ..., + name: Union[str, _NotSetType] = ..., + default_branch_only: Union[str, _NotSetType] = ..., ) -> Repository: ... def get_git_blob(self, sha: str) -> GitBlob: ... def get_git_commit(self, sha: str) -> GitCommit: ... diff --git a/tests/Framework.py b/tests/Framework.py index 24ab745a..9341c634 100644 --- a/tests/Framework.py +++ b/tests/Framework.py @@ -196,6 +196,7 @@ class ReplayingConnection: if isinstance(input, str): trInput = input.replace("\n", "").replace("\r", "") if input.startswith("{"): + assert expectedInput.startswith("{"), expectedInput assert json.loads(trInput) == json.loads(expectedInput) else: assert trInput == expectedInput diff --git a/tests/ReplayData/AuthenticatedUser.testCreateFork.txt b/tests/ReplayData/AuthenticatedUser.testCreateFork.txt index fd247441..f96e67dc 100644 --- a/tests/ReplayData/AuthenticatedUser.testCreateFork.txt +++ b/tests/ReplayData/AuthenticatedUser.testCreateFork.txt @@ -25,8 +25,8 @@ POST api.github.com None /repos/nvie/gitflow/forks -{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} -None +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python', 'Content-Type': 'application/json'} +{} 202 [('status', '202 Accepted'), ('x-ratelimit-remaining', '4958'), ('content-length', '3486'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"92f5b171559bccda47d7ebb598ba4f73"'), ('date', 'Sat, 26 May 2012 20:23:35 GMT'), ('content-type', 'application/json; charset=utf-8')] {"parent":{"clone_url":"https://github.com/nvie/gitflow.git","has_downloads":true,"watchers":3973,"updated_at":"2012-05-26T20:23:35Z","master_branch":"develop","homepage":"http://nvie.com/posts/a-successful-git-branching-model/","url":"https://api.github.com/repos/nvie/gitflow","has_wiki":true,"has_issues":true,"fork":false,"forks":331,"size":4602,"private":false,"open_issues":92,"svn_url":"https://github.com/nvie/gitflow","owner":{"url":"https://api.github.com/users/nvie","avatar_url":"https://secure.gravatar.com/avatar/c5a7f21b46df698f3db31c37ed0cf55a?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"c5a7f21b46df698f3db31c37ed0cf55a","login":"nvie","id":83844},"name":"gitflow","language":"Shell","description":"Git extensions to provide high-level repository operations for Vincent Driessen's branching model.","ssh_url":"git@github.com:nvie/gitflow.git","git_url":"git://github.com/nvie/gitflow.git","pushed_at":"2012-02-14T13:11:04Z","created_at":"2010-01-20T23:14:12Z","id":481366,"mirror_url":null,"html_url":"https://github.com/nvie/gitflow","full_name":"nvie/gitflow"},"clone_url":"https://github.com/jacquev6/gitflow.git","has_downloads":true,"watchers":1,"updated_at":"2012-05-26T20:23:35Z","source":{"clone_url":"https://github.com/nvie/gitflow.git","has_downloads":true,"watchers":3973,"updated_at":"2012-05-26T20:23:35Z","master_branch":"develop","homepage":"http://nvie.com/posts/a-successful-git-branching-model/","url":"https://api.github.com/repos/nvie/gitflow","has_wiki":true,"has_issues":true,"fork":false,"forks":331,"size":4602,"private":false,"open_issues":92,"svn_url":"https://github.com/nvie/gitflow","owner":{"url":"https://api.github.com/users/nvie","avatar_url":"https://secure.gravatar.com/avatar/c5a7f21b46df698f3db31c37ed0cf55a?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"c5a7f21b46df698f3db31c37ed0cf55a","login":"nvie","id":83844},"name":"gitflow","language":"Shell","description":"Git extensions to provide high-level repository operations for Vincent Driessen's branching model.","ssh_url":"git@github.com:nvie/gitflow.git","git_url":"git://github.com/nvie/gitflow.git","pushed_at":"2012-02-14T13:11:04Z","created_at":"2010-01-20T23:14:12Z","id":481366,"mirror_url":null,"html_url":"https://github.com/nvie/gitflow","full_name":"nvie/gitflow"},"permissions":{"pull":true,"admin":true,"push":true},"master_branch":"develop","homepage":"http://nvie.com/posts/a-successful-git-branching-model/","url":"https://api.github.com/repos/jacquev6/gitflow","has_wiki":true,"has_issues":false,"fork":true,"forks":0,"size":4602,"private":false,"open_issues":0,"svn_url":"https://github.com/jacquev6/gitflow","owner":{"url":"https://api.github.com/users/jacquev6","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b68de5ae38616c296fa345d2b9df2225","login":"jacquev6","id":327146},"name":"gitflow","language":"Shell","description":"Git extensions to provide high-level repository operations for Vincent Driessen's branching model.","ssh_url":"git@github.com:jacquev6/gitflow.git","git_url":"git://github.com/jacquev6/gitflow.git","pushed_at":"2012-02-14T13:11:04Z","created_at":"2012-05-26T20:23:35Z","id":4457584,"mirror_url":null,"html_url":"https://github.com/jacquev6/gitflow","full_name":"jacquev6/gitflow"} diff --git a/tests/ReplayData/Organization.testCreateFork.txt b/tests/ReplayData/Organization.testCreateFork.txt index 22b02631..48e401b9 100644 --- a/tests/ReplayData/Organization.testCreateFork.txt +++ b/tests/ReplayData/Organization.testCreateFork.txt @@ -24,9 +24,9 @@ https POST api.github.com None -/repos/jacquev6/PyGithub/forks?org=BeaverSoftware -{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} -None +/repos/jacquev6/PyGithub/forks +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python', 'Content-Type': 'application/json'} +{"organization": "BeaverSoftware"} 202 [('status', '202 Accepted'), ('x-ratelimit-remaining', '4965'), ('content-length', '3681'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"e4abc3ca2f8a9ccbe868ead57057a0e8"'), ('date', 'Sun, 27 May 2012 05:23:18 GMT'), ('content-type', 'application/json; charset=utf-8')] {"parent":{"clone_url":"https://github.com/jacquev6/PyGithub.git","has_downloads":true,"watchers":15,"updated_at":"2012-05-27T05:23:18Z","homepage":"http://vincent-jacques.net/PyGithub","url":"https://api.github.com/repos/jacquev6/PyGithub","mirror_url":null,"has_wiki":false,"has_pages":false,"has_issues":true,"fork":false,"forks":3,"git_url":"git://github.com/jacquev6/PyGithub.git","size":348,"private":false,"open_issues":15,"svn_url":"https://github.com/jacquev6/PyGithub","owner":{"url":"https://api.github.com/users/jacquev6","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b68de5ae38616c296fa345d2b9df2225","login":"jacquev6","id":327146},"name":"PyGithub","language":"Python","description":"Python library implementing the full Github API v3","ssh_url":"git@github.com:jacquev6/PyGithub.git","pushed_at":"2012-05-26T20:54:13Z","created_at":"2012-02-25T12:53:47Z","id":3544490,"html_url":"https://github.com/jacquev6/PyGithub","full_name":"jacquev6/PyGithub"},"organization":{"url":"https://api.github.com/users/BeaverSoftware","avatar_url":"https://secure.gravatar.com/avatar/d563e337cac2fdc644e2aaaad1e23266?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png","gravatar_id":"d563e337cac2fdc644e2aaaad1e23266","login":"BeaverSoftware","id":1424031},"clone_url":"https://github.com/BeaverSoftware/PyGithub.git","has_downloads":true,"watchers":1,"updated_at":"2012-05-27T05:23:18Z","source":{"clone_url":"https://github.com/jacquev6/PyGithub.git","has_downloads":true,"watchers":15,"updated_at":"2012-05-27T05:23:18Z","homepage":"http://vincent-jacques.net/PyGithub","url":"https://api.github.com/repos/jacquev6/PyGithub","mirror_url":null,"has_wiki":false,"has_pages":false,"has_issues":true,"fork":false,"forks":3,"git_url":"git://github.com/jacquev6/PyGithub.git","size":348,"private":false,"open_issues":15,"svn_url":"https://github.com/jacquev6/PyGithub","owner":{"url":"https://api.github.com/users/jacquev6","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b68de5ae38616c296fa345d2b9df2225","login":"jacquev6","id":327146},"name":"PyGithub","language":"Python","description":"Python library implementing the full Github API v3","ssh_url":"git@github.com:jacquev6/PyGithub.git","pushed_at":"2012-05-26T20:54:13Z","created_at":"2012-02-25T12:53:47Z","id":3544490,"html_url":"https://github.com/jacquev6/PyGithub","full_name":"jacquev6/PyGithub"},"permissions":{"pull":true,"admin":true,"push":true},"homepage":"http://vincent-jacques.net/PyGithub","url":"https://api.github.com/repos/BeaverSoftware/PyGithub","mirror_url":null,"has_wiki":false,"has_pages":false,"has_issues":false,"fork":true,"forks":0,"git_url":"git://github.com/BeaverSoftware/PyGithub.git","size":348,"private":false,"open_issues":0,"svn_url":"https://github.com/BeaverSoftware/PyGithub","owner":{"url":"https://api.github.com/users/BeaverSoftware","avatar_url":"https://secure.gravatar.com/avatar/d563e337cac2fdc644e2aaaad1e23266?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png","gravatar_id":"d563e337cac2fdc644e2aaaad1e23266","login":"BeaverSoftware","id":1424031},"name":"PyGithub","language":"Python","description":"Python library implementing the full Github API v3","ssh_url":"git@github.com:BeaverSoftware/PyGithub.git","pushed_at":"2012-05-26T20:54:13Z","created_at":"2012-05-27T05:23:17Z","id":4460027,"html_url":"https://github.com/BeaverSoftware/PyGithub","full_name":"BeaverSoftware/PyGithub"}