• Pthon操作Gitlab API----批量删除,创建,取消保护


    1.需求:大批量的应用上线后合并到Master,其他的分支develop/test/uat等需要同步最新代码的操作。

    2.操作:可以通过传参 ,列表 的方式把每个项目的id值填入,才能对相关项目进行批量操作。

    3.代码:

     1 # -*- coding: utf-8 -*-
     2 __Author__ = "jmmei"
     3 __Date__ = '2019/9/22'
     4 
     5 """
     6 删除wjj_test develop 后,从master分支再创建的需求
     7 pip3 install python-gitlab
     8 """
     9 import gitlab
    10 import os
    11 import sys
    12 
    13 class GitlabAPI():
    14   def __init__(self,url,token,projectid):
    15       self.gl = gitlab.Gitlab(url,token)
    16       self.project=self.gl.projects.get(projectid)
    17 
    18   def get_all_projects(self):
    19     result_list= self.gl.projects.list(all=True,as_list=False)
    20     
    21     return result_list
    22 
    23   def get_all_branches(self):
    24     #branches = self.project.branches.list()  #默认是第一页的分支列表
    25     branches = self.project.branches.list(all=True)
    26     branches_list=[]
    27     for i in branches:
    28       branches_list.append(i.name)
    29 
    30     return branches_list
    31 
    32 
    33   def del_branches(self,branch):
    34       self.project.branches.delete(branch)
    35       
    36    
    37 
    38 
    39   def create_branches(self,branch):
    40     branch_obj = self.project.branches.create({'branch': branch,'ref': 'master'})
    41     
    42     # 分支保护取消
    43     #branch1.protect()
    44     branch_obj.unprotect()
    45  
    46 
    47 
    48 
    49 if __name__ == '__main__':
    50     url = 'http://www.baidu.com"
    51     token = = 'xxxxxxxx'
    52     last_list=["develop","wjj_test","wjj_uat","wjj_uat_match"]
    53     
    54     #通过输入参数获取第一个参数,仅限Linux环境中使用
    55     #proid = sys.argv[1]
    56     #通过列表,windows环境批处理
    57     proid_list=["188","265"]
    58     sum=0
    59 
    60     for proid in proid_list:
    61       py_git=GitlabAPI(url,token,proid)
    62       return_list=py_git.get_all_branches()
    63       for branch in last_list:
    64         if branch not in return_list:
    65           py_git.create_branches(branch)
    66           print("创建%s分支成功."%branch)
    67         else:
    68           py_git.del_branches(branch)
    69           py_git.create_branches(branch) 
    70           print("创建%s分支成功."%branch)
    71       sum+=1 
    72       print("projectid:%s从Master拉取创建成功,第%s次------->>>>>>>>"%(proid,sum))
    73 
    74     '''
    75     all_projects=py_git.get_all_projects()
    76     print(获取所有项目的name 和id)
    77     for p in all_projects:
    78       print(p.name, p.id)
    79     '''
    80 
    81     #py_git=GitlabAPI(url,token,x)
    82     #判断last_list是否在return_list中,删除和创建分支

    注意:另外还有python操作gitlab aip的其他操作,请参考官方文档。

    http://www.cnblogs.com/Jame-mei
  • 相关阅读:
    pkg_resources.DistributionNotFound: The 'catkin-pkg==0.4.9' distribution was not found
    gsl库安装
    json ubuntu下安装
    系统安装情况以及深度学习环境搭建
    ros 编程习惯
    ubuntu系统ftp连接 以及ssh连接
    redmine问题
    maven仓库私服配置
    SVN配置管理(trunk、branches、tags)
    Gitolite配置管理和GIT基本操作
  • 原文地址:https://www.cnblogs.com/Jame-mei/p/11936806.html
Copyright © 2020-2023  润新知