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

Create from template #1

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -502,6 +502,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
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
Expand Up @@ -405,6 +405,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,
):
"""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,
}
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
Expand Up @@ -469,6 +469,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 @@ -3408,6 +3416,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 @@ -3549,6 +3558,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
26 changes: 26 additions & 0 deletions tests/AuthenticatedUser.py
Expand Up @@ -661,6 +661,32 @@ 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.assertTrue(repo.private)

def testGetNotification(self):
notification = self.user.get_notification("8406712")
self.assertEqual(notification.id, "8406712")
Expand Down
26 changes: 26 additions & 0 deletions tests/Organization.py
Expand Up @@ -358,6 +358,32 @@ def testCreateFork(self):
self.assertFalse(repo.has_wiki)
self.assertFalse(repo.has_pages)

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.assertTrue(repo.private)

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
Expand Up @@ -89,6 +89,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.assertIs(self.repo.is_template, None)
self.assertEqual(self.repo.language, "Python")
self.assertEqual(self.repo.master_branch, None)
self.assertEqual(self.repo.name, "PyGithub")
Expand Down