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

python使用gitlab-api

原创
作者头像
陈不成i
修改2021-06-07 17:44:54
2.4K0
修改2021-06-07 17:44:54
举报
文章被收录于专栏:ops技术分享

一.简介

公司使用gitlab 来托管代码,日常代码merge request以及其他管理是交给测试,鉴于操作需经常打开网页,重复且繁琐,所以交给Python管理。

官方文档

安装: pip install python-gitlab

二.示例

1.获取gitlab某个项目中,某分支的最新commit信息,提交人、提交时间、commit-id等等,主要用于搭配jenkins做这些信息的展示。当项目构建后,在钉钉显示提交人和commit的id号与jenkins的信息。

2.生成自己的token

file
file

3.代码

  1. #!/usr/bin/python3
  2. import gitlab, json, sys
  3. #[项目组名、项目名、分支]
  4. group_name = sys.argv[1]
  5. job_name = sys.argv[2]
  6. job_branch = sys.argv[3]
  7. job_url = group_name + '/' + job_name
  8. gl = gitlab.Gitlab('http://10.0.23.14/', private_token='Fsdfxs7sdjssd')
  9. projects = gl.projects.list(search=job_name)
  10. for project in projects:
  11. if project.path_with_namespace == job_url:
  12. break
  13. commits = project.commits.list(all=True, query_parameters={'ref_name': job_branch, 'since': '2020-11-11T00:00:00Z'})
  14. print(commits[0].author_name)
  15. print(commits[0].id)

讲解

projects返回一个数组,是前面search名字相似的项目,每个数组的内容都是一个对象。

每个对象返回如下:

代码语言:javascript
复制
<class 'gitlab.v4.objects.Project'> => {'id': 440, 'description': '', 'default_branch': 'master', 'tag_list': [], 'ssh_url_to_repo': 'git@47.94.250.239:jenkins/pipeline-fuction.git', 'http_url_to_repo': 'http://47.94.250.239/jenkins/pipeline-fuction.git', 'web_url': 'http://47.94.250.239/jenkins/pipeline-fuction', 'name': 'pipeline-fuction', 'name_with_namespace': 'jenkins / pipeline-fuction', 'path': 'pipeline-fuction', 'path_with_namespace': 'jenkins/pipeline-fuction', 'star_count': 0, 'forks_count': 0, 'created_at': '2020-10-15T01:24:02.322Z', 'last_activity_at': '2020-11-12T05:45:36.106Z', '_links': {'self': 'http://47.94.250.239/api/v4/projects/440', 'issues': 'http://47.94.250.239/api/v4/projects/440/issues', 'merge_requests': 'http://47.94.250.239/api/v4/projects/440/merge_requests', 'repo_branches': 'http://47.94.250.239/api/v4/projects/440/repository/branches', 'labels': 'http://47.94.250.239/api/v4/projects/440/labels', 'events': 'http://47.94.250.239/api/v4/projects/440/events', 'members': 'http://47.94.250.239/api/v4/projects/440/members'}, 'archived': False, 'visibility': 'private', 'owner': {'name': 'jenkins', 'username': 'jenkins', 'id': 5, 'state': 'active', 'avatar_url': 'http://www.gravatar.com/avatar/1656b1f22c7d92fca3ed5ab37fb442a2?s=80&d=identicon', 'web_url': 'http://47.94.250.239/jenkins'}, 'container_registry_enabled': True, 'issues_enabled': True, 'merge_requests_enabled': True, 'wiki_enabled': True, 'jobs_enabled': True, 'snippets_enabled': True, 'shared_runners_enabled': True, 'lfs_enabled': True, 'creator_id': 5, 'namespace': {'id': 8, 'name': 'jenkins', 'path': 'jenkins', 'kind': 'user', 'full_path': 'jenkins', 'parent_id': None}, 'import_status': 'none', 'import_error': None, 'avatar_url': None, 'open_issues_count': 0, 'runners_token': 'mqoNuw7tCrjaz8hP2Do7', 'public_jobs': True, 'ci_config_path': None, 'shared_with_groups': [], 'only_allow_merge_if_pipeline_succeeds': False, 'request_access_enabled': False, 'only_allow_merge_if_all_discussions_are_resolved': False, 'printing_merge_request_link_enabled': True, 'permissions': {'project_access': {'access_level': 40, 'notification_level': 3}, 'group_access': None}}

所以为了获得精确的项目,要用循环拆解开,然后project.path_with_namespace去匹配组名+项目名才行。

path_with_namespace就是这个对象的属性,找到了就break跳出即可。

commits中获取的也是一个数组,里面也是存的commit对象,每个信息如下

代码语言:javascript
复制
<class 'gitlab.v4.objects.ProjectCommit'> => {'id': '29184d28eb302c6ff82f62d943f94e6df32f21f3', 'short_id': '29184d28', 'title': 'Update CgdPortalFuction.groovy', 'created_at': '2020-11-10T16:21:43.000+08:00', 'parent_ids': ['64a9c0dbcd3b348f20c6f268672ab8aab60977cf'], 'message': 'Update CgdPortalFuction.groovy', 'author_name': '朱浩然', 'author_email': '18410168540@163.com', 'authored_date': '2020-11-10T16:21:43.000+08:00', 'committer_name': '朱浩然', 'committer_email': '18410168540@163.com', 'committed_date': '2020-11-10T16:21:43.000+08:00'}

query_parameters是过滤条件,不过只有分支起作用,since我就算写2029年也是没效果,但加上的话就会少取很多历史数据。

比如不加len(commits)是10万条,那加了since条件随意写就是最近的几百条,没仔细研究为啥,能用就行。

根据commit的返回可以输出各种属性,比如提交人、提交id等等

代码语言:javascript
复制
print(commits[0].author_name)

配置文件方式存储token

1.为了保护API 用到的 private_token,一般会将其写到系统的配置文件中去 /etc/python-gitlab.cfg 或者 ~/.python-gitlab.cfg

  1. [global]
  2. default = git
  3. ssh_verify = False
  4. timeout = 10
  5. [git]
  6. url = http://10.0.0.1
  7. private_token = xxxxxxxx
  8. api_version = 3

2.使用

  1. ## login
  2. gl = gitlab.Gitlab.from_config('git', ['~/.python-gitlab.cfg'])
  3. ## 得到第一页project列表
  4. projects = gl.projects.list()
  5. ## 得到所有project
  6. projects = gl.projects.list(all=True)
  7. projects = gl.projects.all()

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.简介
  • 二.示例
    • 讲解
      • 配置文件方式存储token
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档