前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python使用gitlab-api的其他操作

python使用gitlab-api的其他操作

原创
作者头像
陈不成i
修改2021-06-07 17:45:03
1.2K0
修改2021-06-07 17:45:03
举报
文章被收录于专栏:ops技术分享
  1. """
  2. gitlab 经常使用到的api
  3. DOC_URL: http://python-gitlab.readthedocs.io/en/stable/
  4. LOCAL_PATH: C:\Python36\Lib\site-packages\gitlab
  5. """
  6. import gitlab
  7. url = 'http://xxxxxxx'
  8. token = 'xxxxxxxxxxxxxx'
  9. # 登录
  10. gl = gitlab.Gitlab(url, token)
  11. # ---------------------------------------------------------------- #
  12. # 获取第一页project
  13. projects = gl.projects.list()
  14. # 获取所有的project
  15. projects = gl.projects.list(all=True)
  16. # ---------------------------------------------------------------- #
  17. # ---------------------------------------------------------------- #
  18. # 获取所有project的name,id
  19. for p in gl.projects.list(all=True, as_list=False):
  20. print(p.name, p.id)
  21. # ---------------------------------------------------------------- #
  22. # ---------------------------------------------------------------- #
  23. # 获取第一页project的name,id
  24. for p in gl.projects.list(page=1):
  25. print(p.name, p.id)
  26. # ---------------------------------------------------------------- #
  27. # ---------------------------------------------------------------- #
  28. # 通过指定id 获取 project 对象
  29. project = gl.projects.get(501)
  30. # ---------------------------------------------------------------- #
  31. # ---------------------------------------------------------------- #
  32. # 查找项目
  33. projects = gl.projects.list(search='keyword')
  34. # ---------------------------------------------------------------- #
  35. # ---------------------------------------------------------------- #
  36. # 创建一个项目
  37. project = gl.projects.create({'name':'project1'})
  38. # ---------------------------------------------------------------- #
  39. # ---------------------------------------------------------------- #
  40. # 获取公开的项目
  41. projects = gl.projects.list(visibility='public') # public, internal or private
  42. # ---------------------------------------------------------------- #
  43. # 获取 project 对象是以下操作的基础
  44. # ---------------------------------------------------------------- #
  45. # 通过指定project对象获取该项目的所有分支
  46. branches = project.branches.list()
  47. print(branches)
  48. # ---------------------------------------------------------------- #
  49. # ---------------------------------------------------------------- #
  50. # 获取指定分支的属性
  51. branch = project.branches.get('master')
  52. print(branch)
  53. # ---------------------------------------------------------------- #
  54. # ---------------------------------------------------------------- #
  55. # 创建分支
  56. branch = project.branches.create({'branch_name': 'feature1',
  57. 'ref': 'master'})
  58. # ---------------------------------------------------------------- #
  59. # ---------------------------------------------------------------- #
  60. # 删除分支
  61. project.branches.delete('feature1')
  62. # ---------------------------------------------------------------- #
  63. # ---------------------------------------------------------------- #
  64. # 分支保护/取消保护
  65. branch.protect()
  66. branch.unprotect()
  67. # ---------------------------------------------------------------- #
  68. # ---------------------------------------------------------------- #
  69. # 获取指定项目的所有tags
  70. tags = project.tags.list()
  71. # 获取某个指定tag 的信息
  72. tags = project.tags.list('1.0')
  73. # 创建一个tag
  74. tag = project.tags.create({'tag_name':'1.0', 'ref':'master'})
  75. # 设置tags 说明:
  76. tag.set_release_description('awesome v1.0 release')
  77. # 删除tags
  78. project.tags.delete('1.0')
  79. # or
  80. tag.delete()
  81. # ---------------------------------------------------------------- #
  82. # 获取所有commit info
  83. commits = project.commits.list()
  84. for c in commits:
  85. print(c.author_name, c.message, c.title)
  86. # ---------------------------------------------------------------- #
  87. # ---------------------------------------------------------------- #
  88. # 获取指定commit的info
  89. commit = project.commits.get('e3d5a71b')
  90. # ---------------------------------------------------------------- #
  91. # ---------------------------------------------------------------- #
  92. # 获取指定项目的所有merge request
  93. mrs = project.mergerequests.list()
  94. print(mrs)
  95. # ---------------------------------------------------------------- #
  96. # ---------------------------------------------------------------- #
  97. # 获取 指定mr info
  98. mr = project.mergerequests.get(mr_id)
  99. # ---------------------------------------------------------------- #
  100. # ---------------------------------------------------------------- #
  101. # 创建一个merge request
  102. mr = project.mergerequests.create({'source_branch':'cool_feature',
  103. 'target_branch':'master',
  104. 'title':'merge cool feature', })
  105. # ---------------------------------------------------------------- #
  106. # ---------------------------------------------------------------- #
  107. # 更新一个merge request 的描述
  108. mr.description = 'New description'
  109. mr.save()
  110. # ---------------------------------------------------------------- #
  111. # ---------------------------------------------------------------- #
  112. # 开关一个merge request (close or reopen):
  113. mr.state_event = 'close' # or 'reopen'
  114. mr.save()
  115. # ---------------------------------------------------------------- #
  116. # ---------------------------------------------------------------- #
  117. # Delete a MR:
  118. project.mergerequests.delete(mr_id)
  119. # or
  120. mr.delete()
  121. # ---------------------------------------------------------------- #
  122. # ---------------------------------------------------------------- #
  123. # Accept a MR:
  124. mr.merge()
  125. # ---------------------------------------------------------------- #
  126. # ---------------------------------------------------------------- #
  127. # 指定条件过滤 所有的merge request
  128. # state: state of the MR. It can be one of all, merged, opened or closed
  129. # order_by: sort by created_at or updated_at
  130. # sort: sort order (asc or desc)
  131. mrs = project.mergerequests.list(state='merged', sort='asc') # all, merged, opened or closed
  132. # ---------------------------------------------------------------- #
  133. # ---------------------------------------------------------------- #
  134. # 创建一个commit
  135. data = {
  136. 'branch_name': 'master', # v3
  137. 'commit_message': 'blah blah blah',
  138. 'actions': [
  139. {
  140. 'action': 'create',
  141. 'file_path': 'blah',
  142. 'content': 'blah'
  143. }
  144. ]
  145. }
  146. commit = project.commits.create(data)
  147. # ---------------------------------------------------------------- #
  148. # ---------------------------------------------------------------- #
  149. # Compare two branches, tags or commits:
  150. result = project.repository_compare('develop', 'feature-20180104')
  151. print(result)
  152. # get the commits
  153. for commit in result['commits']:
  154. print(commit)
  155. #
  156. # get the diffs
  157. for file_diff in result['diffs']:
  158. print(file_diff)
  159. # ---------------------------------------------------------------- #
  160. # ---------------------------------------------------------------- #
  161. # get the commits
  162. for commit in result['commits']:
  163. print(commit)
  164. #
  165. # get the diffs
  166. for file_diff in result['diffs']:
  167. print(file_diff)
  168. # ---------------------------------------------------------------- #

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档