如何使用PyGithub在组织中创建新的存储库

时间:2018-03-19 20:42:19

标签: python api github github-api pygithub

如何在Github上使用PyGithub在组织中创建新的存储库?特别是我想知道如何使用create_repo方法?

我的问题与this question相同,但我希望创建的存储库出现在组织中。

创建没有组织级别的回购的解决方案是:

g = Github("username", "password")
user = g.get_user()
repo = user.create_repo(full_name)

2 个答案:

答案 0 :(得分:1)

这个链接给了我答案:link

我想我会更新我的问题,让其他人知道解决方案是什么。

非常简单:

from github import Github

# using username and password
g = Github("Username", "Password")
org = g.get_organization('orgName')

repo = org.create_repo("test name")

答案 1 :(得分:0)

以下代码将帮助您在组织中创建新的Repo:

使用用户名和密码建立与github的连接:

g = Github(userName, password)
org = g.get_organization('yourOrgName')

如果您使用的是Github Enterprise,请使用以下代码登录:

g = Github(base_url="https://your_host_name/api/v3", login_or_token="personal_access_token")
org = g.get_organization('yourOrgName')

创建新的存储库:

repo = org.create_repo(projectName, description = projectDescription )

创建回购协议的完整代码:

from github import Github
import pygit2
g = Github(userName, password)
org = g.get_organization('yourOrgName')
repo = org.create_repo(projectName, description = projectDescription )

克隆存储库:

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)

用于克隆,创建并推送到存储库的完整代码:

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)