Python exports the gitlab project

Python export gitlab project

  1. install python-gitlab module
    pip install python-gitlab
    python3 
    pip3 install python-gitlab
    
  2. gitlab application API access token
    path User Settings -> Access Tokens
  3. configure pythongitlab.cfg and place

    in the same directory

    [global]
     default = username
     ssh_verify = False
     timeout = 8
    
     [username]
     url = https://gitlab.com
     private_token = xxxxxxxxxxxx
    

     

    对于API v4使用url https://gitlab.com,如果您使用API v3使用url
    https://gitlab.example.com

    </引用>

      <>

 

import gitlab
gl = gitlab.Gitlab.from_config('username', ['python-gitlab.cfg'])
projects = gl.projects.list()
print(projects)
  • or use the following code to get the group project
    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)
    

Read More: