获取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里的内容就是项目列表

相关推荐
裁二尺秋风21 小时前
CI/CD — Pipeline的使用以及Blue Ocean多分支流水线的使用方法
ci/cd·gitlab·jenkins
北海之灵1 天前
docker desktop部署本地gitlab服务
docker·容器·gitlab
兔老大RabbitMQ1 天前
GitHub vs GitLab 全面对比报告(2025版)
gitlab·github
CC码码2 天前
管理你的多个 Git 密钥(多平台多账号)
git·gitlab·github
CC码码2 天前
管理你的多个 Git 密钥(单平台多账号)
git·gitlab·github
兔老大RabbitMQ3 天前
GitLab详细分析
gitlab
小道仙9715 天前
gitlab对接,gitlabRestApi,gitlab4j-api
java·git·gitlab
tswddd16 天前
项目:Gitlab HSD CI/CD总结
ci/cd·kubernetes·gitlab
许_安16 天前
docker中部署gitlab
docker·eureka·gitlab
Haoea!16 天前
持续集成 CI/CD-Jenkins持续集成GitLab项目打包docker镜像推送k8s集群并部署至rancher
ci/cd·gitlab·jenkins·rancher