1

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

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

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

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

2 回答 2

6

这个链接给了我答案:link

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

很简单:

from github import Github

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

repo = org.create_repo("test name")
于 2018-03-20T00:19:53.447 回答
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)
于 2019-07-26T11:06:26.337 回答