Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support to create repository from a template #1395

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions github/AuthenticatedUser.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,47 @@ def create_fork(self, repo):
self._requester, headers, data, completed=True
)

def create_repo_from_template(
self,
name,
repo,
description=github.GithubObject.NotSet,
private=github.GithubObject.NotSet,
):
"""
:calls: `POST /repos/:template_owner/:template_repo/generate <https://developer.github.com/v3/repos/#create-repository-using-a-repository-template>`
:param name: string
:param repo :class:`github.Repository.Repository`
:param description: string
:param private: bool
:rtype: :class:`github.Repository.Repository`
"""
assert isinstance(name, str), name
assert isinstance(repo, github.Repository.Repository), repo
assert description is github.GithubObject.NotSet or isinstance(
description, str
), description
assert private is github.GithubObject.NotSet or isinstance(
private, bool
), private
post_parameters = {
"name": name,
"owner": self.login,
}
if description is not github.GithubObject.NotSet:
post_parameters["description"] = description
if private is not github.GithubObject.NotSet:
post_parameters["private"] = private
headers, data = self._requester.requestJsonAndCheck(
"POST",
"/repos/" + repo.owner.login + "/" + repo.name + "/generate",
input=post_parameters,
headers={"Accept": Consts.mediaTypeTemplatesPreview},
)
return github.Repository.Repository(
self._requester, headers, data, completed=True
)

def create_gist(self, public, files, description=github.GithubObject.NotSet):
"""
:calls: `POST /gists <http://developer.github.com/v3/gists>`_
Expand Down
3 changes: 3 additions & 0 deletions github/Consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
# https://developer.github.com/changes/2018-05-24-user-migration-api/
mediaTypeMigrationPreview = "application/vnd.github.wyandotte-preview+json"

# https://developer.github.com/changes/2019-07-16-repository-templates-api/
mediaTypeTemplatesPreview = "application/vnd.github.baptiste-preview+json"

# https://developer.github.com/v3/search/#highlighting-code-search-results-1
highLightSearchPreview = "application/vnd.github.v3.text-match+json"

Expand Down
41 changes: 41 additions & 0 deletions github/Organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,47 @@ def create_fork(self, repo):
self._requester, headers, data, completed=True
)

def create_repo_from_template(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should abstract this out to a mix-in, since it's identical...

self,
name,
repo,
description=github.GithubObject.NotSet,
private=github.GithubObject.NotSet,
):
"""self.name
:calls: `POST /repos/:template_owner/:template_repo/generate <https://developer.github.com/v3/repos/#create-repository-using-a-repository-template>`_
:param name: string
:param repo :class:`github.Repository.Repository`
:param description: string
:param private: bool
:rtype: :class:`github.Repository.Repository`
"""
assert isinstance(name, str), name
assert isinstance(repo, github.Repository.Repository), repo
assert description is github.GithubObject.NotSet or isinstance(
description, str
), description
assert private is github.GithubObject.NotSet or isinstance(
private, bool
), private
post_parameters = {
"name": name,
"owner": self.login,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should use self._identity here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neither Organization nor AuthenticatedUser have an _identity property

}
if description is not github.GithubObject.NotSet:
post_parameters["description"] = description
if private is not github.GithubObject.NotSet:
post_parameters["private"] = private
headers, data = self._requester.requestJsonAndCheck(
"POST",
"/repos/" + repo.owner.login + "/" + repo.name + "/generate",
input=post_parameters,
headers={"Accept": Consts.mediaTypeTemplatesPreview},
)
return github.Repository.Repository(
self._requester, headers, data, completed=True
)

def create_hook(
self,
name,
Expand Down
11 changes: 11 additions & 0 deletions github/Repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,14 @@ def id(self):
self._completeIfNotSet(self._id)
return self._id.value

@property
def is_template(self):
"""
:type: bool
"""
self._completeIfNotSet(self._is_template)
return self._is_template.value

@property
def issue_comment_url(self):
"""
Expand Down Expand Up @@ -3157,6 +3165,7 @@ def _initAttributes(self):
self._hooks_url = github.GithubObject.NotSet
self._html_url = github.GithubObject.NotSet
self._id = github.GithubObject.NotSet
self._is_template = github.GithubObject.NotSet
self._issue_comment_url = github.GithubObject.NotSet
self._issue_events_url = github.GithubObject.NotSet
self._issues_url = github.GithubObject.NotSet
Expand Down Expand Up @@ -3292,6 +3301,8 @@ def _useAttributes(self, attributes):
self._html_url = self._makeStringAttribute(attributes["html_url"])
if "id" in attributes: # pragma no branch
self._id = self._makeIntAttribute(attributes["id"])
if "is_template" in attributes: # pragma no branch
self._is_template = self._makeBoolAttribute(attributes["is_template"])
if "issue_comment_url" in attributes: # pragma no branch
self._issue_comment_url = self._makeStringAttribute(
attributes["issue_comment_url"]
Expand Down
2 changes: 1 addition & 1 deletion scripts/add_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@

with open(fileName, "wb") as f:
for line in newLines:
f.write(line + "\n")
f.write((line + "\n").encode())
isouza-daitan marked this conversation as resolved.
Show resolved Hide resolved
isouza-daitan marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions tests/AuthenticatedUser.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,22 @@ def testCreateFork(self):
repo = self.user.create_fork(self.g.get_user("nvie").get_repo("gitflow"))
self.assertEqual(repo.source.full_name, "nvie/gitflow")

def testCreateRepoFromTemplate(self):
template_repo = self.g.get_repo("actions/hello-world-docker-action")

repo = self.user.create_repo_from_template("hello-world-docker-action-new", template_repo)
self.assertEqual(repo.url, "https://api.github.com/repos/jacquev6/hello-world-docker-action-new")
self.assertEqual(repo.is_template, False)

def testCreateRepoFromTemplateWithAllArguments(self):
template_repo = self.g.get_repo("actions/hello-world-docker-action")

description = "My repo from template"
private = True
repo = self.user.create_repo_from_template("hello-world-docker-action-new", template_repo, description=description, private=private)
self.assertEqual(repo.description, description)
self.assertEqual(repo.private, private)
isouza-daitan marked this conversation as resolved.
Show resolved Hide resolved

def testGetNotification(self):
notification = self.user.get_notification("8406712")
self.assertEqual(notification.id, "8406712")
Expand Down
16 changes: 16 additions & 0 deletions tests/Organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ def testCreateFork(self):
repo.url, "https://api.github.com/repos/BeaverSoftware/PyGithub"
)

