This article introduces the python operation gitlab API process analysis, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference learning value, the need for friends can refer to the following
Use the python-gitlab module to call the gitlab API to manage gitlab.
install
pip install python-gitlab # If you are installing to Python3 you can use the following commands pip3 install python-gitlab
configure
In order to protect the private_token used by the API, it is usually written to the system configuration file
/etc/ or ~/.
Configuration example:
root@pts/1 $ cat ~/. [global] default = kaishugit ssh_verify = False timeout = 8 [kaishugit] url = http://10.0.0.6 private_token = xxxxx-V4Yxxxxxxks7u api_version = 3
Program use
When used in a program it can be called directly in the following way
## login gl = .from_config('kaishugit', ['~/.'])
examples
## Get the first page of the project list projects = () ## Get all the projects ## projects = (all=True) projects = ()
append
Customize the script to get the address of the repository for a specific user or group or all of them.
#!/usr/bin/env python3 # encoding: utf-8 # __Author__ = 'Colin' __Date__ = '2018-06-20' import gitlab import os import sys class GitlabAPI(object): def __init__(self, *args, **kwargs): if ('/etc/'): = .from_config('kaishugit', ['/etc/']) elif (('HOME') + '/.'): = .from_config('kaishugit', [('HOME') + '/.']) else: print('You need to make sure there is a file named "/etc/" or "~/."') (5) def get_user_id(self, username): user = .get_by_username(username) return def get_group_id(self, groupname): group = (groupname) return group[0].id def get_all_projects(self): projects = (all=True) result_list = [] for project in projects: result_list.append(project.http_url_to_repo) return result_list def get_user_projects(self, userid): projects = (userid=userid, all=True) result_list = [] for project in projects: result_list.append(project.http_url_to_repo) return result_list def get_group_projects(self, groupname): projects = (groupname=groupname, all=True) result_list = [] for project in projects: result_list.append(project.http_url_to_repo) return result_list if __name__ == '__main__': username='caichenyu' git = GitlabAPI() userid = git.get_user_id(username) print(username + '->' + str(userid)) userprojects = git.get_user_projects(userid) print(userprojects)
This is the whole content of this article.