項目需求:在公司內部搭建的gitlab服務器上,需要在補丁包service-pack分組內,根據補丁包所支持的版本號,再建立一級子分組,然后在版本號的路徑下面,存放對應版本的補丁包項目.
格式形如:
--普通補丁包
--6.0.0
--對應的補丁包項目
官方文檔:https://python-gitlab.readthedocs.io/en/stable/index.html
gitlab官方文檔:https://docs.gitlab.com/ee/api/groups.html
准備工作:
1、先要去gitlab服務器上面生成自己的token。具體操作步驟:點擊個人中心的settings--->左側邊欄的訪問令牌--->添加個人訪問令牌--->設置名稱,過期時間之后點擊創建個人訪問令牌即可。
2、在個人開發的服務器上面配置gitlab賬號,具體步驟百度即可。
主要用到的文檔部分:
Groups部分:https://python-gitlab.readthedocs.io/en/stable/gl_objects/groups.html
Projects部分:https://python-gitlab.readthedocs.io/en/stable/gl_objects/projects.html
# coding:utf-8 # 模塊名字叫python-gitlab,在這里導包的時候是 gitlab import gitlab gl = gitlab.Gitlab(gitlab服務器地址, private_token=前面設置的個人訪問令牌) # 默認all=False,單頁顯示20條數據, # 設置為all=True后顯示所有 groups = gl.groups.list(all=True) # 獲取指定分組 # 分組ID在 gitlab中的設置-->常規-->GroupID 獲取 group = gl.groups.get(分組ID) # 獲取當前分組的所有子分組 all_groups = group.subgroups.list(all=True) # 獲取所有子分組的名字 sub_group_names = [i.name for i in all_groups] # 如果知道了分組的ID,可以直接獲取該分組對象,不必從gitlab服務器的根路徑下開始進行操作.例如: def create_version_group(version_str): """根據補丁支持的版本號在git上面創建對應的group""" gl = gitlab.Gitlab(GIT_URL, private_token=PRIVATE_TOKEN) service_pack_group = gl.groups.get(COMMON_PACK_GROUP_ID) # 獲取當前 補丁包組 下面所有的子組 all_groups = service_pack_group.subgroups.list(all=True) group_names = [i.name for i in all_groups] if version_str not in group_names: data = { "name": version_str, "path": version_str, "parent_id": COMMON_PACK_GROUP_ID } target_group = gl.groups.create(data) print("git上面創建對應的版本分組成功!") return target_group else: return # 這里要注意創建分組的時候.data參數中需要指定name,path,parent_id(就是上一級分組的ID)三個參數.在之前的調試過程中,因為沒有指定parent_id,程序一直拋出401權限認證失敗的異常. # 當前分組下的所有項目 all_projects = target_group.projects.list(all=True) pkg_names = [i.name for i in all_projects] # 刪除指定分組 gl.groups.delete(group_id) # 刪除指定項目 gl.projects.delete(pkg_id)