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的其他操作,请参考官方文档。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值