Skip to content

Commit b94a83c

Browse files
authored
Add new create_fork arguments (#2493)
Adds support for new `create_fork` arguments: - `name` - To set the name of the fork on creation - `default_branch_only` - To only include the default branch Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com>
1 parent e8075c4 commit b94a83c

File tree

9 files changed

+62
-26
lines changed

9 files changed

+62
-26
lines changed

github/AuthenticatedUser.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,18 +495,24 @@ def create_authorization(
495495
self._requester, headers, data, completed=True
496496
)
497497

498-
def create_fork(self, repo):
498+
@staticmethod
499+
def create_fork(
500+
repo,
501+
name=github.GithubObject.NotSet,
502+
default_branch_only=github.GithubObject.NotSet,
503+
):
499504
"""
500505
:calls: `POST /repos/{owner}/{repo}/forks <http://docs.github.com/en/rest/reference/repos#forks>`_
501506
:param repo: :class:`github.Repository.Repository`
507+
:param name: string
508+
:param default_branch_only: bool
502509
:rtype: :class:`github.Repository.Repository`
503510
"""
504511
assert isinstance(repo, github.Repository.Repository), repo
505-
headers, data = self._requester.requestJsonAndCheck(
506-
"POST", f"/repos/{repo.owner.login}/{repo.name}/forks"
507-
)
508-
return github.Repository.Repository(
509-
self._requester, headers, data, completed=True
512+
return repo.create_fork(
513+
organization=github.GithubObject.NotSet,
514+
name=name,
515+
default_branch_only=default_branch_only,
510516
)
511517

512518
def create_repo_from_template(

github/AuthenticatedUser.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ class AuthenticatedUser(CompletableGithubObject):
5555
client_secret: Union[str, _NotSetType] = ...,
5656
onetime_password: Union[str, None] = ...,
5757
) -> Authorization: ...
58-
def create_fork(self, repo: Repository) -> Repository: ...
58+
@staticmethod
59+
def create_fork(
60+
repo: Repository,
61+
name: Union[str, _NotSetType] = ...,
62+
default_branch_only: Union[str, _NotSetType] = ...,
63+
) -> Repository: ...
5964
def create_gist(
6065
self,
6166
public: bool,

github/Organization.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -384,23 +384,24 @@ def add_to_public_members(self, public_member):
384384
"PUT", f"{self.url}/public_members/{public_member._identity}"
385385
)
386386

387-
def create_fork(self, repo):
387+
def create_fork(
388+
self,
389+
repo,
390+
name=github.GithubObject.NotSet,
391+
default_branch_only=github.GithubObject.NotSet,
392+
):
388393
"""
389394
:calls: `POST /repos/{owner}/{repo}/forks <https://docs.github.com/en/rest/reference/repos#forks>`_
390395
:param repo: :class:`github.Repository.Repository`
396+
:param name: string
397+
:param default_branch_only: bool
391398
:rtype: :class:`github.Repository.Repository`
392399
"""
393400
assert isinstance(repo, github.Repository.Repository), repo
394-
url_parameters = {
395-
"org": self.login,
396-
}
397-
headers, data = self._requester.requestJsonAndCheck(
398-
"POST",
399-
f"/repos/{repo.owner.login}/{repo.name}/forks",
400-
parameters=url_parameters,
401-
)
402-
return github.Repository.Repository(
403-
self._requester, headers, data, completed=True
401+
return repo.create_fork(
402+
self,
403+
name=name,
404+
default_branch_only=default_branch_only,
404405
)
405406

406407
def create_repo_from_template(

github/Organization.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ class Organization(CompletableGithubObject):
3535
@property
3636
def company(self) -> Optional[str]: ...
3737
def convert_to_outside_collaborator(self, member: NamedUser) -> None: ...
38-
def create_fork(self, repo: Repository) -> Repository: ...
38+
def create_fork(
39+
self,
40+
repo: Repository,
41+
name: Union[str, _NotSetType] = ...,
42+
default_branch_only: Union[str, _NotSetType] = ...,
43+
) -> Repository: ...
3944
def create_hook(
4045
self,
4146
name: str,

github/Repository.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2419,10 +2419,17 @@ def get_forks(self):
24192419
Repository, self._requester, f"{self.url}/forks", None
24202420
)
24212421

2422-
def create_fork(self, organization=github.GithubObject.NotSet):
2422+
def create_fork(
2423+
self,
2424+
organization=github.GithubObject.NotSet,
2425+
name=github.GithubObject.NotSet,
2426+
default_branch_only=github.GithubObject.NotSet,
2427+
):
24232428
"""
24242429
:calls: `POST /repos/{owner}/{repo}/forks <https://docs.github.com/en/rest/reference/repos#forks>`_
24252430
:param organization: :class:`github.Organization.Organization` or string
2431+
:param name: string
2432+
:param default_branch_only: bool
24262433
:rtype: :class:`github.Repository.Repository`
24272434
"""
24282435
post_parameters = {}
@@ -2432,6 +2439,14 @@ def create_fork(self, organization=github.GithubObject.NotSet):
24322439
post_parameters["organization"] = organization
24332440
else:
24342441
assert organization is github.GithubObject.NotSet, organization
2442+
assert name is github.GithubObject.NotSet or isinstance(name, str), name
2443+
assert default_branch_only is github.GithubObject.NotSet or isinstance(
2444+
default_branch_only, bool
2445+
), default_branch_only
2446+
if name is not github.GithubObject.NotSet:
2447+
post_parameters["name"] = name
2448+
if default_branch_only is not github.GithubObject.NotSet:
2449+
post_parameters["default_branch_only"] = default_branch_only
24352450
headers, data = self._requester.requestJsonAndCheck(
24362451
"POST",
24372452
f"{self.url}/forks",

github/Repository.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,10 @@ class Repository(CompletableGithubObject):
365365
def get_events(self) -> PaginatedList[Event]: ...
366366
def get_forks(self) -> PaginatedList[Repository]: ...
367367
def create_fork(
368-
self, organization: Union[Organization, str, _NotSetType] = ...
368+
self,
369+
organization: Union[Organization, str, _NotSetType] = ...,
370+
name: Union[str, _NotSetType] = ...,
371+
default_branch_only: Union[str, _NotSetType] = ...,
369372
) -> Repository: ...
370373
def get_git_blob(self, sha: str) -> GitBlob: ...
371374
def get_git_commit(self, sha: str) -> GitCommit: ...

tests/Framework.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def __readNextRequest(self, verb, url, input, headers):
196196
if isinstance(input, str):
197197
trInput = input.replace("\n", "").replace("\r", "")
198198
if input.startswith("{"):
199+
assert expectedInput.startswith("{"), expectedInput
199200
assert json.loads(trInput) == json.loads(expectedInput)
200201
else:
201202
assert trInput == expectedInput

tests/ReplayData/AuthenticatedUser.testCreateFork.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ POST
2525
api.github.com
2626
None
2727
/repos/nvie/gitflow/forks
28-
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
29-
None
28+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python', 'Content-Type': 'application/json'}
29+
{}
3030
202
3131
[('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')]
3232
{"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"}

tests/ReplayData/Organization.testCreateFork.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ https
2424
POST
2525
api.github.com
2626
None
27-
/repos/jacquev6/PyGithub/forks?org=BeaverSoftware
28-
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
29-
None
27+
/repos/jacquev6/PyGithub/forks
28+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python', 'Content-Type': 'application/json'}
29+
{"organization": "BeaverSoftware"}
3030
202
3131
[('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')]
3232
{"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"}

0 commit comments

Comments
 (0)