def testCreateRepoFromTemplate(self):
template_repo = self.g.get_repo("actions/hello-world-docker-action")

repo = self.org.create_repo_from_template("hello-world-docker-action-new", template_repo)
self.assertEqual(repo.url, "https://api.github.com/repos/BeaverSoftware/hello-world-docker-action-new")
self.assertEqual(repo.is_template, False)

def testCreateRepoFromTemplateWithAllArguments(self):
template_repo = self.g.get_repo("actions/hello-world-docker-action")

description = "My repo from template"
private = True
repo = self.org.create_repo_from_template("hello-world-docker-action-new", template_repo, description=description, private=private)
self.assertEqual(repo.description, description)
self.assertEqual(repo.private, private)
isouza-daitan marked this conversation as resolved.
Show resolved Hide resolved

def testInviteUserWithNeither(self):
with self.assertRaises(AssertionError) as raisedexp:
self.org.invite_user()
Expand Down
33 changes: 33 additions & 0 deletions tests/ReplayData/AuthenticatedUser.testCreateRepoFromTemplate.txt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions tests/ReplayData/Organization.testCreateRepoFromTemplate.txt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/Repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def testAttributes(self):
self.assertEqual(self.repo.homepage, "http://vincent-jacques.net/PyGithub")
self.assertEqual(self.repo.html_url, "https://github.com/jacquev6/PyGithub")
self.assertEqual(self.repo.id, 3544490)
self.assertEqual(self.repo.is_template, None)
isouza-daitan marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(self.repo.language, "Python")
self.assertEqual(self.repo.master_branch, None)
self.assertEqual(self.repo.name, "PyGithub")
Expand Down