Get the project branch version on gitlab

# -*- coding: utf-8 -*-

import requests

gitlab_url = 'http://172.16.67.163:8083/'
access_token = 'glpat-xcyhWihzE7Z3SxQVicuY'

# 获取项目列表
projects_url = gitlab_url + '/api/v4/projects'
headers = {'PRIVATE-TOKEN': access_token}

page = 1
projects = []
while True:
    params = {'page': page, 'per_page': 100}  # 每页100个项目,根据需要可以调整
    response = requests.get(projects_url, headers=headers, params=params)
    if response.status_code == 200:
        project_data = response.json()
        if len(project_data) == 0:
            break  # 没有更多项目了,退出循环
        projects.extend(project_data)
        page += 1
    else:
        print('Error occurred while fetching projects:', response.text)
        break

# 循环迭代项目列表
for project in projects:
    project_id = project['id']
    project_name = project['name']
    branches_url = '{}/api/v4/projects/{}/repository/branches'.format(gitlab_url, project_id)

    # 获取项目的分支信息
    response = requests.get(branches_url, headers=headers)
    if response.status_code == 200:
        branches = response.json()

        # 打印项目的分支信息
        print('Project: ' + project_name)
        print('Branches:')
        for branch in branches:
            print("- " + branch['name'])
    else:
        print('Error occurred while fetching branches for project', project_name)

    print('\n')

What you get is a list, and you still need to think about how to deal with it, and update it after thinking about it.

Guess you like

Origin blog.csdn.net/xiaodaiwang/article/details/132497826
Recommended