获取gitlab上项目列表过程及脚本

一、使用Gitlab API查询项目列表

1、首先获取访问令牌:在Gitlab上生成一个访问令牌,以便能够使用API进行身份验证。可以在GitLab的用户设置中创建一个访问令牌。

2、使用curl发送GET请求的命令:

复制代码
curl --header "PRIVATE-TOKEN: <your-access-token>" "https://gitlab.example.com/api/v4/projects"

将<your-access-token>替换为生成的实际访问令牌,https://gitlab.example.com替换为GitLab实例的URL

输入命令:

复制代码
 curl --header "PRIVATE-TOKEN:glpat--MKao_8dXyrghuymmg4k" "http://172.16.67.163:8083/api/v4/projects" 

这时候获取到的项目信息是包括了各种内容:ID,name,所属群组,git地址,最近一次提交等等。

3、但是,GitLab API 默认通过分页方式返回数据。每次请求只返回一页的结果,默认每页显示 20 条记录。为了获取更多结果,可以使用相应的参数进行分页操作,例如 page 和 per_page。

page 参数表示要获取的页数,而 per_page 参数表示每页返回的项目数量。

例如:将 per_page 设置为较大的值(例如 100)以便一次性获取更多的项目:

复制代码
curl --header "PRIVATE-TOKEN: <your-access-token>" "https://gitlab.example.com/api/v4/projects?per_page=100"

输入命令:

复制代码
curl --header "PRIVATE-TOKEN:glpat--MKao_8dXyrghuymmg4k" "http://172.16.67.163:8083/api/v4/projects?per_page=100" 

4、要获取第二页的项目(即101到200),可以将命令修改为:

复制代码
curl --header "PRIVATE-TOKEN: <your-access-token>" "https://gitlab.example.com/api/v4/projects?per_page=100&page=2"

输入命令:就可以获取101到200个项目情况。

复制代码
curl --header "PRIVATE-TOKEN:glpat--MKao_8dXyrghuymmg4k" "http://172.16.67.163:8083/api/v4/projects?per_page=100&page=2" 

二、提取项目名称列表。

获取到以上内容后需要用jq 提取出项目名称列表,运行一下程序,就可以将项目列表打印到屏幕上。

复制代码
# -*- coding: UTF-8 -*-
import json

# 读取 JSON 文件,data5.json是前面获取的项目信息。
with open('data5.json') as file:
   data = json.load(file)

# 提取包含 "name" 字段的内容,并显示全部
names = [item['name'] for item in data if 'name' in item]
for name in names:
   print(name)

三、完整处理脚本

先分别生成data.json文件,然后合并为一个data.json文件,再用Python处理,获取列表:

1、data1.json-data4.json 是分别获取的项目信息

2、hebingjson.py 是合并data1.json-data4.json为data.json的程序

3、data.py是提取项目列表的程序

4、liebiao.txt是获取到的项目列表,包括个人创建的项目,所以会比root账号看到的要多。

1、huoquliebiao.sh 内容:获取到data1.json-data4.json

复制代码
#!/bin/bash

gitlab_url="http://172.16.67.163:8083"
access_token="glpat--MKao_8dXyrghuymmg4k"

cd /home/test2

curl -H "PRIVATE-TOKEN:$access_token" "$gitlab_url/api/v4/projects?per_page=100" > data1.json

curl -H "PRIVATE-TOKEN:$access_token" "$gitlab_url/api/v4/projects?per_page=100&page=2" > data2.json

curl -H "PRIVATE-TOKEN:$access_token" "$gitlab_url/api/v4/projects?per_page=100&page=3" > data3.json

curl -H "PRIVATE-TOKEN:$access_token" "$gitlab_url/api/v4/projects?per_page=100&page=4" > data4.json

2、hebingjson.py将data1.json-data4.json合并为data.json

复制代码
# -*- coding: utf-8 -*-
import json

# 需要合并的 JSON 文件路径列表
json_files = ['data1.json', 'data2.json', 'data3.json', 'data4.json']

# 存储合并后的数据
merged_data = []

# 合并 JSON 文件的数据
for file in json_files:
    with open(file, 'r') as f:
        data = json.load(f)
        merged_data.extend(data)

# 将合并后的数据写入新的 JSON 文件
output_file = 'data.json'  # 新的 JSON 文件名
with open(output_file, 'w') as f:
    json.dump(merged_data, f)

print('合并完成,并写入新的 JSON 文件:', output_file)

3、**data.py:**提取项目列表的程序

复制代码
# -*- coding: UTF-8 -*-

import json

# 读取 JSON 文件
with open('data.json') as file:
   data = json.load(file)

# 提取包含 "name" 字段的内容,并显示全部
names = [item['name'] for item in data if 'name' in item]
for name in names:
#   print(name)
   print(name.encode('utf-8'))

4、运行步骤

cd /home/test2
chmod +x huoquliebiao.sh
./huoquliebiao.sh

python hebingjson.py

python data.py > liebiao.txt

liebiao.txt里的内容就是项目列表

相关推荐
Patrick_Wilson1 天前
为什么gitlab的MR会默认有一个merge commit
前端·git·gitlab
极小狐1 天前
极狐GitLab Duo 功能更新:扩展 MCP 工具集、支持 MR 事件触发
运维·gitlab·agent·mr·极狐gitlab·mcp·极狐gitlab duo
2501_928996222 天前
GitLab CVE-2026-19478漏洞解析:Gitaly ref攻击绕过审计与中科热备下的代码资产保护重构
重构·gitlab
秦渝兴2 天前
GitLab CI/CD 流水线实战
ci/cd·gitlab
Ningcode_cloud3 天前
什么是CICD? GitLab + Jenkins 持续集成实战部署手册
运维·ci/cd·云原生·容器·gitlab·jenkins
姚永强3 天前
gitlab安装
运维·gitlab
力江4 天前
GitLab 私有化部署实战 —— 从 Omnibus 到 Docker 的踩坑之旅
docker·容器·gitlab
极小狐8 天前
从 GitLab Flow 到 Pipeline:分支策略 + CI/CD + 运维一文打通
ci/cd·gitlab·devops·极狐gitlab
猫头虎9 天前
GitHub 入门教程:如何加入并为开源项目贡献代码
gitee·开源·gitlab·github·开放原子·开源协议·gitcode