https://developer.github.com/v3/libraries/
https://github.com/mamh-mixed/python-github-api
安装这个库,用起来比较方便
$ pip install PyGithub
获取jenkinsci下面的所有仓库
# coding=utf-8
import os
import sys
from github import Github
def get_org_repos(g, organization_name):
organization_repos = g.get_organization(organization_name).get_repos()
for repo in organization_repos:
print(repo.full_name) # 有了仓库名称,就可以 去 git clone 下载这些仓库代码了。
def main(argv): # main 方法
g = Github("这里使用token比较好,使用账号密码会报错")
organization_name = "jenkinsci"
get_org_repos(g, organization_name)
if __name__ == "__main__":
main(sys.argv[1:])
g.get_organization(organization_name) 会返回一个 class Organization(github.GithubObject.CompletableGithubObject): 实例。文档接口https://docs.github.com/en/rest/reference/orgs
这个实例里面有个 get_repos 方法,用来获取仓库信息的。它返回 一个 github.PaginatedList.PaginatedList 实例,我们去for循环遍历就行了。
class PaginatedList(PaginatedListBase):
"""
This class abstracts the `pagination of the API <http://developer.github.com/v3/#pagination>`_.
You can simply enumerate through instances of this class::
for repo in user.get_repos():
print(repo.name)
If you want to know the total number of items in the list::
print(user.get_repos().totalCount)
print(len(user.get_repos()))
You can also index them or take slices::
second_repo = user.get_repos()[1]
first_repos = user.get_repos()[:10]
If you want to iterate in reversed order, just do::
for repo in user.get_repos().reversed:
print(repo.name)
And if you really need it, you can explicitly access a specific page::
some_repos = user.get_repos().get_page(0)
some_other_repos = user.get_repos().get_page(3)
"""
这个里面存放的是 class Repository(github.GithubObject.CompletableGithubObject) 实例,文档接口https://docs.github.com/en/rest#pagination
通过Repository实例我们可以获取仓库的 full_name, 等等信息。fork,是否是克隆的其他的仓库的。如果是克隆的还会有个parent信息。
parent也是一个Repository实例。