当前位置:   article > 正文

使用gitpython和python-gitlab操作git_gitlabpython根据http_url_to_repo获取仓库id

gitlabpython根据http_url_to_repo获取仓库id

python有三个git相关的库,分别是gitpython、python-gitlab、gitlab库。

  • gitpython:是一个与Git库交互的Python库,可以实现绝大部分的Git读写操作。主要是取代shell的命令操作,通过这个进行本地仓库的一些拉取代码、创建分支、push代码等操作。
  • python-gitlab:是一个用来连接git,进行git信息查询的库。比如查询git的项目、users、提交到mr等。可以参考文档:https://python-gitlab.readthedocs.io/en/master/install.html
  • gitlab:没有仔细了解过。

这篇博文主要讲两个库的用法。

 

Gitpython

安装:

仍然要先安装python专门用来操作git的库,使用pip即可

pip install gitpython

连接:

  1. from git import Repo
  2. repo =Repo("/Users/alice/practice") # git文件的路径

操作Git:

repo提供了一些方法可以调用,如下:

  1. from git import Repo
  2. repo =Repo("/Users/alice/practice") #git文件的路径
  3. # 克隆 git clone
  4. repo.clone_from('url','to_path')
  5. # 拉取代码
  6. repo.remote().pull()
  7. # 获取当前所在的分支 git branch
  8. print(repo.active_branch)
  9. # 获取所有分支
  10. print (repo.branches) # 也可以直接缩写print(r.branches)
  11. print([str(b) for b in repo.branches])
  12. print (repo.tags)
  13. print([str(b) for b in repo.tags])
  14. # 创建分支 git branch
  15. repo.create_head("分支名")
  16. # 添加版本号 git tag -a
  17. repo.create_tag("tag号")
  18. # 添加到缓存区 git add命令
  19. # repo.index.add(["文件名"])
  20. # 提交到版本库 git commit -m
  21. repo.index.commit("message")
  22. # 回退到某个版本 git reset --hard hash
  23. repo.index.reset(commit="哈希地址",head=True)
  24. # 推送代码到远程分支
  25. repo.remote().push('远程分支名')
  26. # 查看git log
  27. repo.iter_commits()
  28. print([str(i.message) for i in r.iter_commits()]) # 获取提交信息
  29. print([str(i.message) for i in r.iter_commits()]) # 查看提交的hash值

 

python-gitlab

安装:

仍然要先安装库,使用pip即可

  1. pip install python-gitlab
  2. # 如果权限问题失败可以使用
  3. sudo pip install python-gitlab
  4. #如果是本地已安装要升级可以采用
  5. sudo pip install --upgrade python-gitlab

连接:

  1. import gitlab
  2. git_url = 'https://gitlab.com/sweet' #gitlab服务端地址
  3. private_token = 'xxxxxx' #获取git的token
  4. # private token or personal token authentication
  5. gl = gitlab.Gitlab(git_url, private_token, api_version='4') #api_version默认是4,具体看自己公司的版本

操作Git:

python-gitlab提供了一些方法可以调用,如下:

  1. ## 获取project列表
  2. projects = git.projects.list()
  3. print (projects)
  4. ## 获取所有project
  5. projects = git.projects.all()
  6. projects = git.projects.list(all=True)
  7. for project in projects:
  8. print (project.name,project.id,projects.http_url_to_repo)
  9. groups = gl.groups.list(all=True)
  10. for group in all_groups:
  11. print (group.name,group.id)
  12. project = git.projects.get(24664833) #项目的id
  13. group = gl.groups.get(2)
  14. commit = project.commits.list()[0] #获取最新的提交人信息,这里我取的第一个人的
  15. git_name =commit.committer_name #可以直接提取用户信息里的name,也可以获取提交的id,created_at,message
  16. members = group.members.list(all=True)
  17. for me in members:
  18. print (me.username,me.id)
  19. # 获取git所有的user
  20. users = gl.users.list(all=True)
  21. for user in users:
  22. print (user.username,user.id,user.name,user.state)
  23. # 创建一个新用户
  24. user_data = {'email': 'jen@foo.com', 'username': 'jen', 'name': 'Jen'}
  25. user = gl.users.create(user_data)
  26. print(user)
  27. # 示例:获取gitlab指定组内所有user以及project名称以及ID信息,本例中组ID为58
  28. gid = int(raw_input('Input the group ID: '))
  29. group = gl.groups.get(gid)
  30. print (gid, group.name)

 

使用Requests查询Git信息

了解了git的连接方式,如果希望获取Git上MR的信息,也可以实现了:

  • 使用python-gitlab来连接Git
  • 使用Requets库来访问接口和拿到返回数据
  • 使用json库提供的方法来解析返回的json
  1. import requests
  2. import json
  3. def work():
  4. mrs_url = 'https://gitlab.com/api/v4/projects/2466000000/merge_requests'
  5. headers = {
  6. "Accept": "application/json, text/plain, */*",
  7. "PRIVATE-TOKEN": "xxxxxxx",
  8. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
  9. }
  10. params = {
  11. "page_size": 20
  12. }
  13. response = requests.get(mrs_url, headers=headers, params=params)
  14. result = json.loads(response.text)
  15. for mr in result:
  16. print (mr['iid'], mr['state'], mr['merge_status'], mr['author']['name'], mr['title'])
  17. work()

