Python 导出gitlab项目
-
安装python-gitlab模块
pip install python-gitlab python3 pip3 install python-gitlab
-
gitlab 申请api access token
路径 User Settings -> Access Tokens -
配置 python-gitlab.cfg,可以放在同一目录下
[global] default = username ssh_verify = False timeout = 8 [username] url = https://gitlab.com private_token = xxxxxxxxxxxx
For API v4 use url https://gitlab.com, if you use API v3 use url
https://gitlab.example.com
-
代码
import gitlab gl = gitlab.Gitlab.from_config('username', ['python-gitlab.cfg']) projects = gl.projects.list() print(projects)
-
或者使用以下代码获取group项目
import gitlab class GitlabAPI(object): def __init__(self, *args, **kwargs): self.gl = gitlab.Gitlab('https://gitlab.com', private_token='xxxxxxxxxxxxx', api_version='4') # 可行 先获取所有group ID, 然后根据group ID 获取项目地址 def get_all_group(self): return self.gl.groups.list(all=True) def get_group_by_id(self, groupID): group = self.gl.groups.get(groupID) projects = group.projects.list(all=True) result_list = [] for project in projects: result_list.append(project.http_url_to_repo) return result_list apiObject = GitlabAPI() groupProjects = apiObject.get_group_by_id(填写获取到的groupID) print(groupProjects)