How to create a new repository with PyGithub

Question:

How can I create a new repository with PyGithub on Github?
In particular I like to know how to use the create_repo method? How do I generate a AuthenticatedUser?

Asked By: ustroetz

||

Answers:

To create a repository, you can use GitPython. Here is a tutorial on how to init a rep. It’s as simple as:

import git

repo_dir = os.path.join(rw_dir, 'my-new-repo')
file_name = os.path.join(repo_dir, 'new-file')

r = git.Repo.init(repo_dir)

You can also use Dulwich to create a repository:

from dulwich.repo import Repo
x = Repo.init("/path/to/new/repo")

Once you have that done, you can use PyGithub to access the repositories (or stick to the APIs provided above):

from github import Github

g = Github("user", "password")
for repo in g.get_user().get_repos():
    print repo.name
Answered By: runDOSrun

The solution to my question is the following

g = Github(token)
user = g.get_user()
repo = user.create_repo(full_name)
Answered By: ustroetz

I stumbled onto this question when trying to figure out how to create an AuthenticatedUser object. Turns out you get a NamedUser when you pass any argument to get_user, and if you give it no arguments, you get the AuthenticatedUser corresponding to the creds you used when creating the Github object.

As a minimal example, the following:

from github import Github
g = Github("my GitHub API token")

user = g.get_user('myname')
print user
authed = g.get_user()
print authed

yields

<github.NamedUser.NamedUser object at 0x7f95d5eeed10>
<github.AuthenticatedUser.AuthenticatedUser object at 0x7f95d5684410>

Once you have an AuthenticatedUser object, you can call CreateRepo as explained in the docs that you linked.

Answered By: edunham

I stumbled across this question trying to figure out how to coax PyGithub into creating a Repository within an Organization and thought it would be relevant here.

g = Github(token)
organization = g.get_organization("org-name")
organization.create_repo(
        name,
        allow_rebase_merge=True,
        auto_init=False,
        description=description,
        has_issues=True,
        has_projects=False,
        has_wiki=False,
        private=True,
       )

The full set of keyword arguments may be found here: https://developer.github.com/v3/repos/#input

Answered By: MXWest

Answer to the question:

login via token:

g = Github(token)

user = g.get_user()

repo = user.create_repo(repo_name)

print(repo)#To 

login via username and password:

g = Github("user", "password")

user = g.get_user()

repo = user.create_repo(repo_name)

print(repo) 

Github Enterprise with the custom hostname.

login to Enterprise GitHub which has organizations

g = Github(base_url="https://{hostname}/api/v3", login_or_token="token")

org = g.get_organization("org name")

repo = org.create_repo(repo_name)
Answered By: mohit sehrawat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.