【问题标题】:How to create a new repository in an organization with PyGithub如何使用 PyGithub 在组织中创建新存储库
【发布时间】:2018-08-28 13:11:39
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python api github github-api pygithub


    【解决方案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")
    

    【讨论】:

      【解决方案2】:

      以下代码将帮助您在组织中创建新的 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)
      

      【讨论】:

      • 另外,得到:AttributeError: 'IndexFile' object has no attribute 'add_all' :(
      猜你喜欢
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      相关资源
      最近更新 更多