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

How to create a new repository in an organization with PyGithub

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

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

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

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

这个link给了我答案:link

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

很简单:

from github import Github

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

repo = org.create_repo("test name")

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

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

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

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

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 )

创建 Repo 的完整代码:

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

克隆一个 repo :

repoClone = pygit2.clone_repository(repo.git_url, 'path_where_to_clone')

将代码推送到 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)

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

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)