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 question is identical to this question, but I would like the created repository to appear in an organization.
The solution to creating a repo without the organization level is:
g = Github("username", "password")
user = g.get_user()
repo = user.create_repo(full_name)
This link gave me the answer: link
I thought I would update my question to let others know what the solution was.
Pretty simple:
from github import Github
# using username and password
g = Github("Username", "Password")
org = g.get_organization('orgName')
repo = org.create_repo("test name")
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)