运行结果为:

  1. 2 opened can_be_merged Alice Practice2 merge to master
  2. 1 opened can_be_merged Alice Practice1 to master

附gitlab的MR返回json信息参考:

  1. [
  2. {
  3. "id":9014000000xx,
  4. "iid":2,
  5. "project_id":2466400000xx,
  6. "title":"Practice2 merge to master",
  7. "description":"",
  8. "state":"opened",
  9. "created_at":"2020-02-27T13:19:07.992Z",
  10. "updated_at":"2020-02-27T13:19:07.992Z",
  11. "merged_by":null,
  12. "merged_at":null,
  13. "closed_by":null,
  14. "closed_at":null,
  15. "target_branch":"master",
  16. "source_branch":"practice2",
  17. "user_notes_count":0,
  18. "upvotes":0,
  19. "downvotes":0,
  20. "author":{
  21. "id":8269000000xx,
  22. "name":"Alice",
  23. "username":"sweet",
  24. "state":"active",
  25. "avatar_url":"https://assets.gitlab-static.net/uploads/-/system/user/avatar/826900000xx/xx.png",
  26. "web_url":"https://gitlab.com/sweet"
  27. },
  28. "assignees":[
  29. ],
  30. "assignee":null,
  31. "reviewers":[
  32. ],
  33. "source_project_id":2466400000xx,
  34. "target_project_id":2466400000xx,
  35. "labels":[
  36. ],
  37. "work_in_progress":false,
  38. "milestone":null,
  39. "merge_when_pipeline_succeeds":false,
  40. "merge_status":"can_be_merged",
  41. "sha":"b9f4599858c76a0ccfxx",
  42. "merge_commit_sha":null,
  43. "squash_commit_sha":null,
  44. "discussion_locked":null,
  45. "should_remove_source_branch":null,
  46. "force_remove_source_branch":true,
  47. "reference":"!2",
  48. "references":{
  49. "short":"!2",
  50. "relative":"!2",
  51. "full":"sweet/practice!2"
  52. },
  53. "web_url":"https://gitlab.com/sweet/practice/-/merge_requests/2",
  54. "time_stats":{
  55. "time_estimate":0,
  56. "total_time_spent":0,
  57. "human_time_estimate":null,
  58. "human_total_time_spent":null
  59. },
  60. "squash":false,
  61. "task_completion_status":{
  62. "count":0,
  63. "completed_count":0
  64. },
  65. "has_conflicts":false,
  66. "blocking_discussions_resolved":true,
  67. "approvals_before_merge":null
  68. },
  69. {
  70. "id":901400000xx,
  71. "iid":1,
  72. "project_id":246600000xx,
  73. "title":"Practice1 to master",
  74. "description":"",
  75. "state":"opened",
  76. "created_at":"2020-02-27T12:58:55.990Z",
  77. "updated_at":"2020-02-27T12:58:55.990Z",
  78. "merged_by":null,
  79. "merged_at":null,
  80. "closed_by":null,
  81. "closed_at":null,
  82. "target_branch":"master",
  83. "source_branch":"practice1",
  84. "user_notes_count":0,
  85. "upvotes":0,
  86. "downvotes":0,
  87. "author":{
  88. "id":826000000xx,
  89. "name":"Alice",
  90. "username":"sweet",
  91. "state":"active",
  92. "avatar_url":"https://assets.gitlab-static.net/uploads/-/system/user/avatar/8269000000xx/xx.png",
  93. "web_url":"https://gitlab.com/sweet"
  94. },
  95. "assignees":[
  96. ],
  97. "assignee":null,
  98. "reviewers":[
  99. ],
  100. "source_project_id":2466400000xx,
  101. "target_project_id":2466400000xx,
  102. "labels":[
  103. ],
  104. "work_in_progress":false,
  105. "milestone":null,
  106. "merge_when_pipeline_succeeds":false,
  107. "merge_status":"can_be_merged",
  108. "sha":"b9f4599858c76axx",
  109. "merge_commit_sha":null,
  110. "squash_commit_sha":null,
  111. "discussion_locked":null,
  112. "should_remove_source_branch":null,
  113. "force_remove_source_branch":true,
  114. "reference":"!1",
  115. "references":{
  116. "short":"!1",
  117. "relative":"!1",
  118. "full":"sweet/practice!1"
  119. },
  120. "web_url":"https://gitlab.com/sweet/practice/-/merge_requests/1",
  121. "time_stats":{
  122. "time_estimate":0,
  123. "total_time_spent":0,
  124. "human_time_estimate":null,
  125. "human_total_time_spent":null
  126. },
  127. "squash":false,
  128. "task_completion_status":{
  129. "count":0,
  130. "completed_count":0
  131. },
  132. "has_conflicts":false,
  133. "blocking_discussions_resolved":true,
  134. "approvals_before_merge":null
  135. }
  136. ]

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/141732
推荐阅读
相关标签
  

闽ICP备14008679号