How can I create a new repository in an organization with PyGithub on Github? In particular I like to know how to use the create_repo
method?
My questio
Below code will help you to create new Repo in an organization:
using username and password establish connection to github:
g = Github(userName, password)
org = g.get_organization('yourOrgName')
If you are using Github Enterprise then use below code to login:
g = Github(base_url="https://your_host_name/api/v3", login_or_token="personal_access_token")
org = g.get_organization('yourOrgName')
create the new repository:
repo = org.create_repo(projectName, description = projectDescription )
full code to create a Repo:
from github import Github
import pygit2
g = Github(userName, password)
org = g.get_organization('yourOrgName')
repo = org.create_repo(projectName, description = projectDescription )
Clone a repo :
repoClone = pygit2.clone_repository(repo.git_url, 'path_where_to_clone')
push code to repo:
repoClone.remotes.set_url("origin", repo.clone_url)
index = repoClone.index
index.add_all()
index.write()
tree = index.write_tree()
oid = repoClone.create_commit('refs/heads/master', author, commiter, "init commit",tree,[repoClone.head.peel().hex])
remote = repoClone.remotes["origin"]
credentials = pygit2.UserPass(userName, password)
#if the above credentials does not work,use the below one
#credentials = pygit2.UserPass("personal_access_token", 'x-oauth-basic')
remote.credentials = credentials
callbacks=pygit2.RemoteCallbacks(credentials=credentials)
remote.push(['refs/heads/master'],callbacks=callbacks)
Full code to clone,create and push to a repo:
from github import Github
import pygit2
g = Github(userName, password)
org = g.get_organization('yourOrgName')
repo = org.create_repo(projectName, description = projectDescription )
repo.create_file("/README.md", "init commit", Readme_file)
repoClone = pygit2.clone_repository(repo.git_url, 'path_where_to_clone')
repoClone.remotes.set_url("origin", repo.clone_url)
index = repoClone.index
index.add_all()
index.write()
tree = index.write_tree()
oid = repoClone.create_commit('refs/heads/master', author, commiter, "init commit",tree,[repoClone.head.peel().hex])
remote = repoClone.remotes["origin"]
credentials = pygit2.UserPass(userName, password)
#if the above credentials does not work,use the below one
#credentials = pygit2.UserPass("personal_access_token", 'x-oauth-basic')
remote.credentials = credentials
callbacks=pygit2.RemoteCallbacks(credentials=credentials)
remote.push(['refs/heads/master'],callbacks=callbacks)