Gitlab API 使用總結-從基礎概念出發,快速開發

#!/bin/python3
# encoding: utf-8
#

__Author__ = 'xxx'
__Date__ = 'xxx'
"""
Concept:
Git
  Group
    SubGroup
      GroupProject --> update to Project 
        RepoTree
        file
          
        
Git
  Project
    RepoTree
    file

Related url: https://python-gitlab.readthedocs.io/en/stable/gl_objects/groups.html#id1 
"""

import gitlab
import os
import sys

class GitlabAPI(object):
    def __init__(self):
        if os.path.exists('/etc/python-gitlab.cfg'):
            self.gl = gitlab.Gitlab.from_config('zhangzh', ['/etc/python-gitlab.cfg'])
            #self.gl = gitlab.Gitlab('http://10.0.xx.xx',email='[email protected]',password='xxxxxx')
        elif os.path.exists(os.getenv('HOME') + '/.python-gitlab.cfg'):
            self.gl = gitlab.Gitlab.from_config('zhangzh', [os.getenv('HOME') + '/.python-gitlab.cfg'])
        else:
            print('You need to make sure there is a file named "/etc/python-gitlab.cfg" or "~/.python-gitlab.cfg"')
            sys.exit(5)
    
    #List the groups 		
    def list_groups(self):
        #return self.gl.groups.list(all=True)
        return self.gl.groups.list()

    #List the subgroups for a group
    def list_subgroups_of_one_group(self, group_id):
        group = self.gl.groups.get(group_id)
        return  group.subgroups.list(all=True)
		
    #List a group’s projects
    def list_projects_of_one_group(self, group_id):
        group = self.gl.groups.get(group_id)
        return group.projects.list(all=True)
		
	
    #List the repository tree from project
    def list_repo_tree_of_one_project( self, project):
        return project.repository_tree()
    
    #List the repository tree from group project   
    def list_repo_tree_of_one_group_project( self, group_project_id):
        manageable_project = self.gl.projects.get(group_project_id, lazy=True)
        return manageable_project, manageable_project.repository_tree()

    
    def get_user_id(self, username):
        user = self.gl.users.get_by_username(username)
        return user.id
 
    def get_group_id(self, groupname):
        group = self.gl.users.search(groupname)
        return group[0].id
 
    def get_all_projects(self):
        projects = self.gl.projects.list(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 = self.gl.projects.owned(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 = self.gl.projects.owned(groupname=groupname, all=True)
        result_list = []
        for project in projects:
            result_list.append(project.http_url_to_repo)
        return result_list
    
    def find_file_within_project(self,file_path):
         #Pay attention page setting
         projectAll =  self.gl.projects.list(all=True)
         for project in projectAll:
             try:
                 groupProjectTree = self.list_repo_tree_of_one_project(project)
             except BaseException:
                 pass
             else:
                 for fileTree in groupProjectTree:
                     path = fileTree['path'] + file_path
                     try:
                         file = project.files.get(file_path= path, ref='master')
                     except BaseException:
                         #print("cann't find this file")
                         pass
                     else:
                         print(project.web_url[38:] + '\n')
                         break



    def find_file_within_group(self, group_id, file_path):
         subgroups = self.list_subgroups_of_one_group(group_id)
         groupProjectList = []
         for subgroup in subgroups:
             groupProjectList.append(self.list_projects_of_one_group(subgroup.id))
         for projectList in groupProjectList:
             for project in projectList:
                 try:
                     #groupProjectTree  = self.list_repo_tree_of_one_project(project)
                     manage_project , groupProjectTree  = self.list_repo_tree_of_one_group_project(project.id)
                 except BaseException:
                     pass
                 else:
                     for fileTree in groupProjectTree:
                         path = fileTree['path'] + file_path
                         try:
                             file = manage_project.files.get(file_path= path, ref='master')
                         except BaseException:
                             #print("cann't find this file")
                             pass
                         else:
                             print(project.web_url[38:] + '\n')
                             break





if __name__ == '__main__':
    git = GitlabAPI()
    
    #group_id : 375
    #file_path: "/kssl/HRP/.prefork/lib/apr.exp" this path is base on project root directory
    #you can change as your specified
    git.find_file_within_group(375,"/kssl/HRP/.prefork/lib/apr.exp")
    #git.find_file_within_project("/kssl/HRP/.prefork/lib/apr.exp")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章