网站首页 > 精选文章 正文
前提
在 gitlab 中你的工程创建 Access Token
然后你会得到一个 21 位 access token,代码中需要用到。
代码
'''
说明:
1.登录gitlab的root账号(登录账号根据需要访问的项目而定)
2.gitlab_token是gitlab用户的setting中生成的字符串,使用户可以通过http的形式下载代码
3.在gitlab用户的Settings->Access Tokens,创建你的access token,它会生成一个21位的字符串(只出现一次,注意小心保管)
'''
# -*- coding:utf-8 -*-
import json
import requests
# gitlab地址
git_url = 'https://gitlab.xxx.com'
# gitlab的token
git_token = 'xxxxxxxxxxxxxxxxxxxxx'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
}
session = requests.Session()
headers['PRIVATE-TOKEN'] = git_token
session.headers = headers
git_login = session.get(git_url, headers=headers)
# 获取当前账号有权限的全部工程列表
# 注意:参数per_page最大为100,传入超过100的值时,gitlab的url请求中的per_page默认取100
def gitlab_projects(per_page=100):
print("正在获取gitlab上工程...")
projects_api = git_url + '/api/v4/projects?simple=yes&per_page=%s' % per_page
projects_headers = session.head(projects_api).headers
projects_num = int(projects_headers['X-Total']) # 获取工程总数
projects_pages = int(projects_headers['X-Total-Pages']) # 获取工程总页数
print("工程总数:", projects_num)
result = []
for i in range(projects_pages):
page = i + 1
projects_url = projects_api + '&page=' + str(page)
projects = session.get(projects_url).text
projects_json = json.loads(projects)
if type(projects_json) == list:
result = result + projects_json
else:
print("projects_url: %s" % projects_url)
print("headers: %s" % session.head(projects_api).headers)
print("response: %s" % projects_json)
print("工程获取完成~")
return result
# 获取工程的分支
# 注意:参数per_page最大为100,传入超过100的值时,gitlab的url请求中的per_page默认取100
def gitlab_project_branches(project_id, project_name, page=1, per_page=100, get_all_branches=False, max_branch_count=200):
print('工程id是' + str(project_id) + ':', '工程名是' + str(project_name))
next_page = page
branch_count = max_branch_count
branch_names = []
while next_page > 0 and branch_count > 0:
project_branches_api = git_url + '/api/v4/projects/%s/repository/branches?page=%s&per_page=%s' % (project_id, next_page, per_page)
branch_headers = session.head(project_branches_api).headers
next_page = int(branch_headers['X-Next-Page']) if "X-Next-Page" in branch_headers and branch_headers['X-Next-Page'] != '' else 0
if not get_all_branches:
branch_count = branch_count - per_page
branches_info = session.get(project_branches_api).text
branches_info = json.loads(branches_info)
for v in branches_info:
branch_names.append(v["name"])
print("分支信息获取完成~")
if get_all_branches:
return branch_names
return branch_names[:max_branch_count]
# 增量获取所有工程所有分支的提交日志
# 注意:参数per_page最大为100,传入超过100的值时,gitlab的url请求中的per_page默认取100
def get_project_commits(project_id, project_branch_name, page=1, per_page=100, get_all_commits=False, max_commit_count=200):
commit_all = []
next_page = page
commit_count = max_commit_count
while next_page and commit_count > 0:
if not get_all_commits:
commit_count = commit_count - per_page
commit_api = git_url + '/api/v4/projects/%s/repository/commits?ref_name=%s&page=%s&per_page=%s' % (project_id, project_branch_name, next_page, per_page)
commit_headers = session.head(commit_api).headers
next_page = commit_headers["X-Next-Page"]
commit_info = session.get(commit_api).text
commit_all = commit_all + json.loads(commit_info)
print("%s分支获取的commit信息获取完成" % project_branch_name)
if get_all_commits:
return commit_all
return commit_all[:max_commit_count]
# 1.获取所有的项目工程
project_result = gitlab_projects()
print(project_result)
print(len(project_result))
# 2.获取指定项目工程的分支名
branch_result = gitlab_project_branches(project_id=12345, project_name='my_project', page=1, per_page=100, get_all_branches=True)
print(branch_result)
print(len(branch_result))
# 3.获取指定项目工程的commit记录
commit_result = get_project_commits(project_id=12345, project_branch_name='master', page=1, per_page=100, get_all_commits=True)
print(commit_result)
print(len(commit_result))
猜你喜欢
- 2025-01-29 GitLab 14.6发布,优化Geo高可用,安全更新等
- 2025-01-29 Centos下安装gitlab(centos安装git命令)
- 2025-01-29 Gitlab runner(gitlab runner多台服务器部署)
- 2025-01-29 gitlab+jenkins实现自动化部署(jenkins gitlab自动部署)
- 2025-01-29 Java教程:GitLab在项目的环境搭建和基本的使用
- 2025-01-29 使用Java统计gitlab代码行数(gitee统计代码量)
- 2025-01-29 使用GitLab实现CICD(gitlab cicd)
- 2025-01-29 代码管理-git、github、gitlab(代码管理软件)
- 2025-01-29 CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接
- 2025-01-29 GitLab发布Web IDE 可直接在其网站上编辑文件
- 05-3022《Vue 入门教程》VueRouter 路由嵌套
- 05-30前端面试题-Vue 项目中,你做过哪些性能优化?
- 05-30超简 Vue3+elementPlus 后台管理系统
- 05-30还有前端不知道Electron的?手把手教你把Vue项目打包成桌面程序
- 05-30Nuxt最简入门,让vue项目快速被搜索引擎收录
- 05-30Mac上最美最好用软件系列
- 05-30AI编程小白必备|Cursor安装及配置教程
- 05-30好玩儿的编程语言——文言文编程语言
- 最近发表
- 标签列表
-
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)