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

相关推荐
Jeanyia1 天前
GitLab 配套 Git 常用命令
git·gitlab
极小狐1 天前
极狐GitLab 补丁版本:19.2.1、19.1.3、19.0.5
网络·安全·gitlab·极狐gitlab·安全更新·补丁版本
rustfs2 天前
GitLab 如何与 RustFS 集成?
分布式·rust·gitlab
郝亚军4 天前
gitlab常用命令
gitlab
xixingzhe27 天前
Java 使用 GitLab4J 实现 WebHook
gitlab
童槿顏丶9 天前
GitLab部署到Linux
linux·运维·gitlab
InfinitePlus9 天前
Gitlab docker版本安装
docker·容器·gitlab
进击切图仔9 天前
GitLab 新用户创建与项目授权操作指南
gitlab
技术小结-李爽10 天前
【工具】git与gitee和gitlab和github等等有什么区别?
git·gitee·gitlab
xiaoxiangsiyan11 天前
GitLab CI/CD 自托管(EE 企业版)+ Kubernetes Runner 集群 + ArgoCD(GitOps 部署)
运维·网络·ci/cd·容器·kubernetes·gitlab·argocd